/
paper-step.js
executable file
·469 lines (444 loc) · 11.1 KB
/
paper-step.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
'use strict';
Polymer({
is: "paper-step",
properties: {
/**
* Computed css classes based on the current `step` and the total
* number value of `steps`.
*/
_cssClass: {
type: String,
computed: '__cssClass(step, steps)'
},
/**
* Computed for the `paper-step` image icon based on the supplied `icon`
* attribute and the `completed` property.
*/
_image: {
type: String,
computed: '_icon(editable)'
},
/**
* Indicates if the current `paper-step` is selected and toggles the
* active/inactive css style rules.
*/
_selected: {
type: Boolean,
value: false
},
/**
* If true, the submit buttons which are automatically
* added will be hidden for this `paper-step` element.
*/
actionsDisabled: {
type: Boolean
},
/**
* If true, the current step has been validated and marked as done.
*/
completed: {
type: Boolean,
value: false,
reflectToAttribute: true,
observer: '_onComplete'
},
/**
* Specify an alternate label to use for the `continue` button.
*/
continueLabel: {
type: String,
value: 'Continue'
},
/**
* A copy of the serialized form data for the last successful submit.
*/
data: Object,
/**
* Used internally to debouce custom events:
* `paper-step-complete`, and `paper-step-next`.
*/
duplicate: {
type: Boolean,
value: false
},
/**
*
*/
editable: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* An object of initial values for this `paper-step`, the object should be
* name value pairs which correspond to the form input names.
*/
initial: {
type: Object,
observer: '_onChangeInitial'
},
/**
* The text to display in the steps area next to the image icon.
*/
label: {
type: String
},
lastErrorResponse: Object,
lastSuccessResponse: Object,
/**
* To indicate if the current step required or optional.
*/
optional: {
type: Boolean,
value: false
},
/**
* Specify an alternate label to use for the `reset` button.
*/
resetLabel: {
type: String,
value: 'Reset'
},
selectable: {
type: Boolean,
computed: '_selectable(completed, editable)',
reflectToAttribute: true
},
skipLabel: {
type: String,
value: 'Skip'
},
/**
* The 1 based index value for the current `paper-step` in `paper-steps`.
*
* `Automatically calculated`.
*/
step: {
type: Number
},
/**
* The value for the total number of `paper-step` elements (readonly).
*
* `Automatically calculated`.
*/
steps: {
type: Number
}
},
behaviors: [],
listeners: {
//error after submit, IronRequestElement in event.detail object
'iron-form-error': '_onResponse', //'_onError',
//cannot submit, form is invalid
// 'iron-form-invalid': '',
'iron-form-presubmit': '_onPreSubmit',
// //after form is reset
// 'iron-form-reset': '_reset',
//after form submitted, IronRequestElement inevent.detail object
'iron-form-response': '_onResponse',
'iron-form-submit': '_onSubmit', //after submitted
'continue.tap': '_submit',
'reset.tap': '_reset',
'skip.tap': '_skip'
},
// Element Lifecycle
ready: function() {},
attached: function() {
var
parent = this.parentNode && this.parentNode.indexOf && this.parentNode || null,
form = this._getForm()
;
if (!Boolean(parent)) {
try {
if (this.duplicate) {
parent = this.parentNode.$.selector;
} else {
parent = this.parentNode.$.steps_content;
}
} catch (e) {
parent = null;
}
}
// debugger;
if (!parent || parent.hasOwnProperty('indexOf') || typeof parent.indexOf != 'function') {
this.step = 1;
this.steps = 1;
try {
console.log('Improperly declared paper-step element, ' +
'it is missing the parent iterator.');
} catch (e) {}
return;
}
if (!Boolean(this.step)) {
this.step = parent.indexOf(this) + 1;
}
if (!Boolean(this.steps)) {
this.steps = parent.items.length;
}
if (form && Polymer.isInstance(form)) {
var i, child, len,
children = form.getEffectiveChildren()
;
for (i=0, len=children.length; i<len; i++) {
child = children[i];
this.listen(child, 'keyup', '_onChange');
}
this.listen(form, 'iron-activate', '_onActivate');
}
},
detached: function() {},
/**
* Computes `_cssClass` property.
* @return {string}.
*/
__cssClass: function(step, steps) {
var cls = (this.optional ? 'optional' : 'required') + '-step ';
if (step === 1) {
cls += 'first ';
}
if (step === steps) {
cls += 'last ';
}
return cls;
},
/**
* Attempts to retrieve the `iron-form` element for the current `paper-step`.
*/
_getForm: function() {
var
el = Polymer.dom(this),
form = el && el.node &&
el.node.querySelector('form[is="iron-form"]')
;
return form || false;
},
/**
* Attempts to retrieve the `submit` button for the current `paper-step`.
*/
_getPrimaryButton: function() {
var el, button;
if (this.actionsDisabled) {
el = this._getForm();
if (el) {
el = Polymer.dom(el).node.querySelectorAll(
'button,paper-button,input[type="submit"]');
button = el && el.length && el[0];
if (button) {
return button;
}
}
} else {
return this.$.continue;
}
return undefined;
},
/**
* Computes `image` property based on step `icon` and current `step`.
*/
_icon: function(editable) {
if (editable) {
return 'icons:create';
}
return 'icons:check';
// return 'image:brightness-1';
},
/**
* Event handler to ignore form elements which trigger any iron-select
* events.
*/
_onActivate: function(e) {
//ignore iron-selector events from form children
e.stopPropagation();
},
/**
* Event handler to change the completed state of a `paper-step` once
* any form inputs are changed.
*/
_onChange: function(e) {
if (this.completed) {
this.completed = false;
}
},
/**
*
*/
_onChangeInitial: function(_new, _old) {
var
i, len, input, elems, key, value,
changed = [],
form = this._getForm(),
initial = _new,
allowedInput = function(value) {
return value !== undefined && value !== null;
}
;
// _new instanceof Object
if (form && Boolean(initial) && initial instanceof Object) {
elems = form.elements;
// loop over form.elements and set inputs with non-empty name value from
// initial object
for (i=0, len=elems.length; i<len; i++) {
input = elems[i];
// if input.name != "" then set it with input.value = 'value'
if (Boolean(input) && input.name != "") {
try {
value = initial[input.name] || null;
if (allowedInput(value)) {
if (Polymer.isInstance(input)) {
input.set('value', value);
input.fire('blur', value);
} else {
input.value = value;
}
changed.push(input.name);
}
} catch (e) {
// console.log('Error while setting initial for "'+ input.name +'": ' + e);
}
}
}
// loop over name value pairs
for (key in initial) {
input = form.querySelector('[name="'+ key +'"] input');
value = initial[key] || null;
if (allowedInput(value) && Boolean(input) && !changed.includes(key)) {
try {
if (Polymer.isInstance(input)) {
input.set('value', initial[key]);
input.fire('blur', value);
} else {
input.value = initial[key];
}
} catch (e) {
// console.log('Error while setting initial for "'+ input.name +'": ' + e);
}
}
}
}
},
/**
* Notify parent of completed state change.
*/
_onComplete: function(_new, _old) {
if (this.duplicate === false) {
if (_new === true) {
this.fire('paper-step-complete', this.step);
this.fire('paper-step-next', this.step);
} else if (_new === false) {
this.fire('paper-step-incomplete', this.step);
}
}
},
/**
*
*/
_onError: function(e) {},
/**
*
*/
_onPreSubmit: function(e) {
var
form = this._getForm()
;
if (this.completed) {
e.preventDefault();
this.fire('paper-step-already-complete', this);
}
},
/**
* Handles iron form events for `iron-form-error` and `iron-form-response`.
*/
_onResponse: function(e) {
var
el = this._getPrimaryButton(),
request = e && e.detail || false
;
if (Polymer.isInstance(el) && el.disabled !== undefined) {
el.disabled = false;
}
if (request && request.status === 200) {
this.lastSuccessResponse = request;
try {
this.data = this._getForm().serialize();
} catch (ex) {
this.data = {}
}
this.completed = true;
} else {
this.lastErrorResponse = request;
}
},
/**
* Hooks into the `iron-form-submit` event to disable the form submit button.
*/
_onSubmit: function(e) {
var
el = this._getPrimaryButton()
;
if (Polymer.isInstance(el) && el.disabled !== undefined) {
el.disabled = true;
}
},
/**
*/
_selectable: function(completed, editable) {
return editable || !completed;
},
/**
* When the `Reset` button is pressed, this restores the form elements
* to their initial state by calling `form.reset()`.
*/
_reset: function(e) {
var
event = e,
that = this,
form
;
this.debounce('paper-step-reset', function() {
form = that._getForm();
// reset form if it exists.
if (form) {
//TODO: confirm?
//then reset
form.reset();
that.completed = false;
}
}, 300);
},
/**
* When the `Skip` button is pressed, this will the `_reset()` method,
* and set the current `paper-step.completed` property to `true`, and then
* advance to the next `paper-step` element.
*/
_skip: function(e) {
var
event = e,
that = this,
form
;
//reset form
this._reset(e);
this.debounce('paper-step-skip', function() {
this.completed = true;
}, 300);
},
/**
* When the `Continue` button is pressed, check if form is valid
* and then trigger the form `submit` event.
*/
_submit: function(e) {
var
event = e,
that = this,
form
;
// collapse multiple submissions and only trigger once.
this.debounce('paper-steps-submit', function() {
form = that._getForm();
// submit form if it exists.
if (form) {
form.submit();
}
}, 300);
}
});