10
(function (global, factory) {
11
if (typeof define === "function" && define.amd) {
12
define(['jquery'], factory);
13
} else if (typeof exports !== "undefined") {
14
factory(require('jquery'));
19
factory(global.jquery);
20
global.bootstrapSwitch = mod.exports;
22
})(this, function (_jquery) {
25
var _jquery2 = _interopRequireDefault(_jquery);
27
function _interopRequireDefault(obj) {
28
return obj && obj.__esModule ? obj : {
33
var _extends = Object.assign || function (target) {
34
for (var i = 1; i < arguments.length; i++) {
35
var source = arguments[i];
37
for (var key in source) {
38
if (Object.prototype.hasOwnProperty.call(source, key)) {
39
target[key] = source[key];
47
function _classCallCheck(instance, Constructor) {
48
if (!(instance instanceof Constructor)) {
49
throw new TypeError("Cannot call a class as a function");
53
var _createClass = function () {
54
function defineProperties(target, props) {
55
for (var i = 0; i < props.length; i++) {
56
var descriptor = props[i];
57
descriptor.enumerable = descriptor.enumerable || false;
58
descriptor.configurable = true;
59
if ("value" in descriptor) descriptor.writable = true;
60
Object.defineProperty(target, descriptor.key, descriptor);
64
return function (Constructor, protoProps, staticProps) {
65
if (protoProps) defineProperties(Constructor.prototype, protoProps);
66
if (staticProps) defineProperties(Constructor, staticProps);
71
var $ = _jquery2.default || window.jQuery || window.$;
73
var BootstrapSwitch = function () {
74
function BootstrapSwitch(element) {
77
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
79
_classCallCheck(this, BootstrapSwitch);
81
this.$element = $(element);
82
this.options = $.extend({}, $.fn.bootstrapSwitch.defaults, this._getElementOptions(), options);
83
this.prevOptions = {};
84
this.$wrapper = $('<div>', {
85
class: function _class() {
87
classes.push(_this.options.state ? 'on' : 'off');
88
if (_this.options.size) {
89
classes.push(_this.options.size);
91
if (_this.options.disabled) {
92
classes.push('disabled');
94
if (_this.options.readonly) {
95
classes.push('readonly');
97
if (_this.options.indeterminate) {
98
classes.push('indeterminate');
100
if (_this.options.inverse) {
101
classes.push('inverse');
103
if (_this.$element.attr('id')) {
104
classes.push('id-' + _this.$element.attr('id'));
106
return classes.map(_this._getClass.bind(_this)).concat([_this.options.baseClass], _this._getClasses(_this.options.wrapperClass)).join(' ');
109
this.$container = $('<div>', { class: this._getClass('container') });
110
this.$on = $('<span>', {
111
html: this.options.onText,
112
class: this._getClass('handle-on') + ' ' + this._getClass(this.options.onColor)
114
this.$off = $('<span>', {
115
html: this.options.offText,
116
class: this._getClass('handle-off') + ' ' + this._getClass(this.options.offColor)
118
this.$label = $('<span>', {
119
html: this.options.labelText,
120
class: this._getClass('label')
123
this.$element.on('init.bootstrapSwitch', this.options.onInit.bind(this, element));
124
this.$element.on('switchChange.bootstrapSwitch', function () {
125
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
126
args[_key] = arguments[_key];
129
if (_this.options.onSwitchChange.apply(element, args) === false) {
130
if (_this.$element.is(':radio')) {
131
$('[name="' + _this.$element.attr('name') + '"]').trigger('previousState.bootstrapSwitch', true);
133
_this.$element.trigger('previousState.bootstrapSwitch', true);
138
this.$container = this.$element.wrap(this.$container).parent();
139
this.$wrapper = this.$container.wrap(this.$wrapper).parent();
140
this.$element.before(this.options.inverse ? this.$off : this.$on).before(this.$label).before(this.options.inverse ? this.$on : this.$off);
142
if (this.options.indeterminate) {
143
this.$element.prop('indeterminate', true);
147
this._elementHandlers();
148
this._handleHandlers();
149
this._labelHandlers();
151
this._externalLabelHandler();
152
this.$element.trigger('init.bootstrapSwitch', this.options.state);
155
_createClass(BootstrapSwitch, [{
156
key: 'setPrevOptions',
157
value: function setPrevOptions() {
158
this.prevOptions = _extends({}, this.options);
162
value: function state(value, skip) {
163
if (typeof value === 'undefined') {
164
return this.options.state;
166
if (this.options.disabled || this.options.readonly || this.options.state && !this.options.radioAllOff && this.$element.is(':radio')) {
167
return this.$element;
169
if (this.$element.is(':radio')) {
170
$('[name="' + this.$element.attr('name') + '"]').trigger('setPreviousOptions.bootstrapSwitch');
172
this.$element.trigger('setPreviousOptions.bootstrapSwitch');
174
if (this.options.indeterminate) {
175
this.indeterminate(false);
177
this.$element.prop('checked', Boolean(value)).trigger('change.bootstrapSwitch', skip);
178
return this.$element;
182
value: function toggleState(skip) {
183
if (this.options.disabled || this.options.readonly) {
184
return this.$element;
186
if (this.options.indeterminate) {
187
this.indeterminate(false);
188
return this.state(true);
190
return this.$element.prop('checked', !this.options.state).trigger('change.bootstrapSwitch', skip);
195
value: function size(value) {
196
if (typeof value === 'undefined') {
197
return this.options.size;
199
if (this.options.size != null) {
200
this.$wrapper.removeClass(this._getClass(this.options.size));
203
this.$wrapper.addClass(this._getClass(value));
206
this._containerPosition();
207
this.options.size = value;
208
return this.$element;
212
value: function animate(value) {
213
if (typeof value === 'undefined') {
214
return this.options.animate;
216
if (this.options.animate === Boolean(value)) {
217
return this.$element;
219
return this.toggleAnimate();
222
key: 'toggleAnimate',
223
value: function toggleAnimate() {
224
this.options.animate = !this.options.animate;
225
this.$wrapper.toggleClass(this._getClass('animate'));
226
return this.$element;
230
value: function disabled(value) {
231
if (typeof value === 'undefined') {
232
return this.options.disabled;
234
if (this.options.disabled === Boolean(value)) {
235
return this.$element;
237
return this.toggleDisabled();
240
key: 'toggleDisabled',
241
value: function toggleDisabled() {
242
this.options.disabled = !this.options.disabled;
243
this.$element.prop('disabled', this.options.disabled);
244
this.$wrapper.toggleClass(this._getClass('disabled'));
245
return this.$element;
249
value: function readonly(value) {
250
if (typeof value === 'undefined') {
251
return this.options.readonly;
253
if (this.options.readonly === Boolean(value)) {
254
return this.$element;
256
return this.toggleReadonly();
259
key: 'toggleReadonly',
260
value: function toggleReadonly() {
261
this.options.readonly = !this.options.readonly;
262
this.$element.prop('readonly', this.options.readonly);
263
this.$wrapper.toggleClass(this._getClass('readonly'));
264
return this.$element;
267
key: 'indeterminate',
268
value: function indeterminate(value) {
269
if (typeof value === 'undefined') {
270
return this.options.indeterminate;
272
if (this.options.indeterminate === Boolean(value)) {
273
return this.$element;
275
return this.toggleIndeterminate();
278
key: 'toggleIndeterminate',
279
value: function toggleIndeterminate() {
280
this.options.indeterminate = !this.options.indeterminate;
281
this.$element.prop('indeterminate', this.options.indeterminate);
282
this.$wrapper.toggleClass(this._getClass('indeterminate'));
283
this._containerPosition();
284
return this.$element;
288
value: function inverse(value) {
289
if (typeof value === 'undefined') {
290
return this.options.inverse;
292
if (this.options.inverse === Boolean(value)) {
293
return this.$element;
295
return this.toggleInverse();
298
key: 'toggleInverse',
299
value: function toggleInverse() {
300
this.$wrapper.toggleClass(this._getClass('inverse'));
301
var $on = this.$on.clone(true);
302
var $off = this.$off.clone(true);
303
this.$on.replaceWith($off);
304
this.$off.replaceWith($on);
307
this.options.inverse = !this.options.inverse;
308
return this.$element;
312
value: function onColor(value) {
313
if (typeof value === 'undefined') {
314
return this.options.onColor;
316
if (this.options.onColor) {
317
this.$on.removeClass(this._getClass(this.options.onColor));
319
this.$on.addClass(this._getClass(value));
320
this.options.onColor = value;
321
return this.$element;
325
value: function offColor(value) {
326
if (typeof value === 'undefined') {
327
return this.options.offColor;
329
if (this.options.offColor) {
330
this.$off.removeClass(this._getClass(this.options.offColor));
332
this.$off.addClass(this._getClass(value));
333
this.options.offColor = value;
334
return this.$element;
338
value: function onText(value) {
339
if (typeof value === 'undefined') {
340
return this.options.onText;
342
this.$on.html(value);
344
this._containerPosition();
345
this.options.onText = value;
346
return this.$element;
350
value: function offText(value) {
351
if (typeof value === 'undefined') {
352
return this.options.offText;
354
this.$off.html(value);
356
this._containerPosition();
357
this.options.offText = value;
358
return this.$element;
362
value: function labelText(value) {
363
if (typeof value === 'undefined') {
364
return this.options.labelText;
366
this.$label.html(value);
368
this.options.labelText = value;
369
return this.$element;
373
value: function handleWidth(value) {
374
if (typeof value === 'undefined') {
375
return this.options.handleWidth;
377
this.options.handleWidth = value;
379
this._containerPosition();
380
return this.$element;
384
value: function labelWidth(value) {
385
if (typeof value === 'undefined') {
386
return this.options.labelWidth;
388
this.options.labelWidth = value;
390
this._containerPosition();
391
return this.$element;
395
value: function baseClass(value) {
396
return this.options.baseClass;
400
value: function wrapperClass(value) {
401
if (typeof value === 'undefined') {
402
return this.options.wrapperClass;
405
value = $.fn.bootstrapSwitch.defaults.wrapperClass;
407
this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(' '));
408
this.$wrapper.addClass(this._getClasses(value).join(' '));
409
this.options.wrapperClass = value;
410
return this.$element;
414
value: function radioAllOff(value) {
415
if (typeof value === 'undefined') {
416
return this.options.radioAllOff;
418
var val = Boolean(value);
419
if (this.options.radioAllOff === val) {
420
return this.$element;
422
this.options.radioAllOff = val;
423
return this.$element;
427
value: function onInit(value) {
428
if (typeof value === 'undefined') {
429
return this.options.onInit;
432
value = $.fn.bootstrapSwitch.defaults.onInit;
434
this.options.onInit = value;
435
return this.$element;
438
key: 'onSwitchChange',
439
value: function onSwitchChange(value) {
440
if (typeof value === 'undefined') {
441
return this.options.onSwitchChange;
444
value = $.fn.bootstrapSwitch.defaults.onSwitchChange;
446
this.options.onSwitchChange = value;
447
return this.$element;
451
value: function destroy() {
452
var $form = this.$element.closest('form');
454
$form.off('reset.bootstrapSwitch').removeData('bootstrap-switch');
456
this.$container.children().not(this.$element).remove();
457
this.$element.unwrap().unwrap().off('.bootstrapSwitch').removeData('bootstrap-switch');
458
return this.$element;
461
key: '_getElementOptions',
462
value: function _getElementOptions() {
464
state: this.$element.is(':checked'),
465
size: this.$element.data('size'),
466
animate: this.$element.data('animate'),
467
disabled: this.$element.is(':disabled'),
468
readonly: this.$element.is('[readonly]'),
469
indeterminate: this.$element.data('indeterminate'),
470
inverse: this.$element.data('inverse'),
471
radioAllOff: this.$element.data('radio-all-off'),
472
onColor: this.$element.data('on-color'),
473
offColor: this.$element.data('off-color'),
474
onText: this.$element.data('on-text'),
475
offText: this.$element.data('off-text'),
476
labelText: this.$element.data('label-text'),
477
handleWidth: this.$element.data('handle-width'),
478
labelWidth: this.$element.data('label-width'),
479
baseClass: this.$element.data('base-class'),
480
wrapperClass: this.$element.data('wrapper-class')
485
value: function _width() {
488
var $handles = this.$on.add(this.$off).add(this.$label).css('width', '');
489
var handleWidth = this.options.handleWidth === 'auto' ? Math.round(Math.max(this.$on.width(), this.$off.width())) : this.options.handleWidth;
490
$handles.width(handleWidth);
491
this.$label.width(function (index, width) {
492
if (_this2.options.labelWidth !== 'auto') {
493
return _this2.options.labelWidth;
495
if (width < handleWidth) {
500
this._handleWidth = this.$on.outerWidth();
501
this._labelWidth = this.$label.outerWidth();
502
this.$container.width(this._handleWidth * 2 + this._labelWidth);
503
return this.$wrapper.width(this._handleWidth + this._labelWidth);
506
key: '_containerPosition',
507
value: function _containerPosition() {
510
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.state;
511
var callback = arguments[1];
513
this.$container.css('margin-left', function () {
514
var values = [0, '-' + _this3._handleWidth + 'px'];
515
if (_this3.options.indeterminate) {
516
return '-' + _this3._handleWidth / 2 + 'px';
519
if (_this3.options.inverse) {
525
if (_this3.options.inverse) {
535
value: function _init() {
538
var init = function init() {
539
_this4.setPrevOptions();
541
_this4._containerPosition();
542
setTimeout(function () {
543
if (_this4.options.animate) {
544
return _this4.$wrapper.addClass(_this4._getClass('animate'));
548
if (this.$wrapper.is(':visible')) {
552
var initInterval = window.setInterval(function () {
553
if (_this4.$wrapper.is(':visible')) {
555
return window.clearInterval(initInterval);
560
key: '_elementHandlers',
561
value: function _elementHandlers() {
564
return this.$element.on({
565
'setPreviousOptions.bootstrapSwitch': this.setPrevOptions.bind(this),
567
'previousState.bootstrapSwitch': function previousStateBootstrapSwitch() {
568
_this5.options = _this5.prevOptions;
569
if (_this5.options.indeterminate) {
570
_this5.$wrapper.addClass(_this5._getClass('indeterminate'));
572
_this5.$element.prop('checked', _this5.options.state).trigger('change.bootstrapSwitch', true);
575
'change.bootstrapSwitch': function changeBootstrapSwitch(event, skip) {
576
event.preventDefault();
577
event.stopImmediatePropagation();
578
var state = _this5.$element.is(':checked');
579
_this5._containerPosition(state);
580
if (state === _this5.options.state) {
583
_this5.options.state = state;
584
_this5.$wrapper.toggleClass(_this5._getClass('off')).toggleClass(_this5._getClass('on'));
586
if (_this5.$element.is(':radio')) {
587
$('[name="' + _this5.$element.attr('name') + '"]').not(_this5.$element).prop('checked', false).trigger('change.bootstrapSwitch', true);
589
_this5.$element.trigger('switchChange.bootstrapSwitch', [state]);
593
'focus.bootstrapSwitch': function focusBootstrapSwitch(event) {
594
event.preventDefault();
595
_this5.$wrapper.addClass(_this5._getClass('focused'));
598
'blur.bootstrapSwitch': function blurBootstrapSwitch(event) {
599
event.preventDefault();
600
_this5.$wrapper.removeClass(_this5._getClass('focused'));
603
'keydown.bootstrapSwitch': function keydownBootstrapSwitch(event) {
604
if (!event.which || _this5.options.disabled || _this5.options.readonly) {
607
if (event.which === 37 || event.which === 39) {
608
event.preventDefault();
609
event.stopImmediatePropagation();
610
_this5.state(event.which === 39);
616
key: '_handleHandlers',
617
value: function _handleHandlers() {
620
this.$on.on('click.bootstrapSwitch', function (event) {
621
event.preventDefault();
622
event.stopPropagation();
624
return _this6.$element.trigger('focus.bootstrapSwitch');
626
return this.$off.on('click.bootstrapSwitch', function (event) {
627
event.preventDefault();
628
event.stopPropagation();
630
return _this6.$element.trigger('focus.bootstrapSwitch');
634
key: '_labelHandlers',
635
value: function _labelHandlers() {
639
click: function click(event) {
640
event.stopPropagation();
644
'mousedown.bootstrapSwitch touchstart.bootstrapSwitch': function mousedownBootstrapSwitchTouchstartBootstrapSwitch(event) {
645
if (_this7._dragStart || _this7.options.disabled || _this7.options.readonly) {
648
event.preventDefault();
649
event.stopPropagation();
650
_this7._dragStart = (event.pageX || event.originalEvent.touches[0].pageX) - parseInt(_this7.$container.css('margin-left'), 10);
651
if (_this7.options.animate) {
652
_this7.$wrapper.removeClass(_this7._getClass('animate'));
654
_this7.$element.trigger('focus.bootstrapSwitch');
657
'mousemove.bootstrapSwitch touchmove.bootstrapSwitch': function mousemoveBootstrapSwitchTouchmoveBootstrapSwitch(event) {
658
if (_this7._dragStart == null) {
661
var difference = (event.pageX || event.originalEvent.touches[0].pageX) - _this7._dragStart;
662
event.preventDefault();
663
if (difference < -_this7._handleWidth || difference > 0) {
666
_this7._dragEnd = difference;
667
_this7.$container.css('margin-left', _this7._dragEnd + 'px');
670
'mouseup.bootstrapSwitch touchend.bootstrapSwitch': function mouseupBootstrapSwitchTouchendBootstrapSwitch(event) {
671
if (!_this7._dragStart) {
674
event.preventDefault();
675
if (_this7.options.animate) {
676
_this7.$wrapper.addClass(_this7._getClass('animate'));
678
if (_this7._dragEnd) {
679
var state = _this7._dragEnd > -(_this7._handleWidth / 2);
680
_this7._dragEnd = false;
681
_this7.state(_this7.options.inverse ? !state : state);
683
_this7.state(!_this7.options.state);
685
_this7._dragStart = false;
688
'mouseleave.bootstrapSwitch': function mouseleaveBootstrapSwitch() {
689
_this7.$label.trigger('mouseup.bootstrapSwitch');
692
this.$label.on(handlers);
695
key: '_externalLabelHandler',
696
value: function _externalLabelHandler() {
699
var $externalLabel = this.$element.closest('label');
700
$externalLabel.on('click', function (event) {
701
event.preventDefault();
702
event.stopImmediatePropagation();
703
if (event.target === $externalLabel[0]) {
704
_this8.toggleState();
710
value: function _formHandler() {
711
var $form = this.$element.closest('form');
712
if ($form.data('bootstrap-switch')) {
715
$form.on('reset.bootstrapSwitch', function () {
716
window.setTimeout(function () {
717
$form.find('input').filter(function () {
718
return $(this).data('bootstrap-switch');
719
}).each(function () {
720
return $(this).bootstrapSwitch('state', this.checked);
723
}).data('bootstrap-switch', true);
727
value: function _getClass(name) {
728
return this.options.baseClass + '-' + name;
732
value: function _getClasses(classes) {
733
if (!$.isArray(classes)) {
734
return [this._getClass(classes)];
736
return classes.map(this._getClass.bind(this));
740
return BootstrapSwitch;
743
$.fn.bootstrapSwitch = function (option) {
744
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
745
args[_key2 - 1] = arguments[_key2];
748
function reducer(ret, next) {
750
var existingData = $this.data('bootstrap-switch');
751
var data = existingData || new BootstrapSwitch(next, option);
753
$this.data('bootstrap-switch', data);
755
if (typeof option === 'string') {
756
return data[option].apply(data, args);
760
return Array.prototype.reduce.call(this, reducer, this);
762
$.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
763
$.fn.bootstrapSwitch.defaults = {
769
indeterminate: false,
779
baseClass: 'bootstrap-switch',
780
wrapperClass: 'wrapper',
781
onInit: function onInit() {},
782
onSwitchChange: function onSwitchChange() {}