bootstrap-switch.js 19 KB

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