bootstrap-switch.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 <[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. 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. if (_this9.options.onSwitchChange.apply(element, args) === false) {
  347. if (_this9.$element.is(':radio')) {
  348. $('[name="' + _this9.$element.attr('name') + '"]').trigger('previousState.bootstrapSwitch', true);
  349. } else {
  350. _this9.$element.trigger('previousState.bootstrapSwitch', true);
  351. }
  352. }
  353. });
  354. this.$container = this.$element.wrap(this.$container).parent();
  355. this.$wrapper = this.$container.wrap(this.$wrapper).parent();
  356. this.$element.before(this.options.inverse ? this.$off : this.$on).before(this.$label).before(this.options.inverse ? this.$on : this.$off);
  357. if (this.options.indeterminate) {
  358. this.$element.prop('indeterminate', true);
  359. }
  360. prvinit.call(this);
  361. prvelementHandlers.call(this);
  362. prvhandleHandlers.call(this);
  363. prvlabelHandlers.call(this);
  364. prvformHandler.call(this);
  365. prvexternalLabelHandler.call(this);
  366. this.$element.trigger('init.bootstrapSwitch', this.options.state);
  367. }
  368. _createClass(BootstrapSwitch, [{
  369. key: 'setPrevOptions',
  370. value: function setPrevOptions() {
  371. this.prevOptions = _extends({}, this.options);
  372. }
  373. }, {
  374. key: 'state',
  375. value: function state(value, skip) {
  376. if (typeof value === 'undefined') {
  377. return this.options.state;
  378. }
  379. if (this.options.disabled || this.options.readonly || this.options.state && !this.options.radioAllOff && this.$element.is(':radio')) {
  380. return this.$element;
  381. }
  382. if (this.$element.is(':radio')) {
  383. $('[name="' + this.$element.attr('name') + '"]').trigger('setPreviousOptions.bootstrapSwitch');
  384. } else {
  385. this.$element.trigger('setPreviousOptions.bootstrapSwitch');
  386. }
  387. if (this.options.indeterminate) {
  388. this.indeterminate(false);
  389. }
  390. this.$element.prop('checked', Boolean(value)).trigger('change.bootstrapSwitch', skip);
  391. return this.$element;
  392. }
  393. }, {
  394. key: 'toggleState',
  395. value: function toggleState(skip) {
  396. if (this.options.disabled || this.options.readonly) {
  397. return this.$element;
  398. }
  399. if (this.options.indeterminate) {
  400. this.indeterminate(false);
  401. return this.state(true);
  402. }
  403. return this.$element.prop('checked', !this.options.state).trigger('change.bootstrapSwitch', skip);
  404. }
  405. }, {
  406. key: 'size',
  407. value: function size(value) {
  408. if (typeof value === 'undefined') {
  409. return this.options.size;
  410. }
  411. if (this.options.size != null) {
  412. this.$wrapper.removeClass(prvgetClass.call(this, this.options.size));
  413. }
  414. if (value) {
  415. this.$wrapper.addClass(prvgetClass.call(this, value));
  416. }
  417. prvwidth.call(this);
  418. prvcontainerPosition.call(this);
  419. this.options.size = value;
  420. return this.$element;
  421. }
  422. }, {
  423. key: 'animate',
  424. value: function animate(value) {
  425. if (typeof value === 'undefined') {
  426. return this.options.animate;
  427. }
  428. if (this.options.animate === Boolean(value)) {
  429. return this.$element;
  430. }
  431. return this.toggleAnimate();
  432. }
  433. }, {
  434. key: 'toggleAnimate',
  435. value: function toggleAnimate() {
  436. this.options.animate = !this.options.animate;
  437. this.$wrapper.toggleClass(prvgetClass.call(this, 'animate'));
  438. return this.$element;
  439. }
  440. }, {
  441. key: 'disabled',
  442. value: function disabled(value) {
  443. if (typeof value === 'undefined') {
  444. return this.options.disabled;
  445. }
  446. if (this.options.disabled === Boolean(value)) {
  447. return this.$element;
  448. }
  449. return this.toggleDisabled();
  450. }
  451. }, {
  452. key: 'toggleDisabled',
  453. value: function toggleDisabled() {
  454. this.options.disabled = !this.options.disabled;
  455. this.$element.prop('disabled', this.options.disabled);
  456. this.$wrapper.toggleClass(prvgetClass.call(this, 'disabled'));
  457. return this.$element;
  458. }
  459. }, {
  460. key: 'readonly',
  461. value: function readonly(value) {
  462. if (typeof value === 'undefined') {
  463. return this.options.readonly;
  464. }
  465. if (this.options.readonly === Boolean(value)) {
  466. return this.$element;
  467. }
  468. return this.toggleReadonly();
  469. }
  470. }, {
  471. key: 'toggleReadonly',
  472. value: function toggleReadonly() {
  473. this.options.readonly = !this.options.readonly;
  474. this.$element.prop('readonly', this.options.readonly);
  475. this.$wrapper.toggleClass(prvgetClass.call(this, 'readonly'));
  476. return this.$element;
  477. }
  478. }, {
  479. key: 'indeterminate',
  480. value: function indeterminate(value) {
  481. if (typeof value === 'undefined') {
  482. return this.options.indeterminate;
  483. }
  484. if (this.options.indeterminate === Boolean(value)) {
  485. return this.$element;
  486. }
  487. return this.toggleIndeterminate();
  488. }
  489. }, {
  490. key: 'toggleIndeterminate',
  491. value: function toggleIndeterminate() {
  492. this.options.indeterminate = !this.options.indeterminate;
  493. this.$element.prop('indeterminate', this.options.indeterminate);
  494. this.$wrapper.toggleClass(prvgetClass.call(this, 'indeterminate'));
  495. prvcontainerPosition.call(this);
  496. return this.$element;
  497. }
  498. }, {
  499. key: 'inverse',
  500. value: function inverse(value) {
  501. if (typeof value === 'undefined') {
  502. return this.options.inverse;
  503. }
  504. if (this.options.inverse === Boolean(value)) {
  505. return this.$element;
  506. }
  507. return this.toggleInverse();
  508. }
  509. }, {
  510. key: 'toggleInverse',
  511. value: function toggleInverse() {
  512. this.$wrapper.toggleClass(prvgetClass.call(this, 'inverse'));
  513. var $on = this.$on.clone(true);
  514. var $off = this.$off.clone(true);
  515. this.$on.replaceWith($off);
  516. this.$off.replaceWith($on);
  517. this.$on = $off;
  518. this.$off = $on;
  519. this.options.inverse = !this.options.inverse;
  520. return this.$element;
  521. }
  522. }, {
  523. key: 'onColor',
  524. value: function onColor(value) {
  525. if (typeof value === 'undefined') {
  526. return this.options.onColor;
  527. }
  528. if (this.options.onColor) {
  529. this.$on.removeClass(prvgetClass.call(this, this.options.onColor));
  530. }
  531. this.$on.addClass(prvgetClass.call(this, value));
  532. this.options.onColor = value;
  533. return this.$element;
  534. }
  535. }, {
  536. key: 'offColor',
  537. value: function offColor(value) {
  538. if (typeof value === 'undefined') {
  539. return this.options.offColor;
  540. }
  541. if (this.options.offColor) {
  542. this.$off.removeClass(prvgetClass.call(this, this.options.offColor));
  543. }
  544. this.$off.addClass(prvgetClass.call(this, value));
  545. this.options.offColor = value;
  546. return this.$element;
  547. }
  548. }, {
  549. key: 'onText',
  550. value: function onText(value) {
  551. if (typeof value === 'undefined') {
  552. return this.options.onText;
  553. }
  554. this.$on.html(value);
  555. prvwidth.call(this);
  556. prvcontainerPosition.call(this);
  557. this.options.onText = value;
  558. return this.$element;
  559. }
  560. }, {
  561. key: 'offText',
  562. value: function offText(value) {
  563. if (typeof value === 'undefined') {
  564. return this.options.offText;
  565. }
  566. this.$off.html(value);
  567. prvwidth.call(this);
  568. prvcontainerPosition.call(this);
  569. this.options.offText = value;
  570. return this.$element;
  571. }
  572. }, {
  573. key: 'labelText',
  574. value: function labelText(value) {
  575. if (typeof value === 'undefined') {
  576. return this.options.labelText;
  577. }
  578. this.$label.html(value);
  579. prvwidth.call(this);
  580. this.options.labelText = value;
  581. return this.$element;
  582. }
  583. }, {
  584. key: 'handleWidth',
  585. value: function handleWidth(value) {
  586. if (typeof value === 'undefined') {
  587. return this.options.handleWidth;
  588. }
  589. this.options.handleWidth = value;
  590. prvwidth.call(this);
  591. prvcontainerPosition.call(this);
  592. return this.$element;
  593. }
  594. }, {
  595. key: 'labelWidth',
  596. value: function labelWidth(value) {
  597. if (typeof value === 'undefined') {
  598. return this.options.labelWidth;
  599. }
  600. this.options.labelWidth = value;
  601. prvwidth.call(this);
  602. prvcontainerPosition.call(this);
  603. return this.$element;
  604. }
  605. }, {
  606. key: 'baseClass',
  607. value: function baseClass() {
  608. return this.options.baseClass;
  609. }
  610. }, {
  611. key: 'wrapperClass',
  612. value: function wrapperClass(value) {
  613. if (typeof value === 'undefined') {
  614. return this.options.wrapperClass;
  615. }
  616. var wrapperClass = value || $.fn.bootstrapSwitch.defaults.wrapperClass;
  617. this.$wrapper.removeClass(prvgetClasses.call(this, this.options.wrapperClass).join(' '));
  618. this.$wrapper.addClass(prvgetClasses.call(this, wrapperClass).join(' '));
  619. this.options.wrapperClass = wrapperClass;
  620. return this.$element;
  621. }
  622. }, {
  623. key: 'radioAllOff',
  624. value: function radioAllOff(value) {
  625. if (typeof value === 'undefined') {
  626. return this.options.radioAllOff;
  627. }
  628. var val = Boolean(value);
  629. if (this.options.radioAllOff === val) {
  630. return this.$element;
  631. }
  632. this.options.radioAllOff = val;
  633. return this.$element;
  634. }
  635. }, {
  636. key: 'onInit',
  637. value: function onInit(value) {
  638. if (typeof value === 'undefined') {
  639. return this.options.onInit;
  640. }
  641. this.options.onInit = value || $.fn.bootstrapSwitch.defaults.onInit;
  642. return this.$element;
  643. }
  644. }, {
  645. key: 'onSwitchChange',
  646. value: function onSwitchChange(value) {
  647. if (typeof value === 'undefined') {
  648. return this.options.onSwitchChange;
  649. }
  650. this.options.onSwitchChange = value || $.fn.bootstrapSwitch.defaults.onSwitchChange;
  651. return this.$element;
  652. }
  653. }, {
  654. key: 'destroy',
  655. value: function destroy() {
  656. var $form = this.$element.closest('form');
  657. if ($form.length) {
  658. $form.off('reset.bootstrapSwitch').removeData('bootstrap-switch');
  659. }
  660. this.$container.children().not(this.$element).remove();
  661. this.$element.unwrap().unwrap().off('.bootstrapSwitch').removeData('bootstrap-switch');
  662. return this.$element;
  663. }
  664. }]);
  665. return BootstrapSwitch;
  666. }();
  667. function bootstrapSwitch(option) {
  668. for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  669. args[_key2 - 1] = arguments[_key2];
  670. }
  671. function reducer(ret, next) {
  672. var $this = $(next);
  673. var existingData = $this.data('bootstrap-switch');
  674. var data = existingData || new BootstrapSwitch(next, option);
  675. if (!existingData) {
  676. $this.data('bootstrap-switch', data);
  677. }
  678. if (typeof option === 'string') {
  679. return data[option].apply(data, args);
  680. }
  681. return ret;
  682. }
  683. return Array.prototype.reduce.call(this, reducer, this);
  684. }
  685. $.fn.bootstrapSwitch = bootstrapSwitch;
  686. $.fn.bootstrapSwitch.Constructor = BootstrapSwitch;
  687. $.fn.bootstrapSwitch.defaults = {
  688. state: true,
  689. size: null,
  690. animate: true,
  691. disabled: false,
  692. readonly: false,
  693. indeterminate: false,
  694. inverse: false,
  695. radioAllOff: false,
  696. onColor: 'primary',
  697. offColor: 'default',
  698. onText: 'ON',
  699. offText: 'OFF',
  700. labelText: '&nbsp',
  701. handleWidth: 'auto',
  702. labelWidth: 'auto',
  703. baseClass: 'bootstrap-switch',
  704. wrapperClass: 'wrapper',
  705. onInit: function onInit() {},
  706. onSwitchChange: function onSwitchChange() {}
  707. };
  708. });