bootstrap-switch.js 19 KB

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