bootstrap-switch.js 26 KB

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