bootstrap-switch.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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.destroy = function() {
  266. var $form;
  267. $form = this.$element.closest("form");
  268. if ($form.length) {
  269. $form.off("reset.bootstrapSwitch").removeData("bootstrap-switch");
  270. }
  271. this.$container.children().not(this.$element).remove();
  272. this.$element.unwrap().unwrap().off(".bootstrapSwitch").removeData("bootstrap-switch");
  273. return this.$element;
  274. };
  275. BootstrapSwitch.prototype._elementHandlers = function() {
  276. return this.$element.on({
  277. "change.bootstrapSwitch": (function(_this) {
  278. return function(e, skip) {
  279. var checked;
  280. e.preventDefault();
  281. e.stopPropagation();
  282. e.stopImmediatePropagation();
  283. checked = _this.$element.is(":checked");
  284. if (checked === _this.options.state) {
  285. return;
  286. }
  287. _this.options.state = checked;
  288. _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);
  289. if (!skip) {
  290. if (_this.$element.is(":radio")) {
  291. $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop("checked", false).trigger("change.bootstrapSwitch", true);
  292. }
  293. return _this.$element.trigger("switchChange.bootstrapSwitch", [checked]);
  294. }
  295. };
  296. })(this),
  297. "focus.bootstrapSwitch": (function(_this) {
  298. return function(e) {
  299. e.preventDefault();
  300. e.stopPropagation();
  301. e.stopImmediatePropagation();
  302. return _this.$wrapper.addClass("" + _this.options.baseClass + "-" + _this.options.focusedModifierClass);
  303. };
  304. })(this),
  305. "blur.bootstrapSwitch": (function(_this) {
  306. return function(e) {
  307. e.preventDefault();
  308. e.stopPropagation();
  309. e.stopImmediatePropagation();
  310. return _this.$wrapper.removeClass("" + _this.options.baseClass + "-" + _this.options.focusedModifierClass);
  311. };
  312. })(this),
  313. "keydown.bootstrapSwitch": (function(_this) {
  314. return function(e) {
  315. if (!e.which || _this.options.disabled || _this.options.readonly) {
  316. return;
  317. }
  318. switch (e.which) {
  319. case 32:
  320. e.preventDefault();
  321. e.stopPropagation();
  322. e.stopImmediatePropagation();
  323. return _this.toggleState();
  324. case 37:
  325. e.preventDefault();
  326. e.stopPropagation();
  327. e.stopImmediatePropagation();
  328. return _this.state(false);
  329. case 39:
  330. e.preventDefault();
  331. e.stopPropagation();
  332. e.stopImmediatePropagation();
  333. return _this.state(true);
  334. }
  335. };
  336. })(this)
  337. });
  338. };
  339. BootstrapSwitch.prototype._handleHandlers = function() {
  340. this.$on.on("click.bootstrapSwitch", (function(_this) {
  341. return function(e) {
  342. _this.state(false);
  343. return _this.$element.trigger("focus.bootstrapSwitch");
  344. };
  345. })(this));
  346. return this.$off.on("click.bootstrapSwitch", (function(_this) {
  347. return function(e) {
  348. _this.state(true);
  349. return _this.$element.trigger("focus.bootstrapSwitch");
  350. };
  351. })(this));
  352. };
  353. BootstrapSwitch.prototype._labelHandlers = function() {
  354. return this.$label.on({
  355. "mousemove.bootstrapSwitch touchmove.bootstrapSwitch": (function(_this) {
  356. return function(e) {
  357. var left, percent, right;
  358. if (!_this.drag) {
  359. return;
  360. }
  361. e.preventDefault();
  362. percent = (((e.pageX || e.originalEvent.touches[0].pageX) - _this.$wrapper.offset().left) / _this.$wrapper.width()) * 100;
  363. left = 25;
  364. right = 75;
  365. if (percent < left) {
  366. percent = left;
  367. } else if (percent > right) {
  368. percent = right;
  369. }
  370. _this.$container.css("margin-left", "" + (percent - right) + "%");
  371. return _this.$element.trigger("focus.bootstrapSwitch");
  372. };
  373. })(this),
  374. "mousedown.bootstrapSwitch touchstart.bootstrapSwitch": (function(_this) {
  375. return function(e) {
  376. if (_this.drag || _this.options.disabled || _this.options.readonly) {
  377. return;
  378. }
  379. e.preventDefault();
  380. _this.drag = true;
  381. if (_this.options.animate) {
  382. _this.$wrapper.removeClass("" + _this.options.baseClass + "-" + _this.options.animateModifierClass);
  383. }
  384. return _this.$element.trigger("focus.bootstrapSwitch");
  385. };
  386. })(this),
  387. "mouseup.bootstrapSwitch touchend.bootstrapSwitch": (function(_this) {
  388. return function(e) {
  389. if (!_this.drag) {
  390. return;
  391. }
  392. e.preventDefault();
  393. _this.drag = false;
  394. _this.$element.prop("checked", parseInt(_this.$container.css("margin-left"), 10) > -(_this.$container.width() / 6)).trigger("change.bootstrapSwitch");
  395. _this.$container.css("margin-left", "");
  396. if (_this.options.animate) {
  397. return _this.$wrapper.addClass("" + _this.options.baseClass + "-" + _this.options.animateModifierClass);
  398. }
  399. };
  400. })(this),
  401. "mouseleave.bootstrapSwitch": (function(_this) {
  402. return function(e) {
  403. return _this.$label.trigger("mouseup.bootstrapSwitch");
  404. };
  405. })(this)
  406. });
  407. };
  408. BootstrapSwitch.prototype._formHandler = function() {
  409. var $form;
  410. $form = this.$element.closest("form");
  411. if ($form.data("bootstrap-switch")) {
  412. return;
  413. }
  414. return $form.on("reset.bootstrapSwitch", function() {
  415. return window.setTimeout(function() {
  416. return $form.find("input").filter(function() {
  417. return $(this).data("bootstrap-switch");
  418. }).each(function() {
  419. return $(this).bootstrapSwitch("state", false);
  420. });
  421. }, 1);
  422. }).data("bootstrap-switch", true);
  423. };
  424. return BootstrapSwitch;
  425. })();
  426. $.fn.bootstrapSwitch = function() {
  427. var args, option, ret;
  428. option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  429. ret = this;
  430. this.each(function() {
  431. var $this, data;
  432. $this = $(this);
  433. data = $this.data("bootstrap-switch");
  434. if (!data) {
  435. $this.data("bootstrap-switch", data = new BootstrapSwitch(this, option));
  436. }
  437. if (typeof option === "string") {
  438. return ret = data[option].apply(data, args);
  439. }
  440. });
  441. return ret;
  442. };
  443. $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
  444. return $.fn.bootstrapSwitch.defaults = {
  445. state: true,
  446. size: null,
  447. animate: true,
  448. disabled: false,
  449. readonly: false,
  450. onColor: "primary",
  451. offColor: "default",
  452. onText: "ON",
  453. offText: "OFF",
  454. labelText: "&nbsp;",
  455. baseClass: "bootstrap-switch",
  456. wrapperClass: "wrapper",
  457. /*
  458. containerClass: "container"
  459. handleOnClass: "handle-on"
  460. handleOffClass: "handle-off"
  461. labelClass: "label"
  462. */
  463. onModifierClass: "on",
  464. offModifierClass: "off",
  465. focusedModifierClass: "focused",
  466. animateModifierClass: "animate",
  467. disabledModifierClass: "disabled",
  468. readonlyModifierClass: "readonly",
  469. onInit: function() {},
  470. onSwitchChange: function() {}
  471. };
  472. })(window.jQuery, window);
  473. }).call(this);