bootstrap-switch.js 19 KB

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