forked from urdeveloper/ud-stepper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathud-stepper.html
473 lines (413 loc) · 13.3 KB
/
ud-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
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
470
471
472
473
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/elements/dom-repeat.html">
<link rel="import" href="../paper-styles/element-styles/paper-material-styles.html">
<link rel="import" href="../paper-styles/color.html">
<link rel="import" href="../paper-styles/typography.html">
<link rel="import" href="../iron-selector/iron-selector.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../polymer/lib/mixins/mutable-data.html">
<link rel="import" href="ud-step.html">
<link rel="import" href="ud-iconset.html">
<dom-module id="ud-stepper">
<template>
<style include="paper-material-styles">
:host {
display: flex;
flex-direction: column;
}
.header-container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
@apply --ud-stepper-header-container-style;
}
/* Horizontal Styles */
.header {
display: flex;
flex-direction: row;
justify-content: center;
overflow: hidden;
}
:host(:not([vertical])) .header:not(:last-of-type) {
flex: 1;
}
:host(:not([vertical])) .header:not(:last-of-type)::after {
content: '';
display: inline-block;
position: relative;
flex: 1;
top: 36px;
height: 1px;
margin-left: -12px;
width: 200px;
background-color: rgba(0, 0, 0, 0.1);
}
.header .label {
display: flex;
flex-direction: row;
cursor: pointer;
align-items: center;
padding: 24px 24px 24px 24px;
}
.header .label:hover {
background-color: #f0f0f0;
}
.label-icon {
color: rgba(0, 0, 0, 0.38);
@apply --paper-font-caption;
text-align: center;
line-height: 24px;
}
.label-text {
@apply --paper-font-body2;
padding-left: 8px;
color: rgba(0, 0, 0, 0.54);
}
.label-text .optional,
.label-text .summary {
@apply --paper-font-caption;
}
:host(:not([vertical])) .label-text .main {
@apply --paper-font-common-nowrap;
max-width: 120px;
}
.header.selected .label-icon,
.header[completed] .label-icon {
color: var(--ud-stepper-icon-completed-color, var(--google-blue-500));
}
.header.selected .label-text,
.header[completed] .label-text {
color: rgba(0, 0, 0, 0.87);
}
.header[error] .label-text,
.header[error] .label-icon {
color: var(--ud-stepper-icon-error-color, --paper-deep-orange-a700);
}
.step-number {
color: white;
background-color: rgba(0, 0, 0, 0.38);
border-radius: 50%;
line-height: 24px;
width: 24px;
height: 24px;
max-height: 24px;
max-width: 24px;
}
.header.selected .step-number {
background-color: var(--ud-stepper-icon-seleced-color, var(--google-blue-500));
}
/* Vertical Styles */
:host([vertical]) .header-container {
flex-direction: column;
flex-wrap: nowrap;
align-items: stretch;
}
:host([vertical]) .header {
flex-direction: column;
justify-content: flex-start;
overflow: visible;
}
:host([vertical]) .vertical-step ::slotted(ud-step) {
flex: 1;
}
.vertical-step {
padding-left: 32px;
display: flex;
flex-flow: row;
align-items: stretch;
}
.header:not(:last-of-type) .vertical-step::before {
content: '';
display: inline-block;
position: relative;
width: 1px;
left: 3px;
margin-top: -10px;
margin-bottom: -16px;
background-color: rgba(0, 0, 0, 0.1);
}
:host([vertical]) .header .label {
padding: 24px 24px 16px 24px;
}
::slotted(ud-step:not([selected])) {
display: none;
}
.vertical-step ::slotted(ud-step:not([selected])) {
display: none;
}
</style>
<iron-selector id="selector" class="header-container" selectable=".header.selectable" selected-class="selected" selected="{{selected}}"
on-iron-activate="_handleStepActivate" selected-attribute="selected">
<dom-repeat items="[[_steps]]" mutable-data>
<template>
<div class="header selectable" completed$="[[item.completed]]" error$="[[item.error]]">
<div class="label">
<div class="label-icon">
<dom-if if="[[item._currentIcon]]">
<template>
<iron-icon icon="[[item._currentIcon]]"></iron-icon>
</template>
</dom-if>
<dom-if if="[[!item._currentIcon]]">
<template>
<div class="step-number">[[_getStepNumber(index)]]</div>
</template>
</dom-if>
</div>
<div class="label-text">
<div class="main">[[item.title]]</div>
<div class="summary">[[item.summary]]</div>
<dom-if if="[[item.optional]]">
<template>
<div class="optional">[[item.optionalText]]</div>
</template>
</dom-if>
</div>
</div>
<dom-if if="[[vertical]]" restamp>
<template>
<div class="vertical-step step-container" visible$="[[item.selected]]">
<slot name="slot[[index]]"></slot>
</div>
</template>
</dom-if>
</div>
</div>
</template>
</dom-repeat>
</iron-selector>
<dom-if if="[[!vertical]]" restamp>
<template>
<slot name="horizontal"></slot>
</div>
</template>
</dom-if>
</template>
<script>
{
/**
* `ud-stepper`
* Material Design Stepper
*
* ### Styling
*
* The following custom properties and mixins are available for styling:
*
* Custom Property | Description | Default
* ----------------------|-------------|--------
* `--ud-stepper-header-container-style` | Style mixin for the header container | `{}`
* `--ud-stepper-icon-completed-color` | The icon color of a completed step | `--google-blue-500`
* `--ud-stepper-icon-error-color` | The icon color of a step with error | `--paper-deep-orange-a700`
* `--ud-stepper-icon-seleced-color` | The icon color of a selected step | `--google-blue-500`
*
* @customElement
* @polymer
* @memberof UrDeveloper
* @demo demo/index.html
*/
class UdStepperElement extends Polymer.MutableData(Polymer.Element) {
static get is() {
return 'ud-stepper';
}
static get properties() {
return {
/**
* If true, the stepper becomes vertical.
*/
vertical: {
type: Boolean,
value: false,
reflectToAttribute: Boolean
},
/**
* If true, the stepper becomes linear.
*/
linear: {
type: Boolean,
reflectToAttribute: Boolean
},
_steps: {
type: Array
},
/**
* Gets or sets the currently selected step index (zero based).
**/
selected: {
type: String,
notify: true,
observer: '_selectionChanged'
},
/**
* Determines if the stepper is compeleted. The stepper is completed when all its non-optional steps are completed.
**/
completed: {
type: Boolean,
notify: true,
reflectToAttribute: true,
computed: '_isCompleted(_steps)'
}
};
}
static get observers() {
return ['_setSlotNames(_steps,vertical)']
}
constructor() {
super();
this._templateObserver = new Polymer.FlattenedNodesObserver(this, info => {
if (info.addedNodes.filter(this._isStep).length > 0 ||
info.removedNodes.filter(this._isStep).length > 0) {
this._steps = this._findSteps();
this._prepareSteps(this._steps);
if (!this.selected) {
this.selected = 0;
}
}
});
}
ready() {
super.ready();
this.addEventListener('step-action', evt => this._handleStepAction(evt));
this.addEventListener('step-error', evt => {
this.notifyPath('_steps')
});
}
_findSteps() {
return Array.from(this.querySelectorAll('ud-step'));
}
_prepareSteps(steps) {
steps.forEach((step, i) => {
//don't overwrite the step's actions, if they're already set
if (step.actions) return;
//By default all steps have continue and cancel action
const actions = [{
name: 'continue'
}, {
name: 'cancel'
}];
if (!this.linear) {
//Disable the back for the first step
actions.push({
name: 'back',
disabled: i === 0
})
} else if (step.optional) {
//In linear stepper, optional step has a skip action
actions.push({
name: 'skip'
});
}
step.actions = actions;
});
}
_isStep(node) {
return node.nodeType === Node.ELEMENT_NODE && /ud-step/i.test(node.localName);
}
_getStepNumber(index) {
return index + 1;
}
_handleStepAction(evt) {
const actionHandler = this[evt.detail.toLowerCase()];
if (actionHandler) actionHandler.apply(this, [evt.target]);
}
_findNextStep(currentStep) {
if (currentStep == this._steps.length - 1) return currentStep;
//if it's a linear stepper, find the next not completed or editable step
if (this.linear) {
let index = currentStep + 1;
while (index < this._steps.length - 1) {
let step = this._steps[index];
if (step.editable || !step.completed) {
return index;
}
index++;
}
return index;
} else {
return currentStep + 1;
}
}
/**
* Go to the next available step
*/
next() {
if (!this._steps) return;
this.continue(this._steps[this.selected]);
}
/** @protected */
continue(step) {
if (this.linear && step.error) return;
step.completed = true;
this.notifyPath('_steps');
this.selected = this._findNextStep(this.selected);
}
/** @protected */
back(step) {
if (this.selected > 0) {
this.selected = this.selected - 1;
}
}
/** @protected */
skip(step) {
this.selected = this._findNextStep(this.selected);
}
/** @protected */
cancel(step) {
}
/**
* Reset the stepper and all its steps to the initial state.
*/
reset() {
this.selected = 0;
this._steps.forEach(s => s._reset());
this.notifyPath('_steps');
}
/**
* Toggle the orientation between horizontal and vertical
*/
toggleOrientation() {
this.vertical = !this.vertical;
}
_handleStepActivate(evt) {
/*
* the logic here:
* User is allowed to select any step if stepper is not linear
* If stepper is linear:
* - allow user to revisit a completed editable step
* - allow user to revist an optional step as long as is not completed
*/
if (this.linear) {
const step = this._steps[evt.detail.selected];
if (!step) return;
if ((step.completed && step.editable) || (!step.completed && step.optional)) {
return;
}
evt.preventDefault();
}
}
_setSlotNames(steps, vertical) {
if (!this._steps) return;
steps.forEach((step, i) => {
step.setAttribute('slot', this.vertical ? 'slot' + i : 'horizontal');
});
}
_selectionChanged(selected) {
this._steps.forEach((step, i) => {
step.selected = i == selected;
});
}
_isCompleted(steps) {
if (!steps || steps.length === 0) return false;
//stepper is completed when all non-optional steps are completed.
return steps.filter(s => s.completed || s.optional).length == steps.length;
}
}
window.customElements.define(UdStepperElement.is, UdStepperElement);
/**
* @namespace UrDeveloper
*/
window.UrDeveloper = window.UrDeveloper || {};
UrDeveloper.UdStepperElement = UdStepperElement;
}
</script>
</dom-module>