-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodalwizard.js
More file actions
215 lines (192 loc) · 5.91 KB
/
modalwizard.js
File metadata and controls
215 lines (192 loc) · 5.91 KB
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
class Modal {
constructor(modal_html_elem) {
this.pages = [];
this.current_page = 0;
this.elem = modal_html_elem;
}
add_page(page) {
this.pages.push(page);
}
next_clicked() {
if (this.current_page < this.pages.length - 1) {
let old_page = this.current_page;
let new_page = ++this.current_page;
// check if we need to validate this page
if (typeof this.pages[old_page].validation_callback == "function") {
if (!this.pages[old_page].validation_callback()) {
// validation failed
this.current_page--;
return;
}
}
this.pages[old_page].hide();
this.pages[new_page].show();
}
}
back_clicked() {
if (this.current_page > 0) {
let old_page = this.current_page;
let new_page = --this.current_page;
this.pages[old_page].hide();
this.pages[new_page].show();
}
}
finish_clicked() {
this.close();
// gather data from input forms
let data = {};
for (let page of this.pages) {
let input_values = page.input_values;
data[page.elem.id] = input_values;
}
if (this.finish_callback) {
this.finish_callback(data);
}
}
close() {
this.reset();
this.elem.style.display = "none";
}
show() {
this.elem.style.display = "block";
// show first page
if (this.pages[0]) {
this.pages[0].show();
}
}
reset() {
this.current_page = 0;
for (let page of this.pages) {
page.hide();
page.clear();
}
}
/**
* Gets the current page of the modal, starting at 0.
*
* @returns the current page index as integer
*/
get_current_page() {
return this.current_page;
}
}
class Page {
constructor(idx, page_html_element) {
this.index = idx;
this.next_btn = null;
this.back_btn = null;
this.finish_btn = null;
this.close_btn = null;
this.elem = page_html_element;
this.inputs = {};
this.input_values = {};
this.get_inputs();
}
get_inputs() {
let inputs = this.elem.getElementsByTagName("input");
for (let input of inputs) {
this.inputs[input.id] = input;
this.input_values[input.id] = null;
}
}
clear() {
for (let input in this.inputs) {
this.inputs[input].value = "";
this.inputs[input].checked = false;
}
}
show() {
this.elem.style.display = "block";
}
hide() {
this.save_input_values();
this.elem.style.display = "none";
}
save_input_values() {
for (let key in this.inputs) {
let input = this.inputs[key];
if (input.type === "text" || input.type === "password" || input.type === "email" || input.type === "number") {
this.input_values[input.id] = input.value;
} else if (input.type === "checkbox" || input.type === "radio") {
this.input_values[input.id] = input.checked;
}
}
}
}
/**
* Initializes one or more modals on an HTML page. Takes the names of the modal HTML elements as arguments and returns
* a dictionary with modal names as keys and modal objects as values.
*
* @param {*} args List of modal HTML elements, e.g. ['modal1', 'modal2']
* @returns a dictionary containing the modals
*/
function initModals(args) {
let modals = new Object();
for (let modal of args) {
let modal_id = document.getElementById(modal);
if (modal_id === null) {
console.error("modalwizard.js: modal " + modal + " not found, skipping");
} else {
let modal_obj = initModal(modal);
modals[modal] = modal_obj;
}
}
return modals;
}
function initModal(modal) {
let modal_id = document.getElementById(modal);
let modal_obj = new Modal(modal_id);
// get open modal button for this modal
let modal_btn_open = document.getElementById(modal + "_btn_open");
modal_btn_open.onclick = function () {
modal_obj.show();
};
// get pages and hide all but the first one
let idx = 1;
while (true) {
let page_name = modal + "_page" + idx;
let page_id = document.getElementById(page_name);
if (page_id === null) {
break;
}
if (idx > 1) {
page_id.style.display = "none";
}
let page = new Page(idx, page_id);
modal_obj.add_page(page);
idx++;
}
let n_pages = idx - 1;
// get buttons
for (let i = 1; i < n_pages + 1; i++) {
let next_btn = document.getElementById(modal + "_page" + i + "_btn_next");
modal_obj.pages[i - 1].next_btn = next_btn;
if (next_btn) {
next_btn.onclick = function () {
modal_obj.next_clicked();
};
}
let back_btn = document.getElementById(modal + "_page" + i + "_btn_back");
modal_obj.pages[i - 1].back_btn = back_btn;
if (back_btn) {
back_btn.onclick = function () {
modal_obj.back_clicked();
};
}
let finish_btn = document.getElementById(modal + "_page" + i + "_btn_finish");
modal_obj.pages[i - 1].finish_btn = finish_btn;
if (finish_btn) {
finish_btn.onclick = function () {
modal_obj.finish_clicked();
};
}
let close_btn = document.getElementById(modal + "_page" + i + "_btn_close");
modal_obj.pages[i - 1].close_btn = close_btn;
if (close_btn) {
close_btn.onclick = function () {
modal_obj.close();
};
}
}
return modal_obj;
}