-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (145 loc) · 5.02 KB
/
index.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
class StepperForm {
constructor(element) {
this.element = element;
this.stepElements = element.querySelectorAll('.sf-step');
this.nextBtnElements = element.querySelectorAll('button.sf-next, a.sf-next');
this.prevBtnElements = element.querySelectorAll('button.sf-prev, a.sf-next');
this.submitBtnElements = element.querySelectorAll('button.sf-submit, a.sf-submit');
this.indicatorElement = element.querySelector('.sf-indicators');
this.init();
}
init() {
this.steps = StepperForm.createSteps(this.stepElements);
this.indicatorElements = StepperForm.getIndicatorElements(this.steps);
this.indicatorElement.appendChild(StepperForm.createIndicatorListElement(this.indicatorElements));
this._bindUIActions();
this.step = this.steps[0];
}
_bindUIActions() {
StepperForm._addEventListeners([this.element], 'submit', this._handleSubmit, this);
StepperForm._addEventListeners(this.nextBtnElements, 'click', this.nextStep, this);
StepperForm._addEventListeners(this.prevBtnElements, 'click', this.prevStep, this);
StepperForm._addEventListeners(this.indicatorElements, 'click', this._handleIndicatorClick, this);
}
_handleSubmit(event) {
event.preventDefault();
if (this.step.isValid) {
// Do submit
}
}
_handleIndicatorClick(event) {
const indicator = event.currentTarget;
this.currentStep = parseInt(indicator.dataset.step);
}
set currentStep(index) {
const step = this.steps[index];
if (index == undefined || !this.steps.length || !step || (this.step.index < index && !this.step.isValid)) return;
this.steps.forEach(step => (step.isActive = false));
this.step = step;
this.step.isActive = true;
this.updateControls(index);
}
get currentStep() {
return this.step ? this.step.index : null;
}
// Controls
nextStep() {
if (!this.step.isValid) return;
this.currentStep++;
}
prevStep() {
this.currentStep--;
}
updateControls(step) {
this.prevBtnElements.forEach(button => {
step === 0 ? button.classList.add('d-none') : button.classList.remove('d-none');
});
this.nextBtnElements.forEach(button => {
step < this.steps.length - 1 ? button.classList.remove('d-none') : button.classList.add('d-none');
});
this.submitBtnElements.forEach(button => {
step === this.steps.length - 1 ? button.classList.remove('d-none') : button.classList.add('d-none');
});
}
// Static Methods
static createSteps(stepElements) {
if (!stepElements || !stepElements.length) return [];
const steps = [];
stepElements.forEach((element, index) => steps.push(new Step(element, index)));
return steps;
}
static createIndicatorListElement(indicatorElements) {
const indicatorListElement = document.createElement('ul');
indicatorElements.forEach(element => {
indicatorListElement.appendChild(element);
});
return indicatorListElement;
}
static getIndicatorElements(steps) {
return steps.map(step => step.indicatorElement);
}
static _addEventListeners(elementList, eventName, fn, self) {
fn = self ? fn.bind(self) : fn;
elementList.forEach(element => {
element.addEventListener(eventName, fn);
});
}
}
class Step {
constructor(element, index) {
this.element = element;
this.inputElements = element.querySelectorAll('input');
this.index = index;
this.name = element.dataset.name || `Step ${index + 1}`;
this.indicatorElement = Step.createIndicatorElement(this.index, this.name);
this.active = false;
this.valid = true;
this.complete = false;
}
set isActive(value) {
this.active = value;
if (value) {
this.element.classList.remove('d-none');
this.indicatorElement.classList.add('active');
} else {
this.element.classList.add('d-none');
this.indicatorElement.classList.remove('active');
}
}
get isActive() {
return this.active;
}
get isValid() {
this._validate();
return this.valid;
}
set isValid(value) {}
static createIndicatorElement(index, name) {
const indicatorElement = document.createElement('li');
indicatorElement.classList.add('sf-indicator');
indicatorElement.dataset.step = index;
// Indicator Number
const numberElement = document.createElement('span');
numberElement.textContent = index + 1;
numberElement.classList.add('sf-indicator-number');
indicatorElement.appendChild(numberElement);
// Indicator Name
const nameElement = document.createElement('span');
nameElement.textContent = name;
nameElement.classList.add('sf-indicator-name');
indicatorElement.appendChild(nameElement);
return indicatorElement;
}
_validate() {
this.valid = true;
this.inputElements.forEach(input => {
const { valid } = input.validity;
if (!valid) input.reportValidity();
this.valid = valid ? this.valid : false;
});
}
}
window.addEventListener('load', function() {
const formElement = document.querySelector('form.sf-form');
const stepperForm = new StepperForm(formElement);
});