-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEmailForm.js
419 lines (367 loc) · 12.2 KB
/
EmailForm.js
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* # EmailForm
* Copyright(c) 2021 Stefano Balietti <[email protected]>
* MIT Licensed
*
* Displays a form to input email
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('EmailForm', EmailForm);
// ## Meta-data
EmailForm.version = '0.13.1';
EmailForm.description = 'Displays a configurable email form.';
EmailForm.title = false;
EmailForm.className = 'emailform';
EmailForm.texts = {
label: 'Enter your email:',
errString: 'Not a valid email address, ' +
'please correct it and submit it again.',
sent: 'Sent!'
};
/**
* ## EmailForm constructor
*
* @param {object} options configuration option
*/
function EmailForm(opts) {
/**
* ### EmailForm.onsubmit
*
* Options passed to `getValues` when the submit button is pressed
*
* @see EmailForm.getValues
*/
if (!opts.onsubmit) {
this.onsubmit = {
emailOnly: true,
send: true,
updateUI: true
};
}
else if ('object' === typeof opts.onsubmit) {
this.onsubmit = opts.onsubmit;
}
else {
throw new TypeError('EmailForm constructor: opts.onsubmit ' +
'must be object or undefined. Found: ' +
opts.onsubmit);
}
/**
* ### EmailForm._email
*
* Internal storage of the value of the email
*
* This value is used when the form has not been created yet
*
* @see EmailForm.createForm
*/
this._email = opts.email || null;
/**
* ### EmailForm.attempts
*
* Invalid emails tried
*/
this.attempts = [];
/**
* ### EmailForm.timeInput
*
* Time when the email was inserted (first character, last attempt)
*/
this.timeInput = null;
/**
* ### EmailForm.formElement
*
* The email's HTML form
*/
this.formElement = null;
/**
* ### EmailForm.inputElement
*
* The email's HTML input form
*/
this.inputElement = null;
/**
* ### EmailForm.buttonElement
*
* The email's HTML submit button
*/
this.buttonElement = null;
/**
* ### EmailForm.setMsg
*
* If TRUE, a set message is sent instead of a data msg
*
* Default: FALSE
*/
this.setMsg = !!opts.setMsg || false;
/**
* ### EmailForm.showSubmitBtn
*
* If TRUE, a set message is sent instead of a data msg
*
* Default: FALSE
*/
this.showSubmitBtn = 'undefined' === typeof opts.showSubmitBtn ?
true : !!opts.showSubmitBtn;
}
// ## EmailForm methods
EmailForm.prototype.createForm = function() {
var that;
var formElement, labelElement, inputElement, buttonElement;
that = this;
formElement = document.createElement('form');
formElement.className = 'emailform-form';
labelElement = document.createElement('label');
labelElement.innerHTML = this.getText('label');
inputElement = document.createElement('input');
inputElement.setAttribute('type', 'text');
inputElement.setAttribute('placeholder', 'Email');
inputElement.className = 'emailform-input form-control';
formElement.appendChild(labelElement);
formElement.appendChild(inputElement);
// Store references.
this.formElement = formElement;
this.inputElement = inputElement;
if (this.showSubmitBtn) {
buttonElement = document.createElement('input');
buttonElement.setAttribute('type', 'submit');
buttonElement.setAttribute('value', 'Submit email');
buttonElement.className = 'btn btn-lg btn-primary ' +
'emailform-submit';
formElement.appendChild(buttonElement);
// Add listeners on input form.
J.addEvent(formElement, 'submit', function(event) {
event.preventDefault();
that.getValues(that.onsubmit);
}, true);
J.addEvent(formElement, 'input', function() {
if (!that.timeInput) that.timeInput = J.now();
if (that.isHighlighted()) that.unhighlight();
}, true);
// Store reference.
this.buttonElement = buttonElement;
}
// If a value was previously set, insert it in the form.
if (this._email) this.formElement.value = this._email;
this._email = null;
return formElement;
};
/**
* ### EmailForm.verifyInput
*
* Verify current email, updates interface, and optionally marks attempt
*
* @param {boolean} markAttempt Optional. If TRUE, the current email
* is added to the attempts array. Default: TRUE
* @param {boolean} updateUI Optional. If TRUE, the interface is updated.
* Default: FALSE
*
* @return {boolean} TRUE, if the email is valid
*
* @see EmailForm.getValues
* @see getEmail
*/
EmailForm.prototype.verifyInput = function(markAttempt, updateUI) {
var email, res;
email = getEmail.call(this);
res = J.isEmail(email);
if (res && updateUI) {
if (this.inputElement) this.inputElement.disabled = true;
if (this.buttonElement) {
this.buttonElement.disabled = true;
this.buttonElement.value = this.getText('sent');
}
}
else {
if (updateUI && this.buttonElement) {
this.buttonElement.value = this.getText('errString');
}
if ('undefined' === typeof markAttempt || markAttempt) {
this.attempts.push(email);
}
}
return res;
};
/**
* ### EmailForm.append
*
* Appends widget to this.bodyDiv
*/
EmailForm.prototype.append = function() {
this.createForm();
this.bodyDiv.appendChild(this.formElement);
};
/**
* ### EmailForm.setValues
*
* Set the value of the email input form
*/
EmailForm.prototype.setValues = function(options) {
var email;
options = options || {};
if (!options.email) email = J.randomEmail();
else email = options.email;
if (!this.inputElement) this._email = email;
else this.inputElement.value = email;
this.timeInput = J.now();
};
/**
* ### EmailForm.getValues
*
* Returns the email and paradata
*
* @param {object} opts Optional. Configures the return value.
* Available optionts:
*
* - emailOnly: If TRUE, returns just the email (default: FALSE),
* - verify: If TRUE, check if the email is valid (default: TRUE),
* - reset: If TRUTHY and the email is valid, then it resets
* the email value before returning (default: FALSE),
* - markAttempt: If TRUE, getting the value counts as an attempt
* (default: TRUE),
* - updateUI: If TRUE, the UI (form, input, button) is updated.
* Default: FALSE.
* - highlight: If TRUE, if email is not the valid, widget is
* is highlighted. Default: (updateUI || FALSE).
* - send: If TRUE, and the email is valid, then it sends
* a data or set msg. Default: FALSE.
* - sendAnyway: If TRUE, it sends a data or set msg regardless of
* the validity of the email. Default: FALSE.
* - say: same as send, but deprecated.
* - sayAnyway: same as sendAnyway, but deprecated
*
* @return {string|object} The email, and optional paradata
*
* @see EmailForm.sendValues
* @see EmailForm.verifyInput
* @see getEmail
*/
EmailForm.prototype.getValues = function(opts) {
var email, res;
opts = opts || {};
if ('undefined' !== typeof opts.say) {
console.log('***EmailForm.getValues: option say is deprecated, ' +
' use send.***');
opts.send = opts.say;
}
if ('undefined' !== typeof opts.sayAnyway) {
console.log('***EmailForm.getValues: option sayAnyway is ' +
'deprecated, use sendAnyway.***');
opts.sendAnyway = opts.sayAnyway;
}
if ('undefined' === typeof opts.markAttempt) opts.markAttempt = true;
if ('undefined' === typeof opts.highlight) opts.highlight = true;
email = getEmail.call(this);
if (opts.verify !== false) {
res = this.verifyInput(opts.markAttempt, opts.updateUI);
}
// Only value.
if (!opts.emailOnly) {
email = {
time: this.timeInput,
email: email,
attempts: this.attempts,
};
if (opts.markAttempt) email.isCorrect = res;
}
if (res === false) {
if (opts.updateUI || opts.highlight) this.highlight();
this.timeInput = null;
}
// Send the message.
if ((opts.send && res) || opts.sendAnyway) {
this.sendValues({ values: email });
}
if (opts.reset) this.reset();
return email;
};
/**
* ### EmailForm.sendValues
*
* Sends a DATA message with label 'email' with current email and paradata
*
* @param {object} opts Optional. Options to pass to the `getValues`
* method. Additional options:
*
* - values: actual values to send, instead of the return
* value of `getValues`
* - to: recipient of the message. Default: 'SERVER'
*
* @return {string|object} The email, and optional paradata
*
* @see EmailForm.getValues
*/
EmailForm.prototype.sendValues = function(opts) {
var values;
opts = opts || { emailOnly: true };
values = opts.values || this.getValues(opts);
if (this.setMsg) {
if ('string' === typeof values) values = { email: values };
node.set(values, opts.to || 'SERVER');
}
else {
node.say('email', opts.to || 'SERVER', values);
}
return values;
};
/**
* ### EmailForm.highlight
*
* Highlights the email form
*
* @param {string} The style for the form border. Default: '1px solid red'
*
* @see EmailForm.highlighted
*/
EmailForm.prototype.highlight = function(border) {
if (border && 'string' !== typeof border) {
throw new TypeError('EmailForm.highlight: border must be ' +
'string or undefined. Found: ' + border);
}
if (!this.inputElement || this.highlighted === true) return;
this.inputElement.style.border = border || '3px solid red';
this.highlighted = true;
this.emit('highlighted', border);
};
/**
* ### EmailForm.unhighlight
*
* Removes highlight from the form
*
* @see EmailForm.highlighted
*/
EmailForm.prototype.unhighlight = function() {
if (!this.inputElement || this.highlighted !== true) return;
this.inputElement.style.border = '';
this.highlighted = false;
this.emit('unhighlighted');
};
/**
* ### EmailForm.reset
*
* Resets email and collected paradata
*/
EmailForm.prototype.reset = function() {
this.attempts = [];
this.timeInput = null;
this._email = null;
if (this.inputElement) this.inputElement.value = '';
if (this.isHighlighted()) this.unhighlight();
};
// ## Helper methods.
/**
* ### getEmail
*
* Returns the value of the email in form or in `_email`
*
* Must be invoked with right context
*
* @return {string|null} The value of the email, if any
*/
function getEmail() {
return this.inputElement ? this.inputElement.value : this._email;
}
})(node);