bootstrap-switch.min.js 7.4 KB

12345678910111213141516171819202122
  1. /* ========================================================================
  2. * bootstrap-switch - v2.0.1
  3. * http://www.bootstrap-switch.org
  4. * ========================================================================
  5. * Copyright 2012-2013 Mattia Larentis
  6. *
  7. * ========================================================================
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ========================================================================
  20. */
  21. (function(){var a=[].slice;!function(b,c){"use strict";var d;return d=function(){function a(a){var c=this;this.$element=b(a),this.$switchLeft=b("<span>",{"class":function(){var a,b;return a="switch-left",b=c.$element.data("on-color"),null!=b&&(a+=" switch-"+b),a},html:function(){var a,b;return a="ON",b=c.$element.data("on-text"),null!=b&&(a=b),a}}),this.$switchRight=b("<span>",{"class":function(){var a,b;return a="switch-right",b=c.$element.data("off-color"),null!=b&&(a+=" switch-"+b),a},html:function(){var a,b;return a="OFF",b=c.$element.data("off-text"),null!=b&&(a=b),a}}),this.$label=b("<label>",{"for":this.$element.attr("id"),html:function(){var a,b;return a="&nbsp;",b=c.$element.data("label-text"),null!=b&&(a=b),a}}),this.$wrapper=b("<div>",{"class":function(){var a,b,d,e,f;if(a=["has-switch"],c.$element.attr("class"))for(f=["mini","small","large"],d=0,e=f.length;e>d;d++)b=f[d],c.$element.hasClass("switch-"+b)&&a.push("switch-"+b);return a.push(c.$element.is(":checked")?"switch-on":"switch-off"),c.$element.data("animate")!==!1&&a.push("switch-animate"),(c.$element.is(":disabled")||c.$element.is("[readonly]"))&&a.push("disabled"),a.join(" ")},tabindex:0}),this.$div=this.$element.wrap(b("<div>")).parent(),this.$wrapper=this.$div.wrap(this.$wrapper).parent(),this.$element.before(this.$switchLeft).before(this.$label).before(this.$switchRight),this._elementHandlers(),this._wrapperHandlers(),this._switchesHandlers(),this._labelHandlers(),this._form()}return a.prototype._constructor=a,a.prototype.state=function(a,b){return"undefined"==typeof a?this.$element.is(":checked"):(this.$element.prop("checked",!!a).trigger("change",b),this.$element)},a.prototype.toggleState=function(a){return this.$element.prop("checked",!this.$element.is(":checked")).trigger("change",a),this.$element},a.prototype.disabled=function(a){return"undefined"==typeof a?this.$element.is(":disabled"):(a=!!a,this.$wrapper[a?"addClass":"removeClass"]("disabled"),this.$element.prop("disabled",a),this.$element)},a.prototype.toggleDisabled=function(){return this.$element.prop("disabled",!this.$element.is(":disabled")),this.$wrapper.toggleClass("disabled"),this.$element},a.prototype.readOnly=function(a){return"undefined"==typeof a?this.$element.is("[readonly]"):(readonly?(this.$wrapper.addClass("disabled"),this.$element.prop("readonly",!0)):(this.$wrapper.removeClass("disabled"),this.$element.prop("readonly",!1)),this.$element)},a.prototype.toggleReadOnly=function(){return this.$element.prop("readonly",!this.$element.is("[readonly]")).parents(".has-switch").toggleClass("disabled"),this.$element},a.prototype.labelText=function(a){return this.$element.siblings("label").html(a||"&nbsp"),this.$element},a.prototype.onText=function(a){return"undefined"==typeof a?this.$switchLeft.html():(this.$switchLeft.html(a),this.$element)},a.prototype.offText=function(a){return"undefined"==typeof a?this.$switchRight.html():(this.$switchRight.html(a),this.$element)},a.prototype.onColor=function(a){var b;return b=this.$element.data("on-color"),"undefined"==typeof a?b:(null!=b&&this.$switchLeft.removeClass("switch-"+b),this.$switchLeft.addClass("switch-"+a),this.$element.data("on-color",a),this.$element)},a.prototype.offColor=function(a){var b;return b=this.$element.data("off-color"),"undefined"==typeof a?b:(null!=b&&this.$switchRight.removeClass("switch-"+b),this.$switchRight.addClass("switch-"+a),this.$element.data("off-color",a),this.$element)},a.prototype.animate=function(a){return"undefined"==typeof a?this.$element.data("animate"):(a=!!a,this.$wrapper[a?"addClass":"removeClass"]("switch-animate"),this.$element.data("animate",a),this.$element)},a.prototype.size=function(a){var b,c,d,e;if("undefined"==typeof a)return this.$wrapper.hasClass("switch-"+a);for(e=["mini","small","large"],c=0,d=e.length;d>c;c++)b=e[c],this.$wrapper[b!==a?"removeClass":"addClass"]("switch-"+b);return this.$element},a.prototype.destroy=function(){var a;return a=this.$element.closest("form"),this.$div.children().not(this.$element).remove(),this.$element.unwrap().unwrap().off("change"),a.length&&a.off("reset").removeData("bootstrap-switch"),this.$element},a.prototype._elementHandlers=function(){var a=this;return this.$element.on("change",function(b,c){var d,e;return b.preventDefault(),d=a.$element.is(":checked"),e=a.$wrapper.hasClass("switch-off"),a.$div.css("margin-left",""),e!==d||(d?a.$wrapper.removeClass("switch-off").addClass("switch-on"):a.$wrapper.removeClass("switch-on").addClass("switch-off"),a.$element.data("animate")!==!1&&a.$wrapper.addClass("switch-animate"),"boolean"==typeof c&&c)?void 0:a.$element.trigger("switch-change",{el:a.$element,value:d})})},a.prototype._wrapperHandlers=function(){var a=this;return this.$wrapper.on("keydown",function(b){if(b.which&&!a.$element.is(":disabled")&&!a.$element.is("[readonly]"))switch(b.which){case 32:return b.preventDefault(),a.toggleState();case 37:if(b.preventDefault(),a.$element.is(":checked"))return a.toggleState();break;case 39:if(b.preventDefault(),!a.$element.is(":checked"))return a.toggleState()}})},a.prototype._switchesHandlers=function(){var a=this;return this.$switchLeft.on("click",function(){return a.toggleState()}),this.$switchRight.on("click",function(){return a.toggleState()})},a.prototype._labelHandlers=function(){var a=this;return this.$label.on("click",function(b){return b.preventDefault(),b.stopImmediatePropagation(),a.$wrapper.removeClass("switch-animate"),a.moving?void 0:(a.$label.on("mousemove",function(b){var c,d,e,f;return e=(b.pageX||b.originalEvent.targetTouches[0].pageX)-a.$wrapper.offset().left,d=e/a.$wrapper.width()*100,c=25,f=75,a.moving=!0,c>d?d=c:d>f&&(d=f),a.$div.css("margin-left",""+(d-f)+"%")}),a.$label.on("mouseup mouseleave",function(b){return b.preventDefault(),b.stopImmediatePropagation(),a.$element.prop("checked",parseInt(a.$div.css("margin-left"),10)>-25).trigger("change"),a.$label.off("mousemove")}),a.$label.trigger("mousemove"))})},a.prototype._form=function(){var a;return a=this.$element.closest("form"),a.data("bootstrap-switch")?void 0:a.data("bootstrap-switch",!0).on("reset",function(){return c.setTimeout(function(){return a.find(".has-switch").each(function(){var a;return a=b(this).find("input"),a.prop("checked",a.is(":checked")).trigger("change")})},1)})},a}(),b.fn.extend({bootstrapSwitch:function(){var c,e,f;return e=arguments[0],c=2<=arguments.length?a.call(arguments,1):[],f=this,this.each(function(){var a,g;return a=b(this),g=a.data("bootstrap-switch"),g||a.data("bootstrap-switch",g=new d(this,e)),"string"==typeof e?f=g[e].apply(g,c):void 0}),f}})}(window.jQuery,window)}).call(this);