bootstrap-switch.js 22 KB

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