PageSourceSearch

https://brejinho.pe.gov.br/site/wp-content/themes/idata-gov/js/now-ui-kit/plugins/bootstrap-switch.js?ver=1.24

js brejinho.pe.gov.br collected 2026-10-03 01:32:12 UTC 26,857 bytes, 744 lines download raw bytes

1/* ========================================================================
2 * bootstrap-switch - v3.3.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          inverse: this.$element.data("inverse"),
42          radioAllOff: this.$element.data("radio-all-off"),
43          onColor: this.$element.data("on-color"),
44          offColor: this.$element.data("off-color"),
45          onText: this.$element.data("on-text"),
46          offText: this.$element.data("off-text"),
47          labelText: this.$element.data("label-text"),
48          handleWidth: this.$element.data("handle-width"),
49          labelWidth: this.$element.data("label-width"),
50          baseClass: this.$element.data("base-class"),
51          wrapperClass: this.$element.data("wrapper-class")
52        }, options);
53        this.prevOptions = {};
54        this.$wrapper = $("<div>", {
55          "class": (function(_this) {
56            return function() {
57              var classes;
58              classes = ["" + _this.options.baseClass].concat(_this._getClasses(_this.options.wrapperClass));
59              classes.push(_this.options.state ? _this.options.baseClass + "-on" : _this.options.baseClass + "-off");
60              if (_this.options.size != null) {
61                classes.push(_this.options.baseClass + "-" + _this.options.size);
62              }
63              if (_this.options.disabled) {
64                classes.push(_this.options.baseClass + "-disabled");
65              }
66              if (_this.options.readonly) {
67                classes.push(_this.options.baseClass + "-readonly");
68              }
69              if (_this.options.indeterminate) {
70                classes.push(_this.options.baseClass + "-indeterminate");
71              }
72              if (_this.options.inverse) {
73                classes.push(_this.options.baseClass + "-inverse");
74              }
75              if (_this.$element.attr("id")) {
76                classes.push(_this.options.baseClass + "-id-" + (_this.$element.attr("id")));
77              }
78              return classes.join(" ");
79            };
80          })(this)()
81        });
82        this.$container = $("<div>", {
83          "class": this.options.baseClass + "-container"
84        });
85        this.$on = $("<span>", {
86          html: this.options.onText,
87          "class": this.options.baseClass + "-handle-on " + this.options.baseClass + "-" + this.options.onColor
88        });
89        this.$off = $("<span>", {
90          html: this.options.offText,
91          "class": this.options.baseClass + "-handle-off " + this.options.baseClass + "-" + this.options.offColor
92        });
93        this.$label = $("<span>", {
94          html: this.options.labelText,
95          "class": this.options.baseClass + "-label"
96        });
97        this.$element.on("init.bootstrapSwitch", (function(_this) {
98          return function() {
99            return _this.options.onInit.apply(element, arguments);
100          };
101        })(this));
102        this.$element.on("switchChange.bootstrapSwitch", (function(_this) {
103          return function(e) {
104            if (false === _this.options.onSwitchChange.apply(element, arguments)) {
105              if (_this.$element.is(":radio")) {
106                return $("[name='" + (_this.$element.attr('name')) + "']").trigger("previousState.bootstrapSwitch", true);
107              } else {
108                return _this.$element.trigger("previousState.bootstrapSwitch", true);
109              }
110            }
111          };
112        })(this));
113        this.$container = this.$element.wrap(this.$container).parent();
114        this.$wrapper = this.$container.wrap(this.$wrapper).parent();
115        this.$element.before(this.options.inverse ? this.$off : this.$on).before(this.$label).before(this.options.inverse ? this.$on : this.$off);
116        if (this.options.indeterminate) {
117          this.$element.prop("indeterminate", true);
118        }
119        this._init();
120        this._elementHandlers();
121        this._handleHandlers();
122        this._labelHandlers();
123        this._formHandler();
124        this._externalLabelHandler();
125        this.$element.trigger("init.bootstrapSwitch", this.options.state);
126      }
127
128      BootstrapSwitch.prototype._constructor = BootstrapSwitch;
129
130      BootstrapSwitch.prototype.setPrevOptions = function() {
131        return this.prevOptions = $.extend(true, {}, this.options);
132      };
133
134      BootstrapSwitch.prototype.state = function(value, skip) {
135        if (typeof value === "undefined") {
136          return this.options.state;
137        }
138        if (this.options.disabled || this.options.readonly) {
139          return this.$element;
140        }
141        if (this.options.state && !this.options.radioAllOff && this.$element.is(":radio")) {
142          return this.$element;
143        }
144        if (this.$element.is(":radio")) {
145          $("[name='" + (this.$element.attr('name')) + "']").trigger("setPreviousOptions.bootstrapSwitch");
146        } else {
147          this.$element.trigger("setPreviousOptions.bootstrapSwitch");
148        }
149        if (this.options.indeterminate) {
150          this.indeterminate(false);
151        }
152        value = !!value;
153        this.$element.prop("checked", value).trigger("change.bootstrapSwitch", skip);
154        return this.$element;
155      };
156
157      BootstrapSwitch.prototype.toggleState = function(skip) {
158        if (this.options.disabled || this.options.readonly) {
159          return this.$element;
160        }
161        if (this.options.indeterminate) {
162          this.indeterminate(false);
163          return this.state(true);
164        } else {
165          return this.$element.prop("checked", !this.options.state).trigger("change.bootstrapSwitch", skip);
166        }
167      };
168
169      BootstrapSwitch.prototype.size = function(value) {
170        if (typeof value === "undefined") {
171          return this.options.size;
172        }
173        if (this.options.size != null) {
174          this.$wrapper.removeClass(this.options.baseClass + "-" + this.options.size);
175        }
176        if (value) {
177          this.$wrapper.addClass(this.options.baseClass + "-" + value);
178        }
179        this._width();
180        this._containerPosition();
181        this.options.size = value;
182        return this.$element;
183      };
184
185      BootstrapSwitch.prototype.animate = function(value) {
186        if (typeof value === "undefined") {
187          return this.options.animate;
188        }
189        value = !!value;
190        if (value === this.options.animate) {
191          return this.$element;
192        }
193        return this.toggleAnimate();
194      };
195
196      BootstrapSwitch.prototype.toggleAnimate = function() {
197        this.options.animate = !this.options.animate;
198        this.$wrapper.toggleClass(this.options.baseClass + "-animate");
199        return this.$element;
200      };
201
202      BootstrapSwitch.prototype.disabled = function(value) {
203        if (typeof value === "undefined") {
204          return this.options.disabled;
205        }
206        value = !!value;
207        if (value === this.options.disabled) {
208          return this.$element;
209        }
210        return this.toggleDisabled();
211      };
212
213      BootstrapSwitch.prototype.toggleDisabled = function() {
214        this.options.disabled = !this.options.disabled;
215        this.$element.prop("disabled", this.options.disabled);
216        this.$wrapper.toggleClass(this.options.baseClass + "-disabled");
217        return this.$element;
218      };
219
220      BootstrapSwitch.prototype.readonly = function(value) {
221        if (typeof value === "undefined") {
222          return this.options.readonly;
223        }
224        value = !!value;
225        if (value === this.options.readonly) {
226          return this.$element;
227        }
228        return this.toggleReadonly();
229      };
230
231      BootstrapSwitch.prototype.toggleReadonly = function() {
232        this.options.readonly = !this.options.readonly;
233        this.$element.prop("readonly", this.options.readonly);
234        this.$wrapper.toggleClass(this.options.baseClass + "-readonly");
235        return this.$element;
236      };
237
238      BootstrapSwitch.prototype.indeterminate = function(value) {
239        if (typeof value === "undefined") {
240          return this.options.indeterminate;
241        }
242        value = !!value;
243        if (value === this.options.indeterminate) {
244          return this.$element;
245        }
246        return this.toggleIndeterminate();
247      };
248
249      BootstrapSwitch.prototype.toggleIndeterminate = function() {
250        this.options.indeterminate = !this.options.indeterminate;
251        this.$element.prop("indeterminate", this.options.indeterminate);
252        this.$wrapper.toggleClass(this.options.baseClass + "-indeterminate");
253        this._containerPosition();
254        return this.$element;
255      };
256
257      BootstrapSwitch.prototype.inverse = function(value) {
258        if (typeof value === "undefined") {
259          return this.options.inverse;
260        }
261        value = !!value;
262        if (value === this.options.inverse) {
263          return this.$element;
264        }
265        return this.toggleInverse();
266      };
267
268      BootstrapSwitch.prototype.toggleInverse = function() {
269        var $off, $on;
270        this.$wrapper.toggleClass(this.options.baseClass + "-inverse");
271        $on = this.$on.clone(true);
272        $off = this.$off.clone(true);
273        this.$on.replaceWith($off);
274        this.$off.replaceWith($on);
275        this.$on = $off;
276        this.$off = $on;
277        this.options.inverse = !this.options.inverse;
278        return this.$element;
279      };
280
281      BootstrapSwitch.prototype.onColor = function(value) {
282        var color;
283        color = this.options.onColor;
284        if (typeof value === "undefined") {
285          return color;
286        }
287        if (color != null) {
288          this.$on.removeClass(this.options.baseClass + "-" + color);
289        }
290        this.$on.addClass(this.options.baseClass + "-" + value);
291        this.options.onColor = value;
292        return this.$element;
293      };
294
295      BootstrapSwitch.prototype.offColor = function(value) {
296        var color;
297        color = this.options.offColor;
298        if (typeof value === "undefined") {
299          return color;
300        }
301        if (color != null) {
302          this.$off.removeClass(this.options.baseClass + "-" + color);
303        }
304        this.$off.addClass(this.options.baseClass + "-" + value);
305        this.options.offColor = value;
306        return this.$element;
307      };
308
309      BootstrapSwitch.prototype.onText = function(value) {
310        if (typeof value === "undefined") {
311          return this.options.onText;
312        }
313        this.$on.html(value);
314        this._width();
315        this._containerPosition();
316        this.options.onText = value;
317        return this.$element;
318      };
319
320      BootstrapSwitch.prototype.offText = function(value) {
321        if (typeof value === "undefined") {
322          return this.options.offText;
323        }
324        this.$off.html(value);
325        this._width();
326        this._containerPosition();
327        this.options.offText = value;
328        return this.$element;
329      };
330
331      BootstrapSwitch.prototype.labelText = function(value) {
332        if (typeof value === "undefined") {
333          return this.options.labelText;
334        }
335        this.$label.html(value);
336        this._width();
337        this.options.labelText = value;
338        return this.$element;
339      };
340
341      BootstrapSwitch.prototype.handleWidth = function(value) {
342        if (typeof value === "undefined") {
343          return this.options.handleWidth;
344        }
345        this.options.handleWidth = value;
346        this._width();
347        this._containerPosition();
348        return this.$element;
349      };
350
351      BootstrapSwitch.prototype.labelWidth = function(value) {
352        if (typeof value === "undefined") {
353          return this.options.labelWidth;
354        }
355        this.options.labelWidth = value;
356        this._width();
357        this._containerPosition();
358        return this.$element;
359      };
360
361      BootstrapSwitch.prototype.baseClass = function(value) {
362        return this.options.baseClass;
363      };
364
365      BootstrapSwitch.prototype.wrapperClass = function(value) {
366        if (typeof value === "undefined") {
367          return this.options.wrapperClass;
368        }
369        if (!value) {
370          value = $.fn.bootstrapSwitch.defaults.wrapperClass;
371        }
372        this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(" "));
373        this.$wrapper.addClass(this._getClasses(value).join(" "));
374        this.options.wrapperClass = value;
375        return this.$element;
376      };
377
378      BootstrapSwitch.prototype.radioAllOff = function(value) {
379        if (typeof value === "undefined") {
380          return this.options.radioAllOff;
381        }
382        value = !!value;
383        if (value === this.options.radioAllOff) {
384          return this.$element;
385        }
386        this.options.radioAllOff = value;
387        return this.$element;
388      };
389
390      BootstrapSwitch.prototype.onInit = function(value) {
391        if (typeof value === "undefined") {
392          return this.options.onInit;
393        }
394        if (!value) {
395          value = $.fn.bootstrapSwitch.defaults.onInit;
396        }
397        this.options.onInit = value;
398        return this.$element;
399      };
400
401      BootstrapSwitch.prototype.onSwitchChange = function(value) {
402        if (typeof value === "undefined") {
403          return this.options.onSwitchChange;
404        }
405        if (!value) {
406          value = $.fn.bootstrapSwitch.defaults.onSwitchChange;
407        }
408        this.options.onSwitchChange = value;
409        return this.$element;
410      };
411
412      BootstrapSwitch.prototype.destroy = function() {
413        var $form;
414        $form = this.$element.closest("form");
415        if ($form.length) {
416          $form.off("reset.bootstrapSwitch").removeData("bootstrap-switch");
417        }
418        this.$container.children().not(this.$element).remove();
419        this.$element.unwrap().unwrap().off(".bootstrapSwitch").removeData("bootstrap-switch");
420        return this.$element;
421      };
422
423      BootstrapSwitch.prototype._width = function() {
424        var $handles, handleWidth;
425        $handles = this.$on.add(this.$off);
426        $handles.add(this.$label).css("width", "");
427        handleWidth = this.options.handleWidth === "auto" ? Math.max(this.$on.width(), this.$off.width()) : this.options.handleWidth;
428        $handles.width(handleWidth);
429        this.$label.width((function(_this) {
430          return function(index, width) {
431            if (_this.options.labelWidth !== "auto") {
432              return _this.options.labelWidth;
433            }
434            if (width < handleWidth) {
435              return handleWidth;
436            } else {
437              return width;
438            }
439          };
440        })(this));
441        this._handleWidth = this.$on.outerWidth();
442        this._labelWidth = this.$label.outerWidth();
443        this.$container.width((this._handleWidth * 2) + this._labelWidth);
444        return this.$wrapper.width(this._handleWidth + this._labelWidth);
445      };
446
447      BootstrapSwitch.prototype._containerPosition = function(state, callback) {
448        if (state == null) {
449          state = this.options.state;
450        }
451        this.$container.css("margin-left", (function(_this) {
452          return function() {
453            var values;
454            values = [0, "-" + _this._handleWidth + "px"];
455            if (_this.options.indeterminate) {
456              return "-" + (_this._handleWidth / 2) + "px";
457            }
458            if (state) {
459              if (_this.options.inverse) {
460                return values[1];
461              } else {
462                return values[0];
463              }
464            } else {
465              if (_this.options.inverse) {
466                return values[0];
467              } else {
468                return values[1];
469              }
470            }
471          };
472        })(this));
473        if (!callback) {
474          return;
475        }
476        return setTimeout(function() {
477          return callback();
478        }, 50);
479      };
480
481      BootstrapSwitch.prototype._init = function() {
482        var init, initInterval;
483        init = (function(_this) {
484          return function() {
485            _this.setPrevOptions();
486            _this._width();
487            return _this._containerPosition(null, function() {
488              if (_this.options.animate) {
489                return _this.$wrapper.addClass(_this.options.baseClass + "-animate");
490              }
491            });
492          };
493        })(this);
494        if (this.$wrapper.is(":visible")) {
495          return init();
496        }
497        return initInterval = window.setInterval((function(_this) {
498          return function() {
499            if (_this.$wrapper.is(":visible")) {
500              init();
501              return window.clearInterval(initInterval);
502            }
503          };
504        })(this), 50);
505      };
506
507      BootstrapSwitch.prototype._elementHandlers = function() {
508        return this.$element.on({
509          "setPreviousOptions.bootstrapSwitch": (function(_this) {
510            return function(e) {
511              return _this.setPrevOptions();
512            };
513          })(this),
514          "previousState.bootstrapSwitch": (function(_this) {
515            return function(e) {
516              _this.options = _this.prevOptions;
517              if (_this.options.indeterminate) {
518                _this.$wrapper.addClass(_this.options.baseClass + "-indeterminate");
519              }
520              return _this.$element.prop("checked", _this.options.state).trigger("change.bootstrapSwitch", true);
521            };
522          })(this),
523          "change.bootstrapSwitch": (function(_this) {
524            return function(e, skip) {
525              var state;
526              e.preventDefault();
527              e.stopImmediatePropagation();
528              state = _this.$element.is(":checked");
529              _this._containerPosition(state);
530              if (state === _this.options.state) {
531                return;
532              }
533              _this.options.state = state;
534              _this.$wrapper.toggleClass(_this.options.baseClass + "-off").toggleClass(_this.options.baseClass + "-on");
535              if (!skip) {
536                if (_this.$element.is(":radio")) {
537                  $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop("checked", false).trigger("change.bootstrapSwitch", true);
538                }
539                return _this.$element.trigger("switchChange.bootstrapSwitch", [state]);
540              }
541            };
542          })(this),
543          "focus.bootstrapSwitch": (function(_this) {
544            return function(e) {
545              e.preventDefault();
546              return _this.$wrapper.addClass(_this.options.baseClass + "-focused");
547            };
548          })(this),
549          "blur.bootstrapSwitch": (function(_this) {
550            return function(e) {
551              e.preventDefault();
552              return _this.$wrapper.removeClass(_this.options.baseClass + "-focused");
553            };
554          })(this),
555          "keydown.bootstrapSwitch": (function(_this) {
556            return function(e) {
557              if (!e.which || _this.options.disabled || _this.options.readonly) {
558                return;
559              }
560              switch (e.which) {
561                case 37:
562                  e.preventDefault();
563                  e.stopImmediatePropagation();
564                  return _this.state(false);
565                case 39:
566                  e.preventDefault();
567                  e.stopImmediatePropagation();
568                  return _this.state(true);
569              }
570            };
571          })(this)
572        });
573      };
574
575      BootstrapSwitch.prototype._handleHandlers = function() {
576        this.$on.on("click.bootstrapSwitch", (function(_this) {
577          return function(event) {
578            event.preventDefault();
579            event.stopPropagation();
580            _this.state(false);
581            return _this.$element.trigger("focus.bootstrapSwitch");
582          };
583        })(this));
584        return this.$off.on("click.bootstrapSwitch", (function(_this) {
585          return function(event) {
586            event.preventDefault();
587            event.stopPropagation();
588            _this.state(true);
589            return _this.$element.trigger("focus.bootstrapSwitch");
590          };
591        })(this));
592      };
593
594      BootstrapSwitch.prototype._labelHandlers = function() {
595        return this.$label.on({
596          "click": function(e) {
597            return e.stopPropagation();
598          },
599          "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (function(_this) {
600            return function(e) {
601              if (_this._dragStart || _this.options.disabled || _this.options.readonly) {
602                return;
603              }
604              e.preventDefault();
605              e.stopPropagation();
606              _this._dragStart = (e.pageX || e.originalEvent.touches[0].pageX) - parseInt(_this.$container.css("margin-left"), 10);
607              if (_this.options.animate) {
608                _this.$wrapper.removeClass(_this.options.baseClass + "-animate");
609              }
610              return _this.$element.trigger("focus.bootstrapSwitch");
611            };
612          })(this),
613          "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (function(_this) {
614            return function(e) {
615              var difference;
616              if (_this._dragStart == null) {
617                return;
618              }
619              e.preventDefault();
620              difference = (e.pageX || e.originalEvent.touches[0].pageX) - _this._dragStart;
621              if (difference < -_this._handleWidth || difference > 0) {
622                return;
623              }
624              _this._dragEnd = difference;
625              return _this.$container.css("margin-left", _this._dragEnd + "px");
626            };
627          })(this),
628          "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (function(_this) {
629            return function(e) {
630              var state;
631              if (!_this._dragStart) {
632                return;
633              }
634              e.preventDefault();
635              if (_this.options.animate) {
636                _this.$wrapper.addClass(_this.options.baseClass + "-animate");
637              }
638              if (_this._dragEnd) {
639                state = _this._dragEnd > -(_this._handleWidth / 2);
640                _this._dragEnd = false;
641                _this.state(_this.options.inverse ? !state : state);
642              } else {
643                _this.state(!_this.options.state);
644              }
645              return _this._dragStart = false;
646            };
647          })(this),
648          "mouseleave.bootstrapSwitch": (function(_this) {
649            return function(e) {
650              return _this.$label.trigger("mouseup.bootstrapSwitch");
651            };
652          })(this)
653        });
654      };
655
656      BootstrapSwitch.prototype._externalLabelHandler = function() {
657        var $externalLabel;
658        $externalLabel = this.$element.closest("label");
659        return $externalLabel.on("click", (function(_this) {
660          return function(event) {
661            event.preventDefault();
662            event.stopImmediatePropagation();
663            if (event.target === $externalLabel[0]) {
664              return _this.toggleState();
665            }
666          };
667        })(this));
668      };
669
670      BootstrapSwitch.prototype._formHandler = function() {
671        var $form;
672        $form = this.$element.closest("form");
673        if ($form.data("bootstrap-switch")) {
674          return;
675        }
676        return $form.on("reset.bootstrapSwitch", function() {
677          return window.setTimeout(function() {
678            return $form.find("input").filter(function() {
679              return $(this).data("bootstrap-switch");
680            }).each(function() {
681              return $(this).bootstrapSwitch("state", this.checked);
682            });
683          }, 1);
684        }).data("bootstrap-switch", true);
685      };
686
687      BootstrapSwitch.prototype._getClasses = function(classes) {
688        var c, cls, i, len;
689        if (!$.isArray(classes)) {
690          return [this.options.baseClass + "-" + classes];
691        }
692        cls = [];
693        for (i = 0, len = classes.length; i < len; i++) {
694          c = classes[i];
695          cls.push(this.options.baseClass + "-" + c);
696        }
697        return cls;
698      };
699
700      return BootstrapSwitch;
701
702    })();
703    $.fn.bootstrapSwitch = function() {
704      var args, option, ret;
705      option = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
706      ret = this;
707      this.each(function() {
708        var $this, data;
709        $this = $(this);
710        data = $this.data("bootstrap-switch");
711        if (!data) {
712          $this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
713        }
714        if (typeof option === "string") {
715          return ret = data[option].apply(data, args);
716        }
717      });
718      return ret;
719    };
720    $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
721    return $.fn.bootstrapSwitch.defaults = {
722      state: true,
723      size: null,
724      animate: true,
725      disabled: false,
726      readonly: false,
727      indeterminate: false,
728      inverse: false,
729      radioAllOff: false,
730      onColor: "primary",
731      offColor: "default",
732      onText: "ON",
733      offText: "OFF",
734      labelText: "&nbsp;",
735      handleWidth: "auto",
736      labelWidth: "auto",
737      baseClass: "bootstrap-switch",
738      wrapperClass: "wrapper",
739      onInit: function() {},
740      onSwitchChange: function() {}
741    };
742  })(window.jQuery, window);
743
744}).call(this);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.