-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbootstrap-switch-button.js
More file actions
executable file
·248 lines (220 loc) · 8.85 KB
/
bootstrap-switch-button.js
File metadata and controls
executable file
·248 lines (220 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
/*\
|*| :: Bootstrap Switch Button ::
|*|
|*| Bootstrap Switch Button
|*| https://github.com/gitbrent/bootstrap-switch-button
|*|
|*| This library is released under the MIT Public License (MIT)
|*|
|*| Bootstrap Switch Button (C) 2019-present Brent Ely (https://github.com/gitbrent)
|*|
|*| Permission is hereby granted, free of charge, to any person obtaining a copy
|*| of this software and associated documentation files (the "Software"), to deal
|*| in the Software without restriction, including without limitation the rights
|*| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|*| copies of the Software, and to permit persons to whom the Software is
|*| furnished to do so, subject to the following conditions:
|*|
|*| The above copyright notice and this permission notice shall be included in all
|*| copies or substantial portions of the Software.
|*|
|*| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|*| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|*| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|*| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|*| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|*| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|*| SOFTWARE.
\*/
'use strict';
(function() {
/**
* `SwitchBtn` is instantiated for each switch-button
*/
class SwitchBtn {
constructor(element, options) {
const DEFAULTS = {
onlabel: 'On',
onstyle: 'primary',
offlabel: 'Off',
offstyle: 'light',
size: '',
style: '',
width: null,
height: null,
};
options = options || {};
// A: Capture ref to HMTL element
this.element = element;
// B: Set options
this.options = {
onlabel: element.getAttribute('data-onlabel') || options.onlabel || DEFAULTS.onlabel,
onstyle: element.getAttribute('data-onstyle') || options.onstyle || DEFAULTS.onstyle,
offlabel: element.getAttribute('data-offlabel') || options.offlabel || DEFAULTS.offlabel,
offstyle: element.getAttribute('data-offstyle') || options.offstyle || DEFAULTS.offstyle,
size: element.getAttribute('data-size') || options.size || DEFAULTS.size,
style: element.getAttribute('data-style') || options.style || DEFAULTS.style,
width: element.getAttribute('data-width') || options.width || DEFAULTS.width,
height: element.getAttribute('data-height') || options.height || DEFAULTS.height,
};
// LAST: Render switchButton
this.render();
}
render() {
function calcH(el) {
const styles = window.getComputedStyle(el);
const height = el.offsetHeight;
const borderTopWidth = parseFloat(styles.borderTopWidth);
const borderBottomWidth = parseFloat(styles.borderBottomWidth);
const paddingTop = parseFloat(styles.paddingTop);
const paddingBottom = parseFloat(styles.paddingBottom);
return height - borderBottomWidth - borderTopWidth - paddingTop - paddingBottom;
}
// 1: On
var switchOn = document.createElement('label');
switchOn.setAttribute('class', 'btn btn-' + this.options.onstyle + ' btn-' + this.options.size);
switchOn.setAttribute('for', this.element.id);
switchOn.innerHTML = this.options.onlabel;
// 2: Off
var switchOff = document.createElement('label');
switchOff.setAttribute('class', 'btn btn-' + this.options.offstyle + ' btn-' + this.options.size);
switchOn.setAttribute('for', this.element.id);
switchOff.innerHTML = this.options.offlabel;
// 3: Handle
var switchHandle = document.createElement('span');
switchHandle.setAttribute('class', 'switch-handle btn btn-light btn-' + this.options.size);
// 4: Button-Group
var switchGroup = document.createElement('div');
switchGroup.setAttribute('class', 'switch-group');
switchGroup.appendChild(switchOn);
switchGroup.appendChild(switchOff);
switchGroup.appendChild(switchHandle);
// 5: Container
var switchCont = document.createElement('div');
switchCont.setAttribute('class', 'switch btn');
if (this.options.size) switchCont.classList.add('btn-' + this.options.size);
if (this.options.style) switchCont.classList.add(this.options.style);
// 6: Replace HTML checkbox with Switch-Button
this.element.parentElement.insertBefore(switchCont, this.element);
//this.element.style.display = 'none';
switchCont.appendChild(this.element);
switchCont.appendChild(switchGroup);
// 7: Set button W/H, lineHeight
{
// A: Set style W/H
// NOTE: `offsetWidth` returns *rounded* integer values, so use `getBoundingClientRect` instead.
switchCont.style.width =
(this.options.width ||
Math.max(switchOn.getBoundingClientRect().width, switchOff.getBoundingClientRect().width + switchHandle.getBoundingClientRect().width / 2)) + 'px';
switchCont.style.height = (this.options.height || Math.max(switchOn.getBoundingClientRect().height, switchOff.getBoundingClientRect().height)) + 'px';
// B: Apply on/off class
switchOn.classList.add('switch-on');
switchOff.classList.add('switch-off');
// C: Finally, set lineHeight if needed
if (this.options.height) {
switchOn.style.lineHeight = calcH(switchOn) + 'px';
switchOff.style.lineHeight = calcH(switchOff) + 'px';
}
}
// 8: Add listeners
switchCont.addEventListener('touchstart', this.toggle.bind(this));
switchCont.addEventListener('click', this.toggle.bind(this));
// 9: Set switch to bootstrap object
this.switch = switchCont;
// 10: Keep reference to this instance for subsequent calls via `getElementById().switchButton()`
this.element.switchBtn = this;
// 11: Fire events
this.update(true);
//this.trigger(true); // 20200215: TODO: theres an Issue on a diff version of the lib where triggering change on load isnt a good thing
}
toggle(event) {
if (this.element.checked) this.off();
else this.on();
if (typeof event !== 'undefined' && event) event.preventDefault();
}
on(silent) {
if (this.element.disabled) return false;
this.element.checked = true;
this.update(silent)
}
off(silent) {
if (this.element.disabled) return false;
this.element.checked = false;
this.update(silent)
}
/**
* Keep Switch-Button and corresponding checkbox in-sync
*/
enable() {
this.element.removeAttribute('disabled');
this.update();
}
disable() {
this.switch.classList.add('disabled');
this.update();
}
update(silent) {
if (this.element.disabled) {
this.switch.classList.add('disabled');
this.switch.setAttribute('disabled', 'disabled');
this.element.setAttribute('disabled', 'disabled');
} else {
this.switch.classList.remove('disabled');
this.switch.removeAttribute('disabled');
this.element.removeAttribute('disabled');
}
if (this.element.checked) {
this.switch.classList.remove('btn-' + this.options.offstyle);
this.switch.classList.add('btn-' + this.options.onstyle);
this.switch.classList.remove('off');
} else {
this.switch.classList.remove('btn-' + this.options.onstyle);
this.switch.classList.add('btn-' + this.options.offstyle);
this.switch.classList.add('off');
}
this.trigger(silent);
}
trigger(silent) {
if (!silent) this.element.dispatchEvent(new Event('change', { bubbles: true }));
//this.update(silent);
}
destroy() {
// A: Remove button-group from UI, replace checkbox element
this.switch.parentNode.insertBefore(this.element, this.switch);
this.switch.parentNode.removeChild(this.switch);
// B: Delete internal refs
delete this.element.switchBtn;
delete this.switch;
}
}
/**
* Add `switchButton` prototype function to HTML Elements
* Enables execution when used with HTML - ex: `document.getElementById('switch-trigger').switchButton('on')`
*/
Element.prototype.switchButton = function(options, silent) {
var _switchBtn = this.switchBtn || new SwitchBtn(this, options);
// Execute method calls
if (options && typeof options === 'string') {
if (options.toLowerCase() == 'toggle') _switchBtn.toggle();
else if (options.toLowerCase() == 'on') _switchBtn.on(silent);
else if (options.toLowerCase() == 'off') _switchBtn.off(silent);
else if (options.toLowerCase() == 'enable') _switchBtn.enable();
else if (options.toLowerCase() == 'disable') _switchBtn.disable();
else if (options.toLowerCase() == 'destroy') _switchBtn.destroy();
}
};
/**
* Replace all `.switch-button` inputs with "Bootstrap-Switch-Button"
* Executes once page elements have rendered enabling script to be placed in `<head>`
*/
if (typeof window !== 'undefined')
window.onload = function() {
document.querySelectorAll('input[type=checkbox][data-toggle="switchbutton"]').forEach(function(ele) {
ele.switchButton();
});
};
// Export library if possible
if (typeof module !== 'undefined' && module.exports) {
module.exports = SwitchBtn;
}
})();