diff options
Diffstat (limited to 'js/dist/dropdown.js')
| -rw-r--r-- | js/dist/dropdown.js | 727 |
1 files changed, 342 insertions, 385 deletions
diff --git a/js/dist/dropdown.js b/js/dist/dropdown.js index 07cb3fc3d..fe8bec5c7 100644 --- a/js/dist/dropdown.js +++ b/js/dist/dropdown.js @@ -1,13 +1,13 @@ /*! - * Bootstrap dropdown.js v5.0.0-beta2 (https://getbootstrap.com/) + * Bootstrap dropdown.js v5.1.0 (https://getbootstrap.com/) * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors) * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ (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'; + 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')) : + typeof define === 'function' && define.amd ? define(['@popperjs/core', './dom/event-handler', './dom/manipulator', './dom/selector-engine', './base-component'], factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global.Popper, global.EventHandler, global.Manipulator, global.SelectorEngine, global.Base)); +}(this, (function (Popper, EventHandler, Manipulator, SelectorEngine, BaseComponent) { 'use strict'; function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } @@ -32,82 +32,31 @@ } var Popper__namespace = /*#__PURE__*/_interopNamespace(Popper); - 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); - function _defineProperties(target, props) { - for (var i = 0; i < props.length; i++) { - var descriptor = props[i]; - descriptor.enumerable = descriptor.enumerable || false; - descriptor.configurable = true; - if ("value" in descriptor) descriptor.writable = true; - Object.defineProperty(target, descriptor.key, descriptor); - } - } - - function _createClass(Constructor, protoProps, staticProps) { - if (protoProps) _defineProperties(Constructor.prototype, protoProps); - if (staticProps) _defineProperties(Constructor, staticProps); - return Constructor; - } - - function _extends() { - _extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; - - for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } - - return target; - }; - - return _extends.apply(this, arguments); - } - - function _inheritsLoose(subClass, superClass) { - subClass.prototype = Object.create(superClass.prototype); - subClass.prototype.constructor = subClass; - - _setPrototypeOf(subClass, superClass); - } - - function _setPrototypeOf(o, p) { - _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { - o.__proto__ = p; - return o; - }; - - return _setPrototypeOf(o, p); - } - /** * -------------------------------------------------------------------------- - * Bootstrap (v5.0.0-beta2): util/index.js + * Bootstrap (v5.1.0): util/index.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ - var toType = function toType(obj) { + const toType = obj => { if (obj === null || obj === undefined) { - return "" + obj; + return `${obj}`; } return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase(); }; - var getSelector = function getSelector(element) { - var selector = element.getAttribute('data-bs-target'); + const getSelector = element => { + let selector = element.getAttribute('data-bs-target'); if (!selector || selector === '#') { - var hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes, + 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 @@ -118,7 +67,7 @@ if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) { - hrefAttr = '#' + hrefAttr.split('#')[1]; + hrefAttr = `#${hrefAttr.split('#')[1]}`; } selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null; @@ -127,48 +76,78 @@ return selector; }; - var getElementFromSelector = function getElementFromSelector(element) { - var selector = getSelector(element); + const getElementFromSelector = element => { + const selector = getSelector(element); return selector ? document.querySelector(selector) : null; }; - var isElement = function isElement(obj) { - return (obj[0] || obj).nodeType; + const isElement = obj => { + if (!obj || typeof obj !== 'object') { + return false; + } + + if (typeof obj.jquery !== 'undefined') { + obj = obj[0]; + } + + return typeof obj.nodeType !== 'undefined'; + }; + + const getElement = obj => { + if (isElement(obj)) { + // it's a jQuery object or a node element + return obj.jquery ? obj[0] : obj; + } + + if (typeof obj === 'string' && obj.length > 0) { + return document.querySelector(obj); + } + + return null; }; - var typeCheckConfig = function typeCheckConfig(componentName, config, configTypes) { - Object.keys(configTypes).forEach(function (property) { - var expectedTypes = configTypes[property]; - var value = config[property]; - var valueType = value && isElement(value) ? 'element' : toType(value); + 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); if (!new RegExp(expectedTypes).test(valueType)) { - throw new TypeError(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\".")); + throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`); } }); }; - var isVisible = function isVisible(element) { - if (!element) { + const isVisible = element => { + if (!isElement(element) || element.getClientRects().length === 0) { return false; } - if (element.style && element.parentNode && element.parentNode.style) { - var elementStyle = getComputedStyle(element); - var parentNodeStyle = getComputedStyle(element.parentNode); - return elementStyle.display !== 'none' && parentNodeStyle.display !== 'none' && elementStyle.visibility !== 'hidden'; + return getComputedStyle(element).getPropertyValue('visibility') === 'visible'; + }; + + const isDisabled = element => { + if (!element || element.nodeType !== Node.ELEMENT_NODE) { + return true; + } + + if (element.classList.contains('disabled')) { + return true; } - return false; - }; + if (typeof element.disabled !== 'undefined') { + return element.disabled; + } - var noop = function noop() { - return function () {}; + return element.hasAttribute('disabled') && element.getAttribute('disabled') !== 'false'; }; - var getjQuery = function getjQuery() { - var _window = window, - jQuery = _window.jQuery; + const noop = () => {}; + + const getjQuery = () => { + const { + jQuery + } = window; if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) { return jQuery; @@ -177,92 +156,132 @@ return null; }; - var onDOMContentLoaded = function onDOMContentLoaded(callback) { + const DOMContentLoadedCallbacks = []; + + const onDOMContentLoaded = callback => { if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', callback); + // add listener on the first call when the document is in loading state + if (!DOMContentLoadedCallbacks.length) { + document.addEventListener('DOMContentLoaded', () => { + DOMContentLoadedCallbacks.forEach(callback => callback()); + }); + } + + DOMContentLoadedCallbacks.push(callback); } else { callback(); } }; - var isRTL = document.documentElement.dir === 'rtl'; + const isRTL = () => document.documentElement.dir === 'rtl'; - var defineJQueryPlugin = function defineJQueryPlugin(name, plugin) { - onDOMContentLoaded(function () { - var $ = getjQuery(); + const defineJQueryPlugin = plugin => { + onDOMContentLoaded(() => { + const $ = getjQuery(); /* istanbul ignore if */ if ($) { - var JQUERY_NO_CONFLICT = $.fn[name]; + const name = plugin.NAME; + const JQUERY_NO_CONFLICT = $.fn[name]; $.fn[name] = plugin.jQueryInterface; $.fn[name].Constructor = plugin; - $.fn[name].noConflict = function () { + $.fn[name].noConflict = () => { $.fn[name] = JQUERY_NO_CONFLICT; return plugin.jQueryInterface; }; } }); }; + /** + * Return the previous/next element of a list. + * + * @param {array} list The list of elements + * @param activeElement The active element + * @param shouldGetNext Choose to get next or previous element + * @param isCycleAllowed + * @return {Element|elem} The proper element + */ + + + const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed) => { + 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 + + if (index === -1) { + return list[!shouldGetNext && isCycleAllowed ? list.length - 1 : 0]; + } + const listLength = list.length; + index += shouldGetNext ? 1 : -1; + + if (isCycleAllowed) { + index = (index + listLength) % listLength; + } + + return list[Math.max(0, Math.min(index, listLength - 1))]; + }; + + /** + * -------------------------------------------------------------------------- + * Bootstrap (v5.1.0): dropdown.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) + * -------------------------------------------------------------------------- + */ /** * ------------------------------------------------------------------------ * Constants * ------------------------------------------------------------------------ */ - var NAME = 'dropdown'; - var DATA_KEY = 'bs.dropdown'; - var EVENT_KEY = "." + DATA_KEY; - var DATA_API_KEY = '.data-api'; - var ESCAPE_KEY = 'Escape'; - var SPACE_KEY = 'Space'; - var TAB_KEY = 'Tab'; - var ARROW_UP_KEY = 'ArrowUp'; - var ARROW_DOWN_KEY = 'ArrowDown'; - var RIGHT_MOUSE_BUTTON = 2; // MouseEvent.button value for the secondary button, usually the right button - - var REGEXP_KEYDOWN = new RegExp(ARROW_UP_KEY + "|" + ARROW_DOWN_KEY + "|" + ESCAPE_KEY); - var EVENT_HIDE = "hide" + EVENT_KEY; - var EVENT_HIDDEN = "hidden" + EVENT_KEY; - var EVENT_SHOW = "show" + EVENT_KEY; - var EVENT_SHOWN = "shown" + EVENT_KEY; - var EVENT_CLICK = "click" + EVENT_KEY; - var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY; - var EVENT_KEYDOWN_DATA_API = "keydown" + EVENT_KEY + DATA_API_KEY; - var EVENT_KEYUP_DATA_API = "keyup" + EVENT_KEY + DATA_API_KEY; - var CLASS_NAME_DISABLED = 'disabled'; - var CLASS_NAME_SHOW = 'show'; - var CLASS_NAME_DROPUP = 'dropup'; - var CLASS_NAME_DROPEND = 'dropend'; - var CLASS_NAME_DROPSTART = 'dropstart'; - var CLASS_NAME_NAVBAR = 'navbar'; - var SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]'; - var SELECTOR_FORM_CHILD = '.dropdown form'; - var SELECTOR_MENU = '.dropdown-menu'; - var SELECTOR_NAVBAR_NAV = '.navbar-nav'; - var SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'; - var PLACEMENT_TOP = isRTL ? 'top-end' : 'top-start'; - var PLACEMENT_TOPEND = isRTL ? 'top-start' : 'top-end'; - var PLACEMENT_BOTTOM = isRTL ? 'bottom-end' : 'bottom-start'; - var PLACEMENT_BOTTOMEND = isRTL ? 'bottom-start' : 'bottom-end'; - var PLACEMENT_RIGHT = isRTL ? 'left-start' : 'right-start'; - var PLACEMENT_LEFT = isRTL ? 'right-start' : 'left-start'; - var Default = { + 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_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], - flip: true, boundary: 'clippingParents', reference: 'toggle', display: 'dynamic', - popperConfig: null + popperConfig: null, + autoClose: true }; - var DefaultType = { + const DefaultType = { offset: '(array|string|function)', - flip: 'boolean', boundary: '(string|element)', reference: '(string|element|object)', display: 'string', - popperConfig: '(null|object|function)' + popperConfig: '(null|object|function)', + autoClose: '(boolean|string)' }; /** * ------------------------------------------------------------------------ @@ -270,90 +289,53 @@ * ------------------------------------------------------------------------ */ - var Dropdown = /*#__PURE__*/function (_BaseComponent) { - _inheritsLoose(Dropdown, _BaseComponent); - - function Dropdown(element, config) { - var _this; - - _this = _BaseComponent.call(this, element) || this; - _this._popper = null; - _this._config = _this._getConfig(config); - _this._menu = _this._getMenuElement(); - _this._inNavbar = _this._detectNavbar(); - - _this._addEventListeners(); - - return _this; + 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(); } // Getters - var _proto = Dropdown.prototype; - - // Public - _proto.toggle = function toggle() { - if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED)) { - return; - } + static get Default() { + return Default; + } - var isActive = this._element.classList.contains(CLASS_NAME_SHOW); + static get DefaultType() { + return DefaultType; + } - Dropdown.clearMenus(); + static get NAME() { + return NAME; + } // Public - if (isActive) { - return; - } - this.show(); - }; + toggle() { + return this._isShown() ? this.hide() : this.show(); + } - _proto.show = function show() { - if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || this._menu.classList.contains(CLASS_NAME_SHOW)) { + show() { + if (isDisabled(this._element) || this._isShown(this._menu)) { return; } - var parent = Dropdown.getParentFromElement(this._element); - var relatedTarget = { + const relatedTarget = { relatedTarget: this._element }; - var showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW, relatedTarget); + const showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW, relatedTarget); if (showEvent.defaultPrevented) { return; - } // Totally disable Popper for Dropdowns in Navbar + } + const parent = Dropdown.getParentFromElement(this._element); // Totally disable Popper for Dropdowns in Navbar if (this._inNavbar) { Manipulator__default['default'].setDataAttribute(this._menu, 'popper', 'none'); } else { - if (typeof Popper__namespace === 'undefined') { - throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)'); - } - - var referenceElement = this._element; - - if (this._config.reference === 'parent') { - referenceElement = parent; - } else if (isElement(this._config.reference)) { - referenceElement = this._config.reference; // Check if it's jQuery element - - if (typeof this._config.reference.jquery !== 'undefined') { - referenceElement = this._config.reference[0]; - } - } else if (typeof this._config.reference === 'object') { - referenceElement = this._config.reference; - } - - var popperConfig = this._getPopperConfig(); - - var isDisplayStatic = popperConfig.modifiers.find(function (modifier) { - return modifier.name === 'applyStyles' && modifier.enabled === false; - }); - this._popper = Popper.createPopper(referenceElement, this._menu, popperConfig); - - if (isDisplayStatic) { - Manipulator__default['default'].setDataAttribute(this._menu, 'popper', 'static'); - } + this._createPopper(parent); } // 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 @@ -361,101 +343,126 @@ if ('ontouchstart' in document.documentElement && !parent.closest(SELECTOR_NAVBAR_NAV)) { - var _ref; - - (_ref = []).concat.apply(_ref, document.body.children).forEach(function (elem) { - return EventHandler__default['default'].on(elem, 'mouseover', null, noop()); - }); + [].concat(...document.body.children).forEach(elem => EventHandler__default['default'].on(elem, 'mouseover', noop)); } this._element.focus(); this._element.setAttribute('aria-expanded', true); - this._menu.classList.toggle(CLASS_NAME_SHOW); + this._menu.classList.add(CLASS_NAME_SHOW); - this._element.classList.toggle(CLASS_NAME_SHOW); + this._element.classList.add(CLASS_NAME_SHOW); EventHandler__default['default'].trigger(this._element, EVENT_SHOWN, relatedTarget); - }; + } - _proto.hide = function hide() { - if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || !this._menu.classList.contains(CLASS_NAME_SHOW)) { + hide() { + if (isDisabled(this._element) || !this._isShown(this._menu)) { return; } - var relatedTarget = { + const relatedTarget = { relatedTarget: this._element }; - var hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE, relatedTarget); - if (hideEvent.defaultPrevented) { - return; - } + this._completeHide(relatedTarget); + } + dispose() { if (this._popper) { this._popper.destroy(); } - this._menu.classList.toggle(CLASS_NAME_SHOW); + super.dispose(); + } - this._element.classList.toggle(CLASS_NAME_SHOW); + update() { + this._inNavbar = this._detectNavbar(); - Manipulator__default['default'].removeDataAttribute(this._menu, 'popper'); - EventHandler__default['default'].trigger(this._element, EVENT_HIDDEN, relatedTarget); - }; + if (this._popper) { + this._popper.update(); + } + } // Private - _proto.dispose = function dispose() { - _BaseComponent.prototype.dispose.call(this); - EventHandler__default['default'].off(this._element, EVENT_KEY); - this._menu = null; + _completeHide(relatedTarget) { + const hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE, relatedTarget); - if (this._popper) { - this._popper.destroy(); + if (hideEvent.defaultPrevented) { + return; + } // If this is a touch-enabled device we remove the extra + // empty mouseover listeners we added for iOS support - this._popper = null; - } - }; - _proto.update = function update() { - this._inNavbar = this._detectNavbar(); + if ('ontouchstart' in document.documentElement) { + [].concat(...document.body.children).forEach(elem => EventHandler__default['default'].off(elem, 'mouseover', noop)); + } if (this._popper) { - this._popper.update(); + this._popper.destroy(); } - } // Private - ; - _proto._addEventListeners = function _addEventListeners() { - var _this2 = this; + this._menu.classList.remove(CLASS_NAME_SHOW); - EventHandler__default['default'].on(this._element, EVENT_CLICK, function (event) { - event.preventDefault(); - event.stopPropagation(); + this._element.classList.remove(CLASS_NAME_SHOW); - _this2.toggle(); - }); - }; + this._element.setAttribute('aria-expanded', 'false'); + + Manipulator__default['default'].removeDataAttribute(this._menu, 'popper'); + EventHandler__default['default'].trigger(this._element, EVENT_HIDDEN, relatedTarget); + } - _proto._getConfig = function _getConfig(config) { - config = _extends({}, this.constructor.Default, Manipulator__default['default'].getDataAttributes(this._element), config); + _getConfig(config) { + config = { ...this.constructor.Default, + ...Manipulator__default['default'].getDataAttributes(this._element), + ...config + }; 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 - throw new TypeError(NAME.toUpperCase() + ": Option \"reference\" provided type \"object\" without a required \"getBoundingClientRect\" method."); + throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`); } return config; - }; + } + + _createPopper(parent) { + if (typeof Popper__namespace === 'undefined') { + throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)'); + } + + let referenceElement = this._element; + + if (this._config.reference === 'parent') { + referenceElement = parent; + } else if (isElement(this._config.reference)) { + referenceElement = getElement(this._config.reference); + } else if (typeof this._config.reference === 'object') { + referenceElement = this._config.reference; + } + + const popperConfig = this._getPopperConfig(); + + 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'); + } + } - _proto._getMenuElement = function _getMenuElement() { + _isShown(element = this._element) { + return element.classList.contains(CLASS_NAME_SHOW); + } + + _getMenuElement() { return SelectorEngine__default['default'].next(this._element, SELECTOR_MENU)[0]; - }; + } - _proto._getPlacement = function _getPlacement() { - var parentDropdown = this._element.parentNode; + _getPlacement() { + const parentDropdown = this._element.parentNode; if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) { return PLACEMENT_RIGHT; @@ -466,46 +473,41 @@ } // We need to trim the value because custom properties can also include spaces - var isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end'; + const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end'; if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) { return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP; } return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM; - }; - - _proto._detectNavbar = function _detectNavbar() { - return this._element.closest("." + CLASS_NAME_NAVBAR) !== null; - }; + } - _proto._getOffset = function _getOffset() { - var _this3 = this; + _detectNavbar() { + return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null; + } - var offset = this._config.offset; + _getOffset() { + const { + offset + } = this._config; if (typeof offset === 'string') { - return offset.split(',').map(function (val) { - return Number.parseInt(val, 10); - }); + return offset.split(',').map(val => Number.parseInt(val, 10)); } if (typeof offset === 'function') { - return function (popperData) { - return offset(popperData, _this3._element); - }; + return popperData => offset(popperData, this._element); } return offset; - }; + } - _proto._getPopperConfig = function _getPopperConfig() { - var defaultBsPopperConfig = { + _getPopperConfig() { + const defaultBsPopperConfig = { placement: this._getPlacement(), modifiers: [{ name: 'preventOverflow', options: { - altBoundary: this._config.flip, boundary: this._config.boundary } }, { @@ -523,99 +525,92 @@ }]; } - return _extends({}, defaultBsPopperConfig, typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig); + return { ...defaultBsPopperConfig, + ...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig) + }; + } + + _selectMenuItem({ + key, + target + }) { + const items = SelectorEngine__default['default'].find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(isVisible); + + if (!items.length) { + return; + } // if target isn't included in items (e.g. when expanding the dropdown) + // allow cycling to get the last item in case key equals ARROW_UP_KEY + + + getNextActiveElement(items, target, key === ARROW_DOWN_KEY, !items.includes(target)).focus(); } // Static - ; - Dropdown.dropdownInterface = function dropdownInterface(element, config) { - var data = Data__default['default'].getData(element, DATA_KEY); - var _config = typeof config === 'object' ? config : null; + static jQueryInterface(config) { + return this.each(function () { + const data = Dropdown.getOrCreateInstance(this, config); - if (!data) { - data = new Dropdown(element, _config); - } + if (typeof config !== 'string') { + return; + } - if (typeof config === 'string') { if (typeof data[config] === 'undefined') { - throw new TypeError("No method named \"" + config + "\""); + throw new TypeError(`No method named "${config}"`); } data[config](); - } - }; - - Dropdown.jQueryInterface = function jQueryInterface(config) { - return this.each(function () { - Dropdown.dropdownInterface(this, config); }); - }; + } - Dropdown.clearMenus = function clearMenus(event) { + static clearMenus(event) { if (event && (event.button === RIGHT_MOUSE_BUTTON || event.type === 'keyup' && event.key !== TAB_KEY)) { return; } - var toggles = SelectorEngine__default['default'].find(SELECTOR_DATA_TOGGLE); - - for (var i = 0, len = toggles.length; i < len; i++) { - var context = Data__default['default'].getData(toggles[i], DATA_KEY); - var relatedTarget = { - relatedTarget: toggles[i] - }; - - if (event && event.type === 'click') { - relatedTarget.clickEvent = event; - } - - if (!context) { - continue; - } + const toggles = SelectorEngine__default['default'].find(SELECTOR_DATA_TOGGLE); - var dropdownMenu = context._menu; + for (let i = 0, len = toggles.length; i < len; i++) { + const context = Dropdown.getInstance(toggles[i]); - if (!toggles[i].classList.contains(CLASS_NAME_SHOW)) { + if (!context || context._config.autoClose === false) { continue; } - if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.key === TAB_KEY) && dropdownMenu.contains(event.target)) { + if (!context._isShown()) { continue; } - var hideEvent = EventHandler__default['default'].trigger(toggles[i], EVENT_HIDE, relatedTarget); - - if (hideEvent.defaultPrevented) { - continue; - } // If this is a touch-enabled device we remove the extra - // empty mouseover listeners we added for iOS support + const relatedTarget = { + relatedTarget: context._element + }; + if (event) { + const composedPath = event.composedPath(); + const isMenuTarget = composedPath.includes(context._menu); - if ('ontouchstart' in document.documentElement) { - var _ref2; + if (composedPath.includes(context._element) || context._config.autoClose === 'inside' && !isMenuTarget || context._config.autoClose === 'outside' && isMenuTarget) { + continue; + } // Tab navigation through the dropdown menu or events from contained inputs shouldn't close the menu - (_ref2 = []).concat.apply(_ref2, document.body.children).forEach(function (elem) { - return EventHandler__default['default'].off(elem, 'mouseover', null, noop()); - }); - } - toggles[i].setAttribute('aria-expanded', 'false'); + if (context._menu.contains(event.target) && (event.type === 'keyup' && event.key === TAB_KEY || /input|select|option|textarea|form/i.test(event.target.tagName))) { + continue; + } - if (context._popper) { - context._popper.destroy(); + if (event.type === 'click') { + relatedTarget.clickEvent = event; + } } - dropdownMenu.classList.remove(CLASS_NAME_SHOW); - toggles[i].classList.remove(CLASS_NAME_SHOW); - Manipulator__default['default'].removeDataAttribute(dropdownMenu, 'popper'); - EventHandler__default['default'].trigger(toggles[i], EVENT_HIDDEN, relatedTarget); + context._completeHide(relatedTarget); } - }; + } - Dropdown.getParentFromElement = function getParentFromElement(element) { + static getParentFromElement(element) { return getElementFromSelector(element) || element.parentNode; - }; + } - Dropdown.dataApiKeydownHandler = function dataApiKeydownHandler(event) { + static dataApiKeydownHandler(event) { // If not input/textarea: // - And not a key in REGEXP_KEYDOWN => not a dropdown command // If input/textarea: @@ -627,77 +622,43 @@ return; } + const isActive = this.classList.contains(CLASS_NAME_SHOW); + + if (!isActive && event.key === ESCAPE_KEY) { + return; + } + event.preventDefault(); event.stopPropagation(); - if (this.disabled || this.classList.contains(CLASS_NAME_DISABLED)) { + if (isDisabled(this)) { return; } - var parent = Dropdown.getParentFromElement(this); - var isActive = this.classList.contains(CLASS_NAME_SHOW); + const getToggleButton = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default['default'].prev(this, SELECTOR_DATA_TOGGLE)[0]; + const instance = Dropdown.getOrCreateInstance(getToggleButton); if (event.key === ESCAPE_KEY) { - var button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default['default'].prev(this, SELECTOR_DATA_TOGGLE)[0]; - button.focus(); - Dropdown.clearMenus(); + instance.hide(); return; } - if (!isActive && (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY)) { - var _button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default['default'].prev(this, SELECTOR_DATA_TOGGLE)[0]; + if (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY) { + if (!isActive) { + instance.show(); + } - _button.click(); + instance._selectMenuItem(event); return; } if (!isActive || event.key === SPACE_KEY) { Dropdown.clearMenus(); - return; - } - - var items = SelectorEngine__default['default'].find(SELECTOR_VISIBLE_ITEMS, parent).filter(isVisible); - - if (!items.length) { - return; } + } - var 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(); - }; - - _createClass(Dropdown, null, [{ - key: "Default", - get: function get() { - return Default; - } - }, { - key: "DefaultType", - get: function get() { - return DefaultType; - } - }, { - key: "DATA_KEY", - get: function get() { - return DATA_KEY; - } - }]); - - return Dropdown; - }(BaseComponent__default['default']); + } /** * ------------------------------------------------------------------------ * Data Api implementation @@ -711,11 +672,7 @@ 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) { event.preventDefault(); - event.stopPropagation(); - Dropdown.dropdownInterface(this, 'toggle'); - }); - EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, function (e) { - return e.stopPropagation(); + Dropdown.getOrCreateInstance(this).toggle(); }); /** * ------------------------------------------------------------------------ @@ -724,7 +681,7 @@ * add .Dropdown to jQuery only if jQuery is present */ - defineJQueryPlugin(NAME, Dropdown); + defineJQueryPlugin(Dropdown); return Dropdown; |
