button.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*!
  2. * Bootstrap button.js v5.0.2 (https://getbootstrap.com/)
  3. * Copyright 2011-2021 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('./dom/selector-engine.js'), require('./dom/event-handler.js'), require('./base-component.js')) :
  8. typeof define === 'function' && define.amd ? define(['./dom/selector-engine', './dom/event-handler', './base-component'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Button = factory(global.SelectorEngine, global.EventHandler, global.Base));
  10. }(this, (function (SelectorEngine, EventHandler, BaseComponent) { 'use strict';
  11. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  12. var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
  13. var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
  14. const getjQuery = () => {
  15. const {
  16. jQuery
  17. } = window;
  18. if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
  19. return jQuery;
  20. }
  21. return null;
  22. };
  23. const DOMContentLoadedCallbacks = [];
  24. const onDOMContentLoaded = callback => {
  25. if (document.readyState === 'loading') {
  26. // add listener on the first call when the document is in loading state
  27. if (!DOMContentLoadedCallbacks.length) {
  28. document.addEventListener('DOMContentLoaded', () => {
  29. DOMContentLoadedCallbacks.forEach(callback => callback());
  30. });
  31. }
  32. DOMContentLoadedCallbacks.push(callback);
  33. } else {
  34. callback();
  35. }
  36. };
  37. const defineJQueryPlugin = plugin => {
  38. onDOMContentLoaded(() => {
  39. const $ = getjQuery();
  40. /* istanbul ignore if */
  41. if ($) {
  42. const name = plugin.NAME;
  43. const JQUERY_NO_CONFLICT = $.fn[name];
  44. $.fn[name] = plugin.jQueryInterface;
  45. $.fn[name].Constructor = plugin;
  46. $.fn[name].noConflict = () => {
  47. $.fn[name] = JQUERY_NO_CONFLICT;
  48. return plugin.jQueryInterface;
  49. };
  50. }
  51. });
  52. };
  53. /**
  54. * --------------------------------------------------------------------------
  55. * Bootstrap (v5.0.2): button.js
  56. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  57. * --------------------------------------------------------------------------
  58. */
  59. /**
  60. * ------------------------------------------------------------------------
  61. * Constants
  62. * ------------------------------------------------------------------------
  63. */
  64. const NAME = 'button';
  65. const DATA_KEY = 'bs.button';
  66. const EVENT_KEY = `.${DATA_KEY}`;
  67. const DATA_API_KEY = '.data-api';
  68. const CLASS_NAME_ACTIVE = 'active';
  69. const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="button"]';
  70. const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
  71. /**
  72. * ------------------------------------------------------------------------
  73. * Class Definition
  74. * ------------------------------------------------------------------------
  75. */
  76. class Button extends BaseComponent__default['default'] {
  77. // Getters
  78. static get NAME() {
  79. return NAME;
  80. } // Public
  81. toggle() {
  82. // Toggle class and sync the `aria-pressed` attribute with the return value of the `.toggle()` method
  83. this._element.setAttribute('aria-pressed', this._element.classList.toggle(CLASS_NAME_ACTIVE));
  84. } // Static
  85. static jQueryInterface(config) {
  86. return this.each(function () {
  87. const data = Button.getOrCreateInstance(this);
  88. if (config === 'toggle') {
  89. data[config]();
  90. }
  91. });
  92. }
  93. }
  94. /**
  95. * ------------------------------------------------------------------------
  96. * Data Api implementation
  97. * ------------------------------------------------------------------------
  98. */
  99. EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
  100. event.preventDefault();
  101. const button = event.target.closest(SELECTOR_DATA_TOGGLE);
  102. const data = Button.getOrCreateInstance(button);
  103. data.toggle();
  104. });
  105. /**
  106. * ------------------------------------------------------------------------
  107. * jQuery
  108. * ------------------------------------------------------------------------
  109. * add .Button to jQuery only if jQuery is present
  110. */
  111. defineJQueryPlugin(Button);
  112. return Button;
  113. })));
  114. //# sourceMappingURL=button.js.map