bootstrap-switch.js 18 KB

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