bootstrap5-toggle 4.3.4 → 4.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +126 -43
- package/LICENSE +23 -23
- package/README.md +137 -94
- package/css/bootstrap5-toggle.css +1 -1
- package/css/bootstrap5-toggle.min.css +1 -1
- package/js/bootstrap5-toggle.ecmas.js +410 -334
- package/js/bootstrap5-toggle.ecmas.min.js +2 -2
- package/js/bootstrap5-toggle.ecmas.min.js.map +1 -1
- package/js/bootstrap5-toggle.js +365 -315
- package/js/bootstrap5-toggle.min.js +2 -2
- package/js/bootstrap5-toggle.min.js.map +1 -1
- package/package.json +3 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* Copyright Notice
|
|
2
|
-
* bootstrap5-toggle v4.3.
|
|
2
|
+
* bootstrap5-toggle v4.3.6
|
|
3
3
|
* https://palcarazm.github.io/bootstrap5-toggle/
|
|
4
4
|
* @author 2011-2014 Min Hur (https://github.com/minhur)
|
|
5
5
|
* @author 2018-2019 Brent Ely (https://github.com/gitbrent)
|
|
@@ -11,336 +11,412 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
(function() {
|
|
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
|
-
|
|
14
|
+
"use strict";
|
|
15
|
+
|
|
16
|
+
(function () {
|
|
17
|
+
/**
|
|
18
|
+
* `Toggle` is instantiated for each toggle-button
|
|
19
|
+
*/
|
|
20
|
+
class Toggle {
|
|
21
|
+
constructor(element, options) {
|
|
22
|
+
const DEFAULTS = {
|
|
23
|
+
on: "On",
|
|
24
|
+
onstyle: "primary",
|
|
25
|
+
onvalue: null,
|
|
26
|
+
off: "Off",
|
|
27
|
+
offstyle: "secondary",
|
|
28
|
+
offvalue: null,
|
|
29
|
+
size: "",
|
|
30
|
+
style: "",
|
|
31
|
+
width: null,
|
|
32
|
+
height: null,
|
|
33
|
+
tabindex: 0,
|
|
34
|
+
tristate: false,
|
|
35
|
+
name: null,
|
|
36
|
+
};
|
|
37
|
+
options = options || {};
|
|
38
|
+
|
|
39
|
+
// A: Capture ref to HMTL element
|
|
40
|
+
this.element = element;
|
|
41
|
+
|
|
42
|
+
// B: Set options
|
|
43
|
+
this.options = {
|
|
44
|
+
on: this.element.getAttribute("data-on") || options.on || DEFAULTS.on,
|
|
45
|
+
onstyle:
|
|
46
|
+
this.element.getAttribute("data-onstyle") ||
|
|
47
|
+
options.onstyle ||
|
|
48
|
+
DEFAULTS.onstyle,
|
|
49
|
+
onvalue:
|
|
50
|
+
this.element.getAttribute("value") ||
|
|
51
|
+
this.element.getAttribute("data-onvalue") ||
|
|
52
|
+
options.onvalue ||
|
|
53
|
+
DEFAULTS.onvalue,
|
|
54
|
+
off:
|
|
55
|
+
this.element.getAttribute("data-off") || options.off || DEFAULTS.off,
|
|
56
|
+
offstyle:
|
|
57
|
+
this.element.getAttribute("data-offstyle") ||
|
|
58
|
+
options.offstyle ||
|
|
59
|
+
DEFAULTS.offstyle,
|
|
60
|
+
offvalue:
|
|
61
|
+
this.element.getAttribute("data-offvalue") ||
|
|
62
|
+
options.offvalue ||
|
|
63
|
+
DEFAULTS.offvalue,
|
|
64
|
+
size:
|
|
65
|
+
this.element.getAttribute("data-size") ||
|
|
66
|
+
options.size ||
|
|
67
|
+
DEFAULTS.size,
|
|
68
|
+
style:
|
|
69
|
+
this.element.getAttribute("data-style") ||
|
|
70
|
+
options.style ||
|
|
71
|
+
DEFAULTS.style,
|
|
72
|
+
width:
|
|
73
|
+
this.element.getAttribute("data-width") ||
|
|
74
|
+
options.width ||
|
|
75
|
+
DEFAULTS.width,
|
|
76
|
+
height:
|
|
77
|
+
this.element.getAttribute("data-height") ||
|
|
78
|
+
options.height ||
|
|
79
|
+
DEFAULTS.height,
|
|
80
|
+
tabindex:
|
|
81
|
+
this.element.getAttribute("tabindex") ||
|
|
82
|
+
options.tabindex ||
|
|
83
|
+
DEFAULTS.tabindex,
|
|
84
|
+
tristate:
|
|
85
|
+
this.element.hasAttribute("tristate") ||
|
|
86
|
+
options.tristate ||
|
|
87
|
+
DEFAULTS.tristate,
|
|
88
|
+
name:
|
|
89
|
+
this.element.getAttribute("name") || options.name || DEFAULTS.name,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// LAST: Render Toggle
|
|
93
|
+
this.render();
|
|
94
|
+
}
|
|
95
|
+
render() {
|
|
96
|
+
function calcH(el) {
|
|
97
|
+
const styles = window.getComputedStyle(el);
|
|
98
|
+
const height = el.offsetHeight;
|
|
99
|
+
const borderTopWidth = parseFloat(styles.borderTopWidth);
|
|
100
|
+
const borderBottomWidth = parseFloat(styles.borderBottomWidth);
|
|
101
|
+
const paddingTop = parseFloat(styles.paddingTop);
|
|
102
|
+
const paddingBottom = parseFloat(styles.paddingBottom);
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
height -
|
|
106
|
+
borderBottomWidth -
|
|
107
|
+
borderTopWidth -
|
|
108
|
+
paddingTop -
|
|
109
|
+
paddingBottom
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
// 0: Parse size
|
|
113
|
+
let size;
|
|
114
|
+
switch (this.options.size) {
|
|
115
|
+
case "large":
|
|
116
|
+
case "lg":
|
|
117
|
+
size = "btn-lg";
|
|
118
|
+
break;
|
|
119
|
+
case "small":
|
|
120
|
+
case "sm":
|
|
121
|
+
size = "btn-sm";
|
|
122
|
+
break;
|
|
123
|
+
case "mini":
|
|
124
|
+
case "xs":
|
|
125
|
+
size = "btn-xs";
|
|
126
|
+
break;
|
|
127
|
+
default:
|
|
128
|
+
size = "";
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 1: On
|
|
133
|
+
let ecmasToggleOn = document.createElement("label");
|
|
134
|
+
ecmasToggleOn.setAttribute(
|
|
135
|
+
"class",
|
|
136
|
+
"btn btn-" + this.options.onstyle + " " + size
|
|
137
|
+
);
|
|
138
|
+
if (this.element.id) ecmasToggleOn.setAttribute("for", this.element.id);
|
|
139
|
+
ecmasToggleOn.innerHTML = this.options.on;
|
|
140
|
+
|
|
141
|
+
// 2: Off
|
|
142
|
+
let ecmasToggleOff = document.createElement("label");
|
|
143
|
+
ecmasToggleOff.setAttribute(
|
|
144
|
+
"class",
|
|
145
|
+
"btn btn-" + this.options.offstyle + " " + size
|
|
146
|
+
);
|
|
147
|
+
if (this.element.id) ecmasToggleOff.setAttribute("for", this.element.id);
|
|
148
|
+
ecmasToggleOff.innerHTML = this.options.off;
|
|
149
|
+
|
|
150
|
+
// 3: Handle
|
|
151
|
+
let ecmasToggleHandle = document.createElement("span");
|
|
152
|
+
ecmasToggleHandle.setAttribute("class", "toggle-handle btn " + size);
|
|
153
|
+
|
|
154
|
+
// 4: Toggle Group
|
|
155
|
+
let ecmasToggleGroup = document.createElement("div");
|
|
156
|
+
ecmasToggleGroup.setAttribute("class", "toggle-group");
|
|
157
|
+
ecmasToggleGroup.appendChild(ecmasToggleOn);
|
|
158
|
+
ecmasToggleGroup.appendChild(ecmasToggleOff);
|
|
159
|
+
ecmasToggleGroup.appendChild(ecmasToggleHandle);
|
|
160
|
+
|
|
161
|
+
// 5: Toggle
|
|
162
|
+
let ecmasToggle = document.createElement("div");
|
|
163
|
+
ecmasToggle.setAttribute("class", "toggle btn");
|
|
164
|
+
ecmasToggle.classList.add(
|
|
165
|
+
this.element.checked
|
|
166
|
+
? "btn-" + this.options.onstyle
|
|
167
|
+
: "btn-" + this.options.offstyle
|
|
168
|
+
);
|
|
169
|
+
ecmasToggle.setAttribute("tabindex", this.options.tabindex);
|
|
170
|
+
if (!this.element.checked) ecmasToggle.classList.add("off");
|
|
171
|
+
if (this.options.size) ecmasToggle.classList.add(size);
|
|
172
|
+
if (this.options.style) {
|
|
173
|
+
this.options.style.split(" ").forEach((style) => {
|
|
174
|
+
ecmasToggle.classList.add(style);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
if (this.element.disabled || this.element.readOnly) {
|
|
178
|
+
ecmasToggle.classList.add("disabled");
|
|
179
|
+
ecmasToggle.setAttribute("disabled", "disabled");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// 6: Set form values
|
|
183
|
+
if (this.options.onvalue)
|
|
184
|
+
this.element.setAttribute("value", this.options.onvalue);
|
|
185
|
+
let invElement = null;
|
|
186
|
+
if (this.options.offvalue) {
|
|
187
|
+
invElement = this.element.cloneNode();
|
|
188
|
+
invElement.setAttribute("value", this.options.offvalue);
|
|
189
|
+
invElement.setAttribute("data-toggle", "invert-toggle");
|
|
190
|
+
invElement.removeAttribute("id");
|
|
191
|
+
invElement.checked = !this.element.checked;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// 7: Replace HTML checkbox with Toggle-Button
|
|
195
|
+
this.element.parentElement.insertBefore(ecmasToggle, this.element);
|
|
196
|
+
ecmasToggle.appendChild(this.element);
|
|
197
|
+
if (invElement) ecmasToggle.appendChild(invElement);
|
|
198
|
+
ecmasToggle.appendChild(ecmasToggleGroup);
|
|
199
|
+
|
|
200
|
+
// 8: Set button W/H, lineHeight
|
|
201
|
+
{
|
|
202
|
+
// A: Set style W/H
|
|
203
|
+
// NOTE: `offsetWidth` returns *rounded* integer values, so use `getBoundingClientRect` instead.
|
|
204
|
+
ecmasToggle.style.width =
|
|
205
|
+
(this.options.width ||
|
|
206
|
+
Math.max(
|
|
207
|
+
ecmasToggleOn.getBoundingClientRect().width,
|
|
208
|
+
ecmasToggleOff.getBoundingClientRect().width
|
|
209
|
+
) +
|
|
210
|
+
ecmasToggleHandle.getBoundingClientRect().width / 2) + "px";
|
|
211
|
+
ecmasToggle.style.height =
|
|
212
|
+
(this.options.height ||
|
|
213
|
+
Math.max(
|
|
214
|
+
ecmasToggleOn.getBoundingClientRect().height,
|
|
215
|
+
ecmasToggleOff.getBoundingClientRect().height
|
|
216
|
+
)) + "px";
|
|
217
|
+
|
|
218
|
+
// B: Apply on/off class
|
|
219
|
+
ecmasToggleOn.classList.add("toggle-on");
|
|
220
|
+
ecmasToggleOff.classList.add("toggle-off");
|
|
221
|
+
|
|
222
|
+
// C: Finally, set lineHeight if needed
|
|
223
|
+
if (this.options.height) {
|
|
224
|
+
ecmasToggleOn.style.lineHeight = calcH(ecmasToggleOn) + "px";
|
|
225
|
+
ecmasToggleOff.style.lineHeight = calcH(ecmasToggleOff) + "px";
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// 9: Add listeners
|
|
230
|
+
ecmasToggle.addEventListener("touchstart", (e) => {
|
|
231
|
+
this.#toggleActionPerformed(e);
|
|
232
|
+
});
|
|
233
|
+
ecmasToggle.addEventListener("click", (e) => {
|
|
234
|
+
this.#toggleActionPerformed(e);
|
|
235
|
+
});
|
|
236
|
+
ecmasToggle.addEventListener("keypress", (e) => {
|
|
237
|
+
if (e.key == " ") {
|
|
238
|
+
this.#toggleActionPerformed(e);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// 10: Set elements to bootstrap object
|
|
243
|
+
this.ecmasToggle = ecmasToggle;
|
|
244
|
+
this.invElement = invElement;
|
|
245
|
+
|
|
246
|
+
// 11: Keep reference to this instance for subsequent calls via `getElementById().bootstrapToggle()`
|
|
247
|
+
this.element.bsToggle = this;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Trigger actions
|
|
252
|
+
* @param {Event} e event
|
|
253
|
+
*/
|
|
254
|
+
#toggleActionPerformed(e) {
|
|
255
|
+
if (this.options.tristate) {
|
|
256
|
+
if (this.ecmasToggle.classList.contains("indeterminate")) {
|
|
257
|
+
this.determinate(true);
|
|
258
|
+
this.toggle();
|
|
259
|
+
} else {
|
|
260
|
+
this.indeterminate();
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
this.toggle();
|
|
264
|
+
}
|
|
265
|
+
e.preventDefault();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
toggle(silent = false) {
|
|
269
|
+
if (this.element.checked) this.off(silent);
|
|
270
|
+
else this.on(silent);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
on(silent = false) {
|
|
274
|
+
if (this.element.disabled || this.element.readOnly) return false;
|
|
275
|
+
this.ecmasToggle.classList.remove("btn-" + this.options.offstyle);
|
|
276
|
+
this.ecmasToggle.classList.add("btn-" + this.options.onstyle);
|
|
277
|
+
this.ecmasToggle.classList.remove("off");
|
|
278
|
+
this.element.checked = true;
|
|
279
|
+
if (this.invElement) this.invElement.checked = false;
|
|
280
|
+
if (!silent) this.trigger();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
off(silent = false) {
|
|
284
|
+
if (this.element.disabled || this.element.readOnly) return false;
|
|
285
|
+
this.ecmasToggle.classList.remove("btn-" + this.options.onstyle);
|
|
286
|
+
this.ecmasToggle.classList.add("btn-" + this.options.offstyle);
|
|
287
|
+
this.ecmasToggle.classList.add("off");
|
|
288
|
+
this.element.checked = false;
|
|
289
|
+
if (this.invElement) this.invElement.checked = true;
|
|
290
|
+
if (!silent) this.trigger();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
indeterminate(silent = false) {
|
|
294
|
+
if (
|
|
295
|
+
!this.options.tristate ||
|
|
296
|
+
this.element.disabled ||
|
|
297
|
+
this.element.readOnly
|
|
298
|
+
)
|
|
299
|
+
return false;
|
|
300
|
+
this.ecmasToggle.classList.add("indeterminate");
|
|
301
|
+
this.element.indeterminate = true;
|
|
302
|
+
this.element.removeAttribute("name");
|
|
303
|
+
if (this.invElement) this.invElement.indeterminate = true;
|
|
304
|
+
if (this.invElement) this.invElement.removeAttribute("name");
|
|
305
|
+
if (!silent) this.trigger();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
determinate(silent = false) {
|
|
309
|
+
if (
|
|
310
|
+
!this.options.tristate ||
|
|
311
|
+
this.element.disabled ||
|
|
312
|
+
this.element.readOnly
|
|
313
|
+
)
|
|
314
|
+
return false;
|
|
315
|
+
this.ecmasToggle.classList.remove("indeterminate");
|
|
316
|
+
this.element.indeterminate = false;
|
|
317
|
+
if (this.options.name)
|
|
318
|
+
this.element.setAttribute("name", this.options.name);
|
|
319
|
+
if (this.invElement) this.invElement.indeterminate = false;
|
|
320
|
+
if (this.invElement && this.options.name)
|
|
321
|
+
this.invElement.setAttribute("name", this.options.name);
|
|
322
|
+
if (!silent) this.trigger();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
enable() {
|
|
326
|
+
this.ecmasToggle.classList.remove("disabled");
|
|
327
|
+
this.ecmasToggle.removeAttribute("disabled");
|
|
328
|
+
this.element.removeAttribute("disabled");
|
|
329
|
+
this.element.removeAttribute("readonly");
|
|
330
|
+
if (this.invElement) {
|
|
331
|
+
this.invElement.removeAttribute("disabled");
|
|
332
|
+
this.invElement.removeAttribute("readonly");
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
disable() {
|
|
337
|
+
this.ecmasToggle.classList.add("disabled");
|
|
338
|
+
this.ecmasToggle.setAttribute("disabled", "");
|
|
339
|
+
this.element.setAttribute("disabled", "");
|
|
340
|
+
this.element.removeAttribute("readonly");
|
|
341
|
+
if (this.invElement) {
|
|
342
|
+
this.invElement.setAttribute("disabled", "");
|
|
343
|
+
this.invElement.removeAttribute("readonly");
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
readonly() {
|
|
348
|
+
this.ecmasToggle.classList.add("disabled");
|
|
349
|
+
this.ecmasToggle.setAttribute("disabled", "");
|
|
350
|
+
this.element.removeAttribute("disabled");
|
|
351
|
+
this.element.setAttribute("readonly", "");
|
|
352
|
+
if (this.invElement) {
|
|
353
|
+
this.invElement.removeAttribute("disabled");
|
|
354
|
+
this.invElement.setAttribute("readonly", "");
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
update(silent) {
|
|
359
|
+
if (this.element.disabled) this.disable();
|
|
360
|
+
else if (this.element.readOnly) this.readonly();
|
|
361
|
+
else this.enable();
|
|
362
|
+
if (this.element.checked) this.on(silent);
|
|
363
|
+
else this.off(silent);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
trigger(silent) {
|
|
367
|
+
if (!silent)
|
|
368
|
+
this.element.dispatchEvent(new Event("change", { bubbles: true }));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
destroy() {
|
|
372
|
+
// A: Remove button-group from UI, replace checkbox element
|
|
373
|
+
this.ecmasToggle.parentNode.insertBefore(this.element, this.ecmasToggle);
|
|
374
|
+
this.ecmasToggle.parentNode.removeChild(this.ecmasToggle);
|
|
375
|
+
|
|
376
|
+
// B: Delete internal refs
|
|
377
|
+
delete this.element.bsToggle;
|
|
378
|
+
delete this.ecmasToggle;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Add `bootstrapToggle` prototype function to HTML Elements
|
|
384
|
+
* Enables execution when used with HTML - ex: `document.getElementById('toggle').bootstrapToggle('on')`
|
|
385
|
+
*/
|
|
386
|
+
Element.prototype.bootstrapToggle = function (options, silent) {
|
|
387
|
+
let _bsToggle = this.bsToggle || new Toggle(this, options);
|
|
388
|
+
|
|
389
|
+
// Execute method calls
|
|
390
|
+
if (options && typeof options === "string") {
|
|
391
|
+
if (options.toLowerCase() == "toggle") _bsToggle.toggle(silent);
|
|
392
|
+
else if (options.toLowerCase() == "on") _bsToggle.on(silent);
|
|
393
|
+
else if (options.toLowerCase() == "off") _bsToggle.off(silent);
|
|
394
|
+
else if (options.toLowerCase() == "indeterminate")
|
|
395
|
+
_bsToggle.indeterminate(silent);
|
|
396
|
+
else if (options.toLowerCase() == "determinate")
|
|
397
|
+
_bsToggle.determinate(silent);
|
|
398
|
+
else if (options.toLowerCase() == "enable") _bsToggle.enable();
|
|
399
|
+
else if (options.toLowerCase() == "disable") _bsToggle.disable();
|
|
400
|
+
else if (options.toLowerCase() == "readonly") _bsToggle.readonly();
|
|
401
|
+
else if (options.toLowerCase() == "destroy") _bsToggle.destroy();
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Replace all `input[type=checkbox][data-toggle="toggle"]` inputs with "Bootstrap-Toggle"
|
|
407
|
+
* Executes once page elements have rendered enabling script to be placed in `<head>`
|
|
408
|
+
*/
|
|
409
|
+
if (typeof window !== "undefined")
|
|
410
|
+
window.onload = function () {
|
|
411
|
+
document
|
|
412
|
+
.querySelectorAll('input[type=checkbox][data-toggle="toggle"]')
|
|
413
|
+
.forEach(function (ele) {
|
|
414
|
+
ele.bootstrapToggle();
|
|
415
|
+
});
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
// Export library if possible
|
|
419
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
420
|
+
module.exports = Toggle;
|
|
421
|
+
}
|
|
422
|
+
})();
|