bootstrap-switch.js 25 KB

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