bootstrap-switch.js 21 KB

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