forked from abpetkov/switchery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchery.js
384 lines (321 loc) · 8.28 KB
/
switchery.js
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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* Switchery 0.8.1
* http://abpetkov.github.io/switchery/
*
* Authored by Alexander Petkov
* https://github.com/abpetkov
*
* Copyright 2013-2015, Alexander Petkov
* License: The MIT License (MIT)
* http://opensource.org/licenses/MIT
*
*/
/**
* External dependencies.
*/
var transitionize = require('transitionize')
, fastclick = require('fastclick')
, classes = require('classes')
, events = require('events');
/**
* Expose `Switchery`.
*/
module.exports = Switchery;
/**
* Set Switchery default values.
*
* @api public
*/
var defaults = {
color : '#64bd63'
, secondaryColor : '#dfdfdf'
, jackColor : '#fff'
, jackSecondaryColor: null
, className : 'switchery'
, disabled : false
, disabledOpacity : 0.5
, speed : '0.4s'
, size : 'default'
};
/**
* Create Switchery object.
*
* @param {Object} element
* @param {Object} options
* @api public
*/
function Switchery(element, options) {
if (!(this instanceof Switchery)) return new Switchery(element, options);
this.element = element;
this.options = options || {};
for (var i in defaults) {
if (this.options[i] == null) {
this.options[i] = defaults[i];
}
}
if (this.element != null && this.element.type == 'checkbox') this.init();
if (this.isDisabled() === true) this.disable();
}
/**
* Hide the target element.
*
* @api private
*/
Switchery.prototype.hide = function() {
this.element.style.display = 'none';
};
/**
* Show custom switch after the target element.
*
* @api private
*/
Switchery.prototype.show = function() {
var switcher = this.create();
this.insertAfter(this.element, switcher);
};
/**
* Create custom switch.
*
* @returns {Object} this.switcher
* @api private
*/
Switchery.prototype.create = function() {
this.switcher = document.createElement('span');
this.jack = document.createElement('small');
this.switcher.appendChild(this.jack);
this.switcher.className = this.options.className;
this.events = events(this.switcher, this);
return this.switcher;
};
/**
* Insert after element after another element.
*
* @param {Object} reference
* @param {Object} target
* @api private
*/
Switchery.prototype.insertAfter = function(reference, target) {
reference.parentNode.insertBefore(target, reference.nextSibling);
};
/**
* Set switch jack proper position.
*
* @param {Boolean} clicked - we need this in order to uncheck the input when the switch is clicked
* @api private
*/
Switchery.prototype.setPosition = function (clicked) {
var checked = this.isChecked()
, switcher = this.switcher
, jack = this.jack;
if (clicked && checked) checked = false;
else if (clicked && !checked) checked = true;
if (checked === true) {
this.element.checked = true;
if (window.getComputedStyle) jack.style.left = parseInt(window.getComputedStyle(switcher).width) - parseInt(window.getComputedStyle(jack).width) + 'px';
else jack.style.left = parseInt(switcher.currentStyle['width']) - parseInt(jack.currentStyle['width']) + 'px';
if (this.options.color) this.colorize();
this.setSpeed();
} else {
jack.style.left = 0;
this.element.checked = false;
this.switcher.style.boxShadow = 'inset 0 0 0 0 ' + this.options.secondaryColor;
this.switcher.style.borderColor = this.options.secondaryColor;
this.switcher.style.backgroundColor = (this.options.secondaryColor !== defaults.secondaryColor) ? this.options.secondaryColor : '#fff';
this.jack.style.backgroundColor = (this.options.jackSecondaryColor !== this.options.jackColor) ? this.options.jackSecondaryColor : this.options.jackColor;
this.setSpeed();
}
};
/**
* Set speed.
*
* @api private
*/
Switchery.prototype.setSpeed = function() {
var switcherProp = {}
, jackProp = {
'background-color': this.options.speed
, 'left': this.options.speed.replace(/[a-z]/, '') / 2 + 's'
};
if (this.isChecked()) {
switcherProp = {
'border': this.options.speed
, 'box-shadow': this.options.speed
, 'background-color': this.options.speed.replace(/[a-z]/, '') * 3 + 's'
};
} else {
switcherProp = {
'border': this.options.speed
, 'box-shadow': this.options.speed
};
}
transitionize(this.switcher, switcherProp);
transitionize(this.jack, jackProp);
};
/**
* Set switch size.
*
* @api private
*/
Switchery.prototype.setSize = function() {
var small = 'switchery-small'
, normal = 'switchery-default'
, large = 'switchery-large';
switch (this.options.size) {
case 'small':
classes(this.switcher).add(small)
break;
case 'large':
classes(this.switcher).add(large)
break;
default:
classes(this.switcher).add(normal)
break;
}
};
/**
* Set switch color.
*
* @api private
*/
Switchery.prototype.colorize = function() {
var switcherHeight = this.switcher.offsetHeight / 2;
this.switcher.style.backgroundColor = this.options.color;
this.switcher.style.borderColor = this.options.color;
this.switcher.style.boxShadow = 'inset 0 0 0 ' + switcherHeight + 'px ' + this.options.color;
this.jack.style.backgroundColor = this.options.jackColor;
};
/**
* Handle the onchange event.
*
* @param {Boolean} state
* @api private
*/
Switchery.prototype.handleOnchange = function(state) {
if (document.dispatchEvent) {
var event = document.createEvent('HTMLEvents');
event.initEvent('change', true, true);
this.element.dispatchEvent(event);
} else {
this.element.fireEvent('onchange');
}
};
/**
* Handle the native input element state change.
* A `change` event must be fired in order to detect the change.
*
* @api private
*/
Switchery.prototype.handleChange = function() {
var self = this
, el = this.element;
if (el.addEventListener) {
el.addEventListener('change', function() {
self.setPosition();
});
} else {
el.attachEvent('onchange', function() {
self.setPosition();
});
}
};
/**
* Handle the switch click event.
*
* @api private
*/
Switchery.prototype.handleClick = function() {
var switcher = this.switcher;
fastclick(switcher);
this.events.bind('click', 'bindClick');
};
/**
* Attach all methods that need to happen on switcher click.
*
* @api private
*/
Switchery.prototype.bindClick = function() {
var parent = this.element.parentNode.tagName.toLowerCase()
, labelParent = (parent === 'label') ? false : true;
this.setPosition(labelParent);
this.handleOnchange(this.element.checked);
};
/**
* Mark an individual switch as already handled.
*
* @api private
*/
Switchery.prototype.markAsSwitched = function() {
this.element.setAttribute('data-switchery', true);
};
/**
* Check if an individual switch is already handled.
*
* @api private
*/
Switchery.prototype.markedAsSwitched = function() {
return this.element.getAttribute('data-switchery');
};
/**
* Initialize Switchery.
*
* @api private
*/
Switchery.prototype.init = function() {
this.hide();
this.show();
this.setSize();
this.setPosition();
this.markAsSwitched();
this.handleChange();
this.handleClick();
};
/**
* See if input is checked.
*
* @returns {Boolean}
* @api public
*/
Switchery.prototype.isChecked = function() {
return this.element.checked;
};
/**
* See if switcher should be disabled.
*
* @returns {Boolean}
* @api public
*/
Switchery.prototype.isDisabled = function() {
return this.options.disabled || this.element.disabled || this.element.readOnly;
};
/**
* Destroy all event handlers attached to the switch.
*
* @api public
*/
Switchery.prototype.destroy = function() {
this.events.unbind();
};
/**
* Enable disabled switch element.
*
* @api public
*/
Switchery.prototype.enable = function() {
if (this.options.disabled) this.options.disabled = false;
if (this.element.disabled) this.element.disabled = false;
if (this.element.readOnly) this.element.readOnly = false;
this.switcher.style.opacity = 1;
this.events.bind('click', 'bindClick');
};
/**
* Disable switch element.
*
* @api public
*/
Switchery.prototype.disable = function() {
if (!this.options.disabled) this.options.disabled = true;
if (!this.element.disabled) this.element.disabled = true;
if (!this.element.readOnly) this.element.readOnly = true;
this.switcher.style.opacity = this.options.disabledOpacity;
this.destroy();
};