source: trip-planner-front/node_modules/bootstrap/js/dist/dropdown.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 21.7 KB
Line 
1/*!
2 * Bootstrap dropdown.js v5.1.3 (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('@popperjs/core'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./dom/selector-engine.js'), require('./base-component.js')) :
8 typeof define === 'function' && define.amd ? define(['@popperjs/core', './dom/event-handler', './dom/manipulator', './dom/selector-engine', './base-component'], factory) :
9 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global.Popper, global.EventHandler, global.Manipulator, global.SelectorEngine, global.Base));
10})(this, (function (Popper, EventHandler, Manipulator, SelectorEngine, BaseComponent) { 'use strict';
11
12 const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { default: e };
13
14 function _interopNamespace(e) {
15 if (e && e.__esModule) return e;
16 const n = Object.create(null);
17 if (e) {
18 for (const k in e) {
19 if (k !== 'default') {
20 const d = Object.getOwnPropertyDescriptor(e, k);
21 Object.defineProperty(n, k, d.get ? d : {
22 enumerable: true,
23 get: () => e[k]
24 });
25 }
26 }
27 }
28 n.default = e;
29 return Object.freeze(n);
30 }
31
32 const Popper__namespace = /*#__PURE__*/_interopNamespace(Popper);
33 const EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
34 const Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
35 const SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
36 const BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
37
38 /**
39 * --------------------------------------------------------------------------
40 * Bootstrap (v5.1.3): util/index.js
41 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
42 * --------------------------------------------------------------------------
43 */
44
45 const toType = obj => {
46 if (obj === null || obj === undefined) {
47 return `${obj}`;
48 }
49
50 return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
51 };
52
53 const getSelector = element => {
54 let selector = element.getAttribute('data-bs-target');
55
56 if (!selector || selector === '#') {
57 let hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes,
58 // so everything starting with `#` or `.`. If a "real" URL is used as the selector,
59 // `document.querySelector` will rightfully complain it is invalid.
60 // See https://github.com/twbs/bootstrap/issues/32273
61
62 if (!hrefAttr || !hrefAttr.includes('#') && !hrefAttr.startsWith('.')) {
63 return null;
64 } // Just in case some CMS puts out a full URL with the anchor appended
65
66
67 if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
68 hrefAttr = `#${hrefAttr.split('#')[1]}`;
69 }
70
71 selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
72 }
73
74 return selector;
75 };
76
77 const getElementFromSelector = element => {
78 const selector = getSelector(element);
79 return selector ? document.querySelector(selector) : null;
80 };
81
82 const isElement = obj => {
83 if (!obj || typeof obj !== 'object') {
84 return false;
85 }
86
87 if (typeof obj.jquery !== 'undefined') {
88 obj = obj[0];
89 }
90
91 return typeof obj.nodeType !== 'undefined';
92 };
93
94 const getElement = obj => {
95 if (isElement(obj)) {
96 // it's a jQuery object or a node element
97 return obj.jquery ? obj[0] : obj;
98 }
99
100 if (typeof obj === 'string' && obj.length > 0) {
101 return document.querySelector(obj);
102 }
103
104 return null;
105 };
106
107 const typeCheckConfig = (componentName, config, configTypes) => {
108 Object.keys(configTypes).forEach(property => {
109 const expectedTypes = configTypes[property];
110 const value = config[property];
111 const valueType = value && isElement(value) ? 'element' : toType(value);
112
113 if (!new RegExp(expectedTypes).test(valueType)) {
114 throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`);
115 }
116 });
117 };
118
119 const isVisible = element => {
120 if (!isElement(element) || element.getClientRects().length === 0) {
121 return false;
122 }
123
124 return getComputedStyle(element).getPropertyValue('visibility') === 'visible';
125 };
126
127 const isDisabled = element => {
128 if (!element || element.nodeType !== Node.ELEMENT_NODE) {
129 return true;
130 }
131
132 if (element.classList.contains('disabled')) {
133 return true;
134 }
135
136 if (typeof element.disabled !== 'undefined') {
137 return element.disabled;
138 }
139
140 return element.hasAttribute('disabled') && element.getAttribute('disabled') !== 'false';
141 };
142
143 const noop = () => {};
144
145 const getjQuery = () => {
146 const {
147 jQuery
148 } = window;
149
150 if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
151 return jQuery;
152 }
153
154 return null;
155 };
156
157 const DOMContentLoadedCallbacks = [];
158
159 const onDOMContentLoaded = callback => {
160 if (document.readyState === 'loading') {
161 // add listener on the first call when the document is in loading state
162 if (!DOMContentLoadedCallbacks.length) {
163 document.addEventListener('DOMContentLoaded', () => {
164 DOMContentLoadedCallbacks.forEach(callback => callback());
165 });
166 }
167
168 DOMContentLoadedCallbacks.push(callback);
169 } else {
170 callback();
171 }
172 };
173
174 const isRTL = () => document.documentElement.dir === 'rtl';
175
176 const defineJQueryPlugin = plugin => {
177 onDOMContentLoaded(() => {
178 const $ = getjQuery();
179 /* istanbul ignore if */
180
181 if ($) {
182 const name = plugin.NAME;
183 const JQUERY_NO_CONFLICT = $.fn[name];
184 $.fn[name] = plugin.jQueryInterface;
185 $.fn[name].Constructor = plugin;
186
187 $.fn[name].noConflict = () => {
188 $.fn[name] = JQUERY_NO_CONFLICT;
189 return plugin.jQueryInterface;
190 };
191 }
192 });
193 };
194 /**
195 * Return the previous/next element of a list.
196 *
197 * @param {array} list The list of elements
198 * @param activeElement The active element
199 * @param shouldGetNext Choose to get next or previous element
200 * @param isCycleAllowed
201 * @return {Element|elem} The proper element
202 */
203
204
205 const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed) => {
206 let index = list.indexOf(activeElement); // if the element does not exist in the list return an element depending on the direction and if cycle is allowed
207
208 if (index === -1) {
209 return list[!shouldGetNext && isCycleAllowed ? list.length - 1 : 0];
210 }
211
212 const listLength = list.length;
213 index += shouldGetNext ? 1 : -1;
214
215 if (isCycleAllowed) {
216 index = (index + listLength) % listLength;
217 }
218
219 return list[Math.max(0, Math.min(index, listLength - 1))];
220 };
221
222 /**
223 * --------------------------------------------------------------------------
224 * Bootstrap (v5.1.3): dropdown.js
225 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
226 * --------------------------------------------------------------------------
227 */
228 /**
229 * ------------------------------------------------------------------------
230 * Constants
231 * ------------------------------------------------------------------------
232 */
233
234 const NAME = 'dropdown';
235 const DATA_KEY = 'bs.dropdown';
236 const EVENT_KEY = `.${DATA_KEY}`;
237 const DATA_API_KEY = '.data-api';
238 const ESCAPE_KEY = 'Escape';
239 const SPACE_KEY = 'Space';
240 const TAB_KEY = 'Tab';
241 const ARROW_UP_KEY = 'ArrowUp';
242 const ARROW_DOWN_KEY = 'ArrowDown';
243 const RIGHT_MOUSE_BUTTON = 2; // MouseEvent.button value for the secondary button, usually the right button
244
245 const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEY}|${ARROW_DOWN_KEY}|${ESCAPE_KEY}`);
246 const EVENT_HIDE = `hide${EVENT_KEY}`;
247 const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
248 const EVENT_SHOW = `show${EVENT_KEY}`;
249 const EVENT_SHOWN = `shown${EVENT_KEY}`;
250 const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
251 const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}`;
252 const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`;
253 const CLASS_NAME_SHOW = 'show';
254 const CLASS_NAME_DROPUP = 'dropup';
255 const CLASS_NAME_DROPEND = 'dropend';
256 const CLASS_NAME_DROPSTART = 'dropstart';
257 const CLASS_NAME_NAVBAR = 'navbar';
258 const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]';
259 const SELECTOR_MENU = '.dropdown-menu';
260 const SELECTOR_NAVBAR_NAV = '.navbar-nav';
261 const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)';
262 const PLACEMENT_TOP = isRTL() ? 'top-end' : 'top-start';
263 const PLACEMENT_TOPEND = isRTL() ? 'top-start' : 'top-end';
264 const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start';
265 const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end';
266 const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start';
267 const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start';
268 const Default = {
269 offset: [0, 2],
270 boundary: 'clippingParents',
271 reference: 'toggle',
272 display: 'dynamic',
273 popperConfig: null,
274 autoClose: true
275 };
276 const DefaultType = {
277 offset: '(array|string|function)',
278 boundary: '(string|element)',
279 reference: '(string|element|object)',
280 display: 'string',
281 popperConfig: '(null|object|function)',
282 autoClose: '(boolean|string)'
283 };
284 /**
285 * ------------------------------------------------------------------------
286 * Class Definition
287 * ------------------------------------------------------------------------
288 */
289
290 class Dropdown extends BaseComponent__default.default {
291 constructor(element, config) {
292 super(element);
293 this._popper = null;
294 this._config = this._getConfig(config);
295 this._menu = this._getMenuElement();
296 this._inNavbar = this._detectNavbar();
297 } // Getters
298
299
300 static get Default() {
301 return Default;
302 }
303
304 static get DefaultType() {
305 return DefaultType;
306 }
307
308 static get NAME() {
309 return NAME;
310 } // Public
311
312
313 toggle() {
314 return this._isShown() ? this.hide() : this.show();
315 }
316
317 show() {
318 if (isDisabled(this._element) || this._isShown(this._menu)) {
319 return;
320 }
321
322 const relatedTarget = {
323 relatedTarget: this._element
324 };
325 const showEvent = EventHandler__default.default.trigger(this._element, EVENT_SHOW, relatedTarget);
326
327 if (showEvent.defaultPrevented) {
328 return;
329 }
330
331 const parent = Dropdown.getParentFromElement(this._element); // Totally disable Popper for Dropdowns in Navbar
332
333 if (this._inNavbar) {
334 Manipulator__default.default.setDataAttribute(this._menu, 'popper', 'none');
335 } else {
336 this._createPopper(parent);
337 } // If this is a touch-enabled device we add extra
338 // empty mouseover listeners to the body's immediate children;
339 // only needed because of broken event delegation on iOS
340 // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
341
342
343 if ('ontouchstart' in document.documentElement && !parent.closest(SELECTOR_NAVBAR_NAV)) {
344 [].concat(...document.body.children).forEach(elem => EventHandler__default.default.on(elem, 'mouseover', noop));
345 }
346
347 this._element.focus();
348
349 this._element.setAttribute('aria-expanded', true);
350
351 this._menu.classList.add(CLASS_NAME_SHOW);
352
353 this._element.classList.add(CLASS_NAME_SHOW);
354
355 EventHandler__default.default.trigger(this._element, EVENT_SHOWN, relatedTarget);
356 }
357
358 hide() {
359 if (isDisabled(this._element) || !this._isShown(this._menu)) {
360 return;
361 }
362
363 const relatedTarget = {
364 relatedTarget: this._element
365 };
366
367 this._completeHide(relatedTarget);
368 }
369
370 dispose() {
371 if (this._popper) {
372 this._popper.destroy();
373 }
374
375 super.dispose();
376 }
377
378 update() {
379 this._inNavbar = this._detectNavbar();
380
381 if (this._popper) {
382 this._popper.update();
383 }
384 } // Private
385
386
387 _completeHide(relatedTarget) {
388 const hideEvent = EventHandler__default.default.trigger(this._element, EVENT_HIDE, relatedTarget);
389
390 if (hideEvent.defaultPrevented) {
391 return;
392 } // If this is a touch-enabled device we remove the extra
393 // empty mouseover listeners we added for iOS support
394
395
396 if ('ontouchstart' in document.documentElement) {
397 [].concat(...document.body.children).forEach(elem => EventHandler__default.default.off(elem, 'mouseover', noop));
398 }
399
400 if (this._popper) {
401 this._popper.destroy();
402 }
403
404 this._menu.classList.remove(CLASS_NAME_SHOW);
405
406 this._element.classList.remove(CLASS_NAME_SHOW);
407
408 this._element.setAttribute('aria-expanded', 'false');
409
410 Manipulator__default.default.removeDataAttribute(this._menu, 'popper');
411 EventHandler__default.default.trigger(this._element, EVENT_HIDDEN, relatedTarget);
412 }
413
414 _getConfig(config) {
415 config = { ...this.constructor.Default,
416 ...Manipulator__default.default.getDataAttributes(this._element),
417 ...config
418 };
419 typeCheckConfig(NAME, config, this.constructor.DefaultType);
420
421 if (typeof config.reference === 'object' && !isElement(config.reference) && typeof config.reference.getBoundingClientRect !== 'function') {
422 // Popper virtual elements require a getBoundingClientRect method
423 throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`);
424 }
425
426 return config;
427 }
428
429 _createPopper(parent) {
430 if (typeof Popper__namespace === 'undefined') {
431 throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)');
432 }
433
434 let referenceElement = this._element;
435
436 if (this._config.reference === 'parent') {
437 referenceElement = parent;
438 } else if (isElement(this._config.reference)) {
439 referenceElement = getElement(this._config.reference);
440 } else if (typeof this._config.reference === 'object') {
441 referenceElement = this._config.reference;
442 }
443
444 const popperConfig = this._getPopperConfig();
445
446 const isDisplayStatic = popperConfig.modifiers.find(modifier => modifier.name === 'applyStyles' && modifier.enabled === false);
447 this._popper = Popper__namespace.createPopper(referenceElement, this._menu, popperConfig);
448
449 if (isDisplayStatic) {
450 Manipulator__default.default.setDataAttribute(this._menu, 'popper', 'static');
451 }
452 }
453
454 _isShown(element = this._element) {
455 return element.classList.contains(CLASS_NAME_SHOW);
456 }
457
458 _getMenuElement() {
459 return SelectorEngine__default.default.next(this._element, SELECTOR_MENU)[0];
460 }
461
462 _getPlacement() {
463 const parentDropdown = this._element.parentNode;
464
465 if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
466 return PLACEMENT_RIGHT;
467 }
468
469 if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
470 return PLACEMENT_LEFT;
471 } // We need to trim the value because custom properties can also include spaces
472
473
474 const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end';
475
476 if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
477 return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP;
478 }
479
480 return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM;
481 }
482
483 _detectNavbar() {
484 return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null;
485 }
486
487 _getOffset() {
488 const {
489 offset
490 } = this._config;
491
492 if (typeof offset === 'string') {
493 return offset.split(',').map(val => Number.parseInt(val, 10));
494 }
495
496 if (typeof offset === 'function') {
497 return popperData => offset(popperData, this._element);
498 }
499
500 return offset;
501 }
502
503 _getPopperConfig() {
504 const defaultBsPopperConfig = {
505 placement: this._getPlacement(),
506 modifiers: [{
507 name: 'preventOverflow',
508 options: {
509 boundary: this._config.boundary
510 }
511 }, {
512 name: 'offset',
513 options: {
514 offset: this._getOffset()
515 }
516 }]
517 }; // Disable Popper if we have a static display
518
519 if (this._config.display === 'static') {
520 defaultBsPopperConfig.modifiers = [{
521 name: 'applyStyles',
522 enabled: false
523 }];
524 }
525
526 return { ...defaultBsPopperConfig,
527 ...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
528 };
529 }
530
531 _selectMenuItem({
532 key,
533 target
534 }) {
535 const items = SelectorEngine__default.default.find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(isVisible);
536
537 if (!items.length) {
538 return;
539 } // if target isn't included in items (e.g. when expanding the dropdown)
540 // allow cycling to get the last item in case key equals ARROW_UP_KEY
541
542
543 getNextActiveElement(items, target, key === ARROW_DOWN_KEY, !items.includes(target)).focus();
544 } // Static
545
546
547 static jQueryInterface(config) {
548 return this.each(function () {
549 const data = Dropdown.getOrCreateInstance(this, config);
550
551 if (typeof config !== 'string') {
552 return;
553 }
554
555 if (typeof data[config] === 'undefined') {
556 throw new TypeError(`No method named "${config}"`);
557 }
558
559 data[config]();
560 });
561 }
562
563 static clearMenus(event) {
564 if (event && (event.button === RIGHT_MOUSE_BUTTON || event.type === 'keyup' && event.key !== TAB_KEY)) {
565 return;
566 }
567
568 const toggles = SelectorEngine__default.default.find(SELECTOR_DATA_TOGGLE);
569
570 for (let i = 0, len = toggles.length; i < len; i++) {
571 const context = Dropdown.getInstance(toggles[i]);
572
573 if (!context || context._config.autoClose === false) {
574 continue;
575 }
576
577 if (!context._isShown()) {
578 continue;
579 }
580
581 const relatedTarget = {
582 relatedTarget: context._element
583 };
584
585 if (event) {
586 const composedPath = event.composedPath();
587 const isMenuTarget = composedPath.includes(context._menu);
588
589 if (composedPath.includes(context._element) || context._config.autoClose === 'inside' && !isMenuTarget || context._config.autoClose === 'outside' && isMenuTarget) {
590 continue;
591 } // Tab navigation through the dropdown menu or events from contained inputs shouldn't close the menu
592
593
594 if (context._menu.contains(event.target) && (event.type === 'keyup' && event.key === TAB_KEY || /input|select|option|textarea|form/i.test(event.target.tagName))) {
595 continue;
596 }
597
598 if (event.type === 'click') {
599 relatedTarget.clickEvent = event;
600 }
601 }
602
603 context._completeHide(relatedTarget);
604 }
605 }
606
607 static getParentFromElement(element) {
608 return getElementFromSelector(element) || element.parentNode;
609 }
610
611 static dataApiKeydownHandler(event) {
612 // If not input/textarea:
613 // - And not a key in REGEXP_KEYDOWN => not a dropdown command
614 // If input/textarea:
615 // - If space key => not a dropdown command
616 // - If key is other than escape
617 // - If key is not up or down => not a dropdown command
618 // - If trigger inside the menu => not a dropdown command
619 if (/input|textarea/i.test(event.target.tagName) ? event.key === SPACE_KEY || event.key !== ESCAPE_KEY && (event.key !== ARROW_DOWN_KEY && event.key !== ARROW_UP_KEY || event.target.closest(SELECTOR_MENU)) : !REGEXP_KEYDOWN.test(event.key)) {
620 return;
621 }
622
623 const isActive = this.classList.contains(CLASS_NAME_SHOW);
624
625 if (!isActive && event.key === ESCAPE_KEY) {
626 return;
627 }
628
629 event.preventDefault();
630 event.stopPropagation();
631
632 if (isDisabled(this)) {
633 return;
634 }
635
636 const getToggleButton = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default.default.prev(this, SELECTOR_DATA_TOGGLE)[0];
637 const instance = Dropdown.getOrCreateInstance(getToggleButton);
638
639 if (event.key === ESCAPE_KEY) {
640 instance.hide();
641 return;
642 }
643
644 if (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY) {
645 if (!isActive) {
646 instance.show();
647 }
648
649 instance._selectMenuItem(event);
650
651 return;
652 }
653
654 if (!isActive || event.key === SPACE_KEY) {
655 Dropdown.clearMenus();
656 }
657 }
658
659 }
660 /**
661 * ------------------------------------------------------------------------
662 * Data Api implementation
663 * ------------------------------------------------------------------------
664 */
665
666
667 EventHandler__default.default.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown.dataApiKeydownHandler);
668 EventHandler__default.default.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown.dataApiKeydownHandler);
669 EventHandler__default.default.on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus);
670 EventHandler__default.default.on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus);
671 EventHandler__default.default.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
672 event.preventDefault();
673 Dropdown.getOrCreateInstance(this).toggle();
674 });
675 /**
676 * ------------------------------------------------------------------------
677 * jQuery
678 * ------------------------------------------------------------------------
679 * add .Dropdown to jQuery only if jQuery is present
680 */
681
682 defineJQueryPlugin(Dropdown);
683
684 return Dropdown;
685
686}));
687//# sourceMappingURL=dropdown.js.map
Note: See TracBrowser for help on using the repository browser.