-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigurationForm.html
167 lines (147 loc) · 6.02 KB
/
ConfigurationForm.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous">
</script>
<style>
/* The form is hidden until previous config values are loaded */
#configureForm {
opacity: 0.0;
transition-property: opacity;
transition-duration: 1.0s;
transition-timing-function: ease-in-out;
transition-delay: 0s;
}
</style>
</head>
<body>
<form id="configureForm">
<div class="form-floating my-3 mx-1">
<input type="email" class="form-control" id="floatingEmail" placeholder="[email protected]">
<label for="floatingEmail" style="color: gray;">Email address</label>
</div>
<div class="form-floating my-3 mx-1">
<input type="password" class="form-control" id="floatingPassword" placeholder="Password">
<label for="floatingPassword" style="color: gray;">Password</label>
</div>
<div class="form-floating my-3 mx-1">
<input type="url" class="form-control" id="floatingProjectUrl", placeholder="http(s)://<hostname>/v1/projects/<projectid>">
<label for="floatingProjectUrl" style="color: gray;">Project Url</label>
</div>
<div class="form-floating my-3 mx-1">
<input type="text" class="form-control" id="floatingFormId", placeholder="<formid>">
<label for="floatingFormId" style="color: gray;">Form Id</label>
</div>
<button type="submit" class="btn btn-outline-primary mx-1" id="configureButton">Configure</button>
</form>
<script>
"use strict";
(function () {
let emailInput = document.getElementById("floatingEmail");
let passwordInput = document.getElementById("floatingPassword");
let projectUrlInput = document.getElementById("floatingProjectUrl");
let formIdInput = document.getElementById("floatingFormId");
// Accept both frontend project url or API in url (v1 or #)
// https://<hostname>/[v1|#]/projects/<projectid>
let URL_REGEX = /^(https:\/\/[^\/]+)\/(v1|#)\/projects\/([^\/]+)$/s;
/**
* Display the previous configuration to the form
* @param prevConfig previous configuration returned from script host (async)
*/
function showPreviousConfig(prevConfig) {
if (prevConfig[0] && prevConfig[1] && prevConfig[2]) {
emailInput.value = prevConfig[0];
projectUrlInput.value = prevConfig[1];
formIdInput.value = prevConfig[2];
}
document.getElementById("configureForm").style.opacity = 1.0;
}
google.script.run.withSuccessHandler(showPreviousConfig).getConfigWithNoPassword();
/*
* Form validations
*/
emailInput.addEventListener("input", e => {
emailInput.setCustomValidity("");
if (!emailInput.checkValidity()) {
emailInput.classList.add("is-invalid");
} else {
emailInput.classList.remove("is-invalid");
}
});
passwordInput.addEventListener("input", e => {
passwordInput.setCustomValidity("");
if (passwordInput.value === "") {
passwordInput.classList.add("is-invalid");
} else {
passwordInput.classList.remove("is-invalid");
}
});
projectUrlInput.addEventListener("input", e => {
projectUrlInput.setCustomValidity("");
if (!URL_REGEX.test(projectUrlInput.value)) {
projectUrlInput.classList.add("is-invalid");
} else {
projectUrlInput.classList.remove("is-invalid");
}
});
formIdInput.addEventListener("input", e => {
formIdInput.setCustomValidity("");
if (formInput.value === "") {
formIdInput.classList.add("is-invalid");
} else {
formIdInput.classList.remove("is-invalid");
}
});
/**
* Validate and upload the form entries to the script.
* Close the dialog if success.
*/
document.getElementById("configureButton").addEventListener("click", () => {
// Set to true if there is validation error
let errorFlag = false;
// Validate email (non-empty + default email check)
if (emailInput.value === "") {
emailInput.classList.add("is-invalid");
emailInput.setCustomValidity("Empty email address");
errorFlag = true;
}
emailInput.checkValidity();
// Validate password (non-empty)
if (passwordInput.value === "") {
passwordInput.classList.add("is-invalid");
passwordInput.setCustomValidity("Empty password");
errorFlag = true;
}
// Validate url (regex) to ensure the parsing of base url, projectid and formid
if (!URL_REGEX.test(projectUrlInput.value)) {
projectUrlInput.classList.add("is-invalid");
projectUrlInput.setCustomValidity("Format: https://<hostname>/[#|v1]/projects/<projectid>");
errorFlag = true;
}
// Validate formId (non-empty)
if (formIdInput.value === "") {
formIdInput.classList.add("is-invalid");
formIdInput.setCustomValidity("Empty form id");
errorFlag = true;
}
// Do not configure if there is validation error
if (errorFlag) {
return;
}
const email_value = emailInput.value;
const password_value = passwordInput.value;
const url = projectUrlInput.value.match(URL_REGEX); // Extract [_, base url, [#|v1], project id]
const formId = formIdInput.value;
google.script.run.setConfig(email_value, password_value, url[1], url[3], formId);
google.script.host.close();
});
})();
</script>
</body>
</html>