bootstrap-switch.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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.bootstrapSwitch", 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.bootstrapSwitch", skip);
  117. };
  118. /*
  119. TODO: refactor
  120. toggleRadioState: (uncheck, skip) ->
  121. $element = @$element.not ":checked"
  122. if uncheck
  123. $element.trigger "change.bootstrapSwitch", skip
  124. else
  125. $element.prop("checked", not @$element.is ":checked").trigger "change.bootstrapSwitch", 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 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. if (!skip) {
  255. if (_this.$element.is(":radio")) {
  256. $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop("checked", false).trigger("change.bootstrapSwitch", true);
  257. }
  258. return _this.$element.trigger("switchChange", {
  259. el: _this.$element,
  260. value: checked
  261. });
  262. }
  263. },
  264. "focus.bootstrapSwitch": function(e) {
  265. e.preventDefault();
  266. e.stopPropagation();
  267. e.stopImmediatePropagation();
  268. return _this.$wrapper.addClass("switch-focused");
  269. },
  270. "blur.bootstrapSwitch": function(e) {
  271. e.preventDefault();
  272. e.stopPropagation();
  273. e.stopImmediatePropagation();
  274. return _this.$wrapper.removeClass("switch-focused");
  275. },
  276. "keydown.bootstrapSwitch": function(e) {
  277. if (!e.which || _this.options.disabled || _this.options.readonly) {
  278. return;
  279. }
  280. switch (e.which) {
  281. case 32:
  282. e.preventDefault();
  283. e.stopPropagation();
  284. e.stopImmediatePropagation();
  285. return _this.toggleState();
  286. case 37:
  287. e.preventDefault();
  288. e.stopPropagation();
  289. e.stopImmediatePropagation();
  290. return _this.state(false);
  291. case 39:
  292. e.preventDefault();
  293. e.stopPropagation();
  294. e.stopImmediatePropagation();
  295. return _this.state(true);
  296. }
  297. }
  298. });
  299. };
  300. BootstrapSwitch.prototype._handleHandlers = function() {
  301. var _this = this;
  302. this.$on.on("click.bootstrapSwitch", function(e) {
  303. _this.state(false);
  304. return _this.$element.trigger("focus.bootstrapSwitch");
  305. });
  306. return this.$off.on("click.bootstrapSwitch", function(e) {
  307. _this.state(true);
  308. return _this.$element.trigger("focus.bootstrapSwitch");
  309. });
  310. };
  311. BootstrapSwitch.prototype._labelHandlers = function() {
  312. var _this = this;
  313. return this.$label.on({
  314. "mousemove.bootstrapSwitch": function(e) {
  315. var left, percent, right;
  316. if (!_this.drag) {
  317. return;
  318. }
  319. percent = ((e.pageX - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
  320. left = 25;
  321. right = 75;
  322. if (percent < left) {
  323. percent = left;
  324. } else if (percent > right) {
  325. percent = right;
  326. }
  327. _this.$div.css("margin-left", "" + (percent - right) + "%");
  328. return _this.$element.trigger("focus.bootstrapSwitch");
  329. },
  330. "mousedown.bootstrapSwitch": function(e) {
  331. if (_this.drag || _this.options.disabled || _this.options.readonly) {
  332. return;
  333. }
  334. _this.drag = true;
  335. if (_this.options.animate) {
  336. _this.$wrapper.removeClass("switch-animate");
  337. }
  338. return _this.$element.trigger("focus.bootstrapSwitch");
  339. },
  340. "mouseup.bootstrapSwitch": function(e) {
  341. if (!_this.drag) {
  342. return;
  343. }
  344. _this.drag = false;
  345. _this.$element.prop("checked", parseInt(_this.$div.css("margin-left"), 10) > -25).trigger("change.bootstrapSwitch");
  346. _this.$div.css("margin-left", "");
  347. if (_this.options.animate) {
  348. return _this.$wrapper.addClass("switch-animate");
  349. }
  350. },
  351. "click.bootstrapSwitch": function(e) {
  352. e.preventDefault();
  353. e.stopImmediatePropagation();
  354. _this.toggleState();
  355. return _this.$element.trigger("focus.bootstrapSwitch");
  356. }
  357. });
  358. };
  359. BootstrapSwitch.prototype._formHandler = function() {
  360. var $form;
  361. $form = this.$element.closest("form");
  362. if ($form.data("bootstrap-switch")) {
  363. return;
  364. }
  365. return $form.on("reset.bootstrapSwitch", function() {
  366. return window.setTimeout(function() {
  367. return $form.find("input").filter(function() {
  368. return $(this).data("bootstrap-switch");
  369. }).each(function() {
  370. return $(this).bootstrapSwitch("state", false);
  371. });
  372. }, 1);
  373. }).data("bootstrap-switch", true);
  374. };
  375. return BootstrapSwitch;
  376. })();
  377. $.fn.extend({
  378. bootstrapSwitch: function() {
  379. var args, option, ret;
  380. option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  381. ret = this;
  382. this.each(function() {
  383. var $this, data;
  384. $this = $(this);
  385. data = $this.data("bootstrap-switch");
  386. if (!data) {
  387. $this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
  388. }
  389. if (typeof option === "string") {
  390. return ret = data[option].apply(data, args);
  391. }
  392. });
  393. return ret;
  394. }
  395. });
  396. return $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
  397. })(window.jQuery, window);
  398. }).call(this);