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.
@@ -1,5 +1,5 @@
1
1
  /* Copyright Notice
2
- * bootstrap5-toggle v4.3.4
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
- '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: this.element.getAttribute('data-onstyle') || options.onstyle || DEFAULTS.onstyle,
46
- onvalue: this.element.getAttribute('value') || this.element.getAttribute('data-onvalue') || options.onvalue || DEFAULTS.onvalue,
47
- off: this.element.getAttribute('data-off') || options.off || DEFAULTS.off,
48
- offstyle: this.element.getAttribute('data-offstyle') || options.offstyle || DEFAULTS.offstyle,
49
- offvalue: this.element.getAttribute('data-offvalue') || options.offvalue || DEFAULTS.offvalue,
50
- size: this.element.getAttribute('data-size') || options.size || DEFAULTS.size,
51
- style: this.element.getAttribute('data-style') || options.style || DEFAULTS.style,
52
- width: this.element.getAttribute('data-width') || options.width || DEFAULTS.width,
53
- height: this.element.getAttribute('data-height') || options.height || DEFAULTS.height,
54
- tabindex: this.element.getAttribute('tabindex') || options.tabindex || DEFAULTS.tabindex,
55
- tristate: this.element.hasAttribute('tristate') || options.tristate || DEFAULTS.tristate,
56
- name: this.element.getAttribute('name') || options.name || DEFAULTS.name,
57
- };
58
-
59
- // LAST: Render Toggle
60
- this.render();
61
- }
62
- render() {
63
- function calcH(el) {
64
- const styles = window.getComputedStyle(el);
65
- const height = el.offsetHeight;
66
- const borderTopWidth = parseFloat(styles.borderTopWidth);
67
- const borderBottomWidth = parseFloat(styles.borderBottomWidth);
68
- const paddingTop = parseFloat(styles.paddingTop);
69
- const paddingBottom = parseFloat(styles.paddingBottom);
70
-
71
- return height - borderBottomWidth - borderTopWidth - paddingTop - paddingBottom;
72
- }
73
- // 0: Parse size
74
- let size;
75
- switch (this.options.size ) {
76
- case 'large':
77
- case 'lg':
78
- size = 'btn-lg';
79
- break;
80
- case 'small':
81
- case 'sm':
82
- size = 'btn-sm';
83
- break;
84
- case 'mini':
85
- case 'xs':
86
- size = 'btn-xs';
87
- break;
88
- default:
89
- size = ''
90
- break;
91
- }
92
-
93
- // 1: On
94
- let ecmasToggleOn = document.createElement('label');
95
- ecmasToggleOn.setAttribute('class', 'btn btn-' + this.options.onstyle + ' ' + size);
96
- ecmasToggleOn.setAttribute('for', this.element.id);
97
- ecmasToggleOn.innerHTML = this.options.on;
98
-
99
- // 2: Off
100
- let ecmasToggleOff = document.createElement('label');
101
- ecmasToggleOff.setAttribute('class', 'btn btn-' + this.options.offstyle + ' ' + size);
102
- ecmasToggleOff.setAttribute('for', this.element.id);
103
- ecmasToggleOff.innerHTML = this.options.off;
104
-
105
- // 3: Handle
106
- let ecmasToggleHandle = document.createElement('span');
107
- ecmasToggleHandle.setAttribute('class', 'toggle-handle btn ' + size);
108
-
109
- // 4: Toggle Group
110
- let ecmasToggleGroup = document.createElement('div');
111
- ecmasToggleGroup.setAttribute('class', 'toggle-group');
112
- ecmasToggleGroup.appendChild(ecmasToggleOn);
113
- ecmasToggleGroup.appendChild(ecmasToggleOff);
114
- ecmasToggleGroup.appendChild(ecmasToggleHandle);
115
-
116
- // 5: Toggle
117
- let ecmasToggle = document.createElement('div');
118
- ecmasToggle.setAttribute('class', 'toggle btn');
119
- ecmasToggle.classList.add(this.element.checked ? 'btn-' + this.options.onstyle : 'btn-' + this.options.offstyle);
120
- ecmasToggle.setAttribute('tabindex',this.options.tabindex);
121
- if (!this.element.checked) ecmasToggle.classList.add('off');
122
- if (this.options.size) ecmasToggle.classList.add(size);
123
- if (this.options.style) {
124
- (this.options.style).split(' ').forEach((style)=>{
125
- ecmasToggle.classList.add(style);
126
- });
127
- }
128
- if (this.element.disabled || this.element.readOnly){
129
- ecmasToggle.classList.add('disabled');
130
- ecmasToggle.setAttribute('disabled', 'disabled');
131
- }
132
-
133
- // 6: Set form values
134
- if(this.options.onvalue) this.element.setAttribute('value', this.options.onvalue);
135
- let invElement = null;
136
- if(this.options.offvalue){
137
- invElement = this.element.cloneNode();
138
- invElement.setAttribute('value',this.options.offvalue);
139
- invElement.setAttribute('data-toggle', 'invert-toggle');
140
- invElement.removeAttribute('id');
141
- invElement.checked = !this.element.checked;
142
- }
143
-
144
- // 7: Replace HTML checkbox with Toggle-Button
145
- this.element.parentElement.insertBefore(ecmasToggle, this.element);
146
- ecmasToggle.appendChild(this.element);
147
- if(invElement) ecmasToggle.appendChild(invElement);
148
- ecmasToggle.appendChild(ecmasToggleGroup);
149
-
150
- // 8: Set button W/H, lineHeight
151
- {
152
- // A: Set style W/H
153
- // NOTE: `offsetWidth` returns *rounded* integer values, so use `getBoundingClientRect` instead.
154
- ecmasToggle.style.width =
155
- (this.options.width ||
156
- Math.max(ecmasToggleOn.getBoundingClientRect().width, ecmasToggleOff.getBoundingClientRect().width) + ecmasToggleHandle.getBoundingClientRect().width / 2) + 'px';
157
- ecmasToggle.style.height = (this.options.height || Math.max(ecmasToggleOn.getBoundingClientRect().height, ecmasToggleOff.getBoundingClientRect().height)) + 'px';
158
-
159
- // B: Apply on/off class
160
- ecmasToggleOn.classList.add('toggle-on');
161
- ecmasToggleOff.classList.add('toggle-off');
162
-
163
- // C: Finally, set lineHeight if needed
164
- if (this.options.height) {
165
- ecmasToggleOn.style.lineHeight = calcH(ecmasToggleOn) + 'px';
166
- ecmasToggleOff.style.lineHeight = calcH(ecmasToggleOff) + 'px';
167
- }
168
- }
169
-
170
- // 9: Add listeners
171
- ecmasToggle.addEventListener('touchstart', (e)=>{
172
- this.#toggleActionPerformed(e)
173
- });
174
- ecmasToggle.addEventListener('click', (e)=>{
175
- this.#toggleActionPerformed(e)
176
- });
177
- ecmasToggle.addEventListener('keypress', (e)=>{
178
- if(e.key == " "){
179
- this.#toggleActionPerformed(e)
180
- }
181
- });
182
-
183
- // 10: Set elements to bootstrap object
184
- this.ecmasToggle = ecmasToggle;
185
- this.invElement = invElement;
186
-
187
- // 11: Keep reference to this instance for subsequent calls via `getElementById().bootstrapToggle()`
188
- this.element.bsToggle = this;
189
- }
190
-
191
- /**
192
- * Trigger actions
193
- * @param {Event} e event
194
- */
195
- #toggleActionPerformed(e){
196
- if(this.options.tristate){
197
- if(this.ecmasToggle.classList.contains('indeterminate')){
198
- this.determinate(true);
199
- this.toggle();
200
- }else{
201
- this.indeterminate();
202
- }
203
- }else{
204
- this.toggle()
205
- }
206
- e.preventDefault()
207
- }
208
-
209
- toggle(silent = false) {
210
- if (this.element.checked) this.off(silent);
211
- else this.on(silent);
212
- }
213
-
214
- on(silent = false) {
215
- if (this.element.disabled || this.element.readOnly) return false;
216
- this.ecmasToggle.classList.remove('btn-' + this.options.offstyle);
217
- this.ecmasToggle.classList.add('btn-' + this.options.onstyle);
218
- this.ecmasToggle.classList.remove('off');
219
- this.element.checked = true;
220
- if(this.invElement) this.invElement.checked = false;
221
- if (!silent) this.trigger();
222
- }
223
-
224
- off(silent = false) {
225
- if (this.element.disabled || this.element.readOnly) return false;
226
- this.ecmasToggle.classList.remove('btn-' + this.options.onstyle);
227
- this.ecmasToggle.classList.add('btn-' + this.options.offstyle);
228
- this.ecmasToggle.classList.add('off');
229
- this.element.checked = false;
230
- if(this.invElement) this.invElement.checked = true;
231
- if (!silent) this.trigger();
232
- }
233
-
234
- indeterminate(silent = false) {
235
- if (!this.options.tristate || this.element.disabled || this.element.readOnly) return false;
236
- this.ecmasToggle.classList.add('indeterminate');
237
- this.element.indeterminate = true;
238
- this.element.removeAttribute('name');
239
- if(this.invElement) this.invElement.indeterminate = true;
240
- if(this.invElement) this.invElement.removeAttribute('name');
241
- if (!silent) this.trigger()
242
- }
243
-
244
- determinate(silent = false) {
245
- if (!this.options.tristate || this.element.disabled || this.element.readOnly) return false;
246
- this.ecmasToggle.classList.remove('indeterminate');
247
- this.element.indeterminate = false;
248
- if(this.options.name) this.element.setAttribute('name', this.options.name);
249
- if(this.invElement) this.invElement.indeterminate = false;
250
- if(this.invElement && this.options.name) this.invElement.setAttribute('name', this.options.name);
251
- if (!silent) this.trigger()
252
- }
253
-
254
- enable() {
255
- this.ecmasToggle.classList.remove('disabled');
256
- this.ecmasToggle.removeAttribute('disabled');
257
- this.element.removeAttribute('disabled');
258
- this.element.removeAttribute('readonly');
259
- if(this.invElement) {
260
- this.invElement.removeAttribute('disabled');
261
- this.invElement.removeAttribute('readonly');
262
- }
263
- }
264
-
265
- disable() {
266
- this.ecmasToggle.classList.add('disabled');
267
- this.ecmasToggle.setAttribute('disabled', '');
268
- this.element.setAttribute('disabled', '');
269
- this.element.removeAttribute('readonly');
270
- if(this.invElement) {
271
- this.invElement.setAttribute('disabled', '');
272
- this.invElement.removeAttribute('readonly');
273
- }
274
- }
275
-
276
- readonly() {
277
- this.ecmasToggle.classList.add('disabled');
278
- this.ecmasToggle.setAttribute('disabled', '');
279
- this.element.removeAttribute('disabled');
280
- this.element.setAttribute('readonly', '');
281
- if(this.invElement) {
282
- this.invElement.removeAttribute('disabled');
283
- this.invElement.setAttribute('readonly', '');
284
- }
285
- }
286
-
287
- update(silent) {
288
- if (this.element.disabled) this.disable();
289
- else if (this.element.readOnly) this.readonly();
290
- else this.enable();
291
- if (this.element.checked) this.on(silent);
292
- else this.off(silent);
293
- }
294
-
295
- trigger(silent) {
296
- if (!silent) this.element.dispatchEvent(new Event('change', { bubbles: true }));
297
- }
298
-
299
- destroy() {
300
- // A: Remove button-group from UI, replace checkbox element
301
- this.ecmasToggle.parentNode.insertBefore(this.element, this.ecmasToggle);
302
- this.ecmasToggle.parentNode.removeChild(this.ecmasToggle);
303
-
304
- // B: Delete internal refs
305
- delete this.element.bsToggle;
306
- delete this.ecmasToggle;
307
- }
308
- }
309
-
310
- /**
311
- * Add `bootstrapToggle` prototype function to HTML Elements
312
- * Enables execution when used with HTML - ex: `document.getElementById('toggle').bootstrapToggle('on')`
313
- */
314
- Element.prototype.bootstrapToggle = function(options, silent) {
315
- let _bsToggle = this.bsToggle || new Toggle(this, options);
316
-
317
- // Execute method calls
318
- if (options && typeof options === 'string') {
319
- if (options.toLowerCase() == 'toggle') _bsToggle.toggle(silent);
320
- else if (options.toLowerCase() == 'on') _bsToggle.on(silent);
321
- else if (options.toLowerCase() == 'off') _bsToggle.off(silent);
322
- else if (options.toLowerCase() == 'indeterminate') _bsToggle.indeterminate(silent);
323
- else if (options.toLowerCase() == 'determinate') _bsToggle.determinate(silent);
324
- else if (options.toLowerCase() == 'enable') _bsToggle.enable();
325
- else if (options.toLowerCase() == 'disable') _bsToggle.disable();
326
- else if (options.toLowerCase() == 'readonly') _bsToggle.readonly();
327
- else if (options.toLowerCase() == 'destroy') _bsToggle.destroy();
328
- }
329
- };
330
-
331
- /**
332
- * Replace all `input[type=checkbox][data-toggle="toggle"]` inputs with "Bootstrap-Toggle"
333
- * Executes once page elements have rendered enabling script to be placed in `<head>`
334
- */
335
- if (typeof window !== 'undefined')
336
- window.onload = function() {
337
- document.querySelectorAll('input[type=checkbox][data-toggle="toggle"]').forEach(function(ele) {
338
- ele.bootstrapToggle();
339
- });
340
- };
341
-
342
- // Export library if possible
343
- if (typeof module !== 'undefined' && module.exports) {
344
- module.exports = Toggle;
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
+ })();