bootstrap-switch.js 15 KB

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