bootstrap5-toggle 4.3.6 → 5.0.0

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,377 +1,450 @@
1
- /* Copyright Notice
2
- * bootstrap5-toggle v4.3.6
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
- +(function ($) {
15
- "use strict";
16
-
17
- // TOGGLE PUBLIC CLASS DEFINITION
18
- // ==============================
19
-
20
- let Toggle = function (element, options) {
21
- // A: Capture ref to HMTL element
22
- this.$element = $(element);
23
- // B: Set options
24
- this.options = $.extend({}, this.defaults(), options);
25
- // LAST: Render Toggle
26
- this.render();
27
- };
28
-
29
- Toggle.DEFAULTS = {
30
- on: "On",
31
- off: "Off",
32
- onstyle: "primary",
33
- offstyle: "secondary",
34
- onvalue: null,
35
- offvalue: null,
36
- size: "normal",
37
- style: "",
38
- width: null,
39
- height: null,
40
- tabindex: 0,
41
- tristate: false,
42
- name: null,
43
- };
44
-
45
- Toggle.prototype.defaults = function () {
46
- return {
47
- on: this.$element.attr("data-on") || Toggle.DEFAULTS.on,
48
- off: this.$element.attr("data-off") || Toggle.DEFAULTS.off,
49
- onstyle: this.$element.attr("data-onstyle") || Toggle.DEFAULTS.onstyle,
50
- offstyle: this.$element.attr("data-offstyle") || Toggle.DEFAULTS.offstyle,
51
- onvalue:
52
- this.$element.attr("value") ||
53
- this.$element.attr("data-onvalue") ||
54
- Toggle.DEFAULTS.onvalue,
55
- offvalue: this.$element.attr("data-offvalue") || Toggle.DEFAULTS.offvalue,
56
- size: this.$element.attr("data-size") || Toggle.DEFAULTS.size,
57
- style: this.$element.attr("data-style") || Toggle.DEFAULTS.style,
58
- width: this.$element.attr("data-width") || Toggle.DEFAULTS.width,
59
- height: this.$element.attr("data-height") || Toggle.DEFAULTS.height,
60
- tabindex: this.$element.attr("tabindex") || Toggle.DEFAULTS.tabindex,
61
- tristate: this.$element.is("[tristate]") || Toggle.DEFAULTS.tristate,
62
- name: this.$element.attr("name") || Toggle.DEFAULTS.name,
63
- };
64
- };
65
-
66
- Toggle.prototype.render = function () {
67
- // 0: Parse size
68
- let size;
69
- switch (this.options.size) {
70
- case "large":
71
- case "lg":
72
- size = "btn-lg";
73
- break;
74
- case "small":
75
- case "sm":
76
- size = "btn-sm";
77
- break;
78
- case "mini":
79
- case "xs":
80
- size = "btn-xs";
81
- break;
82
- default:
83
- size = "";
84
- break;
85
- }
86
-
87
- // 1: On
88
- let $toggleOn = $('<label class="btn">')
89
- .html(this.options.on)
90
- .addClass("btn-" + this.options.onstyle + " " + size);
91
- if (this.$element.prop("id")) {
92
- $toggleOn.prop("for", this.$element.prop("id"));
93
- }
94
-
95
- // 2: Off
96
- let $toggleOff = $('<label class="btn">')
97
- .html(this.options.off)
98
- .addClass("btn-" + this.options.offstyle + " " + size);
99
- if (this.$element.prop("id")) {
100
- $toggleOff.prop("for", this.$element.prop("id"));
101
- }
102
-
103
- // 3: Handle
104
- let $toggleHandle = $('<span class="toggle-handle btn">').addClass(size);
105
-
106
- // 4: Toggle Group
107
- let $toggleGroup = $('<div class="toggle-group">').append(
108
- $toggleOn,
109
- $toggleOff,
110
- $toggleHandle
111
- );
112
-
113
- // 5: Toggle
114
- let $toggle = $(
115
- '<div class="toggle btn" data-toggle="toggle" role="button">'
116
- )
117
- .addClass(
118
- this.$element.prop("checked")
119
- ? "btn-" + this.options.onstyle
120
- : "btn-" + this.options.offstyle + " off"
121
- )
122
- .addClass(size)
123
- .addClass(this.options.style)
124
- .attr("tabindex", this.options.tabindex);
125
- if (this.$element.prop("disabled") || this.$element.prop("readonly")) {
126
- $toggle.addClass("disabled");
127
- $toggle.attr("disabled", "disabled");
128
- }
129
-
130
- // 6: Set form values
131
- if (this.options.onvalue) this.$element.val(this.options.onvalue);
132
- let $invElement = null;
133
- if (this.options.offvalue) {
134
- $invElement = this.$element.clone();
135
- $invElement.val(this.options.offvalue);
136
- $invElement.attr("data-toggle", "invert-toggle");
137
- $invElement.removeAttr("id");
138
- $invElement.prop("checked", !this.$element.prop("checked"));
139
- }
140
-
141
- // 7: Replace HTML checkbox with Toggle-Button
142
- this.$element.wrap($toggle);
143
- $.extend(this, {
144
- $toggle: this.$element.parent(),
145
- $toggleOn: $toggleOn,
146
- $toggleOff: $toggleOff,
147
- $toggleGroup: $toggleGroup,
148
- $invElement: $invElement,
149
- });
150
- this.$toggle.append($invElement, $toggleGroup);
151
-
152
- // 8: Set button W/H, lineHeight
153
- {
154
- // A: Set style W/H
155
- let width =
156
- this.options.width ||
157
- Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth()) +
158
- $toggleHandle.outerWidth() / 2;
159
- let height =
160
- this.options.height ||
161
- Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight());
162
- this.$toggle.css({ width: width, height: height });
163
-
164
- // B: Apply on/off class
165
- $toggleOn.addClass("toggle-on");
166
- $toggleOff.addClass("toggle-off");
167
-
168
- // C: Finally, set lineHeight if needed
169
- if (this.options.height) {
170
- $toggleOn.css("line-height", $toggleOn.height() + "px");
171
- $toggleOff.css("line-height", $toggleOff.height() + "px");
172
- }
173
- }
174
-
175
- // 9: Add listeners
176
- this.$toggle.on("touchstart", (e) => {
177
- toggleActionPerformed(e, this);
178
- });
179
- this.$toggle.on("click", (e) => {
180
- toggleActionPerformed(e, this);
181
- });
182
- this.$toggle.on("keypress", (e) => {
183
- if (e.key == " ") {
184
- toggleActionPerformed(e, this);
185
- }
186
- });
187
- // 10: Set elements to bootstrap object (NOT NEEDED)
188
- // 11: Keep reference to this instance for subsequent calls via `getElementById().bootstrapToggle()` (NOT NEEDED)
189
- };
190
-
191
- /**
192
- * Trigger actions
193
- * @param {Event} e event
194
- * @param {Toggle} target Toggle
195
- */
196
- function toggleActionPerformed(e, target) {
197
- if (target.options.tristate) {
198
- if (target.$toggle.hasClass("indeterminate")) {
199
- target.determinate(true);
200
- target.toggle();
201
- } else {
202
- target.indeterminate();
203
- }
204
- } else {
205
- target.toggle();
206
- }
207
- e.preventDefault();
208
- }
209
-
210
- Toggle.prototype.toggle = function (silent = false) {
211
- if (this.$element.prop("checked")) this.off(silent);
212
- else this.on(silent);
213
- };
214
-
215
- Toggle.prototype.on = function (silent = false) {
216
- if (this.$element.prop("disabled") || this.$element.prop("readonly"))
217
- return false;
218
- this.$toggle
219
- .removeClass("btn-" + this.options.offstyle + " off")
220
- .addClass("btn-" + this.options.onstyle);
221
- this.$element.prop("checked", true);
222
- if (this.$invElement) this.$invElement.prop("checked", false);
223
- if (!silent) this.trigger();
224
- };
225
-
226
- Toggle.prototype.off = function (silent = false) {
227
- if (this.$element.prop("disabled") || this.$element.prop("readonly"))
228
- return false;
229
- this.$toggle
230
- .removeClass("btn-" + this.options.onstyle)
231
- .addClass("btn-" + this.options.offstyle + " off");
232
- this.$element.prop("checked", false);
233
- if (this.$invElement) this.$invElement.prop("checked", true);
234
- if (!silent) this.trigger();
235
- };
236
-
237
- Toggle.prototype.indeterminate = function (silent = false) {
238
- if (
239
- !this.options.tristate ||
240
- this.$element.prop("disabled") ||
241
- this.$element.prop("readonly")
242
- )
243
- return false;
244
- this.$toggle.addClass("indeterminate");
245
- this.$element.prop("indeterminate", true);
246
- this.$element.removeAttr("name");
247
- if (this.$invElement) this.$invElement.prop("indeterminate", true);
248
- if (this.$invElement) this.$invElement.removeAttr("name");
249
- if (!silent) this.trigger();
250
- };
251
-
252
- Toggle.prototype.determinate = function (silent = false) {
253
- if (
254
- !this.options.tristate ||
255
- this.$element.prop("disabled") ||
256
- this.$element.prop("readonly")
257
- )
258
- return false;
259
- this.$toggle.removeClass("indeterminate");
260
- this.$element.prop("indeterminate", false);
261
- if (this.options.name) this.$element.attr("name", this.options.name);
262
- if (this.$invElement) this.$invElement.prop("indeterminate", false);
263
- if (this.$invElement && this.options.name)
264
- this.$invElement.attr("name", this.options.name);
265
- if (!silent) this.trigger();
266
- };
267
-
268
- Toggle.prototype.enable = function () {
269
- this.$toggle.removeClass("disabled");
270
- this.$toggle.removeAttr("disabled");
271
- this.$element.prop("disabled", false);
272
- this.$element.prop("readonly", false);
273
- if (this.$invElement) {
274
- this.$invElement.prop("disabled", false);
275
- this.$invElement.prop("readonly", false);
276
- }
277
- };
278
-
279
- Toggle.prototype.disable = function () {
280
- this.$toggle.addClass("disabled");
281
- this.$toggle.attr("disabled", "disabled");
282
- this.$element.prop("disabled", true);
283
- this.$element.prop("readonly", false);
284
- if (this.$invElement) {
285
- this.$invElement.prop("disabled", true);
286
- this.$invElement.prop("readonly", false);
287
- }
288
- };
289
-
290
- Toggle.prototype.readonly = function () {
291
- this.$toggle.addClass("disabled");
292
- this.$toggle.attr("disabled", "disabled");
293
- this.$element.prop("disabled", false);
294
- this.$element.prop("readonly", true);
295
- if (this.$invElement) {
296
- this.$invElement.prop("disabled", false);
297
- this.$invElement.prop("readonly", true);
298
- }
299
- };
300
-
301
- Toggle.prototype.update = function (silent) {
302
- if (this.$element.prop("disabled")) this.disable();
303
- else if (this.$element.prop("readonly")) this.readonly();
304
- else this.enable();
305
- if (this.$element.prop("checked")) this.on(silent);
306
- else this.off(silent);
307
- };
308
-
309
- Toggle.prototype.trigger = function (silent) {
310
- this.$element.off("change.bs.toggle");
311
- if (!silent) this.$element.change();
312
- this.$element.on(
313
- "change.bs.toggle",
314
- $.proxy(function () {
315
- this.update();
316
- }, this)
317
- );
318
- };
319
-
320
- Toggle.prototype.destroy = function () {
321
- // A: Remove button-group from UI, replace checkbox element
322
- this.$element.off("change.bs.toggle");
323
- this.$toggleGroup.remove();
324
- if (this.$invElement) this.$invElement.remove();
325
-
326
- // B: Delete internal refs
327
- this.$element.removeData("bs.toggle");
328
- this.$element.unwrap();
329
- };
330
-
331
- // TOGGLE PLUGIN DEFINITION
332
- // ========================
333
-
334
- function Plugin(option) {
335
- let optArg = Array.prototype.slice.call(arguments, 1)[0];
336
-
337
- return this.each(function () {
338
- let $this = $(this);
339
- let data = $this.data("bs.toggle");
340
- let options = typeof option == "object" && option;
341
-
342
- if (!data) {
343
- data = new Toggle(this, options);
344
- $this.data("bs.toggle", data);
345
- }
346
- if (
347
- typeof option === "string" &&
348
- data[option] &&
349
- typeof optArg === "boolean"
350
- )
351
- data[option](optArg);
352
- else if (typeof option === "string" && data[option]) data[option]();
353
- //else if (option && !data[option]) console.log('bootstrap-toggle: error: method `'+ option +'` does not exist!');
354
- });
355
- }
356
-
357
- let old = $.fn.bootstrapToggle;
358
-
359
- $.fn.bootstrapToggle = Plugin;
360
- $.fn.bootstrapToggle.Constructor = Toggle;
361
-
362
- // TOGGLE NO CONFLICT
363
- // ==================
364
-
365
- $.fn.toggle.noConflict = function () {
366
- $.fn.bootstrapToggle = old;
367
- return this;
368
- };
369
-
370
- /**
371
- * Replace all `input[type=checkbox][data-toggle="toggle"]` inputs with "Bootstrap-Toggle"
372
- * Executes once page elements have rendered enabling script to be placed in `<head>`
373
- */
374
- $(function () {
375
- $("input[type=checkbox][data-toggle^=toggle]").bootstrapToggle();
376
- });
377
- })(jQuery);
1
+ /* Copyright Notice
2
+ * bootstrap5-toggle v5.0.0-alpha
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
+ +(function ($) {
15
+ "use strict";
16
+
17
+ // TOGGLE PUBLIC CLASS DEFINITION
18
+ // ==============================
19
+
20
+ let Toggle = function (element, options) {
21
+ // A: Capture ref to HMTL element
22
+ this.$element = $(element);
23
+
24
+ // B: Set options
25
+ this.options = $.extend({}, this.defaults(), options);
26
+
27
+ // C: Check deprecations
28
+ if (this.options.onlabel === Toggle.DEPRECATION.value) {
29
+ if (this.$element.attr("data-on")) {
30
+ Toggle.DEPRECATION.log(
31
+ Toggle.DEPRECATION.ATTRIBUTE,
32
+ "data-on",
33
+ "data-onlabel"
34
+ );
35
+ this.options.onlabel = this.$element.attr("data-on");
36
+ } else if (options.on) {
37
+ Toggle.DEPRECATION.log(Toggle.DEPRECATION.OPTION, "on", "onlabel");
38
+ this.options.onlabel = options.on;
39
+ } else {
40
+ this.options.onlabel = Toggle.DEFAULTS.onlabel;
41
+ }
42
+ }
43
+ if (this.options.offlabel === Toggle.DEPRECATION.value) {
44
+ if (this.$element.attr("data-off")) {
45
+ Toggle.DEPRECATION.log(
46
+ Toggle.DEPRECATION.ATTRIBUTE,
47
+ "data-off",
48
+ "data-offlabel"
49
+ );
50
+ this.options.offlabel = this.$element.attr("data-off");
51
+ } else if (options.off) {
52
+ Toggle.DEPRECATION.log(Toggle.DEPRECATION.OPTION, "off", "offlabel");
53
+ this.options.offlabel = options.off;
54
+ } else {
55
+ this.options.offlabel = Toggle.DEFAULTS.offlabel;
56
+ }
57
+ }
58
+
59
+ // LAST: Render Toggle
60
+ this.render();
61
+ };
62
+
63
+ Toggle.DEPRECATION = {
64
+ value:
65
+ "BOOTSTRAP TOGGLE DEPRECATION CHECK -- a0Jhux0QySypjjs4tLtEo8xT2kx0AbYaq9K6mgNjWSs0HF0L8T8J0M0o3Kr7zkm7 --",
66
+ ATTRIBUTE: "attribute",
67
+ OPTION: "option",
68
+ log: function (type, oldlabel, newlabel) {
69
+ console.warn(
70
+ `Bootstrap Toggle deprecation warning: Using ${oldlabel} ${type} is deprected. Use ${newlabel} instead.`
71
+ );
72
+ },
73
+ };
74
+
75
+ Toggle.DEFAULTS = {
76
+ onlabel: "On",
77
+ offlabel: "Off",
78
+ onstyle: "primary",
79
+ offstyle: "secondary",
80
+ onvalue: null,
81
+ offvalue: null,
82
+ ontitle: null,
83
+ offtitle: null,
84
+ size: "normal",
85
+ style: "",
86
+ width: null,
87
+ height: null,
88
+ tabindex: 0,
89
+ tristate: false,
90
+ name: null,
91
+ };
92
+
93
+ Toggle.prototype.defaults = function () {
94
+ return {
95
+ onlabel:
96
+ this.$element.attr("data-onlabel") ||
97
+ Toggle.DEPRECATION.value ||
98
+ Toggle.DEFAULTS.onlabel,
99
+ offlabel:
100
+ this.$element.attr("data-offlabel") ||
101
+ Toggle.DEPRECATION.value ||
102
+ Toggle.DEFAULTS.offlabel,
103
+ onstyle: this.$element.attr("data-onstyle") || Toggle.DEFAULTS.onstyle,
104
+ offstyle: this.$element.attr("data-offstyle") || Toggle.DEFAULTS.offstyle,
105
+ onvalue:
106
+ this.$element.attr("value") ||
107
+ this.$element.attr("data-onvalue") ||
108
+ Toggle.DEFAULTS.onvalue,
109
+ offvalue: this.$element.attr("data-offvalue") || Toggle.DEFAULTS.offvalue,
110
+ ontitle:
111
+ this.$element.attr("data-ontitle") ||
112
+ this.$element.attr("title") ||
113
+ Toggle.DEFAULTS.ontitle,
114
+ offtitle:
115
+ this.$element.attr("data-offtitle") ||
116
+ this.$element.attr("title") ||
117
+ Toggle.DEFAULTS.offtitle,
118
+ size: this.$element.attr("data-size") || Toggle.DEFAULTS.size,
119
+ style: this.$element.attr("data-style") || Toggle.DEFAULTS.style,
120
+ width: this.$element.attr("data-width") || Toggle.DEFAULTS.width,
121
+ height: this.$element.attr("data-height") || Toggle.DEFAULTS.height,
122
+ tabindex: this.$element.attr("tabindex") || Toggle.DEFAULTS.tabindex,
123
+ tristate: this.$element.is("[tristate]") || Toggle.DEFAULTS.tristate,
124
+ name: this.$element.attr("name") || Toggle.DEFAULTS.name,
125
+ };
126
+ };
127
+
128
+ Toggle.prototype.render = function () {
129
+ // 0: Parse size
130
+ let size;
131
+ switch (this.options.size) {
132
+ case "large":
133
+ case "lg":
134
+ size = "btn-lg";
135
+ break;
136
+ case "small":
137
+ case "sm":
138
+ size = "btn-sm";
139
+ break;
140
+ case "mini":
141
+ case "xs":
142
+ size = "btn-xs";
143
+ break;
144
+ default:
145
+ size = "";
146
+ break;
147
+ }
148
+
149
+ // 1: On
150
+ let $toggleOn = $('<span class="btn">')
151
+ .html(this.options.onlabel)
152
+ .addClass("btn-" + this.options.onstyle + " " + size);
153
+ if (this.options.ontitle) {
154
+ $toggleOn.attr("title", this.options.ontitle);
155
+ }
156
+
157
+ // 2: Off
158
+ let $toggleOff = $('<span class="btn">')
159
+ .html(this.options.offlabel)
160
+ .addClass("btn-" + this.options.offstyle + " " + size);
161
+ if (this.options.offtitle) {
162
+ $toggleOff.attr("title", this.options.offtitle);
163
+ }
164
+
165
+ // 3: Handle
166
+ let $toggleHandle = $('<span class="toggle-handle btn">').addClass(size);
167
+
168
+ // 4: Toggle Group
169
+ let $toggleGroup = $('<div class="toggle-group">').append(
170
+ $toggleOn,
171
+ $toggleOff,
172
+ $toggleHandle
173
+ );
174
+
175
+ // 5: Toggle
176
+ let $toggle = $(
177
+ '<div class="toggle btn" data-toggle="toggle" role="button">'
178
+ )
179
+ .addClass(
180
+ this.$element.prop("checked")
181
+ ? "btn-" + this.options.onstyle
182
+ : "btn-" + this.options.offstyle + " off"
183
+ )
184
+ .addClass(size)
185
+ .addClass(this.options.style)
186
+ .attr("tabindex", this.options.tabindex);
187
+ if (this.$element.prop("disabled") || this.$element.prop("readonly")) {
188
+ $toggle.addClass("disabled");
189
+ $toggle.attr("disabled", "disabled");
190
+ }
191
+
192
+ // 6: Set form values
193
+ if (this.options.onvalue) this.$element.val(this.options.onvalue);
194
+ let $invElement = null;
195
+ if (this.options.offvalue) {
196
+ $invElement = this.$element.clone();
197
+ $invElement.val(this.options.offvalue);
198
+ $invElement.attr("data-toggle", "invert-toggle");
199
+ $invElement.removeAttr("id");
200
+ $invElement.prop("checked", !this.$element.prop("checked"));
201
+ }
202
+
203
+ // 7: Replace HTML checkbox with Toggle-Button
204
+ this.$element.wrap($toggle);
205
+ $.extend(this, {
206
+ $toggle: this.$element.parent(),
207
+ $toggleOn: $toggleOn,
208
+ $toggleOff: $toggleOff,
209
+ $toggleGroup: $toggleGroup,
210
+ $invElement: $invElement,
211
+ });
212
+ this.$toggle.append($invElement, $toggleGroup);
213
+
214
+ // 8: Set button W/H, lineHeight
215
+ {
216
+ // A: Set style W/H
217
+ let width =
218
+ this.options.width ||
219
+ Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth()) +
220
+ $toggleHandle.outerWidth() / 2;
221
+ let height =
222
+ this.options.height ||
223
+ Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight());
224
+ this.$toggle.css({ width: width, height: height });
225
+
226
+ // B: Apply on/off class
227
+ $toggleOn.addClass("toggle-on");
228
+ $toggleOff.addClass("toggle-off");
229
+
230
+ // C: Finally, set lineHeight if needed
231
+ if (this.options.height) {
232
+ $toggleOn.css("line-height", $toggleOn.height() + "px");
233
+ $toggleOff.css("line-height", $toggleOff.height() + "px");
234
+ }
235
+ }
236
+
237
+ // 9: Add listeners
238
+ this.$toggle.on("touchstart", (e) => {
239
+ toggleActionPerformed(e, this);
240
+ });
241
+ this.$toggle.on("click", (e) => {
242
+ toggleActionPerformed(e, this);
243
+ });
244
+ this.$toggle.on("keypress", (e) => {
245
+ if (e.key == " ") {
246
+ toggleActionPerformed(e, this);
247
+ }
248
+ });
249
+
250
+ if (this.$element.prop("id")) {
251
+ $('label[for="' + this.$element.prop("id") + '"]').on(
252
+ "touchstart click",
253
+ (_e) => {
254
+ this.toggle();
255
+ this.$toggle.focus();
256
+ }
257
+ );
258
+ }
259
+
260
+ // 10: Set elements to bootstrap object (NOT NEEDED)
261
+ // 11: Keep reference to this instance for subsequent calls via `getElementById().bootstrapToggle()` (NOT NEEDED)
262
+ };
263
+
264
+ /**
265
+ * Trigger actions
266
+ * @param {Event} e event
267
+ * @param {Toggle} target Toggle
268
+ */
269
+ function toggleActionPerformed(e, target) {
270
+ if (target.options.tristate) {
271
+ if (target.$toggle.hasClass("indeterminate")) {
272
+ target.determinate(true);
273
+ target.toggle();
274
+ } else {
275
+ target.indeterminate();
276
+ }
277
+ } else {
278
+ target.toggle();
279
+ }
280
+ e.preventDefault();
281
+ }
282
+
283
+ Toggle.prototype.toggle = function (silent = false) {
284
+ if (this.$element.prop("checked")) this.off(silent);
285
+ else this.on(silent);
286
+ };
287
+
288
+ Toggle.prototype.on = function (silent = false) {
289
+ if (this.$element.prop("disabled") || this.$element.prop("readonly"))
290
+ return false;
291
+ this.$toggle
292
+ .removeClass("btn-" + this.options.offstyle + " off")
293
+ .addClass("btn-" + this.options.onstyle);
294
+ this.$element.prop("checked", true);
295
+ if (this.$invElement) this.$invElement.prop("checked", false);
296
+ if (!silent) this.trigger();
297
+ };
298
+
299
+ Toggle.prototype.off = function (silent = false) {
300
+ if (this.$element.prop("disabled") || this.$element.prop("readonly"))
301
+ return false;
302
+ this.$toggle
303
+ .removeClass("btn-" + this.options.onstyle)
304
+ .addClass("btn-" + this.options.offstyle + " off");
305
+ this.$element.prop("checked", false);
306
+ if (this.$invElement) this.$invElement.prop("checked", true);
307
+ if (!silent) this.trigger();
308
+ };
309
+
310
+ Toggle.prototype.indeterminate = function (silent = false) {
311
+ if (
312
+ !this.options.tristate ||
313
+ this.$element.prop("disabled") ||
314
+ this.$element.prop("readonly")
315
+ )
316
+ return false;
317
+ this.$toggle.addClass("indeterminate");
318
+ this.$element.prop("indeterminate", true);
319
+ this.$element.removeAttr("name");
320
+ if (this.$invElement) this.$invElement.prop("indeterminate", true);
321
+ if (this.$invElement) this.$invElement.removeAttr("name");
322
+ if (!silent) this.trigger();
323
+ };
324
+
325
+ Toggle.prototype.determinate = function (silent = false) {
326
+ if (
327
+ !this.options.tristate ||
328
+ this.$element.prop("disabled") ||
329
+ this.$element.prop("readonly")
330
+ )
331
+ return false;
332
+ this.$toggle.removeClass("indeterminate");
333
+ this.$element.prop("indeterminate", false);
334
+ if (this.options.name) this.$element.attr("name", this.options.name);
335
+ if (this.$invElement) this.$invElement.prop("indeterminate", false);
336
+ if (this.$invElement && this.options.name)
337
+ this.$invElement.attr("name", this.options.name);
338
+ if (!silent) this.trigger();
339
+ };
340
+
341
+ Toggle.prototype.enable = function () {
342
+ this.$toggle.removeClass("disabled");
343
+ this.$toggle.removeAttr("disabled");
344
+ this.$element.prop("disabled", false);
345
+ this.$element.prop("readonly", false);
346
+ if (this.$invElement) {
347
+ this.$invElement.prop("disabled", false);
348
+ this.$invElement.prop("readonly", false);
349
+ }
350
+ };
351
+
352
+ Toggle.prototype.disable = function () {
353
+ this.$toggle.addClass("disabled");
354
+ this.$toggle.attr("disabled", "disabled");
355
+ this.$element.prop("disabled", true);
356
+ this.$element.prop("readonly", false);
357
+ if (this.$invElement) {
358
+ this.$invElement.prop("disabled", true);
359
+ this.$invElement.prop("readonly", false);
360
+ }
361
+ };
362
+
363
+ Toggle.prototype.readonly = function () {
364
+ this.$toggle.addClass("disabled");
365
+ this.$toggle.attr("disabled", "disabled");
366
+ this.$element.prop("disabled", false);
367
+ this.$element.prop("readonly", true);
368
+ if (this.$invElement) {
369
+ this.$invElement.prop("disabled", false);
370
+ this.$invElement.prop("readonly", true);
371
+ }
372
+ };
373
+
374
+ Toggle.prototype.update = function (silent) {
375
+ if (this.$element.prop("disabled")) this.disable();
376
+ else if (this.$element.prop("readonly")) this.readonly();
377
+ else this.enable();
378
+ if (this.$element.prop("checked")) this.on(silent);
379
+ else this.off(silent);
380
+ };
381
+
382
+ Toggle.prototype.trigger = function (silent) {
383
+ this.$element.off("change.bs.toggle");
384
+ if (!silent) this.$element.change();
385
+ this.$element.on(
386
+ "change.bs.toggle",
387
+ $.proxy(function () {
388
+ this.update();
389
+ }, this)
390
+ );
391
+ };
392
+
393
+ Toggle.prototype.destroy = function () {
394
+ // A: Remove button-group from UI, replace checkbox element
395
+ this.$element.off("change.bs.toggle");
396
+ this.$toggleGroup.remove();
397
+ if (this.$invElement) this.$invElement.remove();
398
+
399
+ // B: Delete internal refs
400
+ this.$element.removeData("bs.toggle");
401
+ this.$element.unwrap();
402
+ };
403
+
404
+ // TOGGLE PLUGIN DEFINITION
405
+ // ========================
406
+
407
+ function Plugin(option) {
408
+ let optArg = Array.prototype.slice.call(arguments, 1)[0];
409
+
410
+ return this.each(function () {
411
+ let $this = $(this);
412
+ let data = $this.data("bs.toggle");
413
+ let options = typeof option == "object" && option;
414
+
415
+ if (!data) {
416
+ data = new Toggle(this, options);
417
+ $this.data("bs.toggle", data);
418
+ }
419
+ if (
420
+ typeof option === "string" &&
421
+ data[option] &&
422
+ typeof optArg === "boolean"
423
+ )
424
+ data[option](optArg);
425
+ else if (typeof option === "string" && data[option]) data[option]();
426
+ //else if (option && !data[option]) console.log('bootstrap-toggle: error: method `'+ option +'` does not exist!');
427
+ });
428
+ }
429
+
430
+ let old = $.fn.bootstrapToggle;
431
+
432
+ $.fn.bootstrapToggle = Plugin;
433
+ $.fn.bootstrapToggle.Constructor = Toggle;
434
+
435
+ // TOGGLE NO CONFLICT
436
+ // ==================
437
+
438
+ $.fn.toggle.noConflict = function () {
439
+ $.fn.bootstrapToggle = old;
440
+ return this;
441
+ };
442
+
443
+ /**
444
+ * Replace all `input[type=checkbox][data-toggle="toggle"]` inputs with "Bootstrap-Toggle"
445
+ * Executes once page elements have rendered enabling script to be placed in `<head>`
446
+ */
447
+ $(function () {
448
+ $("input[type=checkbox][data-toggle^=toggle]").bootstrapToggle();
449
+ });
450
+ })(jQuery);