0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-04 16:24:22 +01:00
Bootstrap/js/dist/dropdown.js

678 lines
22 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap dropdown.js v5.0.0 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 07:41:12 +01:00
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@popperjs/core'), require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./dom/selector-engine.js'), require('./base-component.js')) :
typeof define === 'function' && define.amd ? define(['@popperjs/core', './dom/data', './dom/event-handler', './dom/manipulator', './dom/selector-engine', './base-component'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global.Popper, global.Data, global.EventHandler, global.Manipulator, global.SelectorEngine, global.Base));
}(this, (function (Popper, Data, EventHandler, Manipulator, SelectorEngine, BaseComponent) { 'use strict';
2019-03-01 17:31:34 +01:00
2020-09-14 17:12:06 +02:00
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
2020-12-07 16:50:24 +01:00
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var Popper__namespace = /*#__PURE__*/_interopNamespace(Popper);
2020-09-14 17:12:06 +02:00
var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
var SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.0): util/index.js
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 17:31:34 +01:00
* --------------------------------------------------------------------------
*/
2021-03-23 17:26:54 +01:00
const toType = obj => {
2020-03-28 11:29:08 +01:00
if (obj === null || obj === undefined) {
2021-03-23 17:26:54 +01:00
return `${obj}`;
2020-03-28 11:29:08 +01:00
}
2019-03-01 17:31:34 +01:00
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
};
2021-03-23 17:26:54 +01:00
const getSelector = element => {
let selector = element.getAttribute('data-bs-target');
2019-03-01 17:31:34 +01:00
if (!selector || selector === '#') {
2021-03-23 17:26:54 +01:00
let hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttr || !hrefAttr.includes('#') && !hrefAttr.startsWith('.')) {
return null;
} // Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
hrefAttr = `#${hrefAttr.split('#')[1]}`;
}
2019-08-27 15:03:21 +02:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 17:31:34 +01:00
}
2019-08-27 15:03:21 +02:00
return selector;
};
2021-03-23 17:26:54 +01:00
const getElementFromSelector = element => {
const selector = getSelector(element);
2019-08-27 15:03:21 +02:00
return selector ? document.querySelector(selector) : null;
2019-03-01 17:31:34 +01:00
};
2021-03-23 17:26:54 +01:00
const isElement = obj => (obj[0] || obj).nodeType;
2019-03-01 17:31:34 +01:00
2021-03-23 17:26:54 +01:00
const typeCheckConfig = (componentName, config, configTypes) => {
Object.keys(configTypes).forEach(property => {
const expectedTypes = configTypes[property];
const value = config[property];
const valueType = value && isElement(value) ? 'element' : toType(value);
2019-03-01 17:31:34 +01:00
if (!new RegExp(expectedTypes).test(valueType)) {
throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`);
2019-03-01 17:31:34 +01:00
}
});
};
2021-03-23 17:26:54 +01:00
const isVisible = element => {
2019-11-08 09:11:23 +01:00
if (!element) {
return false;
}
if (element.style && element.parentNode && element.parentNode.style) {
2021-03-23 17:26:54 +01:00
const elementStyle = getComputedStyle(element);
const parentNodeStyle = getComputedStyle(element.parentNode);
2019-11-08 09:11:23 +01:00
return elementStyle.display !== 'none' && parentNodeStyle.display !== 'none' && elementStyle.visibility !== 'hidden';
}
return false;
};
const isDisabled = element => {
if (!element || element.nodeType !== Node.ELEMENT_NODE) {
return true;
}
if (element.classList.contains('disabled')) {
return true;
}
if (typeof element.disabled !== 'undefined') {
return element.disabled;
}
return element.hasAttribute('disabled') && element.getAttribute('disabled') !== 'false';
};
const noop = () => {};
2019-03-01 17:31:34 +01:00
2021-03-23 17:26:54 +01:00
const getjQuery = () => {
const {
jQuery
} = window;
2019-08-27 15:03:21 +02:00
2020-11-23 14:17:16 +01:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 15:03:21 +02:00
return jQuery;
}
return null;
};
2021-03-23 17:26:54 +01:00
const onDOMContentLoaded = callback => {
2020-11-11 18:07:37 +01:00
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
};
2021-03-23 17:26:54 +01:00
const isRTL = () => document.documentElement.dir === 'rtl';
2020-12-03 14:08:31 +01:00
2021-03-23 17:26:54 +01:00
const defineJQueryPlugin = (name, plugin) => {
onDOMContentLoaded(() => {
const $ = getjQuery();
/* istanbul ignore if */
2020-12-03 15:18:59 +01:00
if ($) {
2021-03-23 17:26:54 +01:00
const JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
$.fn[name].noConflict = () => {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 15:18:59 +01:00
}
});
};
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.0): dropdown.js
2021-03-23 17:26:54 +01:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2020-12-03 15:18:59 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2021-03-23 17:26:54 +01:00
const NAME = 'dropdown';
const DATA_KEY = 'bs.dropdown';
const EVENT_KEY = `.${DATA_KEY}`;
const DATA_API_KEY = '.data-api';
const ESCAPE_KEY = 'Escape';
const SPACE_KEY = 'Space';
const TAB_KEY = 'Tab';
const ARROW_UP_KEY = 'ArrowUp';
const ARROW_DOWN_KEY = 'ArrowDown';
const RIGHT_MOUSE_BUTTON = 2; // MouseEvent.button value for the secondary button, usually the right button
const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEY}|${ARROW_DOWN_KEY}|${ESCAPE_KEY}`);
const EVENT_HIDE = `hide${EVENT_KEY}`;
const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
const EVENT_SHOW = `show${EVENT_KEY}`;
const EVENT_SHOWN = `shown${EVENT_KEY}`;
const EVENT_CLICK = `click${EVENT_KEY}`;
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}`;
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`;
const CLASS_NAME_SHOW = 'show';
const CLASS_NAME_DROPUP = 'dropup';
const CLASS_NAME_DROPEND = 'dropend';
const CLASS_NAME_DROPSTART = 'dropstart';
const CLASS_NAME_NAVBAR = 'navbar';
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]';
const SELECTOR_MENU = '.dropdown-menu';
const SELECTOR_NAVBAR_NAV = '.navbar-nav';
const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)';
const PLACEMENT_TOP = isRTL() ? 'top-end' : 'top-start';
const PLACEMENT_TOPEND = isRTL() ? 'top-start' : 'top-end';
const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start';
const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end';
const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start';
const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start';
const Default = {
offset: [0, 2],
2020-12-07 16:50:24 +01:00
boundary: 'clippingParents',
2018-11-13 07:41:12 +01:00
reference: 'toggle',
2019-08-27 15:03:21 +02:00
display: 'dynamic',
popperConfig: null,
autoClose: true
2018-11-13 07:41:12 +01:00
};
2021-03-23 17:26:54 +01:00
const DefaultType = {
offset: '(array|string|function)',
2018-11-13 07:41:12 +01:00
boundary: '(string|element)',
reference: '(string|element|object)',
2019-08-27 15:03:21 +02:00
display: 'string',
popperConfig: '(null|object|function)',
autoClose: '(boolean|string)'
2018-11-13 07:41:12 +01:00
};
2019-10-08 08:39:10 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-13 21:48:34 +02:00
2021-03-23 17:26:54 +01:00
class Dropdown extends BaseComponent__default['default'] {
constructor(element, config) {
super(element);
this._popper = null;
this._config = this._getConfig(config);
this._menu = this._getMenuElement();
this._inNavbar = this._detectNavbar();
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
this._addEventListeners();
} // Getters
2020-12-03 15:18:59 +01:00
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
static get Default() {
return Default;
}
2019-03-01 17:31:34 +01:00
2021-03-23 17:26:54 +01:00
static get DefaultType() {
return DefaultType;
}
2015-08-13 06:12:03 +02:00
2021-03-23 17:26:54 +01:00
static get DATA_KEY() {
return DATA_KEY;
} // Public
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
toggle() {
if (isDisabled(this._element)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
const isActive = this._element.classList.contains(CLASS_NAME_SHOW);
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
if (isActive) {
this.hide();
2018-11-13 07:41:12 +01:00
return;
}
2017-10-30 00:19:14 +01:00
2019-08-27 15:03:21 +02:00
this.show();
2021-03-23 17:26:54 +01:00
}
2019-08-27 15:03:21 +02:00
2021-03-23 17:26:54 +01:00
show() {
if (isDisabled(this._element) || this._menu.classList.contains(CLASS_NAME_SHOW)) {
2019-08-27 15:03:21 +02:00
return;
}
2021-03-23 17:26:54 +01:00
const parent = Dropdown.getParentFromElement(this._element);
const relatedTarget = {
2018-11-13 07:41:12 +01:00
relatedTarget: this._element
};
2021-03-23 17:26:54 +01:00
const showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW, relatedTarget);
2017-10-30 00:19:14 +01:00
2019-03-01 17:31:34 +01:00
if (showEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
2020-11-23 14:17:16 +01:00
} // Totally disable Popper for Dropdowns in Navbar
2017-09-06 06:05:12 +02:00
2017-03-20 03:03:32 +01:00
if (this._inNavbar) {
Manipulator__default['default'].setDataAttribute(this._menu, 'popper', 'none');
} else {
2020-12-07 16:50:24 +01:00
if (typeof Popper__namespace === 'undefined') {
2020-11-23 14:17:16 +01:00
throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)');
2018-11-13 07:41:12 +01:00
}
2018-02-11 23:53:29 +01:00
2021-03-23 17:26:54 +01:00
let referenceElement = this._element;
2017-12-23 01:21:54 +01:00
2018-11-13 07:41:12 +01:00
if (this._config.reference === 'parent') {
referenceElement = parent;
2019-03-01 17:31:34 +01:00
} else if (isElement(this._config.reference)) {
2018-11-13 07:41:12 +01:00
referenceElement = this._config.reference; // Check if it's jQuery element
2017-12-23 01:21:54 +01:00
2018-11-13 07:41:12 +01:00
if (typeof this._config.reference.jquery !== 'undefined') {
referenceElement = this._config.reference[0];
2018-07-24 02:51:14 +02:00
}
} else if (typeof this._config.reference === 'object') {
referenceElement = this._config.reference;
2018-07-24 02:51:14 +02:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
const popperConfig = this._getPopperConfig();
2021-03-23 17:26:54 +01:00
const isDisplayStatic = popperConfig.modifiers.find(modifier => modifier.name === 'applyStyles' && modifier.enabled === false);
this._popper = Popper__namespace.createPopper(referenceElement, this._menu, popperConfig);
if (isDisplayStatic) {
Manipulator__default['default'].setDataAttribute(this._menu, 'popper', 'static');
}
2018-11-13 07:41:12 +01:00
} // If this is a touch-enabled device we add extra
// empty mouseover listeners to the body's immediate children;
// only needed because of broken event delegation on iOS
// https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
2017-09-30 23:28:03 +02:00
2020-05-13 20:53:43 +02:00
if ('ontouchstart' in document.documentElement && !parent.closest(SELECTOR_NAVBAR_NAV)) {
[].concat(...document.body.children).forEach(elem => EventHandler__default['default'].on(elem, 'mouseover', noop));
2018-11-13 07:41:12 +01:00
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._element.focus();
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._element.setAttribute('aria-expanded', true);
2015-05-13 21:48:34 +02:00
this._menu.classList.toggle(CLASS_NAME_SHOW);
this._element.classList.toggle(CLASS_NAME_SHOW);
EventHandler__default['default'].trigger(this._element, EVENT_SHOWN, relatedTarget);
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
hide() {
if (isDisabled(this._element) || !this._menu.classList.contains(CLASS_NAME_SHOW)) {
2018-11-13 07:41:12 +01:00
return;
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
const relatedTarget = {
2018-11-13 07:41:12 +01:00
relatedTarget: this._element
2018-07-24 02:51:14 +02:00
};
2017-09-30 23:28:03 +02:00
this._completeHide(relatedTarget);
2021-03-23 17:26:54 +01:00
}
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
dispose() {
2018-11-13 07:41:12 +01:00
this._menu = null;
2017-09-30 23:28:03 +02:00
2019-08-27 15:03:21 +02:00
if (this._popper) {
2018-11-13 07:41:12 +01:00
this._popper.destroy();
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._popper = null;
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
super.dispose();
}
update() {
2018-11-13 07:41:12 +01:00
this._inNavbar = this._detectNavbar();
2017-09-30 23:28:03 +02:00
2019-08-27 15:03:21 +02:00
if (this._popper) {
2020-12-07 16:50:24 +01:00
this._popper.update();
2018-11-13 07:41:12 +01:00
}
2019-01-04 17:29:45 +01:00
} // Private
2017-10-16 00:51:44 +02:00
2021-03-23 17:26:54 +01:00
_addEventListeners() {
EventHandler__default['default'].on(this._element, EVENT_CLICK, event => {
2018-11-13 07:41:12 +01:00
event.preventDefault();
2021-03-23 17:26:54 +01:00
this.toggle();
2018-11-13 07:41:12 +01:00
});
2021-03-23 17:26:54 +01:00
}
2018-02-19 23:50:56 +01:00
_completeHide(relatedTarget) {
const hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE, relatedTarget);
if (hideEvent.defaultPrevented) {
return;
} // If this is a touch-enabled device we remove the extra
// empty mouseover listeners we added for iOS support
if ('ontouchstart' in document.documentElement) {
[].concat(...document.body.children).forEach(elem => EventHandler__default['default'].off(elem, 'mouseover', noop));
}
if (this._popper) {
this._popper.destroy();
}
this._menu.classList.remove(CLASS_NAME_SHOW);
this._element.classList.remove(CLASS_NAME_SHOW);
this._element.setAttribute('aria-expanded', 'false');
Manipulator__default['default'].removeDataAttribute(this._menu, 'popper');
EventHandler__default['default'].trigger(this._element, EVENT_HIDDEN, relatedTarget);
}
2021-03-23 17:26:54 +01:00
_getConfig(config) {
config = { ...this.constructor.Default,
...Manipulator__default['default'].getDataAttributes(this._element),
...config
};
2019-03-01 17:31:34 +01:00
typeCheckConfig(NAME, config, this.constructor.DefaultType);
if (typeof config.reference === 'object' && !isElement(config.reference) && typeof config.reference.getBoundingClientRect !== 'function') {
// Popper virtual elements require a getBoundingClientRect method
2021-03-23 17:26:54 +01:00
throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`);
}
2018-11-13 07:41:12 +01:00
return config;
2021-03-23 17:26:54 +01:00
}
2018-02-19 23:50:56 +01:00
2021-03-23 17:26:54 +01:00
_getMenuElement() {
2020-09-14 17:12:06 +02:00
return SelectorEngine__default['default'].next(this._element, SELECTOR_MENU)[0];
2021-03-23 17:26:54 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
_getPlacement() {
const parentDropdown = this._element.parentNode;
2017-09-30 23:28:03 +02:00
2020-12-07 16:50:24 +01:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
return PLACEMENT_RIGHT;
2018-11-13 07:41:12 +01:00
}
2018-07-24 02:51:14 +02:00
2020-12-07 16:50:24 +01:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
return PLACEMENT_LEFT;
} // We need to trim the value because custom properties can also include spaces
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end';
2018-11-13 07:41:12 +01:00
2020-12-07 16:50:24 +01:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP;
2018-11-13 07:41:12 +01:00
}
2018-07-24 02:51:14 +02:00
2020-12-07 16:50:24 +01:00
return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM;
2021-03-23 17:26:54 +01:00
}
2021-03-23 17:26:54 +01:00
_detectNavbar() {
return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null;
}
2021-03-23 17:26:54 +01:00
_getOffset() {
const {
offset
} = this._config;
if (typeof offset === 'string') {
2021-03-23 17:26:54 +01:00
return offset.split(',').map(val => Number.parseInt(val, 10));
}
if (typeof offset === 'function') {
2021-03-23 17:26:54 +01:00
return popperData => offset(popperData, this._element);
}
return offset;
2021-03-23 17:26:54 +01:00
}
2021-03-23 17:26:54 +01:00
_getPopperConfig() {
const defaultBsPopperConfig = {
2018-11-13 07:41:12 +01:00
placement: this._getPlacement(),
2020-12-07 16:50:24 +01:00
modifiers: [{
name: 'preventOverflow',
options: {
boundary: this._config.boundary
}
}, {
name: 'offset',
options: {
offset: this._getOffset()
2017-09-06 06:05:12 +02:00
}
2020-12-07 16:50:24 +01:00
}]
2020-11-23 14:17:16 +01:00
}; // Disable Popper if we have a static display
2017-06-18 09:14:35 +02:00
2018-11-13 07:41:12 +01:00
if (this._config.display === 'static') {
defaultBsPopperConfig.modifiers = [{
2020-12-07 16:50:24 +01:00
name: 'applyStyles',
2018-11-13 07:41:12 +01:00
enabled: false
2020-12-07 16:50:24 +01:00
}];
2018-11-13 07:41:12 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
return { ...defaultBsPopperConfig,
...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
};
}
_selectMenuItem(event) {
const items = SelectorEngine__default['default'].find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(isVisible);
if (!items.length) {
return;
}
let index = items.indexOf(event.target); // Up
if (event.key === ARROW_UP_KEY && index > 0) {
index--;
} // Down
if (event.key === ARROW_DOWN_KEY && index < items.length - 1) {
index++;
} // index is -1 if the first keydown is an ArrowUp
index = index === -1 ? 0 : index;
items[index].focus();
2019-01-04 17:29:45 +01:00
} // Static
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
static dropdownInterface(element, config) {
let data = Data__default['default'].get(element, DATA_KEY);
const _config = typeof config === 'object' ? config : null;
2015-05-10 22:47:11 +02:00
2019-03-01 17:31:34 +01:00
if (!data) {
data = new Dropdown(element, _config);
}
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
2021-03-23 17:26:54 +01:00
throw new TypeError(`No method named "${config}"`);
2018-11-13 07:41:12 +01:00
}
2017-05-16 09:59:44 +02:00
2019-03-01 17:31:34 +01:00
data[config]();
}
2021-03-23 17:26:54 +01:00
}
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
static jQueryInterface(config) {
2019-03-01 17:31:34 +01:00
return this.each(function () {
2019-08-27 15:03:21 +02:00
Dropdown.dropdownInterface(this, config);
2018-11-13 07:41:12 +01:00
});
2021-03-23 17:26:54 +01:00
}
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
static clearMenus(event) {
if (event) {
if (event.button === RIGHT_MOUSE_BUTTON || event.type === 'keyup' && event.key !== TAB_KEY) {
return;
}
if (/input|select|option|textarea|form/i.test(event.target.tagName)) {
2021-03-23 17:26:54 +01:00
return;
}
2018-11-13 07:41:12 +01:00
}
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
const toggles = SelectorEngine__default['default'].find(SELECTOR_DATA_TOGGLE);
2017-03-20 03:03:32 +01:00
2021-03-23 17:26:54 +01:00
for (let i = 0, len = toggles.length; i < len; i++) {
const context = Data__default['default'].get(toggles[i], DATA_KEY);
2017-09-30 23:28:03 +02:00
if (!context || context._config.autoClose === false) {
2018-11-13 07:41:12 +01:00
continue;
2018-07-24 02:51:14 +02:00
}
2015-05-10 22:47:11 +02:00
if (!context._element.classList.contains(CLASS_NAME_SHOW)) {
2018-11-13 07:41:12 +01:00
continue;
}
2017-12-31 04:41:36 +01:00
const relatedTarget = {
relatedTarget: context._element
};
2021-03-23 17:26:54 +01:00
if (event) {
const composedPath = event.composedPath();
const isMenuTarget = composedPath.includes(context._menu);
if (composedPath.includes(context._element) || context._config.autoClose === 'inside' && !isMenuTarget || context._config.autoClose === 'outside' && isMenuTarget) {
2021-03-23 17:26:54 +01:00
continue;
} // Tab navigation through the dropdown menu shouldn't close the menu
if (event.type === 'keyup' && event.key === TAB_KEY && context._menu.contains(event.target)) {
2021-03-23 17:26:54 +01:00
continue;
}
2019-08-27 15:03:21 +02:00
if (event.type === 'click') {
relatedTarget.clickEvent = event;
}
2019-08-27 15:03:21 +02:00
}
context._completeHide(relatedTarget);
2018-11-13 07:41:12 +01:00
}
2021-03-23 17:26:54 +01:00
}
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
static getParentFromElement(element) {
2019-08-27 15:03:21 +02:00
return getElementFromSelector(element) || element.parentNode;
2021-03-23 17:26:54 +01:00
}
2015-05-10 22:47:11 +02:00
2021-03-23 17:26:54 +01:00
static dataApiKeydownHandler(event) {
2018-11-13 07:41:12 +01:00
// If not input/textarea:
// - And not a key in REGEXP_KEYDOWN => not a dropdown command
// If input/textarea:
// - If space key => not a dropdown command
// - If key is other than escape
// - If key is not up or down => not a dropdown command
// - If trigger inside the menu => not a dropdown command
2020-05-13 20:53:43 +02:00
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)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-10 22:47:11 +02:00
const isActive = this.classList.contains(CLASS_NAME_SHOW);
if (!isActive && event.key === ESCAPE_KEY) {
return;
}
2018-11-13 07:41:12 +01:00
event.preventDefault();
event.stopPropagation();
2015-05-10 22:47:11 +02:00
if (isDisabled(this)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-08-19 05:28:28 +02:00
const getToggleButton = () => this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default['default'].prev(this, SELECTOR_DATA_TOGGLE)[0];
2015-08-19 05:28:28 +02:00
2020-05-13 20:53:43 +02:00
if (event.key === ESCAPE_KEY) {
getToggleButton().focus();
2020-03-28 11:29:08 +01:00
Dropdown.clearMenus();
return;
}
2015-05-10 22:47:11 +02:00
if (!isActive && (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY)) {
getToggleButton().click();
return;
}
2020-05-13 20:53:43 +02:00
if (!isActive || event.key === SPACE_KEY) {
2019-08-27 15:03:21 +02:00
Dropdown.clearMenus();
2018-11-13 07:41:12 +01:00
return;
}
2017-09-30 23:28:03 +02:00
Dropdown.getInstance(getToggleButton())._selectMenuItem(event);
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown.dataApiKeydownHandler);
EventHandler__default['default'].on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown.dataApiKeydownHandler);
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus);
EventHandler__default['default'].on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus);
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
2018-11-13 07:41:12 +01:00
event.preventDefault();
2021-03-23 17:26:54 +01:00
Dropdown.dropdownInterface(this);
2018-11-13 07:41:12 +01:00
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 18:07:37 +01:00
* add .Dropdown to jQuery only if jQuery is present
2018-11-13 07:41:12 +01:00
*/
defineJQueryPlugin(NAME, Dropdown);
2015-05-10 22:47:11 +02:00
return Dropdown;
2018-07-24 02:51:14 +02:00
2019-11-08 09:11:23 +01:00
})));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=dropdown.js.map