bootstrap-switch.js 18 KB

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