bootstrap-switch.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. (function ($, window) {
  5. var BootstrapSwitch = function () {
  6. function BootstrapSwitch(element) {
  7. var _this = this;
  8. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  9. _classCallCheck(this, BootstrapSwitch);
  10. this.$element = $(element);
  11. this.options = $.extend({}, $.fn.bootstrapSwitch.defaults, this._getElementOptions(), options);
  12. this.prevOptions = {};
  13. this.$wrapper = $('<div>', {
  14. class: function _class() {
  15. var classes = [];
  16. classes.push(_this.options.state ? 'on' : 'off');
  17. if (_this.options.size) {
  18. classes.push(_this.options.size);
  19. }
  20. if (_this.options.disabled) {
  21. classes.push('disabled');
  22. }
  23. if (_this.options.readonly) {
  24. classes.push('readonly');
  25. }
  26. if (_this.options.indeterminate) {
  27. classes.push('indeterminate');
  28. }
  29. if (_this.options.inverse) {
  30. classes.push('inverse');
  31. }
  32. if (_this.$element.attr('id')) {
  33. classes.push('id-' + _this.$element.attr('id'));
  34. }
  35. return classes.map(_this._getClass.bind(_this)).concat([_this.options.baseClass], _this._getClasses(_this.options.wrapperClass)).join(' ');
  36. }
  37. });
  38. this.$container = $('<div>', { class: this._getClass('container') });
  39. this.$on = $('<span>', {
  40. html: this.options.onText,
  41. class: this._getClass('handle-on') + ' ' + this._getClass(this.options.onColor)
  42. });
  43. this.$off = $('<span>', {
  44. html: this.options.offText,
  45. class: this._getClass('handle-off') + ' ' + this._getClass(this.options.offColor)
  46. });
  47. this.$label = $('<span>', {
  48. html: this.options.labelText,
  49. class: this._getClass('label')
  50. });
  51. this.$element.on('init.bootstrapSwitch', this.options.onInit.bind(this, element));
  52. this.$element.on('switchChange.bootstrapSwitch', function () {
  53. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  54. args[_key] = arguments[_key];
  55. }
  56. if (_this.options.onSwitchChange.apply(element, args) === false) {
  57. if (_this.$element.is(':radio')) {
  58. $('[name="' + _this.$element.attr('name') + '"]').trigger('previousState.bootstrapSwitch', true);
  59. } else {
  60. _this.$element.trigger('previousState.bootstrapSwitch', true);
  61. }
  62. }
  63. });
  64. this.$container = this.$element.wrap(this.$container).parent();
  65. this.$wrapper = this.$container.wrap(this.$wrapper).parent();
  66. this.$element.before(this.options.inverse ? this.$off : this.$on).before(this.$label).before(this.options.inverse ? this.$on : this.$off);
  67. if (this.options.indeterminate) {
  68. this.$element.prop('indeterminate', true);
  69. }
  70. this._init();
  71. this._elementHandlers();
  72. this._handleHandlers();
  73. this._labelHandlers();
  74. this._formHandler();
  75. this._externalLabelHandler();
  76. this.$element.trigger('init.bootstrapSwitch', this.options.state);
  77. }
  78. _createClass(BootstrapSwitch, [{
  79. key: 'setPrevOptions',
  80. value: function setPrevOptions() {
  81. this.prevOptions = Object.assign({}, this.options);
  82. }
  83. }, {
  84. key: 'state',
  85. value: function state(value, skip) {
  86. if (typeof value === 'undefined') {
  87. return this.options.state;
  88. }
  89. if (this.options.disabled || this.options.readonly || this.options.state && !this.options.radioAllOff && this.$element.is(':radio')) {
  90. return this.$element;
  91. }
  92. if (this.$element.is(':radio')) {
  93. $('[name="' + this.$element.attr('name') + '"]').trigger('setPreviousOptions.bootstrapSwitch');
  94. } else {
  95. this.$element.trigger('setPreviousOptions.bootstrapSwitch');
  96. }
  97. if (this.options.indeterminate) {
  98. this.indeterminate(false);
  99. }
  100. this.$element.prop('checked', Boolean(value)).trigger('change.bootstrapSwitch', skip);
  101. return this.$element;
  102. }
  103. }, {
  104. key: 'toggleState',
  105. value: function toggleState(skip) {
  106. if (this.options.disabled || this.options.readonly) {
  107. return this.$element;
  108. }
  109. if (this.options.indeterminate) {
  110. this.indeterminate(false);
  111. return this.state(true);
  112. } else {
  113. return this.$element.prop('checked', !this.options.state).trigger('change.bootstrapSwitch', skip);
  114. }
  115. }
  116. }, {
  117. key: 'size',
  118. value: function size(value) {
  119. if (typeof value === 'undefined') {
  120. return this.options.size;
  121. }
  122. if (this.options.size != null) {
  123. this.$wrapper.removeClass(this._getClass(this.options.size));
  124. }
  125. if (value) {
  126. this.$wrapper.addClass(this._getClass(value));
  127. }
  128. this._width();
  129. this._containerPosition();
  130. this.options.size = value;
  131. return this.$element;
  132. }
  133. }, {
  134. key: 'animate',
  135. value: function animate(value) {
  136. if (typeof value === 'undefined') {
  137. return this.options.animate;
  138. }
  139. if (this.options.animate === Boolean(value)) {
  140. return this.$element;
  141. }
  142. return this.toggleAnimate();
  143. }
  144. }, {
  145. key: 'toggleAnimate',
  146. value: function toggleAnimate() {
  147. this.options.animate = !this.options.animate;
  148. this.$wrapper.toggleClass(this._getClass('animate'));
  149. return this.$element;
  150. }
  151. }, {
  152. key: 'disabled',
  153. value: function disabled(value) {
  154. if (typeof value === 'undefined') {
  155. return this.options.disabled;
  156. }
  157. if (this.options.disabled === Boolean(value)) {
  158. return this.$element;
  159. }
  160. return this.toggleDisabled();
  161. }
  162. }, {
  163. key: 'toggleDisabled',
  164. value: function toggleDisabled() {
  165. this.options.disabled = !this.options.disabled;
  166. this.$element.prop('disabled', this.options.disabled);
  167. this.$wrapper.toggleClass(this._getClass('disabled'));
  168. return this.$element;
  169. }
  170. }, {
  171. key: 'readonly',
  172. value: function readonly(value) {
  173. if (typeof value === 'undefined') {
  174. return this.options.readonly;
  175. }
  176. if (this.options.readonly === Boolean(value)) {
  177. return this.$element;
  178. }
  179. return this.toggleReadonly();
  180. }
  181. }, {
  182. key: 'toggleReadonly',
  183. value: function toggleReadonly() {
  184. this.options.readonly = !this.options.readonly;
  185. this.$element.prop('readonly', this.options.readonly);
  186. this.$wrapper.toggleClass(this._getClass('readonly'));
  187. return this.$element;
  188. }
  189. }, {
  190. key: 'indeterminate',
  191. value: function indeterminate(value) {
  192. if (typeof value === 'undefined') {
  193. return this.options.indeterminate;
  194. }
  195. if (this.options.indeterminate === Boolean(value)) {
  196. return this.$element;
  197. }
  198. return this.toggleIndeterminate();
  199. }
  200. }, {
  201. key: 'toggleIndeterminate',
  202. value: function toggleIndeterminate() {
  203. this.options.indeterminate = !this.options.indeterminate;
  204. this.$element.prop('indeterminate', this.options.indeterminate);
  205. this.$wrapper.toggleClass(this._getClass('indeterminate'));
  206. this._containerPosition();
  207. return this.$element;
  208. }
  209. }, {
  210. key: 'inverse',
  211. value: function inverse(value) {
  212. if (typeof value === 'undefined') {
  213. return this.options.inverse;
  214. }
  215. if (this.options.inverse === Boolean(value)) {
  216. return this.$element;
  217. }
  218. return this.toggleInverse();
  219. }
  220. }, {
  221. key: 'toggleInverse',
  222. value: function toggleInverse() {
  223. this.$wrapper.toggleClass(this._getClass('inverse'));
  224. var $on = this.$on.clone(true);
  225. var $off = this.$off.clone(true);
  226. this.$on.replaceWith($off);
  227. this.$off.replaceWith($on);
  228. this.$on = $off;
  229. this.$off = $on;
  230. this.options.inverse = !this.options.inverse;
  231. return this.$element;
  232. }
  233. }, {
  234. key: 'onColor',
  235. value: function onColor(value) {
  236. if (typeof value === 'undefined') {
  237. return this.options.onColor;
  238. }
  239. if (this.options.onColor) {
  240. this.$on.removeClass(this._getClass(this.options.onColor));
  241. }
  242. this.$on.addClass(this._getClass(value));
  243. this.options.onColor = value;
  244. return this.$element;
  245. }
  246. }, {
  247. key: 'offColor',
  248. value: function offColor(value) {
  249. if (typeof value === 'undefined') {
  250. return this.options.offColor;
  251. }
  252. if (this.options.offColor) {
  253. this.$off.removeClass(this._getClass(this.options.offColor));
  254. }
  255. this.$off.addClass(this._getClass(value));
  256. this.options.offColor = value;
  257. return this.$element;
  258. }
  259. }, {
  260. key: 'onText',
  261. value: function onText(value) {
  262. if (typeof value === 'undefined') {
  263. return this.options.onText;
  264. }
  265. this.$on.html(value);
  266. this._width();
  267. this._containerPosition();
  268. this.options.onText = value;
  269. return this.$element;
  270. }
  271. }, {
  272. key: 'offText',
  273. value: function offText(value) {
  274. if (typeof value === 'undefined') {
  275. return this.options.offText;
  276. }
  277. this.$off.html(value);
  278. this._width();
  279. this._containerPosition();
  280. this.options.offText = value;
  281. return this.$element;
  282. }
  283. }, {
  284. key: 'labelText',
  285. value: function labelText(value) {
  286. if (typeof value === 'undefined') {
  287. return this.options.labelText;
  288. }
  289. this.$label.html(value);
  290. this._width();
  291. this.options.labelText = value;
  292. return this.$element;
  293. }
  294. }, {
  295. key: 'handleWidth',
  296. value: function handleWidth(value) {
  297. if (typeof value === 'undefined') {
  298. return this.options.handleWidth;
  299. }
  300. this.options.handleWidth = value;
  301. this._width();
  302. this._containerPosition();
  303. return this.$element;
  304. }
  305. }, {
  306. key: 'labelWidth',
  307. value: function labelWidth(value) {
  308. if (typeof value === 'undefined') {
  309. return this.options.labelWidth;
  310. }
  311. this.options.labelWidth = value;
  312. this._width();
  313. this._containerPosition();
  314. return this.$element;
  315. }
  316. }, {
  317. key: 'baseClass',
  318. value: function baseClass(value) {
  319. return this.options.baseClass;
  320. }
  321. }, {
  322. key: 'wrapperClass',
  323. value: function wrapperClass(value) {
  324. if (typeof value === 'undefined') {
  325. return this.options.wrapperClass;
  326. }
  327. if (!value) {
  328. value = $.fn.bootstrapSwitch.defaults.wrapperClass;
  329. }
  330. this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(' '));
  331. this.$wrapper.addClass(this._getClasses(value).join(' '));
  332. this.options.wrapperClass = value;
  333. return this.$element;
  334. }
  335. }, {
  336. key: 'radioAllOff',
  337. value: function radioAllOff(value) {
  338. if (typeof value === 'undefined') {
  339. return this.options.radioAllOff;
  340. }
  341. var val = Boolean(value);
  342. if (this.options.radioAllOff === val) {
  343. return this.$element;
  344. }
  345. this.options.radioAllOff = val;
  346. return this.$element;
  347. }
  348. }, {
  349. key: 'onInit',
  350. value: function onInit(value) {
  351. if (typeof value === 'undefined') {
  352. return this.options.onInit;
  353. }
  354. if (!value) {
  355. value = $.fn.bootstrapSwitch.defaults.onInit;
  356. }
  357. this.options.onInit = value;
  358. return this.$element;
  359. }
  360. }, {
  361. key: 'onSwitchChange',
  362. value: function onSwitchChange(value) {
  363. if (typeof value === 'undefined') {
  364. return this.options.onSwitchChange;
  365. }
  366. if (!value) {
  367. value = $.fn.bootstrapSwitch.defaults.onSwitchChange;
  368. }
  369. this.options.onSwitchChange = value;
  370. return this.$element;
  371. }
  372. }, {
  373. key: 'destroy',
  374. value: function destroy() {
  375. var $form = this.$element.closest('form');
  376. if ($form.length) {
  377. $form.off('reset.bootstrapSwitch').removeData('bootstrap-switch');
  378. }
  379. this.$container.children().not(this.$element).remove();
  380. this.$element.unwrap().unwrap().off('.bootstrapSwitch').removeData('bootstrap-switch');
  381. return this.$element;
  382. }
  383. }, {
  384. key: '_getElementOptions',
  385. value: function _getElementOptions() {
  386. return {
  387. state: this.$element.is(':checked'),
  388. size: this.$element.data('size'),
  389. animate: this.$element.data('animate'),
  390. disabled: this.$element.is(':disabled'),
  391. readonly: this.$element.is('[readonly]'),
  392. indeterminate: this.$element.data('indeterminate'),
  393. inverse: this.$element.data('inverse'),
  394. radioAllOff: this.$element.data('radio-all-off'),
  395. onColor: this.$element.data('on-color'),
  396. offColor: this.$element.data('off-color'),
  397. onText: this.$element.data('on-text'),
  398. offText: this.$element.data('off-text'),
  399. labelText: this.$element.data('label-text'),
  400. handleWidth: this.$element.data('handle-width'),
  401. labelWidth: this.$element.data('label-width'),
  402. baseClass: this.$element.data('base-class'),
  403. wrapperClass: this.$element.data('wrapper-class')
  404. };
  405. }
  406. }, {
  407. key: '_width',
  408. value: function _width() {
  409. var _this2 = this;
  410. var $handles = this.$on.add(this.$off).add(this.$label).css('width', '');
  411. var handleWidth = void 0;
  412. if (this.options.handleWidth === 'auto') {
  413. handleWidth = Math.round(Math.max(this.$on.width(), this.$off.width()));
  414. } else {
  415. handleWidth = this.options.handleWidth;
  416. }
  417. $handles.width(handleWidth);
  418. this.$label.width(function (index, width) {
  419. if (_this2.options.labelWidth !== 'auto') {
  420. return _this2.options.labelWidth;
  421. }
  422. if (width < handleWidth) {
  423. return handleWidth;
  424. }
  425. return width;
  426. });
  427. this._handleWidth = this.$on.outerWidth();
  428. this._labelWidth = this.$label.outerWidth();
  429. this.$container.width(this._handleWidth * 2 + this._labelWidth);
  430. return this.$wrapper.width(this._handleWidth + this._labelWidth);
  431. }
  432. }, {
  433. key: '_containerPosition',
  434. value: function _containerPosition() {
  435. var _this3 = this;
  436. var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.state;
  437. var callback = arguments[1];
  438. this.$container.css('margin-left', function () {
  439. var values = [0, '-' + _this3._handleWidth + 'px'];
  440. if (_this3.options.indeterminate) {
  441. return '-' + _this3._handleWidth / 2 + 'px';
  442. }
  443. if (state) {
  444. if (_this3.options.inverse) {
  445. return values[1];
  446. } else {
  447. return values[0];
  448. }
  449. } else {
  450. if (_this3.options.inverse) {
  451. return values[0];
  452. } else {
  453. return values[1];
  454. }
  455. }
  456. });
  457. }
  458. }, {
  459. key: '_init',
  460. value: function _init() {
  461. var _this4 = this;
  462. var init = function init() {
  463. _this4.setPrevOptions();
  464. _this4._width();
  465. _this4._containerPosition();
  466. setTimeout(function () {
  467. if (_this4.options.animate) {
  468. return _this4.$wrapper.addClass(_this4._getClass('animate'));
  469. }
  470. }, 50);
  471. };
  472. if (this.$wrapper.is(':visible')) {
  473. init();
  474. return;
  475. }
  476. var initInterval = window.setInterval(function () {
  477. if (_this4.$wrapper.is(':visible')) {
  478. init();
  479. return window.clearInterval(initInterval);
  480. }
  481. }, 50);
  482. }
  483. }, {
  484. key: '_elementHandlers',
  485. value: function _elementHandlers() {
  486. var _this5 = this;
  487. return this.$element.on({
  488. 'setPreviousOptions.bootstrapSwitch': this.setPrevOptions.bind(this),
  489. 'previousState.bootstrapSwitch': function previousStateBootstrapSwitch() {
  490. _this5.options = _this5.prevOptions;
  491. if (_this5.options.indeterminate) {
  492. _this5.$wrapper.addClass(_this5._getClass('indeterminate'));
  493. }
  494. _this5.$element.prop('checked', _this5.options.state).trigger('change.bootstrapSwitch', true);
  495. },
  496. 'change.bootstrapSwitch': function changeBootstrapSwitch(event, skip) {
  497. event.preventDefault();
  498. event.stopImmediatePropagation();
  499. var state = _this5.$element.is(':checked');
  500. _this5._containerPosition(state);
  501. if (state === _this5.options.state) {
  502. return;
  503. }
  504. _this5.options.state = state;
  505. _this5.$wrapper.toggleClass(_this5._getClass('off')).toggleClass(_this5._getClass('on'));
  506. if (!skip) {
  507. if (_this5.$element.is(':radio')) {
  508. $('[name="' + _this5.$element.attr('name') + '"]').not(_this5.$element).prop('checked', false).trigger('change.bootstrapSwitch', true);
  509. }
  510. _this5.$element.trigger('switchChange.bootstrapSwitch', [state]);
  511. }
  512. },
  513. 'focus.bootstrapSwitch': function focusBootstrapSwitch(event) {
  514. event.preventDefault();
  515. _this5.$wrapper.addClass(_this5._getClass('focused'));
  516. },
  517. 'blur.bootstrapSwitch': function blurBootstrapSwitch(event) {
  518. event.preventDefault();
  519. _this5.$wrapper.removeClass(_this5._getClass('focused'));
  520. },
  521. 'keydown.bootstrapSwitch': function keydownBootstrapSwitch(event) {
  522. if (!event.which || _this5.options.disabled || _this5.options.readonly) {
  523. return;
  524. }
  525. if (event.which === 37 || event.which === 39) {
  526. event.preventDefault();
  527. event.stopImmediatePropagation();
  528. _this5.state(event.which === 39);
  529. }
  530. }
  531. });
  532. }
  533. }, {
  534. key: '_handleHandlers',
  535. value: function _handleHandlers() {
  536. var _this6 = this;
  537. this.$on.on('click.bootstrapSwitch', function (event) {
  538. event.preventDefault();
  539. event.stopPropagation();
  540. _this6.state(false);
  541. return _this6.$element.trigger('focus.bootstrapSwitch');
  542. });
  543. return this.$off.on('click.bootstrapSwitch', function (event) {
  544. event.preventDefault();
  545. event.stopPropagation();
  546. _this6.state(true);
  547. return _this6.$element.trigger('focus.bootstrapSwitch');
  548. });
  549. }
  550. }, {
  551. key: '_labelHandlers',
  552. value: function _labelHandlers() {
  553. var _this7 = this;
  554. var handlers = {
  555. click: function click(event) {
  556. event.stopPropagation();
  557. },
  558. 'mousedown.bootstrapSwitch touchstart.bootstrapSwitch': function mousedownBootstrapSwitchTouchstartBootstrapSwitch(event) {
  559. if (_this7._dragStart || _this7.options.disabled || _this7.options.readonly) {
  560. return;
  561. }
  562. event.preventDefault();
  563. event.stopPropagation();
  564. _this7._dragStart = (event.pageX || event.originalEvent.touches[0].pageX) - parseInt(_this7.$container.css('margin-left'), 10);
  565. if (_this7.options.animate) {
  566. _this7.$wrapper.removeClass(_this7._getClass('animate'));
  567. }
  568. _this7.$element.trigger('focus.bootstrapSwitch');
  569. },
  570. 'mousemove.bootstrapSwitch touchmove.bootstrapSwitch': function mousemoveBootstrapSwitchTouchmoveBootstrapSwitch(event) {
  571. if (_this7._dragStart == null) {
  572. return;
  573. }
  574. var difference = (event.pageX || event.originalEvent.touches[0].pageX) - _this7._dragStart;
  575. event.preventDefault();
  576. if (difference < -_this7._handleWidth || difference > 0) {
  577. return;
  578. }
  579. _this7._dragEnd = difference;
  580. _this7.$container.css('margin-left', _this7._dragEnd + 'px');
  581. },
  582. 'mouseup.bootstrapSwitch touchend.bootstrapSwitch': function mouseupBootstrapSwitchTouchendBootstrapSwitch(event) {
  583. if (!_this7._dragStart) {
  584. return;
  585. }
  586. event.preventDefault();
  587. if (_this7.options.animate) {
  588. _this7.$wrapper.addClass(_this7._getClass('animate'));
  589. }
  590. if (_this7._dragEnd) {
  591. var state = _this7._dragEnd > -(_this7._handleWidth / 2);
  592. _this7._dragEnd = false;
  593. _this7.state(_this7.options.inverse ? !state : state);
  594. } else {
  595. _this7.state(!_this7.options.state);
  596. }
  597. _this7._dragStart = false;
  598. },
  599. 'mouseleave.bootstrapSwitch': function mouseleaveBootstrapSwitch() {
  600. _this7.$label.trigger('mouseup.bootstrapSwitch');
  601. }
  602. };
  603. this.$label.on(handlers);
  604. }
  605. }, {
  606. key: '_externalLabelHandler',
  607. value: function _externalLabelHandler() {
  608. var _this8 = this;
  609. var $externalLabel = this.$element.closest('label');
  610. $externalLabel.on('click', function (event) {
  611. event.preventDefault();
  612. event.stopImmediatePropagation();
  613. if (event.target === $externalLabel[0]) {
  614. _this8.toggleState();
  615. }
  616. });
  617. }
  618. }, {
  619. key: '_formHandler',
  620. value: function _formHandler() {
  621. var $form = this.$element.closest('form');
  622. if ($form.data('bootstrap-switch')) {
  623. return;
  624. }
  625. $form.on('reset.bootstrapSwitch', function () {
  626. window.setTimeout(function () {
  627. $form.find('input').filter(function () {
  628. return $(this).data('bootstrap-switch');
  629. }).each(function () {
  630. return $(this).bootstrapSwitch('state', this.checked);
  631. });
  632. }, 1);
  633. }).data('bootstrap-switch', true);
  634. }
  635. }, {
  636. key: '_getClass',
  637. value: function _getClass(name) {
  638. return this.options.baseClass + '-' + name;
  639. }
  640. }, {
  641. key: '_getClasses',
  642. value: function _getClasses(classes) {
  643. if (!$.isArray(classes)) {
  644. return [this._getClass(classes)];
  645. }
  646. return classes.map(this._getClass.bind(this));
  647. }
  648. }]);
  649. return BootstrapSwitch;
  650. }();
  651. $.fn.bootstrapSwitch = function (option) {
  652. for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  653. args[_key2 - 1] = arguments[_key2];
  654. }
  655. var ret = this;
  656. this.each(function () {
  657. var $this = $(this);
  658. var data = $this.data('bootstrap-switch');
  659. if (!data) {
  660. data = new BootstrapSwitch(this, option);
  661. $this.data('bootstrap-switch', data);
  662. }
  663. if (typeof option === 'string') {
  664. ret = data[option].apply(data, args);
  665. }
  666. });
  667. return ret;
  668. };
  669. $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
  670. $.fn.bootstrapSwitch.defaults = {
  671. state: true,
  672. size: null,
  673. animate: true,
  674. disabled: false,
  675. readonly: false,
  676. indeterminate: false,
  677. inverse: false,
  678. radioAllOff: false,
  679. onColor: 'primary',
  680. offColor: 'default',
  681. onText: 'ON',
  682. offText: 'OFF',
  683. labelText: '&nbsp',
  684. handleWidth: 'auto',
  685. labelWidth: 'auto',
  686. baseClass: 'bootstrap-switch',
  687. wrapperClass: 'wrapper',
  688. onInit: function onInit() {},
  689. onSwitchChange: function onSwitchChange() {}
  690. };
  691. })(window.jQuery, window);