|
@@ -23,31 +23,29 @@
|
|
|
var __slice = [].slice;
|
|
|
|
|
|
(function($, window) {
|
|
|
- "use strict";
|
|
|
+ 'use strict';
|
|
|
var BootstrapSwitch;
|
|
|
BootstrapSwitch = (function() {
|
|
|
- BootstrapSwitch.prototype.name = "bootstrap-switch";
|
|
|
-
|
|
|
function BootstrapSwitch(element, options) {
|
|
|
if (options == null) {
|
|
|
options = {};
|
|
|
}
|
|
|
this.$element = $(element);
|
|
|
this.options = $.extend({}, $.fn.bootstrapSwitch.defaults, options, {
|
|
|
- state: this.$element.is(":checked"),
|
|
|
- size: this.$element.data("size"),
|
|
|
- animate: this.$element.data("animate"),
|
|
|
- disabled: this.$element.is(":disabled"),
|
|
|
- readonly: this.$element.is("[readonly]"),
|
|
|
- onColor: this.$element.data("on-color"),
|
|
|
- offColor: this.$element.data("off-color"),
|
|
|
- onText: this.$element.data("on-text"),
|
|
|
- offText: this.$element.data("off-text"),
|
|
|
- labelText: this.$element.data("label-text"),
|
|
|
- baseClass: this.$element.data("base-class"),
|
|
|
- wrapperClass: this.$element.data("wrapper-class")
|
|
|
+ state: this.$element.is(':checked'),
|
|
|
+ size: this.$element.data('size'),
|
|
|
+ animate: this.$element.data('animate'),
|
|
|
+ disabled: this.$element.is(':disabled'),
|
|
|
+ readonly: this.$element.is('[readonly]'),
|
|
|
+ onColor: this.$element.data('on-color'),
|
|
|
+ offColor: this.$element.data('off-color'),
|
|
|
+ onText: this.$element.data('on-text'),
|
|
|
+ offText: this.$element.data('off-text'),
|
|
|
+ labelText: this.$element.data('label-text'),
|
|
|
+ baseClass: this.$element.data('base-class'),
|
|
|
+ wrapperClass: this.$element.data('wrapper-class')
|
|
|
});
|
|
|
- this.$wrapper = $("<div>", {
|
|
|
+ this.$wrapper = $('<div>', {
|
|
|
"class": (function(_this) {
|
|
|
return function() {
|
|
|
var classes;
|
|
@@ -65,42 +63,42 @@
|
|
|
if (_this.options.readonly) {
|
|
|
classes.push("" + _this.options.baseClass + "-readonly");
|
|
|
}
|
|
|
- if (_this.$element.attr("id")) {
|
|
|
- classes.push("" + _this.options.baseClass + "-id-" + (_this.$element.attr("id")));
|
|
|
+ if (_this.$element.attr('id')) {
|
|
|
+ classes.push("" + _this.options.baseClass + "-id-" + (_this.$element.attr('id')));
|
|
|
}
|
|
|
- return classes.join(" ");
|
|
|
+ return classes.join(' ');
|
|
|
};
|
|
|
})(this)()
|
|
|
});
|
|
|
- this.$container = $("<div>", {
|
|
|
+ this.$container = $('<div>', {
|
|
|
"class": "" + this.options.baseClass + "-container"
|
|
|
});
|
|
|
- this.$on = $("<span>", {
|
|
|
+ this.$on = $('<span>', {
|
|
|
html: this.options.onText,
|
|
|
"class": "" + this.options.baseClass + "-handle-on " + this.options.baseClass + "-" + this.options.onColor
|
|
|
});
|
|
|
- this.$off = $("<span>", {
|
|
|
+ this.$off = $('<span>', {
|
|
|
html: this.options.offText,
|
|
|
"class": "" + this.options.baseClass + "-handle-off " + this.options.baseClass + "-" + this.options.offColor
|
|
|
});
|
|
|
- this.$label = $("<label>", {
|
|
|
- "for": this.$element.attr("id"),
|
|
|
+ this.$label = $('<label>', {
|
|
|
+ "for": this.$element.attr('id'),
|
|
|
html: this.options.labelText,
|
|
|
"class": "" + this.options.baseClass + "-label"
|
|
|
});
|
|
|
- this.$element.on("init.bootstrapSwitch", (function(_this) {
|
|
|
+ this.$element.on('init.bootstrapSwitch', (function(_this) {
|
|
|
return function() {
|
|
|
return _this.options.onInit.apply(element, arguments);
|
|
|
};
|
|
|
})(this));
|
|
|
- this.$element.on("switchChange.bootstrapSwitch", (function(_this) {
|
|
|
+ this.$element.on('switchChange.bootstrapSwitch', (function(_this) {
|
|
|
return function() {
|
|
|
return _this.options.onSwitchChange.apply(element, arguments);
|
|
|
};
|
|
|
})(this));
|
|
|
this.$container = this.$element.wrap(this.$container).parent();
|
|
|
this.$wrapper = this.$container.wrap(this.$wrapper).parent();
|
|
|
- this.$element.before(this.$on).before(this.$label).before(this.$off).trigger("init.bootstrapSwitch");
|
|
|
+ this.$element.before(this.$on).before(this.$label).before(this.$off).trigger('init.bootstrapSwitch');
|
|
|
this._elementHandlers();
|
|
|
this._handleHandlers();
|
|
|
this._labelHandlers();
|
|
@@ -110,14 +108,14 @@
|
|
|
BootstrapSwitch.prototype._constructor = BootstrapSwitch;
|
|
|
|
|
|
BootstrapSwitch.prototype.state = function(value, skip) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.state;
|
|
|
}
|
|
|
if (this.options.disabled || this.options.readonly) {
|
|
|
return this.$element;
|
|
|
}
|
|
|
value = !!value;
|
|
|
- this.$element.prop("checked", value).trigger("change.bootstrapSwitch", skip);
|
|
|
+ this.$element.prop('checked', value).trigger('change.bootstrapSwitch', skip);
|
|
|
return this.$element;
|
|
|
};
|
|
|
|
|
@@ -125,11 +123,11 @@
|
|
|
if (this.options.disabled || this.options.readonly) {
|
|
|
return this.$element;
|
|
|
}
|
|
|
- return this.$element.prop("checked", !this.options.state).trigger("change.bootstrapSwitch", skip);
|
|
|
+ return this.$element.prop('checked', !this.options.state).trigger('change.bootstrapSwitch', skip);
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.size = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.size;
|
|
|
}
|
|
|
if (this.options.size != null) {
|
|
@@ -143,46 +141,46 @@
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.animate = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.animate;
|
|
|
}
|
|
|
value = !!value;
|
|
|
- this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-animate");
|
|
|
+ this.$wrapper[value ? 'addClass' : 'removeClass']("" + this.options.baseClass + "-animate");
|
|
|
this.options.animate = value;
|
|
|
return this.$element;
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.disabled = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.disabled;
|
|
|
}
|
|
|
value = !!value;
|
|
|
- this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-disabled");
|
|
|
- this.$element.prop("disabled", value);
|
|
|
+ this.$wrapper[value ? 'addClass' : 'removeClass']("" + this.options.baseClass + "-disabled");
|
|
|
+ this.$element.prop('disabled', value);
|
|
|
this.options.disabled = value;
|
|
|
return this.$element;
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.toggleDisabled = function() {
|
|
|
- this.$element.prop("disabled", !this.options.disabled);
|
|
|
+ this.$element.prop('disabled', !this.options.disabled);
|
|
|
this.$wrapper.toggleClass("" + this.options.baseClass + "-disabled");
|
|
|
this.options.disabled = !this.options.disabled;
|
|
|
return this.$element;
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.readonly = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.readonly;
|
|
|
}
|
|
|
value = !!value;
|
|
|
- this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-readonly");
|
|
|
- this.$element.prop("readonly", value);
|
|
|
+ this.$wrapper[value ? 'addClass' : 'removeClass']("" + this.options.baseClass + "-readonly");
|
|
|
+ this.$element.prop('readonly', value);
|
|
|
this.options.readonly = value;
|
|
|
return this.$element;
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.toggleReadonly = function() {
|
|
|
- this.$element.prop("readonly", !this.options.readonly);
|
|
|
+ this.$element.prop('readonly', !this.options.readonly);
|
|
|
this.$wrapper.toggleClass("" + this.options.baseClass + "-readonly");
|
|
|
this.options.readonly = !this.options.readonly;
|
|
|
return this.$element;
|
|
@@ -191,7 +189,7 @@
|
|
|
BootstrapSwitch.prototype.onColor = function(value) {
|
|
|
var color;
|
|
|
color = this.options.onColor;
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return color;
|
|
|
}
|
|
|
if (color != null) {
|
|
@@ -205,7 +203,7 @@
|
|
|
BootstrapSwitch.prototype.offColor = function(value) {
|
|
|
var color;
|
|
|
color = this.options.offColor;
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return color;
|
|
|
}
|
|
|
if (color != null) {
|
|
@@ -217,7 +215,7 @@
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.onText = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.onText;
|
|
|
}
|
|
|
this.$on.html(value);
|
|
@@ -226,7 +224,7 @@
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.offText = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.offText;
|
|
|
}
|
|
|
this.$off.html(value);
|
|
@@ -235,7 +233,7 @@
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.labelText = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.labelText;
|
|
|
}
|
|
|
this.$label.html(value);
|
|
@@ -248,52 +246,52 @@
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.wrapperClass = function(value) {
|
|
|
- if (typeof value === "undefined") {
|
|
|
+ if (typeof value === 'undefined') {
|
|
|
return this.options.wrapperClass;
|
|
|
}
|
|
|
if (!value) {
|
|
|
value = $.fn.bootstrapSwitch.defaults.wrapperClass;
|
|
|
}
|
|
|
- this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(" "));
|
|
|
- this.$wrapper.addClass(this._getClasses(value).join(" "));
|
|
|
+ this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(' '));
|
|
|
+ this.$wrapper.addClass(this._getClasses(value).join(' '));
|
|
|
this.options.wrapperClass = value;
|
|
|
return this.$element;
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype.destroy = function() {
|
|
|
var $form;
|
|
|
- $form = this.$element.closest("form");
|
|
|
+ $form = this.$element.closest('form');
|
|
|
if ($form.length) {
|
|
|
- $form.off("reset.bootstrapSwitch").removeData("bootstrap-switch");
|
|
|
+ $form.off('reset.bootstrapSwitch').removeData('bootstrap-switch');
|
|
|
}
|
|
|
this.$container.children().not(this.$element).remove();
|
|
|
- this.$element.unwrap().unwrap().off(".bootstrapSwitch").removeData("bootstrap-switch");
|
|
|
+ this.$element.unwrap().unwrap().off('.bootstrapSwitch').removeData('bootstrap-switch');
|
|
|
return this.$element;
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype._elementHandlers = function() {
|
|
|
return this.$element.on({
|
|
|
- "change.bootstrapSwitch": (function(_this) {
|
|
|
+ 'change.bootstrapSwitch': (function(_this) {
|
|
|
return function(e, skip) {
|
|
|
var checked;
|
|
|
e.preventDefault();
|
|
|
e.stopPropagation();
|
|
|
e.stopImmediatePropagation();
|
|
|
- checked = _this.$element.is(":checked");
|
|
|
+ checked = _this.$element.is(':checked');
|
|
|
if (checked === _this.options.state) {
|
|
|
return;
|
|
|
}
|
|
|
_this.options.state = checked;
|
|
|
_this.$wrapper.removeClass(checked ? "" + _this.options.baseClass + "-off" : "" + _this.options.baseClass + "-on").addClass(checked ? "" + _this.options.baseClass + "-on" : "" + _this.options.baseClass + "-off");
|
|
|
if (!skip) {
|
|
|
- if (_this.$element.is(":radio")) {
|
|
|
- $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop("checked", false).trigger("change.bootstrapSwitch", true);
|
|
|
+ if (_this.$element.is(':radio')) {
|
|
|
+ $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop('checked', false).trigger('change.bootstrapSwitch', true);
|
|
|
}
|
|
|
- return _this.$element.trigger("switchChange.bootstrapSwitch", [checked]);
|
|
|
+ return _this.$element.trigger('switchChange.bootstrapSwitch', [checked]);
|
|
|
}
|
|
|
};
|
|
|
})(this),
|
|
|
- "focus.bootstrapSwitch": (function(_this) {
|
|
|
+ 'focus.bootstrapSwitch': (function(_this) {
|
|
|
return function(e) {
|
|
|
e.preventDefault();
|
|
|
e.stopPropagation();
|
|
@@ -301,7 +299,7 @@
|
|
|
return _this.$wrapper.addClass("" + _this.options.baseClass + "-focused");
|
|
|
};
|
|
|
})(this),
|
|
|
- "blur.bootstrapSwitch": (function(_this) {
|
|
|
+ 'blur.bootstrapSwitch': (function(_this) {
|
|
|
return function(e) {
|
|
|
e.preventDefault();
|
|
|
e.stopPropagation();
|
|
@@ -309,7 +307,7 @@
|
|
|
return _this.$wrapper.removeClass("" + _this.options.baseClass + "-focused");
|
|
|
};
|
|
|
})(this),
|
|
|
- "keydown.bootstrapSwitch": (function(_this) {
|
|
|
+ 'keydown.bootstrapSwitch': (function(_this) {
|
|
|
return function(e) {
|
|
|
if (!e.which || _this.options.disabled || _this.options.readonly) {
|
|
|
return;
|
|
@@ -337,30 +335,31 @@
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype._handleHandlers = function() {
|
|
|
- this.$on.on("click.bootstrapSwitch", (function(_this) {
|
|
|
+ this.$on.on('click.bootstrapSwitch', (function(_this) {
|
|
|
return function(e) {
|
|
|
_this.state(false);
|
|
|
- return _this.$element.trigger("focus.bootstrapSwitch");
|
|
|
+ return _this.$element.trigger('focus.bootstrapSwitch');
|
|
|
};
|
|
|
})(this));
|
|
|
- return this.$off.on("click.bootstrapSwitch", (function(_this) {
|
|
|
+ return this.$off.on('click.bootstrapSwitch', (function(_this) {
|
|
|
return function(e) {
|
|
|
_this.state(true);
|
|
|
- return _this.$element.trigger("focus.bootstrapSwitch");
|
|
|
+ return _this.$element.trigger('focus.bootstrapSwitch');
|
|
|
};
|
|
|
})(this));
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype._labelHandlers = function() {
|
|
|
return this.$label.on({
|
|
|
- "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (function(_this) {
|
|
|
+ 'mousemove.bootstrapSwitch touchmove.bootstrapSwitch': (function(_this) {
|
|
|
return function(e) {
|
|
|
- var left, percent, right;
|
|
|
+ var left, pageX, percent, right;
|
|
|
if (!_this.drag) {
|
|
|
return;
|
|
|
}
|
|
|
e.preventDefault();
|
|
|
- percent = (((e.pageX || e.originalEvent.touches[0].pageX) - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
|
|
|
+ pageX = e.pageX || e.originalEvent.touches[0].pageX;
|
|
|
+ percent = ((pageX - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
|
|
|
left = 25;
|
|
|
right = 75;
|
|
|
if (percent < left) {
|
|
@@ -368,11 +367,11 @@
|
|
|
} else if (percent > right) {
|
|
|
percent = right;
|
|
|
}
|
|
|
- _this.$container.css("margin-left", "" + (percent - right) + "%");
|
|
|
- return _this.$element.trigger("focus.bootstrapSwitch");
|
|
|
+ _this.$container.css('margin-left', "" + (percent - right) + "%");
|
|
|
+ return _this.$element.trigger('focus.bootstrapSwitch');
|
|
|
};
|
|
|
})(this),
|
|
|
- "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (function(_this) {
|
|
|
+ 'mousedown.bootstrapSwitch touchstart.bootstrapSwitch': (function(_this) {
|
|
|
return function(e) {
|
|
|
if (_this.drag || _this.options.disabled || _this.options.readonly) {
|
|
|
return;
|
|
@@ -382,26 +381,26 @@
|
|
|
if (_this.options.animate) {
|
|
|
_this.$wrapper.removeClass("" + _this.options.baseClass + "-animate");
|
|
|
}
|
|
|
- return _this.$element.trigger("focus.bootstrapSwitch");
|
|
|
+ return _this.$element.trigger('focus.bootstrapSwitch');
|
|
|
};
|
|
|
})(this),
|
|
|
- "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (function(_this) {
|
|
|
+ 'mouseup.bootstrapSwitch touchend.bootstrapSwitch': (function(_this) {
|
|
|
return function(e) {
|
|
|
if (!_this.drag) {
|
|
|
return;
|
|
|
}
|
|
|
e.preventDefault();
|
|
|
_this.drag = false;
|
|
|
- _this.$element.prop("checked", parseInt(_this.$container.css("margin-left"), 10) > -(_this.$container.width() / 6)).trigger("change.bootstrapSwitch");
|
|
|
- _this.$container.css("margin-left", "");
|
|
|
+ _this.$element.prop('checked', parseInt(_this.$container.css('margin-left'), 10) > -(_this.$container.width() / 6)).trigger('change.bootstrapSwitch');
|
|
|
+ _this.$container.css('margin-left', '');
|
|
|
if (_this.options.animate) {
|
|
|
return _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
|
|
|
}
|
|
|
};
|
|
|
})(this),
|
|
|
- "mouseleave.bootstrapSwitch": (function(_this) {
|
|
|
+ 'mouseleave.bootstrapSwitch': (function(_this) {
|
|
|
return function(e) {
|
|
|
- return _this.$label.trigger("mouseup.bootstrapSwitch");
|
|
|
+ return _this.$label.trigger('mouseup.bootstrapSwitch');
|
|
|
};
|
|
|
})(this)
|
|
|
});
|
|
@@ -409,19 +408,19 @@
|
|
|
|
|
|
BootstrapSwitch.prototype._formHandler = function() {
|
|
|
var $form;
|
|
|
- $form = this.$element.closest("form");
|
|
|
- if ($form.data("bootstrap-switch")) {
|
|
|
+ $form = this.$element.closest('form');
|
|
|
+ if ($form.data('bootstrap-switch')) {
|
|
|
return;
|
|
|
}
|
|
|
- return $form.on("reset.bootstrapSwitch", function() {
|
|
|
+ return $form.on('reset.bootstrapSwitch', function() {
|
|
|
return window.setTimeout(function() {
|
|
|
- return $form.find("input").filter(function() {
|
|
|
- return $(this).data("bootstrap-switch");
|
|
|
+ return $form.find('input').filter(function() {
|
|
|
+ return $(this).data('bootstrap-switch');
|
|
|
}).each(function() {
|
|
|
- return $(this).bootstrapSwitch("state", this.checked);
|
|
|
+ return $(this).bootstrapSwitch('state', this.checked);
|
|
|
});
|
|
|
}, 1);
|
|
|
- }).data("bootstrap-switch", true);
|
|
|
+ }).data('bootstrap-switch', true);
|
|
|
};
|
|
|
|
|
|
BootstrapSwitch.prototype._getClasses = function(classes) {
|
|
@@ -447,11 +446,11 @@
|
|
|
this.each(function() {
|
|
|
var $this, data;
|
|
|
$this = $(this);
|
|
|
- data = $this.data("bootstrap-switch");
|
|
|
+ data = $this.data('bootstrap-switch');
|
|
|
if (!data) {
|
|
|
- $this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
|
|
|
+ $this.data('bootstrap-switch', data = new BootstrapSwitch(this, option));
|
|
|
}
|
|
|
- if (typeof option === "string") {
|
|
|
+ if (typeof option === 'string') {
|
|
|
return ret = data[option].apply(data, args);
|
|
|
}
|
|
|
});
|
|
@@ -464,13 +463,13 @@
|
|
|
animate: true,
|
|
|
disabled: false,
|
|
|
readonly: false,
|
|
|
- onColor: "primary",
|
|
|
- offColor: "default",
|
|
|
- onText: "ON",
|
|
|
- offText: "OFF",
|
|
|
- labelText: " ",
|
|
|
- baseClass: "bootstrap-switch",
|
|
|
- wrapperClass: "wrapper",
|
|
|
+ onColor: 'primary',
|
|
|
+ offColor: 'default',
|
|
|
+ onText: 'ON',
|
|
|
+ offText: 'OFF',
|
|
|
+ labelText: ' ',
|
|
|
+ baseClass: 'bootstrap-switch',
|
|
|
+ wrapperClass: 'wrapper',
|
|
|
onInit: function() {},
|
|
|
onSwitchChange: function() {}
|
|
|
};
|