bootstrap-switch.js 19 KB

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