bootstrap-switch.js 20 KB

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