bootstrap-switch.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* ========================================================================
  2. * bootstrap-switch - v3.0.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. BootstrapSwitch.prototype.defaults = {
  28. state: true,
  29. size: null,
  30. animate: true,
  31. disabled: false,
  32. readonly: false,
  33. onColor: "primary",
  34. offColor: "default",
  35. onText: "ON",
  36. offText: "OFF",
  37. labelText: " "
  38. };
  39. function BootstrapSwitch(element, options) {
  40. var _this = this;
  41. if (options == null) {
  42. options = {};
  43. }
  44. this.$element = $(element);
  45. this.options = $.extend({}, this.defaults, options, {
  46. state: this.$element.is(":checked"),
  47. size: this.$element.data("size"),
  48. animate: this.$element.data("animate"),
  49. disabled: this.$element.is(":disabled"),
  50. readonly: this.$element.is("[readonly]"),
  51. onColor: this.$element.data("on-color"),
  52. offColor: this.$element.data("off-color"),
  53. onText: this.$element.data("on-text"),
  54. offText: this.$element.data("off-text"),
  55. labelText: this.$element.data("label-text")
  56. });
  57. this.$on = $("<span>", {
  58. "class": "switch-handle-on switch-" + this.options.onColor,
  59. html: this.options.onText
  60. });
  61. this.$off = $("<span>", {
  62. "class": "switch-handle-off switch-" + this.options.offColor,
  63. html: this.options.offText
  64. });
  65. this.$label = $("<label>", {
  66. "for": this.$element.attr("id"),
  67. html: this.options.labelText
  68. });
  69. this.$wrapper = $("<div>", {
  70. "class": function() {
  71. var classes;
  72. classes = ["switch"];
  73. classes.push(_this.options.state ? "switch-on" : "switch-off");
  74. if (_this.options.size != null) {
  75. classes.push("switch-" + _this.options.size);
  76. }
  77. if (_this.options.animate) {
  78. classes.push("switch-animate");
  79. }
  80. if (_this.options.disabled) {
  81. classes.push("switch-disabled");
  82. }
  83. if (_this.options.readonly) {
  84. classes.push("switch-readonly");
  85. }
  86. if (_this.$element.attr("id")) {
  87. classes.push("switch-id-" + (_this.$element.attr("id")));
  88. }
  89. return classes.join(" ");
  90. }
  91. });
  92. this.$div = this.$element.wrap($("<div>")).parent();
  93. this.$wrapper = this.$div.wrap(this.$wrapper).parent();
  94. this.$element.before(this.$on).before(this.$label).before(this.$off);
  95. this._elementHandlers();
  96. this._handleHandlers();
  97. this._labelHandlers();
  98. this._formHandler();
  99. }
  100. BootstrapSwitch.prototype._constructor = BootstrapSwitch;
  101. BootstrapSwitch.prototype.state = function(value, skip) {
  102. if (typeof value === "undefined") {
  103. return this.options.state;
  104. }
  105. if (this.options.disabled || this.options.readonly) {
  106. return this.$element;
  107. }
  108. value = !!value;
  109. this.$element.prop("checked", value).trigger("change", skip);
  110. return this.$element;
  111. };
  112. BootstrapSwitch.prototype.toggleState = function(skip) {
  113. if (this.options.disabled || this.options.readonly) {
  114. return this.$element;
  115. }
  116. return this.$element.prop("checked", !this.options.state).trigger("change", skip);
  117. };
  118. /*
  119. TODO: refactor
  120. toggleRadioState: (uncheck, skip) ->
  121. $element = @$element.not ":checked"
  122. if uncheck
  123. $element.trigger "change", skip
  124. else
  125. $element.prop("checked", not @$element.is ":checked").trigger "change", skip
  126. @$element
  127. */
  128. BootstrapSwitch.prototype.size = function(value) {
  129. if (typeof value === "undefined") {
  130. return this.options.size;
  131. }
  132. if (this.options.size != null) {
  133. this.$wrapper.removeClass("switch-" + this.options.size);
  134. }
  135. this.$wrapper.addClass("switch-" + value);
  136. this.options.size = value;
  137. return this.$element;
  138. };
  139. BootstrapSwitch.prototype.animate = function(value) {
  140. if (typeof value === "undefined") {
  141. return this.options.animate;
  142. }
  143. value = !!value;
  144. this.$wrapper[value ? "addClass" : "removeClass"]("switch-animate");
  145. this.options.animate = value;
  146. return this.$element;
  147. };
  148. BootstrapSwitch.prototype.disabled = function(value) {
  149. if (typeof value === "undefined") {
  150. return this.options.disabled;
  151. }
  152. value = !!value;
  153. this.$wrapper[value ? "addClass" : "removeClass"]("switch-disabled");
  154. this.$element.prop("disabled", value);
  155. this.options.disabled = value;
  156. return this.$element;
  157. };
  158. BootstrapSwitch.prototype.toggleDisabled = function() {
  159. this.$element.prop("disabled", !this.options.disabled);
  160. this.$wrapper.toggleClass("switch-disabled");
  161. this.options.disabled = !this.options.disabled;
  162. return this.$element;
  163. };
  164. BootstrapSwitch.prototype.readonly = function(value) {
  165. if (typeof value === "undefined") {
  166. return this.options.readonly;
  167. }
  168. value = !!value;
  169. this.$wrapper[value ? "addClass" : "removeClass"]("switch-readonly");
  170. this.$element.prop("readonly", value);
  171. this.options.readonly = value;
  172. return this.$element;
  173. };
  174. BootstrapSwitch.prototype.toggleReadonly = function() {
  175. this.$element.prop("readonly", !this.options.readonly);
  176. this.$wrapper.toggleClass("switch-readonly");
  177. this.options.readonly = !this.options.readonly;
  178. return this.$element;
  179. };
  180. BootstrapSwitch.prototype.onColor = function(value) {
  181. var color;
  182. color = this.options.onColor;
  183. if (typeof value === "undefined") {
  184. return color;
  185. }
  186. if (color != null) {
  187. this.$on.removeClass("switch-" + color);
  188. }
  189. this.$on.addClass("switch-" + value);
  190. this.options.onColor = value;
  191. return this.$element;
  192. };
  193. BootstrapSwitch.prototype.offColor = function(value) {
  194. var color;
  195. color = this.options.offColor;
  196. if (typeof value === "undefined") {
  197. return color;
  198. }
  199. if (color != null) {
  200. this.$off.removeClass("switch-" + color);
  201. }
  202. this.$off.addClass("switch-" + value);
  203. this.options.offColor = value;
  204. return this.$element;
  205. };
  206. BootstrapSwitch.prototype.onText = function(value) {
  207. if (typeof value === "undefined") {
  208. return this.options.onText;
  209. }
  210. this.$on.html(value);
  211. this.options.onText = value;
  212. return this.$element;
  213. };
  214. BootstrapSwitch.prototype.offText = function(value) {
  215. if (typeof value === "undefined") {
  216. return this.options.offText;
  217. }
  218. this.$off.html(value);
  219. this.options.offText = value;
  220. return this.$element;
  221. };
  222. BootstrapSwitch.prototype.labelText = function(value) {
  223. if (typeof value === "undefined") {
  224. return this.options.labelText;
  225. }
  226. this.$label.html(value);
  227. this.options.labelText = value;
  228. return this.$element;
  229. };
  230. BootstrapSwitch.prototype.destroy = function() {
  231. var $form;
  232. $form = this.$element.closest("form");
  233. if ($form.length) {
  234. $form.off("reset.bootstrapSwitch").removeData("bootstrap-switch");
  235. }
  236. this.$div.children().not(this.$element).remove();
  237. this.$element.unwrap().unwrap().off(".bootstrapSwitch").removeData("bootstrap-switch");
  238. return this.$element;
  239. };
  240. BootstrapSwitch.prototype._elementHandlers = function() {
  241. var _this = this;
  242. return this.$element.on({
  243. "change.bootstrapSwitch": function(e, skip) {
  244. var $radios, checked;
  245. e.preventDefault();
  246. e.stopPropagation();
  247. e.stopImmediatePropagation();
  248. checked = _this.$element.is(":checked");
  249. if (checked === _this.options.state) {
  250. return;
  251. }
  252. _this.options.state = checked;
  253. _this.$wrapper.removeClass(checked ? "switch-off" : "switch-on").addClass(checked ? "switch-on" : "switch-off");
  254. $radios = _this._radioSiblings();
  255. console.log($radios);
  256. if ($radios) {
  257. $radios.prop("checked", !value).trigger("change", skip);
  258. }
  259. if (!skip) {
  260. return _this.$element.trigger("switchChange", {
  261. el: _this.$element,
  262. value: checked
  263. });
  264. }
  265. },
  266. "focus.bootstrapSwitch": function(e) {
  267. e.preventDefault();
  268. e.stopPropagation();
  269. e.stopImmediatePropagation();
  270. return _this.$wrapper.addClass("switch-focused");
  271. },
  272. "blur.bootstrapSwitch": function(e) {
  273. e.preventDefault();
  274. e.stopPropagation();
  275. e.stopImmediatePropagation();
  276. return _this.$wrapper.removeClass("switch-focused");
  277. },
  278. "keydown.bootstrapSwitch": function(e) {
  279. if (!e.which || _this.options.disabled || _this.options.readonly) {
  280. return;
  281. }
  282. switch (e.which) {
  283. case 32:
  284. e.preventDefault();
  285. e.stopPropagation();
  286. e.stopImmediatePropagation();
  287. return _this.toggleState();
  288. case 37:
  289. e.preventDefault();
  290. e.stopPropagation();
  291. e.stopImmediatePropagation();
  292. return _this.state(false);
  293. case 39:
  294. e.preventDefault();
  295. e.stopPropagation();
  296. e.stopImmediatePropagation();
  297. return _this.state(true);
  298. }
  299. }
  300. });
  301. };
  302. BootstrapSwitch.prototype._handleHandlers = function() {
  303. var _this = this;
  304. this.$on.on("click.bootstrapSwitch", function(e) {
  305. _this.state(false);
  306. return _this.$element.trigger("focus");
  307. });
  308. return this.$off.on("click.bootstrapSwitch", function(e) {
  309. _this.state(true);
  310. return _this.$element.trigger("focus");
  311. });
  312. };
  313. BootstrapSwitch.prototype._labelHandlers = function() {
  314. var _this = this;
  315. return this.$label.on({
  316. "mousemove.bootstrapSwitch": function(e) {
  317. var left, percent, right;
  318. if (!_this.drag) {
  319. return;
  320. }
  321. percent = ((e.pageX - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
  322. left = 25;
  323. right = 75;
  324. if (percent < left) {
  325. percent = left;
  326. } else if (percent > right) {
  327. percent = right;
  328. }
  329. _this.$div.css("margin-left", "" + (percent - right) + "%");
  330. return _this.$element.trigger("focus");
  331. },
  332. "mousedown.bootstrapSwitch": function(e) {
  333. if (_this.drag || _this.options.disabled || _this.options.readonly) {
  334. return;
  335. }
  336. _this.drag = true;
  337. if (_this.options.animate) {
  338. _this.$wrapper.removeClass("switch-animate");
  339. }
  340. return _this.$element.trigger("focus");
  341. },
  342. "mouseup.bootstrapSwitch": function(e) {
  343. if (!_this.drag) {
  344. return;
  345. }
  346. _this.drag = false;
  347. _this.$element.prop("checked", parseInt(_this.$div.css("margin-left"), 10) > -25).trigger("change");
  348. _this.$div.css("margin-left", "");
  349. if (_this.options.animate) {
  350. return _this.$wrapper.addClass("switch-animate");
  351. }
  352. },
  353. "click.bootstrapSwitch": function(e) {
  354. e.preventDefault();
  355. e.stopImmediatePropagation();
  356. _this.toggleState();
  357. return _this.$element.trigger("focus");
  358. }
  359. });
  360. };
  361. BootstrapSwitch.prototype._formHandler = function() {
  362. var $form;
  363. $form = this.$element.closest("form");
  364. if ($form.data("bootstrap-switch")) {
  365. return;
  366. }
  367. return $form.on("reset.bootstrapSwitch", function() {
  368. return window.setTimeout(function() {
  369. return $form.find("input").filter(function() {
  370. return $(this).data("bootstrap-switch");
  371. }).each(function() {
  372. return $(this).bootstrapSwitch("state", false);
  373. });
  374. }, 1);
  375. }).data("bootstrap-switch", true);
  376. };
  377. BootstrapSwitch.prototype._radioSiblings = function() {
  378. var $elements;
  379. if (!this.$element.is(":radio")) {
  380. return false;
  381. }
  382. $elements = $("[name='" + (this.$element.attr('name')) + "']").not(this.$element);
  383. if (!$elements.length) {
  384. return false;
  385. }
  386. return $elements;
  387. };
  388. return BootstrapSwitch;
  389. })();
  390. $.fn.extend({
  391. bootstrapSwitch: function() {
  392. var args, option, ret;
  393. option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  394. ret = this;
  395. this.each(function() {
  396. var $this, data;
  397. $this = $(this);
  398. data = $this.data("bootstrap-switch");
  399. if (!data) {
  400. $this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
  401. }
  402. if (typeof option === "string") {
  403. return ret = data[option].apply(data, args);
  404. }
  405. });
  406. return ret;
  407. }
  408. });
  409. return $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
  410. })(window.jQuery, window);
  411. }).call(this);