bootstrap-switch.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. function BootstrapSwitch(element, options) {
  28. if (options == null) {
  29. options = {};
  30. }
  31. this.$element = $(element);
  32. this.options = $.extend({}, $.fn.bootstrapSwitch.defaults, options, {
  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. onColor: this.$element.data('on-color'),
  39. offColor: this.$element.data('off-color'),
  40. onText: this.$element.data('on-text'),
  41. offText: this.$element.data('off-text'),
  42. labelText: this.$element.data('label-text'),
  43. baseClass: this.$element.data('base-class'),
  44. wrapperClass: this.$element.data('wrapper-class')
  45. });
  46. this.$wrapper = $('<div>', {
  47. "class": (function(_this) {
  48. return function() {
  49. var classes;
  50. classes = ["" + _this.options.baseClass].concat(_this._getClasses(_this.options.wrapperClass));
  51. classes.push(_this.options.state ? "" + _this.options.baseClass + "-on" : "" + _this.options.baseClass + "-off");
  52. if (_this.options.size != null) {
  53. classes.push("" + _this.options.baseClass + "-" + _this.options.size);
  54. }
  55. if (_this.options.animate) {
  56. classes.push("" + _this.options.baseClass + "-animate");
  57. }
  58. if (_this.options.disabled) {
  59. classes.push("" + _this.options.baseClass + "-disabled");
  60. }
  61. if (_this.options.readonly) {
  62. classes.push("" + _this.options.baseClass + "-readonly");
  63. }
  64. if (_this.$element.attr('id')) {
  65. classes.push("" + _this.options.baseClass + "-id-" + (_this.$element.attr('id')));
  66. }
  67. return classes.join(' ');
  68. };
  69. })(this)()
  70. });
  71. this.$container = $('<div>', {
  72. "class": "" + this.options.baseClass + "-container"
  73. });
  74. this.$on = $('<span>', {
  75. html: this.options.onText,
  76. "class": "" + this.options.baseClass + "-handle-on " + this.options.baseClass + "-" + this.options.onColor
  77. });
  78. this.$off = $('<span>', {
  79. html: this.options.offText,
  80. "class": "" + this.options.baseClass + "-handle-off " + this.options.baseClass + "-" + this.options.offColor
  81. });
  82. this.$label = $('<label>', {
  83. "for": this.$element.attr('id'),
  84. html: this.options.labelText,
  85. "class": "" + this.options.baseClass + "-label"
  86. });
  87. this.$element.on('init.bootstrapSwitch', (function(_this) {
  88. return function() {
  89. return _this.options.onInit.apply(element, arguments);
  90. };
  91. })(this));
  92. this.$element.on('switchChange.bootstrapSwitch', (function(_this) {
  93. return function() {
  94. return _this.options.onSwitchChange.apply(element, arguments);
  95. };
  96. })(this));
  97. this.$container = this.$element.wrap(this.$container).parent();
  98. this.$wrapper = this.$container.wrap(this.$wrapper).parent();
  99. this.$element.before(this.$on).before(this.$label).before(this.$off).trigger('init.bootstrapSwitch');
  100. this._elementHandlers();
  101. this._handleHandlers();
  102. this._labelHandlers();
  103. this._formHandler();
  104. }
  105. BootstrapSwitch.prototype._constructor = BootstrapSwitch;
  106. BootstrapSwitch.prototype.state = function(value, skip) {
  107. if (typeof value === 'undefined') {
  108. return this.options.state;
  109. }
  110. if (this.options.disabled || this.options.readonly) {
  111. return this.$element;
  112. }
  113. value = !!value;
  114. this.$element.prop('checked', value).trigger('change.bootstrapSwitch', skip);
  115. return this.$element;
  116. };
  117. BootstrapSwitch.prototype.toggleState = function(skip) {
  118. if (this.options.disabled || this.options.readonly) {
  119. return this.$element;
  120. }
  121. return this.$element.prop('checked', !this.options.state).trigger('change.bootstrapSwitch', skip);
  122. };
  123. BootstrapSwitch.prototype.size = function(value) {
  124. if (typeof value === 'undefined') {
  125. return this.options.size;
  126. }
  127. if (this.options.size != null) {
  128. this.$wrapper.removeClass("" + this.options.baseClass + "-" + this.options.size);
  129. }
  130. if (value) {
  131. this.$wrapper.addClass("" + this.options.baseClass + "-" + value);
  132. }
  133. this.options.size = value;
  134. return this.$element;
  135. };
  136. BootstrapSwitch.prototype.animate = function(value) {
  137. if (typeof value === 'undefined') {
  138. return this.options.animate;
  139. }
  140. value = !!value;
  141. this.$wrapper[value ? 'addClass' : 'removeClass']("" + this.options.baseClass + "-animate");
  142. this.options.animate = value;
  143. return this.$element;
  144. };
  145. BootstrapSwitch.prototype.disabled = function(value) {
  146. if (typeof value === 'undefined') {
  147. return this.options.disabled;
  148. }
  149. value = !!value;
  150. this.$wrapper[value ? 'addClass' : 'removeClass']("" + this.options.baseClass + "-disabled");
  151. this.$element.prop('disabled', value);
  152. this.options.disabled = value;
  153. return this.$element;
  154. };
  155. BootstrapSwitch.prototype.toggleDisabled = function() {
  156. this.$element.prop('disabled', !this.options.disabled);
  157. this.$wrapper.toggleClass("" + this.options.baseClass + "-disabled");
  158. this.options.disabled = !this.options.disabled;
  159. return this.$element;
  160. };
  161. BootstrapSwitch.prototype.readonly = function(value) {
  162. if (typeof value === 'undefined') {
  163. return this.options.readonly;
  164. }
  165. value = !!value;
  166. this.$wrapper[value ? 'addClass' : 'removeClass']("" + this.options.baseClass + "-readonly");
  167. this.$element.prop('readonly', value);
  168. this.options.readonly = value;
  169. return this.$element;
  170. };
  171. BootstrapSwitch.prototype.toggleReadonly = function() {
  172. this.$element.prop('readonly', !this.options.readonly);
  173. this.$wrapper.toggleClass("" + this.options.baseClass + "-readonly");
  174. this.options.readonly = !this.options.readonly;
  175. return this.$element;
  176. };
  177. BootstrapSwitch.prototype.onColor = function(value) {
  178. var color;
  179. color = this.options.onColor;
  180. if (typeof value === 'undefined') {
  181. return color;
  182. }
  183. if (color != null) {
  184. this.$on.removeClass("" + this.options.baseClass + "-" + color);
  185. }
  186. this.$on.addClass("" + this.options.baseClass + "-" + value);
  187. this.options.onColor = value;
  188. return this.$element;
  189. };
  190. BootstrapSwitch.prototype.offColor = function(value) {
  191. var color;
  192. color = this.options.offColor;
  193. if (typeof value === 'undefined') {
  194. return color;
  195. }
  196. if (color != null) {
  197. this.$off.removeClass("" + this.options.baseClass + "-" + color);
  198. }
  199. this.$off.addClass("" + this.options.baseClass + "-" + value);
  200. this.options.offColor = value;
  201. return this.$element;
  202. };
  203. BootstrapSwitch.prototype.onText = function(value) {
  204. if (typeof value === 'undefined') {
  205. return this.options.onText;
  206. }
  207. this.$on.html(value);
  208. this.options.onText = value;
  209. return this.$element;
  210. };
  211. BootstrapSwitch.prototype.offText = function(value) {
  212. if (typeof value === 'undefined') {
  213. return this.options.offText;
  214. }
  215. this.$off.html(value);
  216. this.options.offText = value;
  217. return this.$element;
  218. };
  219. BootstrapSwitch.prototype.labelText = function(value) {
  220. if (typeof value === 'undefined') {
  221. return this.options.labelText;
  222. }
  223. this.$label.html(value);
  224. this.options.labelText = value;
  225. return this.$element;
  226. };
  227. BootstrapSwitch.prototype.baseClass = function(value) {
  228. return this.options.baseClass;
  229. };
  230. BootstrapSwitch.prototype.wrapperClass = function(value) {
  231. if (typeof value === 'undefined') {
  232. return this.options.wrapperClass;
  233. }
  234. if (!value) {
  235. value = $.fn.bootstrapSwitch.defaults.wrapperClass;
  236. }
  237. this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(' '));
  238. this.$wrapper.addClass(this._getClasses(value).join(' '));
  239. this.options.wrapperClass = value;
  240. return this.$element;
  241. };
  242. BootstrapSwitch.prototype.destroy = function() {
  243. var $form;
  244. $form = this.$element.closest('form');
  245. if ($form.length) {
  246. $form.off('reset.bootstrapSwitch').removeData('bootstrap-switch');
  247. }
  248. this.$container.children().not(this.$element).remove();
  249. this.$element.unwrap().unwrap().off('.bootstrapSwitch').removeData('bootstrap-switch');
  250. return this.$element;
  251. };
  252. BootstrapSwitch.prototype._elementHandlers = function() {
  253. return this.$element.on({
  254. 'change.bootstrapSwitch': (function(_this) {
  255. return function(e, skip) {
  256. var checked;
  257. e.preventDefault();
  258. e.stopPropagation();
  259. e.stopImmediatePropagation();
  260. checked = _this.$element.is(':checked');
  261. if (checked === _this.options.state) {
  262. return;
  263. }
  264. _this.options.state = checked;
  265. _this.$wrapper.removeClass(checked ? "" + _this.options.baseClass + "-off" : "" + _this.options.baseClass + "-on").addClass(checked ? "" + _this.options.baseClass + "-on" : "" + _this.options.baseClass + "-off");
  266. if (!skip) {
  267. if (_this.$element.is(':radio')) {
  268. $("[name='" + (_this.$element.attr('name')) + "']").not(_this.$element).prop('checked', false).trigger('change.bootstrapSwitch', true);
  269. }
  270. return _this.$element.trigger('switchChange.bootstrapSwitch', [checked]);
  271. }
  272. };
  273. })(this),
  274. 'focus.bootstrapSwitch': (function(_this) {
  275. return function(e) {
  276. e.preventDefault();
  277. e.stopPropagation();
  278. e.stopImmediatePropagation();
  279. return _this.$wrapper.addClass("" + _this.options.baseClass + "-focused");
  280. };
  281. })(this),
  282. 'blur.bootstrapSwitch': (function(_this) {
  283. return function(e) {
  284. e.preventDefault();
  285. e.stopPropagation();
  286. e.stopImmediatePropagation();
  287. return _this.$wrapper.removeClass("" + _this.options.baseClass + "-focused");
  288. };
  289. })(this),
  290. 'keydown.bootstrapSwitch': (function(_this) {
  291. return function(e) {
  292. if (!e.which || _this.options.disabled || _this.options.readonly) {
  293. return;
  294. }
  295. switch (e.which) {
  296. case 32:
  297. e.preventDefault();
  298. e.stopPropagation();
  299. e.stopImmediatePropagation();
  300. return _this.toggleState();
  301. case 37:
  302. e.preventDefault();
  303. e.stopPropagation();
  304. e.stopImmediatePropagation();
  305. return _this.state(false);
  306. case 39:
  307. e.preventDefault();
  308. e.stopPropagation();
  309. e.stopImmediatePropagation();
  310. return _this.state(true);
  311. }
  312. };
  313. })(this)
  314. });
  315. };
  316. BootstrapSwitch.prototype._handleHandlers = function() {
  317. this.$on.on('click.bootstrapSwitch', (function(_this) {
  318. return function(e) {
  319. _this.state(false);
  320. return _this.$element.trigger('focus.bootstrapSwitch');
  321. };
  322. })(this));
  323. return this.$off.on('click.bootstrapSwitch', (function(_this) {
  324. return function(e) {
  325. _this.state(true);
  326. return _this.$element.trigger('focus.bootstrapSwitch');
  327. };
  328. })(this));
  329. };
  330. BootstrapSwitch.prototype._labelHandlers = function() {
  331. return this.$label.on({
  332. 'mousemove.bootstrapSwitch touchmove.bootstrapSwitch': (function(_this) {
  333. return function(e) {
  334. var left, pageX, percent, right;
  335. if (!_this.drag) {
  336. return;
  337. }
  338. e.preventDefault();
  339. pageX = e.pageX || e.originalEvent.touches[0].pageX;
  340. percent = ((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', this.checked);
  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);