-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSidebar.html
326 lines (292 loc) · 10.3 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!DOCTYPE html>
<html>
<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;
}
label span {
font-weight: normal;
}
#code-options,
#quiz-options,
#respondent-options,
#email-options {
background-color: #eee;
border-color: #eee;
border-width: 5px;
border-style: solid;
display: none;
}
#respondent-options,
#code-options {
display:block;
}
#code-salt,
#code-length,
#respondent-email,
#button-bar,
#submit-subject {
margin-bottom: 10px;
}
#required-score {
display: inline;
}
</style>
</head>
<body>
<div class="sidebar branding-below">
<form>
<div class="block form-group" id="code-options">
<label for="code-salt">
Salt <span>unique key that helps you identify responses from this form</span>
</label>
<input type="text" class="width-100" id="code-salt">
<label for="code-length">
Code Length <span>length of the generated verification codes</span>
</label>
<input type="number" min="1" max="32"
class="width-100" id="code-length">
</div>
<div class="block">
<input type="checkbox" id="email-me">
<label for="email-me"><span>Select if you'd like codes to be emailed to you, too.</span></label>
</div>
<div class="block form-group" id="email-options">
<label for="email-address">Email Address <span>(that receives copy of successful submission results)</span></label>
<input class="width-100" type="email" id="email-address">
</div>
<div class="block form-group" id="respondent-options">
<label for="respondent-email">
Which question asks for their email?
</label>
<select class="width-100" id="respondent-email"></select>
<label for="submit-subject">
Notification email subject:
</label>
<input type="text" class="width-100" id="submit-subject">
<label for="submit-notice">Notification email body:</label>
<textarea rows="8" cols="40" id="submit-notice"
class="width-100"></textarea>
</div>
<div class="block">
<input type="checkbox" id="quiz-score">
<label for="quiz-score">Require minimum score <span>(automatically converts form to quiz)</span></label>
</div>
<div class="block form-group" id="quiz-options">
<label for="required-score">Passing Score</label>
Required percent score (default 90)
<input type="number" id="required-score" value="90"
min="0" max="100" class="width-100" >
</div>
<div class="block" id="button-bar">
<button class="action" id="save-settings">Save</button>
</div>
</form>
</div>
<div class="sidebar bottom">
<span class="gray branding-text">Form Code Generator by <a href="http://alvinwan.com">Alvin Wan</a></span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
/**
* On document load, assign required handlers to each element,
* and attempt to load any saved settings.
*/
$(function() {
$('#save-settings').click(saveSettingsToServer);
$('#quiz-score').click(toggleQuizScore);
$('#email-me').click(toggleEmailMe);
$('#required-score').change(validateNumber);
google.script.run
.withSuccessHandler(loadSettings)
.withFailureHandler(showStatus)
.withUserObject($('#button-bar').get())
.getSettings();
});
/**
* Callback function that populates the notification options using
* previously saved values.
*
* @param {Object} settings The saved settings from the client.
*/
function loadSettings(settings) {
$('#code-salt').val(settings.codeSalt);
$('#code-length').val(settings.codeLength);
$('#required-score').val(!settings.requiredScore ?
90 : settings.requiredScore);
$('#submit-subject').val(!settings.responseSubject ?
'Thank you for filling out our form!' :
settings.responseSubject);
$('#submit-notice').val(!settings.responseText ?
'Thank you for responding to our form!' :
settings.responseText);
if (settings.quizScore === 'true') {
$('#quiz-score').prop('checked', true);
$('#quiz-options').show();
}
if (settings.emailMe === 'true') {
$('#email-me').prop('checked', true);
$('#email-options').show();
$('#email-address').val(settings.emailMeAddress);
}
// Fill the respondent email select box with the
// titles given to the form's text Items. Also include
// the form Item IDs as values so that they can be
// easily recovered during the Save operation.
for (var i = 0; i < settings.textItems.length; i++) {
var option = $('<option>').attr('value', settings.textItems[i]['id'])
.text(settings.textItems[i]['title']);
$('#respondent-email').append(option);
}
$('#respondent-email').val(settings.respondentEmailItemId);
}
/**
* Toggles the visibility of email me options.
*/
function toggleEmailMe() {
if($('#email-me').is(':checked')) {
$('#email-options').show();
} else {
$('#email-options').hide();
}
}
/**
* Toggles the visibility of the quiz options.
*/
function toggleQuizScore() {
$('#status').remove();
if($('#quiz-score').is(':checked')) {
maybeConvertToQuiz();
$('#quiz-options').show();
} else {
$('#quiz-options').hide();
}
}
/**
* Attempt converting form to quiz.
*/
function maybeConvertToQuiz() {
google.script.run
.withSuccessHandler(
function(msg, element) {
showStatus(msg, $('#button-bar'));
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
showStatus(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.convertToQuiz();
}
/**
* Ensures that the entered step is a number between 0
* and 100, inclusive.
*/
function validateNumber() {
var value = $('#required-score').val();
if (!value) {
$('#required-score').val(90);
} else if (value < 0) {
$('#required-score').val(0);
} else if (value > 100) {
$('#required-score').val(100);
}
}
/**
* Collects the options specified in the add-on sidebar and sends them to
* be saved as Properties on the server.
*/
function saveSettingsToServer() {
this.disabled = true;
$('#status').remove();
var quizScore = $('#quiz-score').is(':checked');
var emailMe = $('#email-me').is(':checked');
var settings = {
'quizScore': quizScore,
'emailMe': emailMe
};
settings.codeSalt = $('#code-salt').val().trim();
settings.codeLength = $('#code-length').val().trim();
// Abort save if entered salt is blank
if (!settings.codeSalt) {
showStatus('Enter a code salt', $('#button-bar'));
this.disabled = false;
return;
}
// Abort save if entered code length is blank
let length = parseInt(settings.codeLength);
if (!length || !Number.isInteger(length) || length < 1 || length > 32) {
if (!length) message = 'Enter a code length';
if (!Number.isInteger(length)) message = 'Enter a valid number.';
if (length < 1 || length > 32) message = 'Number must be between 1 and 32';
showStatus(message, $('#button-bar'));
this.disabled = false;
return;
}
// Only save quiz options if quiz score is turned on.
if (quizScore) {
settings.requiredScore = $('#required-score').val();
}
// Only save email options if email me is turned on.
if (emailMe) {
settings.emailMeAddress = $('#email-address').val();
}
settings.respondentEmailItemId = $('#respondent-email').val();
settings.responseSubject = $('#submit-subject').val();
settings.responseText = $('#submit-notice').val();
// Save the settings on the server
google.script.run
.withSuccessHandler(
function(msg, element) {
showStatus('Saved settings', $('#button-bar'));
element.disabled = false;
})
.withFailureHandler(
function(msg, element) {
showStatus(msg, $('#button-bar'));
element.disabled = false;
})
.withUserObject(this)
.saveSettings(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) {
var div = $('<div>')
.attr('id', 'status')
.attr('class','error')
.text(msg);
$(element).after(div);
}
</script>
</body>
</html>