bootstrap-switch.js 21 KB

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