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