bootstrap-switch.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* ========================================================================
  2. * bootstrap-switch - v2.0.1
  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._form();
  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. this.$element.prop("checked", !!value).trigger("change", skip);
  109. return this.$element;
  110. };
  111. BootstrapSwitch.prototype.toggleState = function(skip) {
  112. if (this.options.disabled || this.options.readonly) {
  113. return this.$element;
  114. }
  115. return this.$element.prop("checked", !this.options.state).trigger("change", skip);
  116. };
  117. /*
  118. TODO: refactor
  119. toggleRadioState: (uncheck, skip) ->
  120. $element = @$element.not ":checked"
  121. if uncheck
  122. $element.trigger "change", skip
  123. else
  124. $element.prop("checked", not @$element.is ":checked").trigger "change", skip
  125. @$element
  126. */
  127. BootstrapSwitch.prototype.size = function(value) {
  128. if (typeof value === "undefined") {
  129. return this.options.size;
  130. }
  131. if (this.options.size != null) {
  132. this.$wrapper.removeClass("switch-" + this.options.size);
  133. }
  134. this.$wrapper.addClass("switch-" + value);
  135. this.options.size = value;
  136. return this.$element;
  137. };
  138. BootstrapSwitch.prototype.animate = function(value) {
  139. if (typeof value === "undefined") {
  140. return this.options.animate;
  141. }
  142. value = !!value;
  143. this.$wrapper[value ? "addClass" : "removeClass"]("switch-animate");
  144. this.options.animate = value;
  145. return this.$element;
  146. };
  147. BootstrapSwitch.prototype.disabled = function(value) {
  148. if (typeof value === "undefined") {
  149. return this.options.disabled;
  150. }
  151. value = !!value;
  152. this.$wrapper[value ? "addClass" : "removeClass"]("switch-disabled");
  153. this.$element.prop("disabled", value);
  154. this.options.disabled = value;
  155. return this.$element;
  156. };
  157. BootstrapSwitch.prototype.toggleDisabled = function() {
  158. this.$element.prop("disabled", !this.options.disabled);
  159. this.$wrapper.toggleClass("switch-disabled");
  160. this.options.disabled = !this.options.disabled;
  161. return this.$element;
  162. };
  163. BootstrapSwitch.prototype.readonly = function(value) {
  164. if (typeof value === "undefined") {
  165. return this.options.readonly;
  166. }
  167. value = !!value;
  168. this.$wrapper[value ? "addClass" : "removeClass"]("switch-readonly");
  169. this.$element.prop("readonly", value);
  170. this.options.readonly = value;
  171. return this.$element;
  172. };
  173. BootstrapSwitch.prototype.toggleReadonly = function() {
  174. this.$element.prop("readonly", !this.options.readonly);
  175. this.$wrapper.toggleClass("switch-readonly");
  176. this.options.readonly = !this.options.readonly;
  177. return this.$element;
  178. };
  179. BootstrapSwitch.prototype.onColor = function(value) {
  180. var color;
  181. color = this.options.onColor;
  182. if (typeof value === "undefined") {
  183. return color;
  184. }
  185. if (color != null) {
  186. this.$on.removeClass("switch-" + color);
  187. }
  188. this.$on.addClass("switch-" + value);
  189. this.options.onColor = value;
  190. return this.$element;
  191. };
  192. BootstrapSwitch.prototype.offColor = function(value) {
  193. var color;
  194. color = this.options.offColor;
  195. if (typeof value === "undefined") {
  196. return color;
  197. }
  198. if (color != null) {
  199. this.$off.removeClass("switch-" + color);
  200. }
  201. this.$off.addClass("switch-" + value);
  202. this.options.offColor = value;
  203. return this.$element;
  204. };
  205. BootstrapSwitch.prototype.onText = function(value) {
  206. if (typeof value === "undefined") {
  207. return this.options.onText;
  208. }
  209. this.$on.html(value);
  210. this.options.onText = value;
  211. return this.$element;
  212. };
  213. BootstrapSwitch.prototype.offText = function(value) {
  214. if (typeof value === "undefined") {
  215. return this.options.offText;
  216. }
  217. this.$off.html(value);
  218. this.options.offText = value;
  219. return this.$element;
  220. };
  221. BootstrapSwitch.prototype.labelText = function(value) {
  222. if (typeof value === "undefined") {
  223. return this.options.labelText;
  224. }
  225. this.$label.html(value);
  226. this.options.labelText = value;
  227. return this.$element;
  228. };
  229. BootstrapSwitch.prototype.destroy = function() {
  230. var $form;
  231. $form = this.$element.closest("form");
  232. if ($form.length) {
  233. $form.off("reset.bootstrapSwitch").removeData("bootstrap-switch");
  234. }
  235. this.$div.children().not(this.$element).remove();
  236. this.$element.unwrap().unwrap().off(".bootstrapSwitch").removeData("bootstrap-switch");
  237. return this.$element;
  238. };
  239. BootstrapSwitch.prototype._elementHandlers = function() {
  240. var _this = this;
  241. return this.$element.on({
  242. "change.bootstrapSwitch": function(e, skip) {
  243. var checked;
  244. e.preventDefault();
  245. e.stopPropagation();
  246. e.stopImmediatePropagation();
  247. checked = _this.$element.is(":checked");
  248. if (checked === _this.options.state) {
  249. return;
  250. }
  251. _this.options.state = checked;
  252. _this.$wrapper.removeClass(checked ? "switch-off" : "switch-on").addClass(checked ? "switch-on" : "switch-off");
  253. if (!skip) {
  254. return _this.$element.trigger("switchChange", {
  255. el: _this.$element,
  256. value: checked
  257. });
  258. }
  259. },
  260. "focus.bootstrapSwitch": function(e) {
  261. e.preventDefault();
  262. e.stopPropagation();
  263. e.stopImmediatePropagation();
  264. return _this.$wrapper.addClass("switch-focused");
  265. },
  266. "blur.bootstrapSwitch": function(e) {
  267. e.preventDefault();
  268. e.stopPropagation();
  269. e.stopImmediatePropagation();
  270. return _this.$wrapper.removeClass("switch-focused");
  271. },
  272. "keydown.bootstrapSwitch": function(e) {
  273. if (!e.which || _this.options.disabled || _this.options.readonly) {
  274. return;
  275. }
  276. switch (e.which) {
  277. case 32:
  278. e.preventDefault();
  279. e.stopPropagation();
  280. e.stopImmediatePropagation();
  281. return _this.toggleState();
  282. case 37:
  283. e.preventDefault();
  284. e.stopPropagation();
  285. e.stopImmediatePropagation();
  286. return _this.state(false);
  287. case 39:
  288. e.preventDefault();
  289. e.stopPropagation();
  290. e.stopImmediatePropagation();
  291. return _this.state(true);
  292. }
  293. }
  294. });
  295. };
  296. BootstrapSwitch.prototype._handleHandlers = function() {
  297. var _this = this;
  298. this.$on.on("click.bootstrapSwitch", function(e) {
  299. _this.state(false);
  300. return _this.$element.trigger("focus");
  301. });
  302. return this.$off.on("click.bootstrapSwitch", function(e) {
  303. _this.state(true);
  304. return _this.$element.trigger("focus");
  305. });
  306. };
  307. BootstrapSwitch.prototype._labelHandlers = function() {
  308. var _this = this;
  309. return this.$label.on({
  310. "mousemove.bootstrapSwitch": function(e) {
  311. var left, percent, right;
  312. if (!_this.drag) {
  313. return;
  314. }
  315. percent = ((e.pageX - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
  316. left = 25;
  317. right = 75;
  318. if (percent < left) {
  319. percent = left;
  320. } else if (percent > right) {
  321. percent = right;
  322. }
  323. _this.$div.css("margin-left", "" + (percent - right) + "%");
  324. return _this.$element.trigger("focus");
  325. },
  326. "mousedown.bootstrapSwitch": function(e) {
  327. if (_this.drag || _this.options.disabled || _this.options.readonly) {
  328. return;
  329. }
  330. _this.drag = true;
  331. if (_this.options.animate) {
  332. _this.$wrapper.removeClass("switch-animate");
  333. }
  334. return _this.$element.trigger("focus");
  335. },
  336. "mouseup.bootstrapSwitch": function(e) {
  337. if (!_this.drag) {
  338. return;
  339. }
  340. _this.drag = false;
  341. _this.$element.prop("checked", parseInt(_this.$div.css("margin-left"), 10) > -25).trigger("change");
  342. _this.$div.css("margin-left", "");
  343. if (_this.options.animate) {
  344. return _this.$wrapper.addClass("switch-animate");
  345. }
  346. },
  347. "click.bootstrapSwitch": function(e) {
  348. e.preventDefault();
  349. e.stopImmediatePropagation();
  350. _this.toggleState();
  351. return _this.$element.trigger("focus");
  352. }
  353. });
  354. };
  355. BootstrapSwitch.prototype._form = function() {
  356. var $form;
  357. $form = this.$element.closest("form");
  358. if ($form.data("bootstrap-switch")) {
  359. return;
  360. }
  361. return $form.on("reset.bootstrapSwitch", function() {
  362. return window.setTimeout(function() {
  363. return $form.find("input").filter(function() {
  364. return $(this).data("bootstrap-switch");
  365. }).each(function() {
  366. return $(this).bootstrapSwitch("state", false);
  367. });
  368. }, 1);
  369. }).data("bootstrap-switch", true);
  370. };
  371. return BootstrapSwitch;
  372. })();
  373. $.fn.extend({
  374. bootstrapSwitch: function() {
  375. var args, option, ret;
  376. option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  377. ret = this;
  378. this.each(function() {
  379. var $this, data;
  380. $this = $(this);
  381. data = $this.data("bootstrap-switch");
  382. if (!data) {
  383. $this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
  384. }
  385. if (typeof option === "string") {
  386. return ret = data[option].apply(data, args);
  387. }
  388. });
  389. return ret;
  390. }
  391. });
  392. return $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
  393. })(window.jQuery, window);
  394. }).call(this);