bootstrap-switch.js 16 KB

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