-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar.html
276 lines (254 loc) · 8.71 KB
/
sidebar.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html>
<!-- This file has been modified from the Google Forms add-on sample -->
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<!-- The CSS package above applies Google styling to buttons and other elements. -->
<style>
.branding-below {
bottom: 54px;
top: 0;
}
.branding-text {
left: 7px;
position: relative;
top: 3px;
}
.logo {
vertical-align: middle;
}
.width-100 {
width: 100%;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
label {
font-weight: bold;
}
</style>
</head>
<body>
<div class="sidebar branding-below">
<form method="dialog" target="_self">
<h1>Turnstile Settings</h1>
<div class="block">
<input type="checkbox" id="enable-turnstile">
<label for="enable-turnstile">Enable Turnstile</label>
</div>
<div class="block form-group" id="turnstile-options">
<label for="site-secret">
Site secret
</label>
<input type="text" class="width-100" id="site-secret">
<input type="checkbox" id="mark-as-token-sheet">
<label for="mark-as-token-sheet">Mark as Token Sheet</label>
</div>
<br>
<h1>Notification Settings</h1>
<div class="block">
<input type="checkbox" id="enable-notifications">
<label for="enable-notifications">Enable Notifications</label>
</div>
<p>Notifications are sent only for turnstile validated responses.</p>
<div class="block form-group" id="notification-options">
<label for="webhook-urls">
Webhook URLs
Enter each webhook on a separate line.
</label>
<textarea id="webhook-urls" name="Webhook URLs" rows="5" class="width-100"></textarea>
<label for="notification-template">
Notification Template
</label>
<select id="notification-template"></select>
<label for="response-order">
Response Order
Enter each question name on a separate line.
</label>
<textarea id="response-order" name="Response Order" rows="5" class="width-100"></textarea>
</div>
<div class="block" id="button-bar">
<button class="action" id="save-settings">Save</button>
</div>
</form>
</div>
<div class="sidebar bottom">
<img alt="Add-on logo" class="logo" width="25"
src="https://lh3.googleusercontent.com/-nPgH1PIWWXY/ZK9MQbbYjzI/AAAAAAAAAAM/NbHbkEFaUX8CoFrXiHR0fbR6WrJ1LkxRgCNcBGAsYHQ/s400/Turnstile%2Bfor%2BForms%2B128.png">
<span class="gray branding-text">Turnstile for Forms</span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
const buttonBar = $('#button-bar');
const templateSelect = $('#notification-template');
/**
* On document load, assign required handlers to each element,
* and attempt to load any saved settings.
*/
(function() {
$('#save-settings').on('click', saveSettingsToServer);
$('#enable-turnstile').on('click', toggleEnableTurnstile);
$('#enable-notifications').on('click', toggleEnableNotifications);
if (typeof google !== 'undefined') {
google.script.run
.withSuccessHandler(loadSettings)
.withFailureHandler(showStatus)
.withUserObject(buttonBar)
.getSettings();
google.script.run
.withSuccessHandler(loadSiteSecret)
.withFailureHandler(showStatus)
.withUserObject(buttonBar)
.getSiteSecret();
} else {
console.log('Dev environment detected, skipped loading settings');
}
})();
/**
* Callback function that populates the notification options using
* previously saved values.
*
* @param {Object} settings The saved settings from the client.
*/
function loadSettings(settings) {
const enableTurnstileCheckbox = $('#enable-turnstile');
const markAsTokenSheetCheckbox = $('#mark-as-token-sheet');
const enableNotificationsCheckbox = $('#enable-notifications');
// Turnstile
enableTurnstileCheckbox.prop('checked', settings.enableTurnstile === 'true');
markAsTokenSheetCheckbox.prop('checked', settings.isTokenSheet === 'true');
if (settings.enableTurnstile === 'true') {
$('#turnstile-options').show();
if (settings.isTokenSheet !== 'true') {
// Notifications
if (settings.notificationTemplates) {
for (const [templateDisplayName, templateName] of Object.entries(settings.notificationTemplates)) {
templateSelect.append(new Option(templateDisplayName, templateName));
}
} else {
showStatus('Error: Notification templates undefined', buttonBar);
}
if (settings.enableNotifications === 'true') {
enableNotificationsCheckbox.prop('checked', true);
$('#webhook-urls').val(settings.webhookUrls);
$('#response-order').val(settings.responseOrder);
$('#notification-options').show();
} else {
$('#notification-options').hide();
}
} else {
$('#notification-options').hide();
}
} else {
$('#turnstile-options').hide();
$('#notification-options').hide();
}
}
function loadSiteSecret(siteSecret) {
$('#site-secret').val(siteSecret);
}
/**
* Toggles the visibility of the form creator notification options.
*/
function toggleEnableTurnstile() {
$('#status').remove();
if ($('#enable-turnstile').is(':checked')) {
$('#turnstile-options').show();
} else {
$('#turnstile-options').hide();
}
}
/**
* Toggles the visibility of the form creator notification options.
*/
function toggleEnableNotifications() {
$('#status').remove();
if ($('#enable-notifications').is(':checked')) {
$('#notification-options').show();
} else {
$('#notification-options').hide();
}
}
/**
* Collects the options specified in the add-on sidebar and sends them to
* be saved as Properties on the server.
*/
function saveSettingsToServer() {
buttonBar.prop('disabled', true);
$('#status').remove();
let enableTurnstile = $('#enable-turnstile').is(':checked');
let enableNotifications = $('#enable-notifications').is(':checked');
let settings = {
'enableTurnstile': enableTurnstile,
'enableNotifications': enableNotifications
};
// Only save turnstile options if turnstile is turned on
if (enableTurnstile) {
settings.siteSecret = $('#site-secret').val().trim();
settings.isTokenSheet = $('#mark-as-token-sheet').is(':checked');
// Abort save if any options are missing.
if (!settings.siteSecret) {
showStatus('Enter a site secret', buttonBar);
this.disabled = false;
return;
}
}
// Save notification settings only if notifications are enabled
if (enableNotifications) {
settings.webhookUrls = $('#webhook-urls').val().trim();
settings.responseOrder = $('#response-order').val().trim();
settings.notificationTemplate = $('#notification-template option:selected').val();
// Abort save if any options are missing.
if (!settings.webhookUrls) {
showStatus('Enter at least one webhook URL', buttonBar);
buttonBar.prop('disabled', false);
return;
}
if (!settings.responseOrder) {
showStatus('Enter a response order', buttonBar);
buttonBar.prop('disabled', false);
return;
}
if (!settings.notificationTemplate) {
showStatus('Select a notification template', buttonBar);
buttonBar.prop('disabled', false);
return;
}
}
// Save the settings on the server
if (typeof google !== 'undefined') {
google.script.run
.withSuccessHandler(
function (msg) {
showStatus('Saved settings', buttonBar);
buttonBar.prop('disabled', false);
})
.withFailureHandler(
function (msg) {
showStatus(msg, buttonBar);
buttonBar.prop('disabled', false);
})
.withUserObject(buttonBar)
.saveSettings(settings);
} else {
console.log(JSON.stringify(settings));
}
}
/**
* Inserts a div that contains an status message after a given element.
*
* @param {String} msg The status message to display.
* @param {Object} element The element after which to display the Status.
*/
function showStatus(msg, element) {
const div = $('<div>')
.attr('id', 'status')
.attr('class', 'error')
.text(msg);
$(element).after(div);
}
</script>
</body>
</html>