2015-05-10 13:47:11 -07:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2021-03-23 18:26:54 +02:00
|
|
|
* Bootstrap (v5.0.0-beta3): dropdown.js
|
2020-06-16 21:41:47 +03:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-10 13:47:11 -07:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-06-19 11:17:01 +03:00
|
|
|
import * as Popper from '@popperjs/core'
|
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
import {
|
2020-12-08 07:16:50 +01:00
|
|
|
defineJQueryPlugin,
|
2019-07-23 21:15:00 +02:00
|
|
|
getElementFromSelector,
|
2019-02-23 00:37:55 +02:00
|
|
|
isElement,
|
2019-10-17 15:01:44 +00:00
|
|
|
isVisible,
|
2020-06-26 17:06:20 +03:00
|
|
|
isRTL,
|
2019-02-23 00:37:55 +02:00
|
|
|
noop,
|
|
|
|
typeCheckConfig
|
2019-10-02 11:43:54 +02:00
|
|
|
} from './util/index'
|
|
|
|
import Data from './dom/data'
|
|
|
|
import EventHandler from './dom/event-handler'
|
|
|
|
import Manipulator from './dom/manipulator'
|
|
|
|
import SelectorEngine from './dom/selector-engine'
|
2019-09-04 17:58:29 +03:00
|
|
|
import BaseComponent from './base-component'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-02-26 13:20:34 +02:00
|
|
|
const NAME = 'dropdown'
|
|
|
|
const DATA_KEY = 'bs.dropdown'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2020-03-07 11:31:42 +02:00
|
|
|
|
2020-04-15 16:52:18 +02:00
|
|
|
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
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-04-15 16:52:18 +02:00
|
|
|
const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEY}|${ARROW_DOWN_KEY}|${ESCAPE_KEY}`)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
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_DISABLED = 'disabled'
|
|
|
|
const CLASS_NAME_SHOW = 'show'
|
|
|
|
const CLASS_NAME_DROPUP = 'dropup'
|
2020-06-26 17:06:20 +03:00
|
|
|
const CLASS_NAME_DROPEND = 'dropend'
|
|
|
|
const CLASS_NAME_DROPSTART = 'dropstart'
|
2020-03-07 11:31:42 +02:00
|
|
|
const CLASS_NAME_NAVBAR = 'navbar'
|
|
|
|
|
2020-07-22 22:33:11 +03:00
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]'
|
2020-03-07 11:31:42 +02:00
|
|
|
const SELECTOR_MENU = '.dropdown-menu'
|
|
|
|
const SELECTOR_NAVBAR_NAV = '.navbar-nav'
|
|
|
|
const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'
|
|
|
|
|
2021-02-16 10:14:05 +02:00
|
|
|
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'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
const Default = {
|
2021-02-04 01:07:25 +05:30
|
|
|
offset: [0, 2],
|
2020-06-19 11:17:01 +03:00
|
|
|
boundary: 'clippingParents',
|
2019-02-26 13:20:34 +02:00
|
|
|
reference: 'toggle',
|
2019-08-14 17:27:58 +02:00
|
|
|
display: 'dynamic',
|
|
|
|
popperConfig: null
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultType = {
|
2020-12-12 02:54:29 +02:00
|
|
|
offset: '(array|string|function)',
|
2019-02-26 13:20:34 +02:00
|
|
|
boundary: '(string|element)',
|
2020-12-21 13:32:11 +01:00
|
|
|
reference: '(string|element|object)',
|
2019-08-14 17:27:58 +02:00
|
|
|
display: 'string',
|
2021-02-10 00:46:13 +05:30
|
|
|
popperConfig: '(null|object|function)'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-09-04 17:58:29 +03:00
|
|
|
class Dropdown extends BaseComponent {
|
2018-09-26 10:39:01 +02:00
|
|
|
constructor(element, config) {
|
2019-09-04 17:58:29 +03:00
|
|
|
super(element)
|
|
|
|
|
2019-02-26 13:20:34 +02:00
|
|
|
this._popper = null
|
|
|
|
this._config = this._getConfig(config)
|
|
|
|
this._menu = this._getMenuElement()
|
2018-09-26 10:39:01 +02:00
|
|
|
this._inNavbar = this._detectNavbar()
|
|
|
|
|
|
|
|
this._addEventListeners()
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
|
|
|
|
|
|
|
static get Default() {
|
|
|
|
return Default
|
2017-04-14 11:25:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
static get DefaultType() {
|
|
|
|
return DefaultType
|
2017-04-14 11:25:53 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:58:29 +03:00
|
|
|
static get DATA_KEY() {
|
|
|
|
return DATA_KEY
|
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
|
|
|
|
|
|
|
toggle() {
|
2020-03-07 11:31:42 +02:00
|
|
|
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2020-02-01 14:56:20 +01:00
|
|
|
const isActive = this._element.classList.contains(CLASS_NAME_SHOW)
|
2015-05-10 19:45:38 -07:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
Dropdown.clearMenus()
|
2015-05-10 19:45:38 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (isActive) {
|
|
|
|
return
|
2017-04-14 11:25:53 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 22:02:06 +02:00
|
|
|
this.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
2020-03-07 11:31:42 +02:00
|
|
|
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || this._menu.classList.contains(CLASS_NAME_SHOW)) {
|
2019-08-22 22:02:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const parent = Dropdown.getParentFromElement(this._element)
|
2018-09-26 10:39:01 +02:00
|
|
|
const relatedTarget = {
|
|
|
|
relatedTarget: this._element
|
2017-04-14 11:25:53 +02:00
|
|
|
}
|
2019-08-22 22:02:06 +02:00
|
|
|
|
2020-02-01 14:56:20 +01:00
|
|
|
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, relatedTarget)
|
2017-04-14 11:25:53 +02:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
if (showEvent.defaultPrevented) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2020-11-21 16:22:08 +02:00
|
|
|
// Totally disable Popper for Dropdowns in Navbar
|
2021-02-05 14:57:43 -08:00
|
|
|
if (this._inNavbar) {
|
|
|
|
Manipulator.setDataAttribute(this._menu, 'popper', 'none')
|
|
|
|
} else {
|
2018-09-26 10:39:01 +02:00
|
|
|
if (typeof Popper === 'undefined') {
|
2020-11-21 16:22:08 +02:00
|
|
|
throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)')
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
let referenceElement = this._element
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (this._config.reference === 'parent') {
|
|
|
|
referenceElement = parent
|
2019-02-23 00:37:55 +02:00
|
|
|
} else if (isElement(this._config.reference)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
referenceElement = this._config.reference
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Check if it's jQuery element
|
|
|
|
if (typeof this._config.reference.jquery !== 'undefined') {
|
|
|
|
referenceElement = this._config.reference[0]
|
2017-10-27 09:51:08 +02:00
|
|
|
}
|
2020-12-21 13:32:11 +01:00
|
|
|
} else if (typeof this._config.reference === 'object') {
|
|
|
|
referenceElement = this._config.reference
|
2017-05-22 16:14:10 +02:00
|
|
|
}
|
2017-10-27 09:51:08 +02:00
|
|
|
|
2021-02-05 14:57:43 -08:00
|
|
|
const popperConfig = this._getPopperConfig()
|
|
|
|
const isDisplayStatic = popperConfig.modifiers.find(modifier => modifier.name === 'applyStyles' && modifier.enabled === false)
|
|
|
|
|
2021-02-10 00:34:23 +05:30
|
|
|
this._popper = Popper.createPopper(referenceElement, this._menu, popperConfig)
|
|
|
|
|
2021-02-05 14:57:43 -08:00
|
|
|
if (isDisplayStatic) {
|
|
|
|
Manipulator.setDataAttribute(this._menu, 'popper', 'static')
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02: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
|
|
|
|
if ('ontouchstart' in document.documentElement &&
|
2020-04-28 21:17:23 +02:00
|
|
|
!parent.closest(SELECTOR_NAVBAR_NAV)) {
|
2020-03-25 15:35:02 +01:00
|
|
|
[].concat(...document.body.children)
|
2019-02-26 13:20:34 +02:00
|
|
|
.forEach(elem => EventHandler.on(elem, 'mouseover', null, noop()))
|
2015-05-13 12:48:34 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._element.focus()
|
|
|
|
this._element.setAttribute('aria-expanded', true)
|
2015-05-13 12:48:34 -07:00
|
|
|
|
2020-10-05 18:07:49 +03:00
|
|
|
this._menu.classList.toggle(CLASS_NAME_SHOW)
|
|
|
|
this._element.classList.toggle(CLASS_NAME_SHOW)
|
2021-02-04 01:21:19 +05:30
|
|
|
EventHandler.trigger(this._element, EVENT_SHOWN, relatedTarget)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-13 12:48:34 -07:00
|
|
|
|
2018-10-20 13:27:51 +02:00
|
|
|
hide() {
|
2020-03-07 11:31:42 +02:00
|
|
|
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || !this._menu.classList.contains(CLASS_NAME_SHOW)) {
|
2018-10-20 13:27:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const relatedTarget = {
|
|
|
|
relatedTarget: this._element
|
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2021-02-04 01:21:19 +05:30
|
|
|
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE, relatedTarget)
|
2018-10-20 13:27:51 +02:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
if (hideEvent.defaultPrevented) {
|
2018-10-20 13:27:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-22 22:02:06 +02:00
|
|
|
if (this._popper) {
|
|
|
|
this._popper.destroy()
|
|
|
|
}
|
|
|
|
|
2020-10-05 18:07:49 +03:00
|
|
|
this._menu.classList.toggle(CLASS_NAME_SHOW)
|
|
|
|
this._element.classList.toggle(CLASS_NAME_SHOW)
|
2021-02-05 14:57:43 -08:00
|
|
|
Manipulator.removeDataAttribute(this._menu, 'popper')
|
2021-02-04 01:21:19 +05:30
|
|
|
EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget)
|
2018-10-20 13:27:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
dispose() {
|
2017-09-26 09:09:40 +02:00
|
|
|
EventHandler.off(this._element, EVENT_KEY)
|
2018-09-26 10:39:01 +02:00
|
|
|
this._menu = null
|
2020-06-19 11:17:01 +03:00
|
|
|
|
2019-08-18 22:05:20 +02:00
|
|
|
if (this._popper) {
|
2018-09-26 10:39:01 +02:00
|
|
|
this._popper.destroy()
|
|
|
|
this._popper = null
|
2015-05-13 12:48:34 -07:00
|
|
|
}
|
2021-02-11 21:51:34 -08:00
|
|
|
|
|
|
|
super.dispose()
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-13 12:48:34 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
update() {
|
|
|
|
this._inNavbar = this._detectNavbar()
|
2019-08-18 22:05:20 +02:00
|
|
|
if (this._popper) {
|
2020-06-19 11:17:01 +03:00
|
|
|
this._popper.update()
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-14 11:25:53 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_addEventListeners() {
|
2020-03-07 11:31:42 +02:00
|
|
|
EventHandler.on(this._element, EVENT_CLICK, event => {
|
2018-09-26 10:39:01 +02:00
|
|
|
event.preventDefault()
|
|
|
|
this.toggle()
|
|
|
|
})
|
|
|
|
}
|
2017-04-14 11:25:53 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_getConfig(config) {
|
|
|
|
config = {
|
|
|
|
...this.constructor.Default,
|
2018-06-09 21:11:05 +02:00
|
|
|
...Manipulator.getDataAttributes(this._element),
|
2018-09-26 10:39:01 +02:00
|
|
|
...config
|
2017-04-14 11:25:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 18:40:52 +03:00
|
|
|
typeCheckConfig(NAME, config, this.constructor.DefaultType)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-12-21 13:32:11 +01:00
|
|
|
if (typeof config.reference === 'object' && !isElement(config.reference) &&
|
|
|
|
typeof config.reference.getBoundingClientRect !== 'function'
|
|
|
|
) {
|
|
|
|
// Popper virtual elements require a getBoundingClientRect method
|
2021-01-14 01:43:30 +05:30
|
|
|
throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`)
|
2020-12-21 13:32:11 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
_getMenuElement() {
|
2020-02-01 14:56:20 +01:00
|
|
|
return SelectorEngine.next(this._element, SELECTOR_MENU)[0]
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_getPlacement() {
|
2017-09-26 09:09:40 +02:00
|
|
|
const parentDropdown = this._element.parentNode
|
2017-05-22 16:14:10 +02:00
|
|
|
|
2020-11-25 22:12:32 +02:00
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
|
|
|
|
return PLACEMENT_RIGHT
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
|
|
|
|
return PLACEMENT_LEFT
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to trim the value because custom properties can also include spaces
|
|
|
|
const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end'
|
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
|
2020-11-25 22:12:32 +02:00
|
|
|
return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP
|
2017-05-17 22:03:11 +02:00
|
|
|
}
|
2019-02-26 13:20:34 +02:00
|
|
|
|
2020-11-25 22:12:32 +02:00
|
|
|
return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-05-17 22:03:11 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_detectNavbar() {
|
2020-12-04 16:23:28 +02:00
|
|
|
return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-06-04 17:56:51 +02:00
|
|
|
|
2020-12-12 02:54:29 +02:00
|
|
|
_getOffset() {
|
|
|
|
const { offset } = this._config
|
|
|
|
|
|
|
|
if (typeof offset === 'string') {
|
|
|
|
return offset.split(',').map(val => Number.parseInt(val, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof offset === 'function') {
|
|
|
|
return popperData => offset(popperData, this._element)
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset
|
|
|
|
}
|
|
|
|
|
2019-02-05 03:31:18 -05:00
|
|
|
_getPopperConfig() {
|
2021-02-10 00:46:13 +05:30
|
|
|
const defaultBsPopperConfig = {
|
2018-09-26 10:39:01 +02:00
|
|
|
placement: this._getPlacement(),
|
2020-06-19 11:17:01 +03:00
|
|
|
modifiers: [{
|
|
|
|
name: 'preventOverflow',
|
|
|
|
options: {
|
2021-02-03 20:55:25 -08:00
|
|
|
boundary: this._config.boundary
|
2017-06-04 17:56:51 +02:00
|
|
|
}
|
2020-12-09 19:21:30 +01:00
|
|
|
},
|
2020-12-12 02:54:29 +02:00
|
|
|
{
|
|
|
|
name: 'offset',
|
|
|
|
options: {
|
|
|
|
offset: this._getOffset()
|
|
|
|
}
|
2020-06-19 11:17:01 +03:00
|
|
|
}]
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-06-04 17:56:51 +02:00
|
|
|
|
2020-11-21 16:22:08 +02:00
|
|
|
// Disable Popper if we have a static display
|
2018-09-26 10:39:01 +02:00
|
|
|
if (this._config.display === 'static') {
|
2021-02-10 00:46:13 +05:30
|
|
|
defaultBsPopperConfig.modifiers = [{
|
2020-06-19 11:17:01 +03:00
|
|
|
name: 'applyStyles',
|
2018-09-26 10:39:01 +02:00
|
|
|
enabled: false
|
2020-06-19 11:17:01 +03:00
|
|
|
}]
|
2017-06-04 17:56:51 +02:00
|
|
|
}
|
2019-02-05 03:31:18 -05:00
|
|
|
|
2019-08-14 18:02:41 +02:00
|
|
|
return {
|
2021-02-10 00:46:13 +05:30
|
|
|
...defaultBsPopperConfig,
|
|
|
|
...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
|
2019-08-14 17:27:58 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-06-04 17:56:51 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Static
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static dropdownInterface(element, config) {
|
2021-03-02 15:55:44 +01:00
|
|
|
let data = Data.get(element, DATA_KEY)
|
2017-09-26 09:09:40 +02:00
|
|
|
const _config = typeof config === 'object' ? config : null
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
if (!data) {
|
|
|
|
data = new Dropdown(element, _config)
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
if (typeof config === 'string') {
|
|
|
|
if (typeof data[config] === 'undefined') {
|
2019-02-26 13:20:34 +02:00
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2019-02-26 13:20:34 +02:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static jQueryInterface(config) {
|
2017-09-26 09:09:40 +02:00
|
|
|
return this.each(function () {
|
2019-07-28 15:24:46 +02:00
|
|
|
Dropdown.dropdownInterface(this, config)
|
2018-09-26 10:39:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static clearMenus(event) {
|
2021-01-06 03:07:43 +02:00
|
|
|
if (event) {
|
|
|
|
if (event.button === RIGHT_MOUSE_BUTTON || (event.type === 'keyup' && event.key !== TAB_KEY)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/input|select|textarea|form/i.test(event.target.tagName)) {
|
|
|
|
return
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:35:02 +01:00
|
|
|
const toggles = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
for (let i = 0, len = toggles.length; i < len; i++) {
|
2021-03-02 15:55:44 +01:00
|
|
|
const context = Data.get(toggles[i], DATA_KEY)
|
2018-09-26 10:39:01 +02:00
|
|
|
const relatedTarget = {
|
|
|
|
relatedTarget: toggles[i]
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event && event.type === 'click') {
|
|
|
|
relatedTarget.clickEvent = event
|
|
|
|
}
|
2018-06-25 15:29:34 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (!context) {
|
|
|
|
continue
|
|
|
|
}
|
2017-04-14 11:25:53 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
const dropdownMenu = context._menu
|
2020-02-01 14:56:20 +01:00
|
|
|
if (!toggles[i].classList.contains(CLASS_NAME_SHOW)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2021-01-06 03:07:43 +02:00
|
|
|
if (event) {
|
|
|
|
// Don't close the menu if the clicked element or one of its parents is the dropdown button
|
|
|
|
if ([context._element].some(element => event.composedPath().includes(element))) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tab navigation through the dropdown menu shouldn't close the menu
|
|
|
|
if (event.type === 'keyup' && event.key === TAB_KEY && dropdownMenu.contains(event.target)) {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2021-02-04 01:21:19 +05:30
|
|
|
const hideEvent = EventHandler.trigger(toggles[i], EVENT_HIDE, relatedTarget)
|
2017-09-26 09:09:40 +02:00
|
|
|
if (hideEvent.defaultPrevented) {
|
2018-09-26 10:39:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// If this is a touch-enabled device we remove the extra
|
|
|
|
// empty mouseover listeners we added for iOS support
|
|
|
|
if ('ontouchstart' in document.documentElement) {
|
2020-03-25 15:35:02 +01:00
|
|
|
[].concat(...document.body.children)
|
2019-02-26 13:20:34 +02:00
|
|
|
.forEach(elem => EventHandler.off(elem, 'mouseover', null, noop()))
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-03-18 20:41:13 -04:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
toggles[i].setAttribute('aria-expanded', 'false')
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-08-18 22:05:20 +02:00
|
|
|
if (context._popper) {
|
|
|
|
context._popper.destroy()
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
dropdownMenu.classList.remove(CLASS_NAME_SHOW)
|
2020-02-01 14:56:20 +01:00
|
|
|
toggles[i].classList.remove(CLASS_NAME_SHOW)
|
2021-02-05 14:57:43 -08:00
|
|
|
Manipulator.removeDataAttribute(dropdownMenu, 'popper')
|
2021-02-04 01:21:19 +05:30
|
|
|
EventHandler.trigger(toggles[i], EVENT_HIDDEN, relatedTarget)
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static getParentFromElement(element) {
|
2019-07-23 21:15:00 +02:00
|
|
|
return getElementFromSelector(element) || element.parentNode
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static dataApiKeydownHandler(event) {
|
2018-09-26 10:39:01 +02: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
|
2019-02-26 13:20:34 +02:00
|
|
|
if (/input|textarea/i.test(event.target.tagName) ?
|
2020-04-15 16:52:18 +02:00
|
|
|
event.key === SPACE_KEY || (event.key !== ESCAPE_KEY &&
|
|
|
|
((event.key !== ARROW_DOWN_KEY && event.key !== ARROW_UP_KEY) ||
|
2020-04-28 21:17:23 +02:00
|
|
|
event.target.closest(SELECTOR_MENU))) :
|
2020-04-15 16:52:18 +02:00
|
|
|
!REGEXP_KEYDOWN.test(event.key)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
if (this.disabled || this.classList.contains(CLASS_NAME_DISABLED)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
const parent = Dropdown.getParentFromElement(this)
|
2020-02-01 14:56:20 +01:00
|
|
|
const isActive = this.classList.contains(CLASS_NAME_SHOW)
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2020-04-15 16:52:18 +02:00
|
|
|
if (event.key === ESCAPE_KEY) {
|
2020-02-01 14:56:20 +01:00
|
|
|
const button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
|
|
|
|
button.focus()
|
|
|
|
Dropdown.clearMenus()
|
|
|
|
return
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2021-02-04 01:33:21 +05:30
|
|
|
if (!isActive && (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY)) {
|
|
|
|
const button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
|
|
|
|
button.click()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-15 16:52:18 +02:00
|
|
|
if (!isActive || event.key === SPACE_KEY) {
|
2019-07-28 15:24:46 +02:00
|
|
|
Dropdown.clearMenus()
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2020-06-20 19:00:53 +03:00
|
|
|
const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, parent).filter(isVisible)
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
if (!items.length) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2020-04-15 17:59:31 +02:00
|
|
|
let index = items.indexOf(event.target)
|
2015-08-18 19:22:46 -07:00
|
|
|
|
2020-12-02 06:45:15 +02:00
|
|
|
// Up
|
|
|
|
if (event.key === ARROW_UP_KEY && index > 0) {
|
2018-09-26 10:39:01 +02:00
|
|
|
index--
|
|
|
|
}
|
2015-08-18 19:22:46 -07:00
|
|
|
|
2020-12-02 06:45:15 +02:00
|
|
|
// Down
|
|
|
|
if (event.key === ARROW_DOWN_KEY && index < items.length - 1) {
|
2018-09-26 10:39:01 +02:00
|
|
|
index++
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2020-04-15 17:59:31 +02:00
|
|
|
// index is -1 if the first keydown is an ArrowUp
|
|
|
|
index = index === -1 ? 0 : index
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
items[index].focus()
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown.dataApiKeydownHandler)
|
|
|
|
EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown.dataApiKeydownHandler)
|
|
|
|
EventHandler.on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus)
|
|
|
|
EventHandler.on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus)
|
|
|
|
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
2017-09-26 09:09:40 +02:00
|
|
|
event.preventDefault()
|
2021-01-06 03:07:43 +02:00
|
|
|
Dropdown.dropdownInterface(this)
|
2017-09-26 09:09:40 +02:00
|
|
|
})
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2020-11-01 15:49:51 +02:00
|
|
|
* add .Dropdown to jQuery only if jQuery is present
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2020-11-01 14:32:36 +01:00
|
|
|
|
2020-12-08 07:16:50 +01:00
|
|
|
defineJQueryPlugin(NAME, Dropdown)
|
2015-05-10 13:47:11 -07:00
|
|
|
|
|
|
|
export default Dropdown
|