button.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*!
  2. * Bootstrap button.js v4.6.2 (https://getbootstrap.com/)
  3. * Copyright 2011-2022 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
  8. typeof define === 'function' && define.amd ? define(['jquery'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Button = factory(global.jQuery));
  10. })(this, (function ($) { 'use strict';
  11. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  12. var $__default = /*#__PURE__*/_interopDefaultLegacy($);
  13. function _defineProperties(target, props) {
  14. for (var i = 0; i < props.length; i++) {
  15. var descriptor = props[i];
  16. descriptor.enumerable = descriptor.enumerable || false;
  17. descriptor.configurable = true;
  18. if ("value" in descriptor) descriptor.writable = true;
  19. Object.defineProperty(target, descriptor.key, descriptor);
  20. }
  21. }
  22. function _createClass(Constructor, protoProps, staticProps) {
  23. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  24. if (staticProps) _defineProperties(Constructor, staticProps);
  25. Object.defineProperty(Constructor, "prototype", {
  26. writable: false
  27. });
  28. return Constructor;
  29. }
  30. /**
  31. * Constants
  32. */
  33. var NAME = 'button';
  34. var VERSION = '4.6.2';
  35. var DATA_KEY = 'bs.button';
  36. var EVENT_KEY = "." + DATA_KEY;
  37. var DATA_API_KEY = '.data-api';
  38. var JQUERY_NO_CONFLICT = $__default["default"].fn[NAME];
  39. var CLASS_NAME_ACTIVE = 'active';
  40. var CLASS_NAME_BUTTON = 'btn';
  41. var CLASS_NAME_FOCUS = 'focus';
  42. var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
  43. var EVENT_FOCUS_BLUR_DATA_API = "focus" + EVENT_KEY + DATA_API_KEY + " " + ("blur" + EVENT_KEY + DATA_API_KEY);
  44. var EVENT_LOAD_DATA_API = "load" + EVENT_KEY + DATA_API_KEY;
  45. var SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]';
  46. var SELECTOR_DATA_TOGGLES = '[data-toggle="buttons"]';
  47. var SELECTOR_DATA_TOGGLE = '[data-toggle="button"]';
  48. var SELECTOR_DATA_TOGGLES_BUTTONS = '[data-toggle="buttons"] .btn';
  49. var SELECTOR_INPUT = 'input:not([type="hidden"])';
  50. var SELECTOR_ACTIVE = '.active';
  51. var SELECTOR_BUTTON = '.btn';
  52. /**
  53. * Class definition
  54. */
  55. var Button = /*#__PURE__*/function () {
  56. function Button(element) {
  57. this._element = element;
  58. this.shouldAvoidTriggerChange = false;
  59. } // Getters
  60. var _proto = Button.prototype;
  61. // Public
  62. _proto.toggle = function toggle() {
  63. var triggerChangeEvent = true;
  64. var addAriaPressed = true;
  65. var rootElement = $__default["default"](this._element).closest(SELECTOR_DATA_TOGGLES)[0];
  66. if (rootElement) {
  67. var input = this._element.querySelector(SELECTOR_INPUT);
  68. if (input) {
  69. if (input.type === 'radio') {
  70. if (input.checked && this._element.classList.contains(CLASS_NAME_ACTIVE)) {
  71. triggerChangeEvent = false;
  72. } else {
  73. var activeElement = rootElement.querySelector(SELECTOR_ACTIVE);
  74. if (activeElement) {
  75. $__default["default"](activeElement).removeClass(CLASS_NAME_ACTIVE);
  76. }
  77. }
  78. }
  79. if (triggerChangeEvent) {
  80. // if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
  81. if (input.type === 'checkbox' || input.type === 'radio') {
  82. input.checked = !this._element.classList.contains(CLASS_NAME_ACTIVE);
  83. }
  84. if (!this.shouldAvoidTriggerChange) {
  85. $__default["default"](input).trigger('change');
  86. }
  87. }
  88. input.focus();
  89. addAriaPressed = false;
  90. }
  91. }
  92. if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
  93. if (addAriaPressed) {
  94. this._element.setAttribute('aria-pressed', !this._element.classList.contains(CLASS_NAME_ACTIVE));
  95. }
  96. if (triggerChangeEvent) {
  97. $__default["default"](this._element).toggleClass(CLASS_NAME_ACTIVE);
  98. }
  99. }
  100. };
  101. _proto.dispose = function dispose() {
  102. $__default["default"].removeData(this._element, DATA_KEY);
  103. this._element = null;
  104. } // Static
  105. ;
  106. Button._jQueryInterface = function _jQueryInterface(config, avoidTriggerChange) {
  107. return this.each(function () {
  108. var $element = $__default["default"](this);
  109. var data = $element.data(DATA_KEY);
  110. if (!data) {
  111. data = new Button(this);
  112. $element.data(DATA_KEY, data);
  113. }
  114. data.shouldAvoidTriggerChange = avoidTriggerChange;
  115. if (config === 'toggle') {
  116. data[config]();
  117. }
  118. });
  119. };
  120. _createClass(Button, null, [{
  121. key: "VERSION",
  122. get: function get() {
  123. return VERSION;
  124. }
  125. }]);
  126. return Button;
  127. }();
  128. /**
  129. * Data API implementation
  130. */
  131. $__default["default"](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
  132. var button = event.target;
  133. var initialButton = button;
  134. if (!$__default["default"](button).hasClass(CLASS_NAME_BUTTON)) {
  135. button = $__default["default"](button).closest(SELECTOR_BUTTON)[0];
  136. }
  137. if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
  138. event.preventDefault(); // work around Firefox bug #1540995
  139. } else {
  140. var inputBtn = button.querySelector(SELECTOR_INPUT);
  141. if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
  142. event.preventDefault(); // work around Firefox bug #1540995
  143. return;
  144. }
  145. if (initialButton.tagName === 'INPUT' || button.tagName !== 'LABEL') {
  146. Button._jQueryInterface.call($__default["default"](button), 'toggle', initialButton.tagName === 'INPUT');
  147. }
  148. }
  149. }).on(EVENT_FOCUS_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, function (event) {
  150. var button = $__default["default"](event.target).closest(SELECTOR_BUTTON)[0];
  151. $__default["default"](button).toggleClass(CLASS_NAME_FOCUS, /^focus(in)?$/.test(event.type));
  152. });
  153. $__default["default"](window).on(EVENT_LOAD_DATA_API, function () {
  154. // ensure correct active class is set to match the controls' actual values/states
  155. // find all checkboxes/readio buttons inside data-toggle groups
  156. var buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLES_BUTTONS));
  157. for (var i = 0, len = buttons.length; i < len; i++) {
  158. var button = buttons[i];
  159. var input = button.querySelector(SELECTOR_INPUT);
  160. if (input.checked || input.hasAttribute('checked')) {
  161. button.classList.add(CLASS_NAME_ACTIVE);
  162. } else {
  163. button.classList.remove(CLASS_NAME_ACTIVE);
  164. }
  165. } // find all button toggles
  166. buttons = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
  167. for (var _i = 0, _len = buttons.length; _i < _len; _i++) {
  168. var _button = buttons[_i];
  169. if (_button.getAttribute('aria-pressed') === 'true') {
  170. _button.classList.add(CLASS_NAME_ACTIVE);
  171. } else {
  172. _button.classList.remove(CLASS_NAME_ACTIVE);
  173. }
  174. }
  175. });
  176. /**
  177. * jQuery
  178. */
  179. $__default["default"].fn[NAME] = Button._jQueryInterface;
  180. $__default["default"].fn[NAME].Constructor = Button;
  181. $__default["default"].fn[NAME].noConflict = function () {
  182. $__default["default"].fn[NAME] = JQUERY_NO_CONFLICT;
  183. return Button._jQueryInterface;
  184. };
  185. return Button;
  186. }));
  187. //# sourceMappingURL=button.js.map