bootstrap-switch.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* ========================================================================
  2. * bootstrap-switch - v2.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. (function($) {
  23. $.fn.bootstrapSwitch = function(method) {
  24. var methods;
  25. methods = {
  26. init: function() {
  27. return this.each(function() {
  28. var $div, $element, $form, $label, $switchLeft, $switchRight, $wrapper, changeState;
  29. $element = $(this);
  30. $switchLeft = $("<span>", {
  31. "class": "switch-left",
  32. html: function() {
  33. var html, label;
  34. html = "ON";
  35. label = $element.data("on-label");
  36. if (label != null) {
  37. html = label;
  38. }
  39. return html;
  40. }
  41. });
  42. $switchRight = $("<span>", {
  43. "class": "switch-right",
  44. html: function() {
  45. var html, label;
  46. html = "OFF";
  47. label = $element.data("off-label");
  48. if (label != null) {
  49. html = label;
  50. }
  51. return html;
  52. }
  53. });
  54. $label = $("<label>", {
  55. "for": $element.attr("id"),
  56. html: function() {
  57. var html, icon, label;
  58. html = "&nbsp;";
  59. icon = $element.data("label-icon");
  60. label = $element.data("text-label");
  61. if (icon != null) {
  62. html = "<i class=\"icon " + icon + "\"></i>";
  63. }
  64. if (label != null) {
  65. html = label;
  66. }
  67. return html;
  68. }
  69. });
  70. $div = $("<div>");
  71. $wrapper = $("<div>", {
  72. "class": "has-switch",
  73. tabindex: 0
  74. });
  75. $form = $element.closest("form");
  76. changeState = function() {
  77. if ($label.hasClass("label-change-switch")) {
  78. return;
  79. }
  80. return $label.trigger("mousedown").trigger("mouseup").trigger("click");
  81. };
  82. $element.data("bootstrap-switch", true);
  83. if ($element.data("on") != null) {
  84. $switchLeft.addClass("switch-" + $element.data("on"));
  85. }
  86. if ($element.data("off") != null) {
  87. $switchRight.addClass("switch-" + $element.data("off"));
  88. }
  89. $wrapper.data("animated", false);
  90. if ($element.data("animated") !== false) {
  91. $wrapper.addClass("switch-animate").data("animated", true);
  92. }
  93. $div = $element.wrap($div).parent();
  94. $wrapper = $div.wrap($wrapper).parent();
  95. if ($element.attr("class")) {
  96. $.each(["switch-mini", "switch-small", "switch-large"], function(i, cls) {
  97. if ($element.attr("class").indexOf(cls) >= 0) {
  98. return $wrapper.addClass(cls);
  99. }
  100. });
  101. }
  102. $element.before($switchLeft).before($label).before($switchRight);
  103. $wrapper.addClass($element.is(":checked") ? "switch-on" : "switch-off");
  104. if ($element.is(":disabled") || $element.is("[readonly]")) {
  105. $wrapper.addClass("disabled");
  106. }
  107. $element.on("keydown", function(e) {
  108. if (e.keyCode !== 32) {
  109. return;
  110. }
  111. e.stopImmediatePropagation();
  112. e.preventDefault();
  113. return changeState();
  114. }).on("change", function(e, skip) {
  115. var isChecked, state;
  116. isChecked = $element.is(":checked");
  117. state = $wrapper.hasClass("switch-off");
  118. e.preventDefault();
  119. $div.css("left", "");
  120. if (state !== isChecked) {
  121. return;
  122. }
  123. if (isChecked) {
  124. $wrapper.removeClass("switch-off").addClass("switch-on");
  125. } else {
  126. $wrapper.removeClass("switch-on").addClass("switch-off");
  127. }
  128. if ($wrapper.data("animated") !== false) {
  129. $wrapper.addClass("switch-animate");
  130. }
  131. if (typeof skip === "boolean" && skip) {
  132. return;
  133. }
  134. return $element.trigger("switch-change", {
  135. el: $element,
  136. value: isChecked
  137. });
  138. });
  139. $wrapper.on("keydown", function(e) {
  140. if (!e.which || $element.is(":disabled") || $element.is("[readonly]")) {
  141. return;
  142. }
  143. switch (e.which) {
  144. case 32:
  145. e.preventDefault();
  146. return changeState();
  147. case 37:
  148. e.preventDefault();
  149. if ($element.is(":checked")) {
  150. return changeState();
  151. }
  152. break;
  153. case 39:
  154. e.preventDefault();
  155. if (!$element.is(":checked")) {
  156. return changeState();
  157. }
  158. }
  159. });
  160. $switchLeft.on("click", function() {
  161. return changeState();
  162. });
  163. $switchRight.on("click", function() {
  164. return changeState();
  165. });
  166. $label.on("mousedown touchstart", function(e) {
  167. var moving;
  168. moving = false;
  169. e.preventDefault();
  170. e.stopImmediatePropagation();
  171. $wrapper.removeClass("switch-animate");
  172. if ($element.is(":disabled") || $element.is("[readonly]") || $element.hasClass("radio-no-uncheck")) {
  173. return $label.unbind("click");
  174. }
  175. return $label.on("mousemove touchmove", function(e) {
  176. var left, percent, relativeX, right;
  177. relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $wrapper.offset().left;
  178. percent = (relativeX / $wrapper.width()) * 100;
  179. left = 25;
  180. right = 75;
  181. moving = true;
  182. if (percent < left) {
  183. percent = left;
  184. } else if (percent > right) {
  185. percent = right;
  186. }
  187. return $div.css("left", (percent - right) + "%");
  188. }).on("click touchend", function(e) {
  189. e.stopImmediatePropagation();
  190. e.preventDefault();
  191. $label.unbind("mouseleave");
  192. if (moving) {
  193. $element.prop("checked", parseInt($label.parent().css("left"), 10) > -25);
  194. } else {
  195. $element.prop("checked", !$element.is(":checked"));
  196. }
  197. moving = false;
  198. return $element.trigger("change");
  199. }).on("mouseleave", function(e) {
  200. e.preventDefault();
  201. e.stopImmediatePropagation();
  202. $label.unbind("mouseleave mousemove").trigger("mouseup");
  203. return $element.prop("checked", parseInt($label.parent().css("left"), 10) > -25).trigger("change");
  204. }).on("mouseup", function(e) {
  205. e.stopImmediatePropagation();
  206. e.preventDefault();
  207. return $label.trigger("mouseleave");
  208. });
  209. });
  210. if (!$form.data("bootstrap-switch")) {
  211. return $form.bind("reset", function() {
  212. return window.setTimeout(function() {
  213. return $form.find(".has-switch").each(function() {
  214. var $input;
  215. $input = $(this).find("input");
  216. return $input.prop("checked", $input.is(":checked")).trigger("change");
  217. });
  218. }, 1);
  219. }).data("bootstrap-switch", true);
  220. }
  221. });
  222. },
  223. setDisabled: function(disabled) {
  224. var $element, $wrapper;
  225. $element = $(this);
  226. $wrapper = $element.parents(".has-switch");
  227. if (disabled) {
  228. $wrapper.addClass("disabled");
  229. $element.prop("disabled", true);
  230. } else {
  231. $wrapper.removeClass("disabled");
  232. $element.prop("disabled", false);
  233. }
  234. return $element;
  235. },
  236. toggleDisabled: function() {
  237. var $element;
  238. $element = $(this);
  239. $element.prop("disabled", !$element.is(":disabled")).parents(".has-switch").toggleClass("disabled");
  240. return $element;
  241. },
  242. isDisabled: function() {
  243. return $(this).is(":disabled");
  244. },
  245. setReadOnly: function(readonly) {
  246. var $element, $wrapper;
  247. $element = $(this);
  248. $wrapper = $element.parents(".has-switch");
  249. if (readonly) {
  250. $wrapper.addClass("disabled");
  251. $element.prop("readonly", true);
  252. } else {
  253. $wrapper.removeClass("disabled");
  254. $element.prop("readonly", false);
  255. }
  256. return $element;
  257. },
  258. toggleReadOnly: function() {
  259. var $element;
  260. $element = $(this);
  261. $element.prop("readonly", !$element.is("[readonly]")).parents(".has-switch").toggleClass("disabled");
  262. return $element;
  263. },
  264. isReadOnly: function() {
  265. return $(this).is("[readonly]");
  266. },
  267. toggleState: function(skip) {
  268. var $element;
  269. $element = $(this);
  270. $element.prop("checked", !$element.is(":checked")).trigger("change", skip);
  271. return $element;
  272. },
  273. toggleRadioState: function(skip) {
  274. var $element;
  275. $element = $(this);
  276. $element.not(":checked").prop("checked", !$element.is(":checked")).trigger("change", skip);
  277. return $element;
  278. },
  279. toggleRadioStateAllowUncheck: function(uncheck, skip) {
  280. var $element;
  281. $element = $(this);
  282. if (uncheck) {
  283. $element.not(":checked").trigger("change", skip);
  284. } else {
  285. $element.not(":checked").prop("checked", !$element.is(":checked")).trigger("change", skip);
  286. }
  287. return $element;
  288. },
  289. setState: function(value, skip) {
  290. var $element;
  291. $element = $(this);
  292. $element.prop("checked", value).trigger("change", skip);
  293. return $element;
  294. },
  295. setOnLabel: function(value) {
  296. var $element;
  297. $element = $(this);
  298. $element.siblings(".switch-left").html(value);
  299. return $element;
  300. },
  301. setOffLabel: function(value) {
  302. var $element;
  303. $element = $(this);
  304. $element.siblings(".switch-right").html(value);
  305. return $element;
  306. },
  307. setOnClass: function(value) {
  308. var $element, $switchLeft, cls;
  309. $element = $(this);
  310. $switchLeft = $element.siblings(".switch-left");
  311. cls = $element.attr("data-on");
  312. if (value == null) {
  313. return;
  314. }
  315. if (cls != null) {
  316. $switchLeft.removeClass("switch-" + cls);
  317. }
  318. $switchLeft.addClass("switch-" + value);
  319. return $element;
  320. },
  321. setOffClass: function(value) {
  322. var $element, $switchRight, cls;
  323. $element = $(this);
  324. $switchRight = $element.siblings(".switch-right");
  325. cls = $element.attr("data-off");
  326. if (value == null) {
  327. return;
  328. }
  329. if (cls != null) {
  330. $switchRight.removeClass("switch-" + cls);
  331. }
  332. $switchRight.addClass("switch-" + value);
  333. return $element;
  334. },
  335. setAnimated: function(value) {
  336. var $element, $wrapper;
  337. $element = $(this);
  338. $wrapper = $element.parents(".has-switch");
  339. if (value == null) {
  340. value = false;
  341. }
  342. $wrapper.data("animated", value).attr("data-animated", value)[$wrapper.data("animated") !== false ? "addClass" : "removeClass"]("switch-animate");
  343. return $element;
  344. },
  345. setSizeClass: function(value) {
  346. var $element, $wrapper;
  347. $element = $(this);
  348. $wrapper = $element.parents(".has-switch");
  349. $.each(["switch-mini", "switch-small", "switch-large"], function(i, cls) {
  350. return $wrapper[cls !== value ? "removeClass" : "addClass"](cls);
  351. });
  352. return $element;
  353. },
  354. setTextLabel: function(value) {
  355. var $element;
  356. $element = $(this);
  357. $element.siblings("label").html(value || "&nbsp");
  358. return $element;
  359. },
  360. setTextIcon: function(value) {
  361. var $element;
  362. $element = $(this);
  363. $element.siblings("label").html(value ? "<i class=\"icon " + value + "\"></i>" : "&nbsp;");
  364. return $element;
  365. },
  366. state: function() {
  367. return $(this).is(":checked");
  368. },
  369. destroy: function() {
  370. var $div, $element, $form;
  371. $element = $(this);
  372. $div = $element.parent();
  373. $form = $div.closest("form");
  374. $div.children().not($element).remove();
  375. $element.unwrap().unwrap().off("change");
  376. if ($form.length) {
  377. $form.off("reset").removeData("bootstrap-switch");
  378. }
  379. return $element;
  380. }
  381. };
  382. if (methods[method]) {
  383. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  384. }
  385. if (typeof method === "object" || !method) {
  386. return methods.init.apply(this, arguments);
  387. }
  388. return $.error("Method " + method + " does not exist!");
  389. };
  390. return this;
  391. })(jQuery);
  392. }).call(this);