-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtz-wizard-stepper-behavior.html
158 lines (153 loc) · 5.16 KB
/
mtz-wizard-stepper-behavior.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
<link rel="import" href="../polymer/polymer.html">
<script>
window.mtz = window.mtz || {};
/**
* `mtz.WizardStepperBehavior`
* Integrates with `mtz-wizard` for building out a visualization of the steps then allowing the user to navigate the
* steps.
*
* @polymerBehavior mtz.WizardStepperBehavior
* @demo demo/index.html
*/
mtz.WizardStepperBehavior = {
properties: {
/* The attribute to use to match the selected key against, needs to match up with mtz-wizard */
attrForSelected: {
type: String,
value: 'name'
},
/* The attribute to set on elements when selected */
selectedAttribute: {
type: String,
value: 'active'
},
/* The array of steps to use when generating the stepper. */
steps: {
type: Array,
observer: '__generateSteps'
},
/* The currently selected step, updated when the user performs a click action on a step. */
selected: {
type: String,
notify: true
},
/* The currently selected step index */
selectedIndex: {
type: Number,
notify: true
},
/* i18n text lookup */
i18n: {
type: Object,
value() {
return {};
}
},
/**
* Generated from the steps property and used to create the stepper’s visualization.
* @type {Array<{key: String, label: String, invalid: Boolean, active: Boolean}>}
*/
__steps: {
type: Array,
readOnly: true
},
/* Used to track the mutation observers */
__observers: {
type: Array,
value() {
return [];
}
}
},
/**
* Creates bound functions for using with event listeners.
*/
ready() {
this.__updateStepProperties = this.__updateStepProperties.bind(this);
},
/**
* Takes an i18n key and returns the translation, if no translation is available then it returns the key.
*
* @param {String} key
* @return {String}
*/
localize(key = '') {
// TODO: add the localize behavior that Polymer provides
return this.get(key, this.i18n) || key;
},
/**
* Moves to the step that was clicked on by updating the selected property with the step’s key.
*
* @param {ClickEvent} event
*/
selectStep({model: {index}}) {
this.selected = this.__steps[index].key;
this.selectedIndex = index;
},
/**
* Takes in the array of HTMLElement steps from wizard and creates an Object array to use for generating the stepper
* visualization. Used as a computed function on __steps.
* @private
*
* @param {HTMLElement[]} steps - the steps to generate based on
*/
__generateSteps(steps) {
this.__observers.forEach((observer) => {
observer.disconnect();
});
this.__observers = [];
this._set__steps(steps.map((step, index) => {
// Set the index and attach the new event listeners
step.stepIndex = index;
const observer = new MutationObserver(this.__updateStepProperties);
observer.observe(step, {
attributes: true,
attributeFilter: ['dirty', 'invalid', this.selectedAttribute]
});
this.__observers.push(observer);
// Return the object we want to use to generate the steps.
const data = {
key: this.__serializeElementProperty(step, this.attrForSelected, String),
label: this.__serializeElementProperty(step, 'label', String) || '',
dirty: this.__serializeElementProperty(step, 'dirty', Boolean),
invalid: this.__serializeElementProperty(step, 'invalid', Boolean),
active: this.__serializeElementProperty(step, this.selectedAttribute, Boolean),
optional: this.__serializeElementProperty(step, 'optional', Boolean),
completed: this.__serializeElementProperty(step, 'completed', Boolean),
editable: this.__serializeElementProperty(step, 'editable', Boolean)
};
return data;
}));
},
/**
* Fetches the value and serializes it based on type.
* @private
*
* @param {HTMLElement} element
* @param {String} property
* @param {Function} type - the JS primitive type
* @return {Object} - the value once it's been retrieved and serialized from the element, if there is no type then
* it returns the raw value
*/
__serializeElementProperty(element, property, type) {
let value = element[property] || element.getAttribute(property);
if (type === Boolean) value = value !== null;
return value === null ? null : (type ? type(value) : value);
},
/**
* The function to bind to the event listener for a property changing in the steps array.
* @private
*
* @param {MutationRecord[]} records
*/
__updateStepProperties(records) {
records.forEach((record) => {
if (record.type === 'attributes') {
const index = record.target.stepIndex;
this.set(`__steps.${index}.${record.attributeName}`,
this.__serializeElementProperty(record.target, record.attributeName, Boolean));
}
});
}
};
</script>