You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
15 KiB

9 months ago
  1. /*!
  2. * Bootstrap dropdown.js v5.3.3 (https://getbootstrap.com/)
  3. * Copyright 2011-2024 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('@popperjs/core'), require('./base-component.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./dom/selector-engine.js'), require('./util/index.js')) :
  8. typeof define === 'function' && define.amd ? define(['@popperjs/core', './base-component', './dom/event-handler', './dom/manipulator', './dom/selector-engine', './util/index'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global["@popperjs/core"], global.BaseComponent, global.EventHandler, global.Manipulator, global.SelectorEngine, global.Index));
  10. })(this, (function (Popper, BaseComponent, EventHandler, Manipulator, SelectorEngine, index_js) { 'use strict';
  11. function _interopNamespaceDefault(e) {
  12. const n = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } });
  13. if (e) {
  14. for (const k in e) {
  15. if (k !== 'default') {
  16. const d = Object.getOwnPropertyDescriptor(e, k);
  17. Object.defineProperty(n, k, d.get ? d : {
  18. enumerable: true,
  19. get: () => e[k]
  20. });
  21. }
  22. }
  23. }
  24. n.default = e;
  25. return Object.freeze(n);
  26. }
  27. const Popper__namespace = /*#__PURE__*/_interopNamespaceDefault(Popper);
  28. /**
  29. * --------------------------------------------------------------------------
  30. * Bootstrap dropdown.js
  31. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  32. * --------------------------------------------------------------------------
  33. */
  34. /**
  35. * Constants
  36. */
  37. const NAME = 'dropdown';
  38. const DATA_KEY = 'bs.dropdown';
  39. const EVENT_KEY = `.${DATA_KEY}`;
  40. const DATA_API_KEY = '.data-api';
  41. const ESCAPE_KEY = 'Escape';
  42. const TAB_KEY = 'Tab';
  43. const ARROW_UP_KEY = 'ArrowUp';
  44. const ARROW_DOWN_KEY = 'ArrowDown';
  45. const RIGHT_MOUSE_BUTTON = 2; // MouseEvent.button value for the secondary button, usually the right button
  46. const EVENT_HIDE = `hide${EVENT_KEY}`;
  47. const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
  48. const EVENT_SHOW = `show${EVENT_KEY}`;
  49. const EVENT_SHOWN = `shown${EVENT_KEY}`;
  50. const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
  51. const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}`;
  52. const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`;
  53. const CLASS_NAME_SHOW = 'show';
  54. const CLASS_NAME_DROPUP = 'dropup';
  55. const CLASS_NAME_DROPEND = 'dropend';
  56. const CLASS_NAME_DROPSTART = 'dropstart';
  57. const CLASS_NAME_DROPUP_CENTER = 'dropup-center';
  58. const CLASS_NAME_DROPDOWN_CENTER = 'dropdown-center';
  59. const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]:not(.disabled):not(:disabled)';
  60. const SELECTOR_DATA_TOGGLE_SHOWN = `${SELECTOR_DATA_TOGGLE}.${CLASS_NAME_SHOW}`;
  61. const SELECTOR_MENU = '.dropdown-menu';
  62. const SELECTOR_NAVBAR = '.navbar';
  63. const SELECTOR_NAVBAR_NAV = '.navbar-nav';
  64. const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)';
  65. const PLACEMENT_TOP = index_js.isRTL() ? 'top-end' : 'top-start';
  66. const PLACEMENT_TOPEND = index_js.isRTL() ? 'top-start' : 'top-end';
  67. const PLACEMENT_BOTTOM = index_js.isRTL() ? 'bottom-end' : 'bottom-start';
  68. const PLACEMENT_BOTTOMEND = index_js.isRTL() ? 'bottom-start' : 'bottom-end';
  69. const PLACEMENT_RIGHT = index_js.isRTL() ? 'left-start' : 'right-start';
  70. const PLACEMENT_LEFT = index_js.isRTL() ? 'right-start' : 'left-start';
  71. const PLACEMENT_TOPCENTER = 'top';
  72. const PLACEMENT_BOTTOMCENTER = 'bottom';
  73. const Default = {
  74. autoClose: true,
  75. boundary: 'clippingParents',
  76. display: 'dynamic',
  77. offset: [0, 2],
  78. popperConfig: null,
  79. reference: 'toggle'
  80. };
  81. const DefaultType = {
  82. autoClose: '(boolean|string)',
  83. boundary: '(string|element)',
  84. display: 'string',
  85. offset: '(array|string|function)',
  86. popperConfig: '(null|object|function)',
  87. reference: '(string|element|object)'
  88. };
  89. /**
  90. * Class definition
  91. */
  92. class Dropdown extends BaseComponent {
  93. constructor(element, config) {
  94. super(element, config);
  95. this._popper = null;
  96. this._parent = this._element.parentNode; // dropdown wrapper
  97. // TODO: v6 revert #37011 & change markup https://getbootstrap.com/docs/5.3/forms/input-group/
  98. this._menu = SelectorEngine.next(this._element, SELECTOR_MENU)[0] || SelectorEngine.prev(this._element, SELECTOR_MENU)[0] || SelectorEngine.findOne(SELECTOR_MENU, this._parent);
  99. this._inNavbar = this._detectNavbar();
  100. }
  101. // Getters
  102. static get Default() {
  103. return Default;
  104. }
  105. static get DefaultType() {
  106. return DefaultType;
  107. }
  108. static get NAME() {
  109. return NAME;
  110. }
  111. // Public
  112. toggle() {
  113. return this._isShown() ? this.hide() : this.show();
  114. }
  115. show() {
  116. if (index_js.isDisabled(this._element) || this._isShown()) {
  117. return;
  118. }
  119. const relatedTarget = {
  120. relatedTarget: this._element
  121. };
  122. const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, relatedTarget);
  123. if (showEvent.defaultPrevented) {
  124. return;
  125. }
  126. this._createPopper();
  127. // If this is a touch-enabled device we add extra
  128. // empty mouseover listeners to the body's immediate children;
  129. // only needed because of broken event delegation on iOS
  130. // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
  131. if ('ontouchstart' in document.documentElement && !this._parent.closest(SELECTOR_NAVBAR_NAV)) {
  132. for (const element of [].concat(...document.body.children)) {
  133. EventHandler.on(element, 'mouseover', index_js.noop);
  134. }
  135. }
  136. this._element.focus();
  137. this._element.setAttribute('aria-expanded', true);
  138. this._menu.classList.add(CLASS_NAME_SHOW);
  139. this._element.classList.add(CLASS_NAME_SHOW);
  140. EventHandler.trigger(this._element, EVENT_SHOWN, relatedTarget);
  141. }
  142. hide() {
  143. if (index_js.isDisabled(this._element) || !this._isShown()) {
  144. return;
  145. }
  146. const relatedTarget = {
  147. relatedTarget: this._element
  148. };
  149. this._completeHide(relatedTarget);
  150. }
  151. dispose() {
  152. if (this._popper) {
  153. this._popper.destroy();
  154. }
  155. super.dispose();
  156. }
  157. update() {
  158. this._inNavbar = this._detectNavbar();
  159. if (this._popper) {
  160. this._popper.update();
  161. }
  162. }
  163. // Private
  164. _completeHide(relatedTarget) {
  165. const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE, relatedTarget);
  166. if (hideEvent.defaultPrevented) {
  167. return;
  168. }
  169. // If this is a touch-enabled device we remove the extra
  170. // empty mouseover listeners we added for iOS support
  171. if ('ontouchstart' in document.documentElement) {
  172. for (const element of [].concat(...document.body.children)) {
  173. EventHandler.off(element, 'mouseover', index_js.noop);
  174. }
  175. }
  176. if (this._popper) {
  177. this._popper.destroy();
  178. }
  179. this._menu.classList.remove(CLASS_NAME_SHOW);
  180. this._element.classList.remove(CLASS_NAME_SHOW);
  181. this._element.setAttribute('aria-expanded', 'false');
  182. Manipulator.removeDataAttribute(this._menu, 'popper');
  183. EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget);
  184. }
  185. _getConfig(config) {
  186. config = super._getConfig(config);
  187. if (typeof config.reference === 'object' && !index_js.isElement(config.reference) && typeof config.reference.getBoundingClientRect !== 'function') {
  188. // Popper virtual elements require a getBoundingClientRect method
  189. throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`);
  190. }
  191. return config;
  192. }
  193. _createPopper() {
  194. if (typeof Popper__namespace === 'undefined') {
  195. throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)');
  196. }
  197. let referenceElement = this._element;
  198. if (this._config.reference === 'parent') {
  199. referenceElement = this._parent;
  200. } else if (index_js.isElement(this._config.reference)) {
  201. referenceElement = index_js.getElement(this._config.reference);
  202. } else if (typeof this._config.reference === 'object') {
  203. referenceElement = this._config.reference;
  204. }
  205. const popperConfig = this._getPopperConfig();
  206. this._popper = Popper__namespace.createPopper(referenceElement, this._menu, popperConfig);
  207. }
  208. _isShown() {
  209. return this._menu.classList.contains(CLASS_NAME_SHOW);
  210. }
  211. _getPlacement() {
  212. const parentDropdown = this._parent;
  213. if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
  214. return PLACEMENT_RIGHT;
  215. }
  216. if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
  217. return PLACEMENT_LEFT;
  218. }
  219. if (parentDropdown.classList.contains(CLASS_NAME_DROPUP_CENTER)) {
  220. return PLACEMENT_TOPCENTER;
  221. }
  222. if (parentDropdown.classList.contains(CLASS_NAME_DROPDOWN_CENTER)) {
  223. return PLACEMENT_BOTTOMCENTER;
  224. }
  225. // We need to trim the value because custom properties can also include spaces
  226. const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end';
  227. if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
  228. return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP;
  229. }
  230. return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM;
  231. }
  232. _detectNavbar() {
  233. return this._element.closest(SELECTOR_NAVBAR) !== null;
  234. }
  235. _getOffset() {
  236. const {
  237. offset
  238. } = this._config;
  239. if (typeof offset === 'string') {
  240. return offset.split(',').map(value => Number.parseInt(value, 10));
  241. }
  242. if (typeof offset === 'function') {
  243. return popperData => offset(popperData, this._element);
  244. }
  245. return offset;
  246. }
  247. _getPopperConfig() {
  248. const defaultBsPopperConfig = {
  249. placement: this._getPlacement(),
  250. modifiers: [{
  251. name: 'preventOverflow',
  252. options: {
  253. boundary: this._config.boundary
  254. }
  255. }, {
  256. name: 'offset',
  257. options: {
  258. offset: this._getOffset()
  259. }
  260. }]
  261. };
  262. // Disable Popper if we have a static display or Dropdown is in Navbar
  263. if (this._inNavbar || this._config.display === 'static') {
  264. Manipulator.setDataAttribute(this._menu, 'popper', 'static'); // TODO: v6 remove
  265. defaultBsPopperConfig.modifiers = [{
  266. name: 'applyStyles',
  267. enabled: false
  268. }];
  269. }
  270. return {
  271. ...defaultBsPopperConfig,
  272. ...index_js.execute(this._config.popperConfig, [defaultBsPopperConfig])
  273. };
  274. }
  275. _selectMenuItem({
  276. key,
  277. target
  278. }) {
  279. const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(element => index_js.isVisible(element));
  280. if (!items.length) {
  281. return;
  282. }
  283. // if target isn't included in items (e.g. when expanding the dropdown)
  284. // allow cycling to get the last item in case key equals ARROW_UP_KEY
  285. index_js.getNextActiveElement(items, target, key === ARROW_DOWN_KEY, !items.includes(target)).focus();
  286. }
  287. // Static
  288. static jQueryInterface(config) {
  289. return this.each(function () {
  290. const data = Dropdown.getOrCreateInstance(this, config);
  291. if (typeof config !== 'string') {
  292. return;
  293. }
  294. if (typeof data[config] === 'undefined') {
  295. throw new TypeError(`No method named "${config}"`);
  296. }
  297. data[config]();
  298. });
  299. }
  300. static clearMenus(event) {
  301. if (event.button === RIGHT_MOUSE_BUTTON || event.type === 'keyup' && event.key !== TAB_KEY) {
  302. return;
  303. }
  304. const openToggles = SelectorEngine.find(SELECTOR_DATA_TOGGLE_SHOWN);
  305. for (const toggle of openToggles) {
  306. const context = Dropdown.getInstance(toggle);
  307. if (!context || context._config.autoClose === false) {
  308. continue;
  309. }
  310. const composedPath = event.composedPath();
  311. const isMenuTarget = composedPath.includes(context._menu);
  312. if (composedPath.includes(context._element) || context._config.autoClose === 'inside' && !isMenuTarget || context._config.autoClose === 'outside' && isMenuTarget) {
  313. continue;
  314. }
  315. // Tab navigation through the dropdown menu or events from contained inputs shouldn't close the menu
  316. if (context._menu.contains(event.target) && (event.type === 'keyup' && event.key === TAB_KEY || /input|select|option|textarea|form/i.test(event.target.tagName))) {
  317. continue;
  318. }
  319. const relatedTarget = {
  320. relatedTarget: context._element
  321. };
  322. if (event.type === 'click') {
  323. relatedTarget.clickEvent = event;
  324. }
  325. context._completeHide(relatedTarget);
  326. }
  327. }
  328. static dataApiKeydownHandler(event) {
  329. // If not an UP | DOWN | ESCAPE key => not a dropdown command
  330. // If input/textarea && if key is other than ESCAPE => not a dropdown command
  331. const isInput = /input|textarea/i.test(event.target.tagName);
  332. const isEscapeEvent = event.key === ESCAPE_KEY;
  333. const isUpOrDownEvent = [ARROW_UP_KEY, ARROW_DOWN_KEY].includes(event.key);
  334. if (!isUpOrDownEvent && !isEscapeEvent) {
  335. return;
  336. }
  337. if (isInput && !isEscapeEvent) {
  338. return;
  339. }
  340. event.preventDefault();
  341. // TODO: v6 revert #37011 & change markup https://getbootstrap.com/docs/5.3/forms/input-group/
  342. const getToggleButton = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0] || SelectorEngine.next(this, SELECTOR_DATA_TOGGLE)[0] || SelectorEngine.findOne(SELECTOR_DATA_TOGGLE, event.delegateTarget.parentNode);
  343. const instance = Dropdown.getOrCreateInstance(getToggleButton);
  344. if (isUpOrDownEvent) {
  345. event.stopPropagation();
  346. instance.show();
  347. instance._selectMenuItem(event);
  348. return;
  349. }
  350. if (instance._isShown()) {
  351. // else is escape and we check if it is shown
  352. event.stopPropagation();
  353. instance.hide();
  354. getToggleButton.focus();
  355. }
  356. }
  357. }
  358. /**
  359. * Data API implementation
  360. */
  361. EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown.dataApiKeydownHandler);
  362. EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown.dataApiKeydownHandler);
  363. EventHandler.on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus);
  364. EventHandler.on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus);
  365. EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
  366. event.preventDefault();
  367. Dropdown.getOrCreateInstance(this).toggle();
  368. });
  369. /**
  370. * jQuery
  371. */
  372. index_js.defineJQueryPlugin(Dropdown);
  373. return Dropdown;
  374. }));
  375. //# sourceMappingURL=dropdown.js.map