github.com/apremalal/vamps-core@v1.0.1-0.20161221121535-d430b56ec174/server/webapps/app/base/plugins/bootstrap-switch/js/bootstrap-switch.js (about)

     1  /* ========================================================================
     2   * bootstrap-switch - v3.0.2
     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  
    22  (function() {
    23    var __slice = [].slice;
    24  
    25    (function($, window) {
    26      "use strict";
    27      var BootstrapSwitch;
    28      BootstrapSwitch = (function() {
    29        function BootstrapSwitch(element, options) {
    30          if (options == null) {
    31            options = {};
    32          }
    33          this.$element = $(element);
    34          this.options = $.extend({}, $.fn.bootstrapSwitch.defaults, {
    35            state: this.$element.is(":checked"),
    36            size: this.$element.data("size"),
    37            animate: this.$element.data("animate"),
    38            disabled: this.$element.is(":disabled"),
    39            readonly: this.$element.is("[readonly]"),
    40            indeterminate: this.$element.data("indeterminate"),
    41            onColor: this.$element.data("on-color"),
    42            offColor: this.$element.data("off-color"),
    43            onText: this.$element.data("on-text"),
    44            offText: this.$element.data("off-text"),
    45            labelText: this.$element.data("label-text"),
    46            baseClass: this.$element.data("base-class"),
    47            wrapperClass: this.$element.data("wrapper-class"),
    48            radioAllOff: this.$element.data("radio-all-off")
    49          }, options);
    50          this.$wrapper = $("<div>", {
    51            "class": (function(_this) {
    52              return function() {
    53                var classes;
    54                classes = ["" + _this.options.baseClass].concat(_this._getClasses(_this.options.wrapperClass));
    55                classes.push(_this.options.state ? "" + _this.options.baseClass + "-on" : "" + _this.options.baseClass + "-off");
    56                if (_this.options.size != null) {
    57                  classes.push("" + _this.options.baseClass + "-" + _this.options.size);
    58                }
    59                if (_this.options.animate) {
    60                  classes.push("" + _this.options.baseClass + "-animate");
    61                }
    62                if (_this.options.disabled) {
    63                  classes.push("" + _this.options.baseClass + "-disabled");
    64                }
    65                if (_this.options.readonly) {
    66                  classes.push("" + _this.options.baseClass + "-readonly");
    67                }
    68                if (_this.options.indeterminate) {
    69                  classes.push("" + _this.options.baseClass + "-indeterminate");
    70                }
    71                if (_this.$element.attr("id")) {
    72                  classes.push("" + _this.options.baseClass + "-id-" + (_this.$element.attr("id")));
    73                }
    74                return classes.join(" ");
    75              };
    76            })(this)()
    77          });
    78          this.$container = $("<div>", {
    79            "class": "" + this.options.baseClass + "-container"
    80          });
    81          this.$on = $("<span>", {
    82            html: this.options.onText,
    83            "class": "" + this.options.baseClass + "-handle-on " + this.options.baseClass + "-" + this.options.onColor
    84          });
    85          this.$off = $("<span>", {
    86            html: this.options.offText,
    87            "class": "" + this.options.baseClass + "-handle-off " + this.options.baseClass + "-" + this.options.offColor
    88          });
    89          this.$label = $("<label>", {
    90            html: this.options.labelText,
    91            "class": "" + this.options.baseClass + "-label"
    92          });
    93          if (this.options.indeterminate) {
    94            this.$element.prop("indeterminate", true);
    95          }
    96          this.$element.on("init.bootstrapSwitch", (function(_this) {
    97            return function() {
    98              return _this.options.onInit.apply(element, arguments);
    99            };
   100          })(this));
   101          this.$element.on("switchChange.bootstrapSwitch", (function(_this) {
   102            return function() {
   103              return _this.options.onSwitchChange.apply(element, arguments);
   104            };
   105          })(this));
   106          this.$container = this.$element.wrap(this.$container).parent();
   107          this.$wrapper = this.$container.wrap(this.$wrapper).parent();
   108          this.$element.before(this.$on).before(this.$label).before(this.$off).trigger("init.bootstrapSwitch");
   109          this._elementHandlers();
   110          this._handleHandlers();
   111          this._labelHandlers();
   112          this._formHandler();
   113        }
   114  
   115        BootstrapSwitch.prototype._constructor = BootstrapSwitch;
   116  
   117        BootstrapSwitch.prototype.state = function(value, skip) {
   118          if (typeof value === "undefined") {
   119            return this.options.state;
   120          }
   121          if (this.options.disabled || this.options.readonly || this.options.indeterminate) {
   122            return this.$element;
   123          }
   124          if (this.options.state && !this.options.radioAllOff && this.$element.is(':radio')) {
   125            return this.$element;
   126          }
   127          value = !!value;
   128          this.$element.prop("checked", value).trigger("change.bootstrapSwitch", skip);
   129          return this.$element;
   130        };
   131  
   132        BootstrapSwitch.prototype.toggleState = function(skip) {
   133          if (this.options.disabled || this.options.readonly || this.options.indeterminate) {
   134            return this.$element;
   135          }
   136          return this.$element.prop("checked", !this.options.state).trigger("change.bootstrapSwitch", skip);
   137        };
   138  
   139        BootstrapSwitch.prototype.size = function(value) {
   140          if (typeof value === "undefined") {
   141            return this.options.size;
   142          }
   143          if (this.options.size != null) {
   144            this.$wrapper.removeClass("" + this.options.baseClass + "-" + this.options.size);
   145          }
   146          if (value) {
   147            this.$wrapper.addClass("" + this.options.baseClass + "-" + value);
   148          }
   149          this.options.size = value;
   150          return this.$element;
   151        };
   152  
   153        BootstrapSwitch.prototype.animate = function(value) {
   154          if (typeof value === "undefined") {
   155            return this.options.animate;
   156          }
   157          value = !!value;
   158          this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-animate");
   159          this.options.animate = value;
   160          return this.$element;
   161        };
   162  
   163        BootstrapSwitch.prototype.disabled = function(value) {
   164          if (typeof value === "undefined") {
   165            return this.options.disabled;
   166          }
   167          value = !!value;
   168          this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-disabled");
   169          this.$element.prop("disabled", value);
   170          this.options.disabled = value;
   171          return this.$element;
   172        };
   173  
   174        BootstrapSwitch.prototype.toggleDisabled = function() {
   175          this.$element.prop("disabled", !this.options.disabled);
   176          this.$wrapper.toggleClass("" + this.options.baseClass + "-disabled");
   177          this.options.disabled = !this.options.disabled;
   178          return this.$element;
   179        };
   180  
   181        BootstrapSwitch.prototype.readonly = function(value) {
   182          if (typeof value === "undefined") {
   183            return this.options.readonly;
   184          }
   185          value = !!value;
   186          this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-readonly");
   187          this.$element.prop("readonly", value);
   188          this.options.readonly = value;
   189          return this.$element;
   190        };
   191  
   192        BootstrapSwitch.prototype.toggleReadonly = function() {
   193          this.$element.prop("readonly", !this.options.readonly);
   194          this.$wrapper.toggleClass("" + this.options.baseClass + "-readonly");
   195          this.options.readonly = !this.options.readonly;
   196          return this.$element;
   197        };
   198  
   199        BootstrapSwitch.prototype.indeterminate = function(value) {
   200          if (typeof value === "undefined") {
   201            return this.options.indeterminate;
   202          }
   203          value = !!value;
   204          this.$wrapper[value ? "addClass" : "removeClass"]("" + this.options.baseClass + "-indeterminate");
   205          this.$element.prop("indeterminate", value);
   206          this.options.indeterminate = value;
   207          return this.$element;
   208        };
   209  
   210        BootstrapSwitch.prototype.toggleIndeterminate = function() {
   211          this.$element.prop("indeterminate", !this.options.indeterminate);
   212          this.$wrapper.toggleClass("" + this.options.baseClass + "-indeterminate");
   213          this.options.indeterminate = !this.options.indeterminate;
   214          return this.$element;
   215        };
   216  
   217        BootstrapSwitch.prototype.onColor = function(value) {
   218          var color;
   219          color = this.options.onColor;
   220          if (typeof value === "undefined") {
   221            return color;
   222          }
   223          if (color != null) {
   224            this.$on.removeClass("" + this.options.baseClass + "-" + color);
   225          }
   226          this.$on.addClass("" + this.options.baseClass + "-" + value);
   227          this.options.onColor = value;
   228          return this.$element;
   229        };
   230  
   231        BootstrapSwitch.prototype.offColor = function(value) {
   232          var color;
   233          color = this.options.offColor;
   234          if (typeof value === "undefined") {
   235            return color;
   236          }
   237          if (color != null) {
   238            this.$off.removeClass("" + this.options.baseClass + "-" + color);
   239          }
   240          this.$off.addClass("" + this.options.baseClass + "-" + value);
   241          this.options.offColor = value;
   242          return this.$element;
   243        };
   244  
   245        BootstrapSwitch.prototype.onText = function(value) {
   246          if (typeof value === "undefined") {
   247            return this.options.onText;
   248          }
   249          this.$on.html(value);
   250          this.options.onText = value;
   251          return this.$element;
   252        };
   253  
   254        BootstrapSwitch.prototype.offText = function(value) {
   255          if (typeof value === "undefined") {
   256            return this.options.offText;
   257          }
   258          this.$off.html(value);
   259          this.options.offText = value;
   260          return this.$element;
   261        };
   262  
   263        BootstrapSwitch.prototype.labelText = function(value) {
   264          if (typeof value === "undefined") {
   265            return this.options.labelText;
   266          }
   267          this.$label.html(value);
   268          this.options.labelText = value;
   269          return this.$element;
   270        };
   271  
   272        BootstrapSwitch.prototype.baseClass = function(value) {
   273          return this.options.baseClass;
   274        };
   275  
   276        BootstrapSwitch.prototype.wrapperClass = function(value) {
   277          if (typeof value === "undefined") {
   278            return this.options.wrapperClass;
   279          }
   280          if (!value) {
   281            value = $.fn.bootstrapSwitch.defaults.wrapperClass;
   282          }
   283          this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(" "));
   284          this.$wrapper.addClass(this._getClasses(value).join(" "));
   285          this.options.wrapperClass = value;
   286          return this.$element;
   287        };
   288  
   289        BootstrapSwitch.prototype.radioAllOff = function(value) {
   290          if (typeof value === "undefined") {
   291            return this.options.radioAllOff;
   292          }
   293          this.options.radioAllOff = value;
   294          return this.$element;
   295        };
   296  
   297        BootstrapSwitch.prototype.onInit = function(value) {
   298          if (typeof value === "undefined") {
   299            return this.options.onInit;
   300          }
   301          if (!value) {
   302            value = $.fn.bootstrapSwitch.defaults.onInit;
   303          }
   304          this.options.onInit = value;
   305          return this.$element;
   306        };
   307  
   308        BootstrapSwitch.prototype.onSwitchChange = function(value) {
   309          if (typeof value === "undefined") {
   310            return this.options.onSwitchChange;
   311          }
   312          if (!value) {
   313            value = $.fn.bootstrapSwitch.defaults.onSwitchChange;
   314          }
   315          this.options.onSwitchChange = value;
   316          return this.$element;
   317        };
   318  
   319        BootstrapSwitch.prototype.destroy = function() {
   320          var $form;
   321          $form = this.$element.closest("form");
   322          if ($form.length) {
   323            $form.off("reset.bootstrapSwitch").removeData("bootstrap-switch");
   324          }
   325          this.$container.children().not(this.$element).remove();
   326          this.$element.unwrap().unwrap().off(".bootstrapSwitch").removeData("bootstrap-switch");
   327          return this.$element;
   328        };
   329  
   330        BootstrapSwitch.prototype._elementHandlers = function() {
   331          return this.$element.on({
   332            "change.bootstrapSwitch": (function(_this) {
   333              return function(e, skip) {
   334                var checked;
   335                e.preventDefault();
   336                e.stopImmediatePropagation();
   337                checked = _this.$element.is(":checked");
   338                if (checked === _this.options.state) {
   339                  return;
   340                }
   341                _this.options.state = checked;
   342                _this.$wrapper.removeClass(checked ? "" + _this.options.baseClass + "-off" : "" + _this.options.baseClass + "-on").addClass(checked ? "" + _this.options.baseClass + "-on" : "" + _this.options.baseClass + "-off");
   343                if (!skip) {
   344                  if (_this.$element.is(":radio")) {
   345                    $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop("checked", false).trigger("change.bootstrapSwitch", true);
   346                  }
   347                  return _this.$element.trigger("switchChange.bootstrapSwitch", [checked]);
   348                }
   349              };
   350            })(this),
   351            "focus.bootstrapSwitch": (function(_this) {
   352              return function(e) {
   353                e.preventDefault();
   354                return _this.$wrapper.addClass("" + _this.options.baseClass + "-focused");
   355              };
   356            })(this),
   357            "blur.bootstrapSwitch": (function(_this) {
   358              return function(e) {
   359                e.preventDefault();
   360                return _this.$wrapper.removeClass("" + _this.options.baseClass + "-focused");
   361              };
   362            })(this),
   363            "keydown.bootstrapSwitch": (function(_this) {
   364              return function(e) {
   365                if (!e.which || _this.options.disabled || _this.options.readonly || _this.options.indeterminate) {
   366                  return;
   367                }
   368                switch (e.which) {
   369                  case 37:
   370                    e.preventDefault();
   371                    e.stopImmediatePropagation();
   372                    return _this.state(false);
   373                  case 39:
   374                    e.preventDefault();
   375                    e.stopImmediatePropagation();
   376                    return _this.state(true);
   377                }
   378              };
   379            })(this)
   380          });
   381        };
   382  
   383        BootstrapSwitch.prototype._handleHandlers = function() {
   384          this.$on.on("click.bootstrapSwitch", (function(_this) {
   385            return function(e) {
   386              _this.state(false);
   387              return _this.$element.trigger("focus.bootstrapSwitch");
   388            };
   389          })(this));
   390          return this.$off.on("click.bootstrapSwitch", (function(_this) {
   391            return function(e) {
   392              _this.state(true);
   393              return _this.$element.trigger("focus.bootstrapSwitch");
   394            };
   395          })(this));
   396        };
   397  
   398        BootstrapSwitch.prototype._labelHandlers = function() {
   399          return this.$label.on({
   400            "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (function(_this) {
   401              return function(e) {
   402                var left, pageX, percent, right;
   403                if (!_this.isLabelDragging) {
   404                  return;
   405                }
   406                e.preventDefault();
   407                _this.isLabelDragged = true;
   408                pageX = e.pageX || e.originalEvent.touches[0].pageX;
   409                percent = ((pageX - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
   410                left = 25;
   411                right = 75;
   412                if (_this.options.animate) {
   413                  _this.$wrapper.removeClass("" + _this.options.baseClass + "-animate");
   414                }
   415                if (percent < left) {
   416                  percent = left;
   417                } else if (percent > right) {
   418                  percent = right;
   419                }
   420                _this.$container.css("margin-left", "" + (percent - right) + "%");
   421                return _this.$element.trigger("focus.bootstrapSwitch");
   422              };
   423            })(this),
   424            "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (function(_this) {
   425              return function(e) {
   426                if (_this.isLabelDragging || _this.options.disabled || _this.options.readonly || _this.options.indeterminate) {
   427                  return;
   428                }
   429                e.preventDefault();
   430                _this.isLabelDragging = true;
   431                return _this.$element.trigger("focus.bootstrapSwitch");
   432              };
   433            })(this),
   434            "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (function(_this) {
   435              return function(e) {
   436                if (!_this.isLabelDragging) {
   437                  return;
   438                }
   439                e.preventDefault();
   440                if (_this.isLabelDragged) {
   441                  _this.isLabelDragged = false;
   442                  _this.state(parseInt(_this.$container.css("margin-left"), 10) > -(_this.$container.width() / 6));
   443                  if (_this.options.animate) {
   444                    _this.$wrapper.addClass("" + _this.options.baseClass + "-animate");
   445                  }
   446                  _this.$container.css("margin-left", "");
   447                } else {
   448                  _this.state(!_this.options.state);
   449                }
   450                return _this.isLabelDragging = false;
   451              };
   452            })(this),
   453            "mouseleave.bootstrapSwitch": (function(_this) {
   454              return function(e) {
   455                return _this.$label.trigger("mouseup.bootstrapSwitch");
   456              };
   457            })(this)
   458          });
   459        };
   460  
   461        BootstrapSwitch.prototype._formHandler = function() {
   462          var $form;
   463          $form = this.$element.closest("form");
   464          if ($form.data("bootstrap-switch")) {
   465            return;
   466          }
   467          return $form.on("reset.bootstrapSwitch", function() {
   468            return window.setTimeout(function() {
   469              return $form.find("input").filter(function() {
   470                return $(this).data("bootstrap-switch");
   471              }).each(function() {
   472                return $(this).bootstrapSwitch("state", this.checked);
   473              });
   474            }, 1);
   475          }).data("bootstrap-switch", true);
   476        };
   477  
   478        BootstrapSwitch.prototype._getClasses = function(classes) {
   479          var c, cls, _i, _len;
   480          if (!$.isArray(classes)) {
   481            return ["" + this.options.baseClass + "-" + classes];
   482          }
   483          cls = [];
   484          for (_i = 0, _len = classes.length; _i < _len; _i++) {
   485            c = classes[_i];
   486            cls.push("" + this.options.baseClass + "-" + c);
   487          }
   488          return cls;
   489        };
   490  
   491        return BootstrapSwitch;
   492  
   493      })();
   494      $.fn.bootstrapSwitch = function() {
   495        var args, option, ret;
   496        option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
   497        ret = this;
   498        this.each(function() {
   499          var $this, data;
   500          $this = $(this);
   501          data = $this.data("bootstrap-switch");
   502          if (!data) {
   503            $this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
   504          }
   505          if (typeof option === "string") {
   506            return ret = data[option].apply(data, args);
   507          }
   508        });
   509        return ret;
   510      };
   511      $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
   512      return $.fn.bootstrapSwitch.defaults = {
   513        state: true,
   514        size: null,
   515        animate: true,
   516        disabled: false,
   517        readonly: false,
   518        indeterminate: false,
   519        onColor: "primary",
   520        offColor: "default",
   521        onText: "ON",
   522        offText: "OFF",
   523        labelText: "&nbsp;",
   524        baseClass: "bootstrap-switch",
   525        wrapperClass: "wrapper",
   526        radioAllOff: false,
   527        onInit: function() {},
   528        onSwitchChange: function() {}
   529      };
   530    })(window.jQuery, window);
   531  
   532  }).call(this);