bootstrap-switch.js 24 KB

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