You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bootstrap-switch.js 26KB

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