bootstrap5-toggle 5.0.6 → 5.1.1
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/LICENSE +23 -23
- package/README.md +267 -265
- package/README.template.md +292 -291
- package/css/bootstrap5-toggle.css +211 -211
- package/css/bootstrap5-toggle.min.css +12 -12
- package/css/bootstrap5-toggle.min.css.map +1 -1
- package/js/bootstrap5-toggle.ecmas.js +529 -504
- package/js/bootstrap5-toggle.ecmas.min.js +13 -13
- package/js/bootstrap5-toggle.ecmas.min.js.map +1 -1
- package/js/bootstrap5-toggle.jquery.js +491 -464
- package/js/bootstrap5-toggle.jquery.min.js +13 -13
- package/js/bootstrap5-toggle.jquery.min.js.map +1 -1
- package/package.json +65 -65
- package/debug/.gitignore +0 -2
|
@@ -1,504 +1,529 @@
|
|
|
1
|
-
/* Copyright Notice
|
|
2
|
-
* bootstrap5-toggle v5.
|
|
3
|
-
* https://palcarazm.github.io/bootstrap5-toggle/
|
|
4
|
-
* @author 2011-2014 Min Hur (https://github.com/minhur)
|
|
5
|
-
* @author 2018-2019 Brent Ely (https://github.com/gitbrent)
|
|
6
|
-
* @author 2022 Pablo Alcaraz Martínez (https://github.com/palcarazm)
|
|
7
|
-
* @funding GitHub Sponsors
|
|
8
|
-
* @see https://github.com/sponsors/palcarazm
|
|
9
|
-
* @license MIT
|
|
10
|
-
* @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"use strict";
|
|
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
|
-
this.element.getAttribute("data-
|
|
73
|
-
options.
|
|
74
|
-
|
|
75
|
-
DEFAULTS.
|
|
76
|
-
|
|
77
|
-
this.element.getAttribute("data-
|
|
78
|
-
options.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
this.element.getAttribute("data-
|
|
83
|
-
options.
|
|
84
|
-
DEFAULTS.
|
|
85
|
-
|
|
86
|
-
this.element.getAttribute("data-
|
|
87
|
-
options.
|
|
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
|
-
this.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this.options.
|
|
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
|
-
if (this.
|
|
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
|
-
ecmasToggle.style
|
|
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
|
-
this.element.
|
|
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
|
-
this.element.checked
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
)
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (this.
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
if (
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
this.
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
this.
|
|
421
|
-
this.
|
|
422
|
-
|
|
423
|
-
if (this.
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
this.
|
|
431
|
-
this.
|
|
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
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
*
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
1
|
+
/* Copyright Notice
|
|
2
|
+
* bootstrap5-toggle v5.1.1
|
|
3
|
+
* https://palcarazm.github.io/bootstrap5-toggle/
|
|
4
|
+
* @author 2011-2014 Min Hur (https://github.com/minhur)
|
|
5
|
+
* @author 2018-2019 Brent Ely (https://github.com/gitbrent)
|
|
6
|
+
* @author 2022 Pablo Alcaraz Martínez (https://github.com/palcarazm)
|
|
7
|
+
* @funding GitHub Sponsors
|
|
8
|
+
* @see https://github.com/sponsors/palcarazm
|
|
9
|
+
* @license MIT
|
|
10
|
+
* @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
"use strict";
|
|
15
|
+
function sanitize(text) {
|
|
16
|
+
if (!text) return text; // handle null or undefined
|
|
17
|
+
var map = {
|
|
18
|
+
"&": "&",
|
|
19
|
+
"<": "<",
|
|
20
|
+
">": ">",
|
|
21
|
+
'"': """,
|
|
22
|
+
"'": "'",
|
|
23
|
+
"/": "/",
|
|
24
|
+
};
|
|
25
|
+
return text.replace(/[&<>"'/]/g, function (m) {
|
|
26
|
+
return map[m];
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
(function () {
|
|
31
|
+
/**
|
|
32
|
+
* `Toggle` is instantiated for each toggle-button
|
|
33
|
+
*/
|
|
34
|
+
class Toggle {
|
|
35
|
+
constructor(element, options) {
|
|
36
|
+
const DEPRECATION = {
|
|
37
|
+
value:
|
|
38
|
+
"BOOTSTRAP TOGGLE DEPRECATION CHECK -- a0Jhux0QySypjjs4tLtEo8xT2kx0AbYaq9K6mgNjWSs0HF0L8T8J0M0o3Kr7zkm7 --",
|
|
39
|
+
ATTRIBUTE: "attribute",
|
|
40
|
+
OPTION: "option",
|
|
41
|
+
log: function (type, oldlabel, newlabel) {
|
|
42
|
+
console.warn(
|
|
43
|
+
`Bootstrap Toggle deprecation warning: Using ${oldlabel} ${type} is deprected. Use ${newlabel} instead.`
|
|
44
|
+
);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
const DEFAULTS = {
|
|
48
|
+
onlabel: "On",
|
|
49
|
+
onstyle: "primary",
|
|
50
|
+
onvalue: null,
|
|
51
|
+
ontitle: null,
|
|
52
|
+
offlabel: "Off",
|
|
53
|
+
offstyle: "secondary",
|
|
54
|
+
offvalue: null,
|
|
55
|
+
offtitle: null,
|
|
56
|
+
size: "",
|
|
57
|
+
style: "",
|
|
58
|
+
width: null,
|
|
59
|
+
height: null,
|
|
60
|
+
tabindex: 0,
|
|
61
|
+
tristate: false,
|
|
62
|
+
name: null,
|
|
63
|
+
};
|
|
64
|
+
options = options || {};
|
|
65
|
+
|
|
66
|
+
// A: Capture ref to HMTL element
|
|
67
|
+
this.element = element;
|
|
68
|
+
|
|
69
|
+
// B: Set options
|
|
70
|
+
this.options = {
|
|
71
|
+
onlabel:
|
|
72
|
+
this.element.getAttribute("data-onlabel") ||
|
|
73
|
+
options.onlabel ||
|
|
74
|
+
DEPRECATION.value ||
|
|
75
|
+
DEFAULTS.onlabel,
|
|
76
|
+
onstyle:
|
|
77
|
+
sanitize(this.element.getAttribute("data-onstyle")) ||
|
|
78
|
+
options.onstyle ||
|
|
79
|
+
DEFAULTS.onstyle,
|
|
80
|
+
onvalue:
|
|
81
|
+
sanitize(this.element.getAttribute("value")) ||
|
|
82
|
+
sanitize(this.element.getAttribute("data-onvalue")) ||
|
|
83
|
+
options.onvalue ||
|
|
84
|
+
DEFAULTS.onvalue,
|
|
85
|
+
ontitle:
|
|
86
|
+
sanitize(this.element.getAttribute("data-ontitle")) ||
|
|
87
|
+
options.ontitle ||
|
|
88
|
+
sanitize(this.element.getAttribute("title")) ||
|
|
89
|
+
DEFAULTS.ontitle,
|
|
90
|
+
offlabel:
|
|
91
|
+
this.element.getAttribute("data-offlabel") ||
|
|
92
|
+
options.offlabel ||
|
|
93
|
+
DEPRECATION.value ||
|
|
94
|
+
DEFAULTS.offlabel,
|
|
95
|
+
offstyle:
|
|
96
|
+
sanitize(this.element.getAttribute("data-offstyle")) ||
|
|
97
|
+
options.offstyle ||
|
|
98
|
+
DEFAULTS.offstyle,
|
|
99
|
+
offvalue:
|
|
100
|
+
sanitize(this.element.getAttribute("data-offvalue")) ||
|
|
101
|
+
options.offvalue ||
|
|
102
|
+
DEFAULTS.offvalue,
|
|
103
|
+
offtitle:
|
|
104
|
+
sanitize(this.element.getAttribute("data-offtitle")) ||
|
|
105
|
+
options.offtitle ||
|
|
106
|
+
sanitize(this.element.getAttribute("title")) ||
|
|
107
|
+
DEFAULTS.offtitle,
|
|
108
|
+
size:
|
|
109
|
+
sanitize(this.element.getAttribute("data-size")) ||
|
|
110
|
+
options.size ||
|
|
111
|
+
DEFAULTS.size,
|
|
112
|
+
style:
|
|
113
|
+
sanitize(this.element.getAttribute("data-style")) ||
|
|
114
|
+
options.style ||
|
|
115
|
+
DEFAULTS.style,
|
|
116
|
+
width:
|
|
117
|
+
sanitize(this.element.getAttribute("data-width")) ||
|
|
118
|
+
options.width ||
|
|
119
|
+
DEFAULTS.width,
|
|
120
|
+
height:
|
|
121
|
+
sanitize(this.element.getAttribute("data-height")) ||
|
|
122
|
+
options.height ||
|
|
123
|
+
DEFAULTS.height,
|
|
124
|
+
tabindex:
|
|
125
|
+
sanitize(this.element.getAttribute("tabindex")) ||
|
|
126
|
+
options.tabindex ||
|
|
127
|
+
DEFAULTS.tabindex,
|
|
128
|
+
tristate:
|
|
129
|
+
this.element.hasAttribute("tristate") ||
|
|
130
|
+
options.tristate ||
|
|
131
|
+
DEFAULTS.tristate,
|
|
132
|
+
name:
|
|
133
|
+
sanitize(this.element.getAttribute("name")) ||
|
|
134
|
+
options.name ||
|
|
135
|
+
DEFAULTS.name,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// C: Check deprecations
|
|
139
|
+
if (this.options.onlabel === DEPRECATION.value) {
|
|
140
|
+
if (sanitize(this.element.getAttribute("data-on"))) {
|
|
141
|
+
DEPRECATION.log(DEPRECATION.ATTRIBUTE, "data-on", "data-onlabel");
|
|
142
|
+
this.options.onlabel = this.element.getAttribute("data-on");
|
|
143
|
+
} else if (options.on) {
|
|
144
|
+
DEPRECATION.log(DEPRECATION.OPTION, "on", "onlabel");
|
|
145
|
+
this.options.onlabel = options.on;
|
|
146
|
+
} else {
|
|
147
|
+
this.options.onlabel = DEFAULTS.onlabel;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (this.options.offlabel === DEPRECATION.value) {
|
|
151
|
+
if (sanitize(this.element.getAttribute("data-off"))) {
|
|
152
|
+
DEPRECATION.log(DEPRECATION.ATTRIBUTE, "data-off", "data-offlabel");
|
|
153
|
+
this.options.offlabel = this.element.getAttribute("data-off");
|
|
154
|
+
} else if (options.off) {
|
|
155
|
+
DEPRECATION.log(DEPRECATION.OPTION, "off", "offlabel");
|
|
156
|
+
this.options.offlabel = options.off;
|
|
157
|
+
} else {
|
|
158
|
+
this.options.offlabel = DEFAULTS.offlabel;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// LAST: Render Toggle
|
|
163
|
+
this.render();
|
|
164
|
+
}
|
|
165
|
+
render() {
|
|
166
|
+
function calcH(el) {
|
|
167
|
+
const styles = window.getComputedStyle(el);
|
|
168
|
+
const height = el.offsetHeight;
|
|
169
|
+
const borderTopWidth = parseFloat(styles.borderTopWidth);
|
|
170
|
+
const borderBottomWidth = parseFloat(styles.borderBottomWidth);
|
|
171
|
+
const paddingTop = parseFloat(styles.paddingTop);
|
|
172
|
+
const paddingBottom = parseFloat(styles.paddingBottom);
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
height -
|
|
176
|
+
borderBottomWidth -
|
|
177
|
+
borderTopWidth -
|
|
178
|
+
paddingTop -
|
|
179
|
+
paddingBottom
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
// 0: Parse size
|
|
183
|
+
let size;
|
|
184
|
+
switch (this.options.size) {
|
|
185
|
+
case "large":
|
|
186
|
+
case "lg":
|
|
187
|
+
size = "btn-lg";
|
|
188
|
+
break;
|
|
189
|
+
case "small":
|
|
190
|
+
case "sm":
|
|
191
|
+
size = "btn-sm";
|
|
192
|
+
break;
|
|
193
|
+
case "mini":
|
|
194
|
+
case "xs":
|
|
195
|
+
size = "btn-xs";
|
|
196
|
+
break;
|
|
197
|
+
default:
|
|
198
|
+
size = "";
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// 1: On
|
|
203
|
+
let ecmasToggleOn = document.createElement("span");
|
|
204
|
+
ecmasToggleOn.setAttribute(
|
|
205
|
+
"class",
|
|
206
|
+
"btn btn-" + this.options.onstyle + " " + size
|
|
207
|
+
);
|
|
208
|
+
ecmasToggleOn.innerHTML = this.options.onlabel;
|
|
209
|
+
if (this.options.ontitle) {
|
|
210
|
+
ecmasToggleOn.setAttribute("title", this.options.ontitle);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 2: Off
|
|
214
|
+
let ecmasToggleOff = document.createElement("span");
|
|
215
|
+
ecmasToggleOff.setAttribute(
|
|
216
|
+
"class",
|
|
217
|
+
"btn btn-" + this.options.offstyle + " " + size
|
|
218
|
+
);
|
|
219
|
+
ecmasToggleOff.innerHTML = this.options.offlabel;
|
|
220
|
+
if (this.options.offtitle) {
|
|
221
|
+
ecmasToggleOff.setAttribute("title", this.options.offtitle);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 3: Handle
|
|
225
|
+
let ecmasToggleHandle = document.createElement("span");
|
|
226
|
+
ecmasToggleHandle.setAttribute("class", "toggle-handle btn " + size);
|
|
227
|
+
|
|
228
|
+
// 4: Toggle Group
|
|
229
|
+
let ecmasToggleGroup = document.createElement("div");
|
|
230
|
+
ecmasToggleGroup.setAttribute("class", "toggle-group");
|
|
231
|
+
ecmasToggleGroup.appendChild(ecmasToggleOn);
|
|
232
|
+
ecmasToggleGroup.appendChild(ecmasToggleOff);
|
|
233
|
+
ecmasToggleGroup.appendChild(ecmasToggleHandle);
|
|
234
|
+
|
|
235
|
+
// 5: Toggle
|
|
236
|
+
let ecmasToggle = document.createElement("div");
|
|
237
|
+
ecmasToggle.setAttribute("class", "toggle btn");
|
|
238
|
+
ecmasToggle.classList.add(
|
|
239
|
+
this.element.checked
|
|
240
|
+
? "btn-" + this.options.onstyle
|
|
241
|
+
: "btn-" + this.options.offstyle
|
|
242
|
+
);
|
|
243
|
+
ecmasToggle.setAttribute("tabindex", this.options.tabindex);
|
|
244
|
+
if (!this.element.checked) ecmasToggle.classList.add("off");
|
|
245
|
+
if (this.options.size) ecmasToggle.classList.add(size);
|
|
246
|
+
if (this.options.style) {
|
|
247
|
+
this.options.style.split(" ").forEach((style) => {
|
|
248
|
+
ecmasToggle.classList.add(style);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
if (this.element.disabled || this.element.readOnly) {
|
|
252
|
+
ecmasToggle.classList.add("disabled");
|
|
253
|
+
ecmasToggle.setAttribute("disabled", "disabled");
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 6: Set form values
|
|
257
|
+
if (this.options.onvalue)
|
|
258
|
+
this.element.setAttribute("value", this.options.onvalue);
|
|
259
|
+
let invElement = null;
|
|
260
|
+
if (this.options.offvalue) {
|
|
261
|
+
invElement = this.element.cloneNode();
|
|
262
|
+
invElement.setAttribute("value", this.options.offvalue);
|
|
263
|
+
invElement.setAttribute("data-toggle", "invert-toggle");
|
|
264
|
+
invElement.removeAttribute("id");
|
|
265
|
+
invElement.checked = !this.element.checked;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// 7: Replace HTML checkbox with Toggle-Button
|
|
269
|
+
this.element.parentElement.insertBefore(ecmasToggle, this.element);
|
|
270
|
+
ecmasToggle.appendChild(this.element);
|
|
271
|
+
if (invElement) ecmasToggle.appendChild(invElement);
|
|
272
|
+
ecmasToggle.appendChild(ecmasToggleGroup);
|
|
273
|
+
|
|
274
|
+
// 8: Set button W/H, lineHeight
|
|
275
|
+
{
|
|
276
|
+
// A: Set style W/H
|
|
277
|
+
// NOTE: `offsetWidth` returns *rounded* integer values, so use `getBoundingClientRect` instead.
|
|
278
|
+
if (this.options.width) {
|
|
279
|
+
ecmasToggle.style.width = `${this.options.width}px`;
|
|
280
|
+
} else {
|
|
281
|
+
ecmasToggle.style["min-width"] = "100px"; // First approach for better calculation
|
|
282
|
+
ecmasToggle.style["min-width"] = `${
|
|
283
|
+
Math.max(
|
|
284
|
+
ecmasToggleOn.getBoundingClientRect().width,
|
|
285
|
+
ecmasToggleOff.getBoundingClientRect().width
|
|
286
|
+
) +
|
|
287
|
+
ecmasToggleHandle.getBoundingClientRect().width / 2
|
|
288
|
+
}px`;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (this.options.height) {
|
|
292
|
+
ecmasToggle.style.height = `${this.options.height}px`;
|
|
293
|
+
} else {
|
|
294
|
+
ecmasToggle.style["min-height"] = "36px"; // First approach for better calculation
|
|
295
|
+
ecmasToggle.style["min-height"] = `${Math.max(
|
|
296
|
+
ecmasToggleOn.getBoundingClientRect().height,
|
|
297
|
+
ecmasToggleOff.getBoundingClientRect().height
|
|
298
|
+
)}px`;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// B: Apply on/off class
|
|
302
|
+
ecmasToggleOn.classList.add("toggle-on");
|
|
303
|
+
ecmasToggleOff.classList.add("toggle-off");
|
|
304
|
+
|
|
305
|
+
// C: Finally, set lineHeight if needed
|
|
306
|
+
if (this.options.height) {
|
|
307
|
+
ecmasToggleOn.style.lineHeight = calcH(ecmasToggleOn) + "px";
|
|
308
|
+
ecmasToggleOff.style.lineHeight = calcH(ecmasToggleOff) + "px";
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// 9: Add listeners
|
|
313
|
+
ecmasToggle.addEventListener(
|
|
314
|
+
"touchstart",
|
|
315
|
+
(e) => {
|
|
316
|
+
this.#toggleActionPerformed(e);
|
|
317
|
+
},
|
|
318
|
+
{ passive: true }
|
|
319
|
+
);
|
|
320
|
+
ecmasToggle.addEventListener("click", (e) => {
|
|
321
|
+
this.#toggleActionPerformed(e);
|
|
322
|
+
});
|
|
323
|
+
ecmasToggle.addEventListener("keypress", (e) => {
|
|
324
|
+
if (e.key == " ") {
|
|
325
|
+
this.#toggleActionPerformed(e);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
if (this.element.id) {
|
|
330
|
+
document
|
|
331
|
+
.querySelectorAll('label[for="' + this.element.id + '"]')
|
|
332
|
+
.forEach((label) => {
|
|
333
|
+
label.addEventListener("touchstart", (_e) => {
|
|
334
|
+
this.toggle();
|
|
335
|
+
ecmasToggle.focus();
|
|
336
|
+
});
|
|
337
|
+
label.addEventListener("click", (_e) => {
|
|
338
|
+
this.toggle();
|
|
339
|
+
ecmasToggle.focus();
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// 10: Set elements to bootstrap object
|
|
345
|
+
this.ecmasToggle = ecmasToggle;
|
|
346
|
+
this.invElement = invElement;
|
|
347
|
+
|
|
348
|
+
// 11: Keep reference to this instance for subsequent calls via `getElementById().bootstrapToggle()`
|
|
349
|
+
this.element.bsToggle = this;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Trigger actions
|
|
354
|
+
* @param {Event} e event
|
|
355
|
+
*/
|
|
356
|
+
#toggleActionPerformed(e) {
|
|
357
|
+
if (this.options.tristate) {
|
|
358
|
+
if (this.ecmasToggle.classList.contains("indeterminate")) {
|
|
359
|
+
this.determinate(true);
|
|
360
|
+
this.toggle();
|
|
361
|
+
} else {
|
|
362
|
+
this.indeterminate();
|
|
363
|
+
}
|
|
364
|
+
} else {
|
|
365
|
+
this.toggle();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
toggle(silent = false) {
|
|
370
|
+
if (this.element.checked) this.off(silent);
|
|
371
|
+
else this.on(silent);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
on(silent = false) {
|
|
375
|
+
if (this.element.disabled || this.element.readOnly) return false;
|
|
376
|
+
this.ecmasToggle.classList.remove("btn-" + this.options.offstyle);
|
|
377
|
+
this.ecmasToggle.classList.add("btn-" + this.options.onstyle);
|
|
378
|
+
this.ecmasToggle.classList.remove("off");
|
|
379
|
+
this.element.checked = true;
|
|
380
|
+
if (this.invElement) this.invElement.checked = false;
|
|
381
|
+
if (!silent) this.trigger();
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
off(silent = false) {
|
|
385
|
+
if (this.element.disabled || this.element.readOnly) return false;
|
|
386
|
+
this.ecmasToggle.classList.remove("btn-" + this.options.onstyle);
|
|
387
|
+
this.ecmasToggle.classList.add("btn-" + this.options.offstyle);
|
|
388
|
+
this.ecmasToggle.classList.add("off");
|
|
389
|
+
this.element.checked = false;
|
|
390
|
+
if (this.invElement) this.invElement.checked = true;
|
|
391
|
+
if (!silent) this.trigger();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
indeterminate(silent = false) {
|
|
395
|
+
if (
|
|
396
|
+
!this.options.tristate ||
|
|
397
|
+
this.element.disabled ||
|
|
398
|
+
this.element.readOnly
|
|
399
|
+
)
|
|
400
|
+
return false;
|
|
401
|
+
this.ecmasToggle.classList.add("indeterminate");
|
|
402
|
+
this.element.indeterminate = true;
|
|
403
|
+
this.element.removeAttribute("name");
|
|
404
|
+
if (this.invElement) this.invElement.indeterminate = true;
|
|
405
|
+
if (this.invElement) this.invElement.removeAttribute("name");
|
|
406
|
+
if (!silent) this.trigger();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
determinate(silent = false) {
|
|
410
|
+
if (
|
|
411
|
+
!this.options.tristate ||
|
|
412
|
+
this.element.disabled ||
|
|
413
|
+
this.element.readOnly
|
|
414
|
+
)
|
|
415
|
+
return false;
|
|
416
|
+
this.ecmasToggle.classList.remove("indeterminate");
|
|
417
|
+
this.element.indeterminate = false;
|
|
418
|
+
if (this.options.name)
|
|
419
|
+
this.element.setAttribute("name", this.options.name);
|
|
420
|
+
if (this.invElement) this.invElement.indeterminate = false;
|
|
421
|
+
if (this.invElement && this.options.name)
|
|
422
|
+
this.invElement.setAttribute("name", this.options.name);
|
|
423
|
+
if (!silent) this.trigger();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
enable() {
|
|
427
|
+
this.ecmasToggle.classList.remove("disabled");
|
|
428
|
+
this.ecmasToggle.removeAttribute("disabled");
|
|
429
|
+
this.element.removeAttribute("disabled");
|
|
430
|
+
this.element.removeAttribute("readonly");
|
|
431
|
+
if (this.invElement) {
|
|
432
|
+
this.invElement.removeAttribute("disabled");
|
|
433
|
+
this.invElement.removeAttribute("readonly");
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
disable() {
|
|
438
|
+
this.ecmasToggle.classList.add("disabled");
|
|
439
|
+
this.ecmasToggle.setAttribute("disabled", "");
|
|
440
|
+
this.element.setAttribute("disabled", "");
|
|
441
|
+
this.element.removeAttribute("readonly");
|
|
442
|
+
if (this.invElement) {
|
|
443
|
+
this.invElement.setAttribute("disabled", "");
|
|
444
|
+
this.invElement.removeAttribute("readonly");
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
readonly() {
|
|
449
|
+
this.ecmasToggle.classList.add("disabled");
|
|
450
|
+
this.ecmasToggle.setAttribute("disabled", "");
|
|
451
|
+
this.element.removeAttribute("disabled");
|
|
452
|
+
this.element.setAttribute("readonly", "");
|
|
453
|
+
if (this.invElement) {
|
|
454
|
+
this.invElement.removeAttribute("disabled");
|
|
455
|
+
this.invElement.setAttribute("readonly", "");
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
update(silent) {
|
|
460
|
+
if (this.element.disabled) this.disable();
|
|
461
|
+
else if (this.element.readOnly) this.readonly();
|
|
462
|
+
else this.enable();
|
|
463
|
+
if (this.element.checked) this.on(silent);
|
|
464
|
+
else this.off(silent);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
trigger(silent) {
|
|
468
|
+
if (!silent)
|
|
469
|
+
this.element.dispatchEvent(new Event("change", { bubbles: true }));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
destroy() {
|
|
473
|
+
// A: Remove button-group from UI, replace checkbox element
|
|
474
|
+
this.ecmasToggle.parentNode.insertBefore(this.element, this.ecmasToggle);
|
|
475
|
+
this.ecmasToggle.parentNode.removeChild(this.ecmasToggle);
|
|
476
|
+
|
|
477
|
+
// B: Delete internal refs
|
|
478
|
+
delete this.element.bsToggle;
|
|
479
|
+
delete this.ecmasToggle;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
rerender() {
|
|
483
|
+
this.destroy();
|
|
484
|
+
this.element.bootstrapToggle();
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Add `bootstrapToggle` prototype function to HTML Elements
|
|
490
|
+
* Enables execution when used with HTML - ex: `document.getElementById('toggle').bootstrapToggle('on')`
|
|
491
|
+
*/
|
|
492
|
+
Element.prototype.bootstrapToggle = function (options, silent) {
|
|
493
|
+
let _bsToggle = this.bsToggle || new Toggle(this, options);
|
|
494
|
+
|
|
495
|
+
// Execute method calls
|
|
496
|
+
if (options && typeof options === "string") {
|
|
497
|
+
if (options.toLowerCase() == "toggle") _bsToggle.toggle(silent);
|
|
498
|
+
else if (options.toLowerCase() == "on") _bsToggle.on(silent);
|
|
499
|
+
else if (options.toLowerCase() == "off") _bsToggle.off(silent);
|
|
500
|
+
else if (options.toLowerCase() == "indeterminate")
|
|
501
|
+
_bsToggle.indeterminate(silent);
|
|
502
|
+
else if (options.toLowerCase() == "determinate")
|
|
503
|
+
_bsToggle.determinate(silent);
|
|
504
|
+
else if (options.toLowerCase() == "enable") _bsToggle.enable();
|
|
505
|
+
else if (options.toLowerCase() == "disable") _bsToggle.disable();
|
|
506
|
+
else if (options.toLowerCase() == "readonly") _bsToggle.readonly();
|
|
507
|
+
else if (options.toLowerCase() == "destroy") _bsToggle.destroy();
|
|
508
|
+
else if (options.toLowerCase() == "rerender") _bsToggle.rerender();
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Replace all `input[type=checkbox][data-toggle="toggle"]` inputs with "Bootstrap-Toggle"
|
|
514
|
+
* Executes once page elements have rendered enabling script to be placed in `<head>`
|
|
515
|
+
*/
|
|
516
|
+
if (typeof window !== "undefined")
|
|
517
|
+
window.onload = function () {
|
|
518
|
+
document
|
|
519
|
+
.querySelectorAll('input[type=checkbox][data-toggle="toggle"]')
|
|
520
|
+
.forEach(function (ele) {
|
|
521
|
+
ele.bootstrapToggle();
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
// Export library if possible
|
|
526
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
527
|
+
module.exports = Toggle;
|
|
528
|
+
}
|
|
529
|
+
})();
|