')
.append($switchOn, $switchOff, $switchHandle);
var $switch = $('
')
.addClass(this.$element.prop('checked') ? '' : ' off');
this.$element.wrap($switch);
$.extend(this, {
$switch: this.$element.parent(),
$switchOn: $switchOn,
$switchOff: $switchOff,
$switchGroup: $switchGroup
});
this.$switch.append($switchGroup);
this.update(true);
this.trigger(true);
};
Switch.prototype.switch = function () {
if (this.$element.prop('checked')) {
this.off();
} else {
this.on();
}
};
Switch.prototype.on = function (silent) {
if (this.$element.prop('disabled')) return false;
this.$switch.removeClass('off');
this.$switch.addClass(this.options.onClass);
this.$element.prop('checked', true)
.attr('checked', 'checked');
if (!silent) this.trigger();
this.options.onOnCallback();
};
Switch.prototype.off = function (silent) {
if (this.$element.prop('disabled')) return false;
this.$switch.removeClass(this.options.onClass);
this.$switch.addClass('off');
this.$element.prop('checked', false)
.removeAttr('checked');
if (!silent) this.trigger();
this.options.onOffCallback();
};
Switch.prototype.enable = function () {
this.$element.prop('disabled', false);
this.$switch.removeAttr('disabled')
.removeClass('disabled');
};
Switch.prototype.disable = function () {
this.$element.prop('disabled', true);
this.$switch.attr('disabled', 'disabled')
.addClass('disabled');
};
Switch.prototype.update = function (silent) {
if (this.$element.prop('disabled')) {
this.disable();
} else {
this.enable();
}
if (this.$element.prop('checked')) {
this.$switch
.addClass(this.options.onClass);
this.on(silent);
} else {
this.$switch
.addClass(this.options.offClass);
this.off(silent);
}
};
Switch.prototype.trigger = function (silent) {
this.$element.off('change.bs.switch');
if (!silent) this.$element.change();
this.$element.on('change.bs.switch', $.proxy(function () {
this.update();
}, this));
};
// SWITCH PLUGIN DEFINITION
// ========================
function Plugin(option) {
return this.each(function () {
var $this = $(this);
var data = $this.data('bs.switch');
var options = typeof option === 'object' && option;
if (!data) $this.data('bs.switch', (data = new Switch(this, options)));
if (typeof option === 'string') data[option]();
});
}
var old = $.fn.switch;
$.fn.switch = Plugin;
$.fn.switch.Constructor = Switch;
// SWITCH NO CONFLICT
// ==================
$.fn.switch.noConflict = function () {
$.fn.switch = old;
return this;
};
// SWITCH DATA-API
// ===============
$(document)
.on('ready.bs.switch.data-api', function () {
var $this = $(this).find('[data-toggle="switch"]');
if ($this.data('switch')) return;
Plugin.call($this);
})
.on('click.bs.switch.data-api', '[data-toggle="switch"]', function () {
var $this = $(this).find('input[type=checkbox]');
if ($this.data('switch')) return;
Plugin.call($this, 'switch');
});
}(jQuery);