-
Notifications
You must be signed in to change notification settings - Fork 3
/
caribou-stepper.html
375 lines (341 loc) · 14.5 KB
/
caribou-stepper.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
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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="caribou-stepper-stepper-property-mixin.html">
<dom-module id="caribou-stepper">
<template>
<style>
:host {
display: block;
--primary-color: #2196F3;
position: relative;
}
:host([horizontal]) {
@apply --layout-horizontal;
}
</style>
<slot></slot>
</template>
<script>
/**
* `<caribou-stepper>` is an element used to display progress through a sequence of logical and numbered steps.
* This element need to be used with the `caribou-step` element.
* You can add the number of step you want inside your stepper.
* You can set multiple attribute to the stepper :
* - linear : this require users to complete one step in order to move on to the next.
* If linear is false, it allow users to enter a multi-step flow at any point.
* - Mobile : TODO - This is used when there is multitude of step on a mobile view we will compact it.
* - Horizontal : True if you want to use the stepper horizontaly. Otherwise (default) it's vertical.
* - OpenFirstStepOnStartup : If true, the first step will be open after load time.
*
* Example:
* ```html
* <caribou-stepper linear>
* <caribou-step>...</caribout-step>
* <caribou-step>...</caribout-step>
* <caribou-step>...</caribout-step>
* </caribou-stepper>
* ```
*
* @polymer
* @customElement
* @memberof Caribouflex
* @mixes Caribouflex.Stepper.StepperPropertyMixin
* @demo demo/index.html
* @element caribou-stepper
*/
class CaribouStepperElement extends Caribouflex.Stepper.StepperPropertyMixin(Polymer.Element) {
/**
* Fired when the stepper is finished and all the steps are saved.
*
* @event stepper-finished
* @param {Object} detail empty
*/
static get is() {
return 'caribou-stepper';
}
static get properties() {
return {
/**
* Contain all the steps registered.
*/
_steps: {
type: Array,
value: function () {
return [];
}
},
/**
* This property is toggled when the reset function is called.
* When we are currently resetting the value, the property is true, false otherwise.
* @readonly
*/
isResetting: {
type: Boolean,
value: false,
readOnly: true
}
};
}
/**
* This function is used by the `caribou-step` to register into the `caribou-stepper`.
* Then multiple event fired from the stepper can be handled :
* - last-step-closed
* - continue-clicked : call the __nextStep() function.
* - finish-clicked : call the __nextStep() function.
* - skip-clicked : call the __skipStep() function.
* - update-clicked : call the __nextStep() function.
*
* All elements registered are added into the _steps property.
*/
_registerStep(element) {
this.push('_steps', element);
// element.addEventListener("last-step-closed", this._validStepper.bind(this));
element.addEventListener("continue-clicked", this.__nextStep.bind(this));
element.addEventListener("finish-clicked", this.__finishStep.bind(this));
element.addEventListener("skip-clicked", this.__skipStep.bind(this));
element.addEventListener("back-clicked", this.__previousStep.bind(this));
element.addEventListener("update-clicked", this.__nextStep.bind(this));
}
/**
* This is a private callback function used to go to the next step when a button is clicked.
* This will save and close the active step, then open the next one.
* @private
*/
__nextStep(e) {
this.openNextStep();
}
/**
* This is a private callback function used to skip the current state and open the next one.
* This can be done only if we are in linear mode.
* @private
*/
__skipStep(e) {
this.skipStep();
}
/**
* This is a private callback function used to skip the current state and open the next one.
* This can be done only if we are in linear mode.
* @private
*/
__previousStep(e) {
this.openPreviousStep();
}
/**
* This is a private callback function used to skip the current state and open the next one.
* This can be done only if we are in linear mode.
* @private
*/
__finishStep(e) {
if (this.activeStep._nestedValidate() && this.activeStep.validate()) {
this.openNextStep();
this._setFinish(true);
this.dispatchEvent(new CustomEvent('stepper-finished', {
bubbles: true,
composed: true
}));
console.log("caribou-stepper finished.");
} else {
this.activeStep.fireInvalidStep();
}
}
/**
* This function is used to open the next step.
* It will save and close the current active step.
* Then it will open the next step if there is one.
*/
openNextStep() {
if (this.activeStep._nestedValidate() && this.activeStep.validate()) {
this.activeStep.saveStep();
//set the value to and from the the transition effect.
this.__fromStep = this.activeStep;
this.__toStep = this.nextStep;
//toggle the step
this.activeStep.toggleStep();
this.removeActiveStep();
if (this.nextStep !== null)
this.nextStep.toggleStep();
} else {
this.activeStep.fireInvalidStep();
}
}
/**
* This function is used to open the next step.
* It will save and close the current active step.
* Then it will open the next step if there is one.
*/
openPreviousStep() {
this.activeStep.toggleStep();
this.removeActiveStep();
if (this.previousStep !== null)
this.previousStep.toggleStep();
}
/**
* This function is used to skip the current active step.
* This function can work only if we are not in linear mode.
* TODO : allow the
*/
skipStep() {
if ((this.linear && this.activeStep.optional) || !this.linear) {
if (this.linear)
this.activeStep.saveStep();
this.activeStep.toggleStep();
if (this.nextStep !== null)
this.nextStep.toggleStep();
} else
console.log("can't skip you are in linear mode.");
}
/**
* This function is used to close all the steps and save the target step
* that is the initiative of the close.
* @param {Object} stepInitiator The step inititor and targeted.
*/
__closeAllStep(stepInitiator) {
//we set the to step used for the transition from the step button.
this.__toStep = stepInitiator;
this.closeAllStep();
}
/**
* This function is used to close all the steps.
*/
closeAllStep() {
for (var i = 0; i < this._steps.length; i++) {
var step = this.getStepById(i);
if (step.isActive()) {
step.toggleStep();
}
}
}
/**
* This function is used to check if all steps are saved,
* if not, it will open the first step in linear order that is not saved.
*
* @return {Object} The stepper to open if there is or null.
* @private
*/
_getUnsavedStep() {
for (var i = 0; i < this._steps.length; i++) {
var step = this.getStepById(i);
if (!step.save) {
if (step !== this.activeStep)
return step;
}
}
return null;
}
/**
* This function is used to check all the previous step the the active one.
* If there is a step that is not saved or editabled, we returned it.
*
* @return {Object} The stepper to open if there is or null.
* @private
*/
_getPreviousStep() {
for (var i = this.activeStep._badgeNumber - 1; i == 0; i--) {
var step = this.getStepById(i);
if (!step.save || step.editable) {
if (step !== this.activeStep)
return step;
}
}
return null;
}
/**
* This function is used to return the step element
* situated at the specific ID.
* @param {Number} id The id of the step to retrieve
* @return {Object} The stepper object.
*/
getStepById(id) {
return this.get(['_steps', id])
}
/**
* This function is used to set the active step.
* @param {Object} step The step object.
*/
setActiveStep(step) {
this.activeStep = step;
// When we are resetting the values, the activeStep and next step depends of the 'openFirstStepOnStartup' property.
if (!this.isResetting) {
this.setNextStep(this.findNextStep(step));
this.setPreviousStep(this.findPreviousStep(step));
if (this.nextStep === null)
this.activeStep.setFinishActionButtonsState(true);
}
}
/**
* This function is used clear the active step property.
*/
removeActiveStep() {
this.activeStep = null;
}
/**
* This function is used to set the nextStep property.
* @param {Object} step The step object
*/
setNextStep(step) {
this.nextStep = step;
}
/**
* This function is used to set the previousStep property.
* @param {Object} step The step object
*/
setPreviousStep(step) {
this.previousStep = step;
}
/**
* This function is used to find the next next following step of a specific one.
* @param {Object} activeStep The step from which we are looking the next possible step.
*/
findNextStep(activeStep) {
if (activeStep.nextElementSibling !== undefined && activeStep.nextElementSibling !== null && !
activeStep.nextElementSibling.save) {
return activeStep.nextElementSibling;
} else if (activeStep.nextElementSibling !== undefined && activeStep.nextElementSibling !==
null)
return this.findNextStep(activeStep.nextElementSibling);
else {
return this._getUnsavedStep();
}
}
/**
* This function is used to find the next next following step of a specific one.
* @param {Object} activeStep The step from which we are looking the next possible step.
*/
findPreviousStep(activeStep) {
if (activeStep.previousElementSibling !== undefined && activeStep.previousElementSibling ===
null)
return activeStep.previousElementSibling;
if (activeStep.previousElementSibling !== undefined && activeStep.previousElementSibling !==
null && (!activeStep.previousElementSibling.save || activeStep.previousElementSibling.editable)
) {
return activeStep.previousElementSibling;
} else if (activeStep.previousElementSibling !== undefined && activeStep.previousElementSibling !==
null)
return this.findPreviousStep(activeStep.previousElementSibling);
else {
return this._getPreviousStep();
}
}
/**
* This function is used to reinitialize all the parameter of this element
* and from all the steppers.
*/
reset() {
this._setIsResetting(true);
this._setFinish(false);
this.activeStep = undefined;
this.nextStep = undefined;
for (var i = 0; i < this._steps.length; i++) {
var step = this.getStepById(i);
step.reset(this.openFirstStepOnStartup);
}
this._setIsResetting(false);
}
}
window.customElements.define(CaribouStepperElement.is, CaribouStepperElement);
/**
* @namespace Caribouflex
*/
window.Caribouflex = window.Caribouflex || {};
window.Caribouflex.CaribouStepperElement = CaribouStepperElement;
</script>
</dom-module>