bootstrap-switch.js 25 KB

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