|
@@ -29,6 +29,7 @@
|
|
|
BootstrapSwitch.prototype.name = "bootstrap-switch";
|
|
|
|
|
|
function BootstrapSwitch(element, options) {
|
|
|
+ var addClasses;
|
|
|
if (options == null) {
|
|
|
options = {};
|
|
|
}
|
|
@@ -45,57 +46,69 @@
|
|
|
offText: this.$element.data("off-text"),
|
|
|
labelText: this.$element.data("label-text")
|
|
|
});
|
|
|
+ this.$wrapper = $("<div>");
|
|
|
+ this.$container = $("<div>");
|
|
|
this.$on = $("<span>", {
|
|
|
- "class": "" + this.options.classes.base + "-" + this.options.classes.handleOn + " " + this.options.classes.base + "-" + this.options.onColor,
|
|
|
html: this.options.onText
|
|
|
});
|
|
|
this.$off = $("<span>", {
|
|
|
- "class": "" + this.options.classes.base + "-" + this.options.classes.handleOff + " " + this.options.classes.base + "-" + this.options.offColor,
|
|
|
html: this.options.offText
|
|
|
});
|
|
|
this.$label = $("<label>", {
|
|
|
- "class": "" + this.options.classes.base + "-" + this.options.classes.label,
|
|
|
"for": this.$element.attr("id"),
|
|
|
html: this.options.labelText
|
|
|
});
|
|
|
- this.$wrapper = $("<div>");
|
|
|
- this.$container = $("<div>", {
|
|
|
- "class": "" + this.options.classes.base + "-" + this.options.classes.container
|
|
|
- });
|
|
|
+ addClasses = (function(_this) {
|
|
|
+ return function(cls) {
|
|
|
+ var c, classes, _i, _len;
|
|
|
+ if (!$.isArray(cls)) {
|
|
|
+ return "" + _this.options.baseClass + "-" + cls;
|
|
|
+ }
|
|
|
+ classes = [];
|
|
|
+ for (_i = 0, _len = cls.length; _i < _len; _i++) {
|
|
|
+ c = cls[_i];
|
|
|
+ classes.push("" + _this.options.baseClass + "-" + c);
|
|
|
+ }
|
|
|
+ return classes.join(" ");
|
|
|
+ };
|
|
|
+ })(this);
|
|
|
this.$wrapper.addClass((function(_this) {
|
|
|
return function() {
|
|
|
var classes;
|
|
|
- classes = ["" + _this.options.classes.base];
|
|
|
- classes.push(_this.options.state ? "" + _this.options.classes.base + "-" + _this.options.classes.modifiers.on : "" + _this.options.classes.base + "-" + _this.options.classes.modifiers.off);
|
|
|
+ classes = ["" + _this.options.baseClass, "" + _this.options.baseClass + "-" + _this.options.wrapperClass];
|
|
|
+ classes.push(_this.options.state ? "" + _this.options.baseClass + "-" + _this.options.onModifierClass : "" + _this.options.baseClass + "-" + _this.options.offModifierClass);
|
|
|
if (_this.options.size != null) {
|
|
|
- classes.push("" + _this.options.classes.base + "-" + _this.options.size);
|
|
|
+ classes.push("" + _this.options.baseClass + "-" + _this.options.size);
|
|
|
}
|
|
|
if (_this.options.animate) {
|
|
|
- classes.push("" + _this.options.classes.base + "-" + _this.options.classes.modifiers.animate);
|
|
|
+ classes.push("" + _this.options.baseClass + "-" + _this.options.animateModifierClass);
|
|
|
}
|
|
|
if (_this.options.disabled) {
|
|
|
- classes.push("" + _this.options.classes.base + "-" + _this.options.classes.modifiers.disabled);
|
|
|
+ classes.push("" + _this.options.baseClass + "-" + _this.options.disabledModifierClass);
|
|
|
}
|
|
|
if (_this.options.readonly) {
|
|
|
- classes.push("" + _this.options.classes.base + "-" + _this.options.classes.modifiers.readonly);
|
|
|
+ classes.push("" + _this.options.baseClass + "-" + _this.options.readonlyModifierClass);
|
|
|
}
|
|
|
if (_this.$element.attr("id")) {
|
|
|
- classes.push("" + _this.options.classes.base + "-id-" + (_this.$element.attr("id")));
|
|
|
+ classes.push("" + _this.options.baseClass + "-id-" + (_this.$element.attr("id")));
|
|
|
}
|
|
|
return classes.join(" ");
|
|
|
};
|
|
|
})(this));
|
|
|
+ this.$container.addClass(addClasses(this.options.containerClass));
|
|
|
+ this.$on.addClass("" + (addClasses(this.options.handleOnClass)) + " " + this.options.baseClass + "-" + this.options.onColor);
|
|
|
+ this.$off.addClass("" + (addClasses(this.options.handleOffClass)) + " " + this.options.baseClass + "-" + this.options.offColor);
|
|
|
+ this.$label.addClass(addClasses(this.options.labelClass));
|
|
|
this.$element.on("init.bootstrapSwitch", (function(_this) {
|
|
|
return function() {
|
|
|
- return _this.options.onInit.apply(_this.$element[0], arguments);
|
|
|
+ return _this.options.onInit.apply(element, arguments);
|
|
|
};
|
|
|
})(this));
|
|
|
this.$element.on("switchChange.bootstrapSwitch", (function(_this) {
|
|
|
return function() {
|
|
|
- return _this.options.onSwitchChange.apply(_this.$element[0], arguments);
|
|
|
+ return _this.options.onSwitchChange.apply(element, arguments);
|
|
|
};
|
|
|
})(this));
|
|
|
- console.log(this.$container);
|
|
|
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");
|
|
@@ -131,9 +144,9 @@
|
|
|
return this.options.size;
|
|
|
}
|
|
|
if (this.options.size != null) {
|
|
|
- this.$wrapper.removeClass("" + this.options.classes.base + "-" + this.options.size);
|
|
|
+ this.$wrapper.removeClass("" + this.options.baseClass + "-" + this.options.size);
|
|
|
}
|
|
|
- this.$wrapper.addClass("" + this.options.classes.base + "-" + value);
|
|
|
+ this.$wrapper.addClass("" + this.options.baseClass + "-" + value);
|
|
|
this.options.size = value;
|
|
|
return this.$element;
|
|
|
};
|
|
@@ -143,7 +156,7 @@
|
|
|
return this.options.animate;
|
|
|
}
|
|
|
value = !!value;
|
|
|
- this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.classes.base + "-" + this.options.classes.modifiers.animate);
|
|
|
+ this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-" + this.options.animateModifierClass);
|
|
|
this.options.animate = value;
|
|
|
return this.$element;
|
|
|
};
|
|
@@ -153,7 +166,7 @@
|
|
|
return this.options.disabled;
|
|
|
}
|
|
|
value = !!value;
|
|
|
- this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.classes.base + "-" + this.options.classes.modifiers.disabled);
|
|
|
+ this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-" + this.options.disabledModifierClass);
|
|
|
this.$element.prop("disabled", value);
|
|
|
this.options.disabled = value;
|
|
|
return this.$element;
|
|
@@ -161,7 +174,7 @@
|
|
|
|
|
|
BootstrapSwitch.prototype.toggleDisabled = function() {
|
|
|
this.$element.prop("disabled", !this.options.disabled);
|
|
|
- this.$wrapper.toggleClass("" + this.options.classes.base + "-" + this.options.classes.modifiers.disabled);
|
|
|
+ this.$wrapper.toggleClass("" + this.options.baseClass + "-" + this.options.disabledModifierClass);
|
|
|
this.options.disabled = !this.options.disabled;
|
|
|
return this.$element;
|
|
|
};
|
|
@@ -171,7 +184,7 @@
|
|
|
return this.options.readonly;
|
|
|
}
|
|
|
value = !!value;
|
|
|
- this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.classes.base + "-" + this.options.classes.modifiers.readonly);
|
|
|
+ this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-" + this.options.readonlyModifierClass);
|
|
|
this.$element.prop("readonly", value);
|
|
|
this.options.readonly = value;
|
|
|
return this.$element;
|
|
@@ -179,7 +192,7 @@
|
|
|
|
|
|
BootstrapSwitch.prototype.toggleReadonly = function() {
|
|
|
this.$element.prop("readonly", !this.options.readonly);
|
|
|
- this.$wrapper.toggleClass("" + this.options.classes.base + "-" + this.options.classes.modifiers.readonly);
|
|
|
+ this.$wrapper.toggleClass("" + this.options.baseClass + "-" + this.options.readonlyModifierClass);
|
|
|
this.options.readonly = !this.options.readonly;
|
|
|
return this.$element;
|
|
|
};
|
|
@@ -191,9 +204,9 @@
|
|
|
return color;
|
|
|
}
|
|
|
if (color != null) {
|
|
|
- this.$on.removeClass("" + this.options.classes.base + "-" + color);
|
|
|
+ this.$on.removeClass("" + this.options.baseClass + "-" + color);
|
|
|
}
|
|
|
- this.$on.addClass("" + this.options.classes.base + "-" + value);
|
|
|
+ this.$on.addClass("" + this.options.baseClass + "-" + value);
|
|
|
this.options.onColor = value;
|
|
|
return this.$element;
|
|
|
};
|
|
@@ -205,9 +218,9 @@
|
|
|
return color;
|
|
|
}
|
|
|
if (color != null) {
|
|
|
- this.$off.removeClass("" + this.options.classes.base + "-" + color);
|
|
|
+ this.$off.removeClass("" + this.options.baseClass + "-" + color);
|
|
|
}
|
|
|
- this.$off.addClass("" + this.options.classes.base + "-" + value);
|
|
|
+ this.$off.addClass("" + this.options.baseClass + "-" + value);
|
|
|
this.options.offColor = value;
|
|
|
return this.$element;
|
|
|
};
|
|
@@ -263,7 +276,7 @@
|
|
|
return;
|
|
|
}
|
|
|
_this.options.state = checked;
|
|
|
- _this.$wrapper.removeClass(checked ? "" + _this.options.classes.base + "-" + _this.options.classes.modifiers.off : "" + _this.options.classes.base + "-" + _this.options.classes.modifiers.on).addClass(checked ? "" + _this.options.classes.base + "-" + _this.options.classes.modifiers.on : "" + _this.options.classes.base + "-" + _this.options.classes.modifiers.off);
|
|
|
+ _this.$wrapper.removeClass(checked ? "" + _this.options.baseClass + "-" + _this.options.offModifierClass : "" + _this.options.baseClass + "-" + _this.options.onModifierClass).addClass(checked ? "" + _this.options.baseClass + "-" + _this.options.onModifierClass : "" + _this.options.baseClass + "-" + _this.options.offModifierClass);
|
|
|
if (!skip) {
|
|
|
if (_this.$element.is(":radio")) {
|
|
|
$("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop("checked", false).trigger("change.bootstrapSwitch", true);
|
|
@@ -277,7 +290,7 @@
|
|
|
e.preventDefault();
|
|
|
e.stopPropagation();
|
|
|
e.stopImmediatePropagation();
|
|
|
- return _this.$wrapper.addClass("" + _this.options.classes.base + "-" + _this.options.classes.modifiers.focused);
|
|
|
+ return _this.$wrapper.addClass("" + _this.options.baseClass + "-" + _this.options.focusedModifierClass);
|
|
|
};
|
|
|
})(this),
|
|
|
"blur.bootstrapSwitch": (function(_this) {
|
|
@@ -285,7 +298,7 @@
|
|
|
e.preventDefault();
|
|
|
e.stopPropagation();
|
|
|
e.stopImmediatePropagation();
|
|
|
- return _this.$wrapper.removeClass("" + _this.options.classes.base + "-" + _this.options.classes.modifiers.focused);
|
|
|
+ return _this.$wrapper.removeClass("" + _this.options.baseClass + "-" + _this.options.focusedModifierClass);
|
|
|
};
|
|
|
})(this),
|
|
|
"keydown.bootstrapSwitch": (function(_this) {
|
|
@@ -357,7 +370,7 @@
|
|
|
}
|
|
|
_this.drag = true;
|
|
|
if (_this.options.animate) {
|
|
|
- _this.$wrapper.removeClass("" + _this.options.classes.base + "-" + _this.options.classes.modifiers.animate);
|
|
|
+ _this.$wrapper.removeClass("" + _this.options.baseClass + "-" + _this.options.animateModifierClass);
|
|
|
}
|
|
|
return _this.$element.trigger("focus.bootstrapSwitch");
|
|
|
};
|
|
@@ -371,7 +384,7 @@
|
|
|
_this.$element.prop("checked", parseInt(_this.$container.css("margin-left"), 10) > -25).trigger("change.bootstrapSwitch");
|
|
|
_this.$container.css("margin-left", "");
|
|
|
if (_this.options.animate) {
|
|
|
- return _this.$wrapper.addClass("" + _this.options.classes.base + "-" + _this.options.classes.modifiers.animate);
|
|
|
+ return _this.$wrapper.addClass("" + _this.options.baseClass + "-" + _this.options.animateModifierClass);
|
|
|
}
|
|
|
};
|
|
|
})(this),
|
|
@@ -440,22 +453,18 @@
|
|
|
onText: "ON",
|
|
|
offText: "OFF",
|
|
|
labelText: " ",
|
|
|
- classes: {
|
|
|
- base: "bootstrap-switch",
|
|
|
- container: "container",
|
|
|
- wrapper: "wrapper",
|
|
|
- handleOn: "handle-on",
|
|
|
- handleOff: "handle-off",
|
|
|
- label: "label",
|
|
|
- modifiers: {
|
|
|
- on: "on",
|
|
|
- off: "off",
|
|
|
- focused: "focused",
|
|
|
- animate: "animate",
|
|
|
- disabled: "disabled",
|
|
|
- readonly: "readonly"
|
|
|
- }
|
|
|
- },
|
|
|
+ baseClass: "bootstrap-switch",
|
|
|
+ wrapperClass: "wrapper",
|
|
|
+ containerClass: "container",
|
|
|
+ handleOnClass: "handle-on",
|
|
|
+ handleOffClass: "handle-off",
|
|
|
+ labelClass: "label",
|
|
|
+ onModifierClass: "on",
|
|
|
+ offModifierClass: "off",
|
|
|
+ focusedModifierClass: "focused",
|
|
|
+ animateModifierClass: "animate",
|
|
|
+ disabledModifierClass: "disabled",
|
|
|
+ readonlyModifierClass: "readonly",
|
|
|
onInit: function() {},
|
|
|
onSwitchChange: function() {}
|
|
|
};
|