2015-05-11 04:47:11 +08:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2023-03-22 15:12:33 +08:00
|
|
|
* Bootstrap dropdown.js
|
2020-06-17 02:41:47 +08:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-11 04:47:11 +08:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-06-19 16:17:01 +08:00
|
|
|
import * as Popper from '@popperjs/core'
|
2023-03-30 01:49:30 +08:00
|
|
|
import BaseComponent from './base-component.js'
|
|
|
|
import EventHandler from './dom/event-handler.js'
|
|
|
|
import Manipulator from './dom/manipulator.js'
|
|
|
|
import SelectorEngine from './dom/selector-engine.js'
|
2019-02-23 06:37:55 +08:00
|
|
|
import {
|
2020-12-08 14:16:50 +08:00
|
|
|
defineJQueryPlugin,
|
2022-10-07 20:25:00 +08:00
|
|
|
execute,
|
2021-05-13 23:17:20 +08:00
|
|
|
getElement,
|
2021-06-10 22:51:01 +08:00
|
|
|
getNextActiveElement,
|
2021-03-30 12:42:23 +08:00
|
|
|
isDisabled,
|
2019-02-23 06:37:55 +08:00
|
|
|
isElement,
|
2020-06-26 22:06:20 +08:00
|
|
|
isRTL,
|
2021-06-10 22:51:01 +08:00
|
|
|
isVisible,
|
2021-12-11 00:18:18 +08:00
|
|
|
noop
|
2022-10-26 13:26:51 +08:00
|
|
|
} from './util/index.js'
|
2018-11-14 17:16:56 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2019-02-26 19:20:34 +08:00
|
|
|
const NAME = 'dropdown'
|
|
|
|
const DATA_KEY = 'bs.dropdown'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2020-03-07 17:31:42 +08:00
|
|
|
|
2020-04-15 22:52:18 +08:00
|
|
|
const ESCAPE_KEY = 'Escape'
|
|
|
|
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 16:39:01 +08:00
|
|
|
|
2020-03-07 17:31:42 +08: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_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'
|
2020-06-26 22:06:20 +08:00
|
|
|
const CLASS_NAME_DROPEND = 'dropend'
|
|
|
|
const CLASS_NAME_DROPSTART = 'dropstart'
|
2022-03-08 19:34:21 +08:00
|
|
|
const CLASS_NAME_DROPUP_CENTER = 'dropup-center'
|
|
|
|
const CLASS_NAME_DROPDOWN_CENTER = 'dropdown-center'
|
2020-03-07 17:31:42 +08:00
|
|
|
|
2022-02-19 22:22:32 +08:00
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]:not(.disabled):not(:disabled)'
|
2022-02-19 22:16:51 +08:00
|
|
|
const SELECTOR_DATA_TOGGLE_SHOWN = `${SELECTOR_DATA_TOGGLE}.${CLASS_NAME_SHOW}`
|
2020-03-07 17:31:42 +08:00
|
|
|
const SELECTOR_MENU = '.dropdown-menu'
|
2021-10-10 20:24:07 +08:00
|
|
|
const SELECTOR_NAVBAR = '.navbar'
|
2020-03-07 17:31:42 +08:00
|
|
|
const SELECTOR_NAVBAR_NAV = '.navbar-nav'
|
|
|
|
const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'
|
|
|
|
|
2021-02-16 16:14:05 +08: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'
|
2022-03-08 19:34:21 +08:00
|
|
|
const PLACEMENT_TOPCENTER = 'top'
|
|
|
|
const PLACEMENT_BOTTOMCENTER = 'bottom'
|
2018-09-26 16:39:01 +08:00
|
|
|
|
|
|
|
const Default = {
|
2022-05-19 21:35:44 +08:00
|
|
|
autoClose: true,
|
2020-06-19 16:17:01 +08:00
|
|
|
boundary: 'clippingParents',
|
2019-08-14 23:27:58 +08:00
|
|
|
display: 'dynamic',
|
2022-05-19 21:35:44 +08:00
|
|
|
offset: [0, 2],
|
2021-04-20 13:19:57 +08:00
|
|
|
popperConfig: null,
|
2022-05-19 21:35:44 +08:00
|
|
|
reference: 'toggle'
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultType = {
|
2022-05-19 21:35:44 +08:00
|
|
|
autoClose: '(boolean|string)',
|
2019-02-26 19:20:34 +08:00
|
|
|
boundary: '(string|element)',
|
2019-08-14 23:27:58 +08:00
|
|
|
display: 'string',
|
2022-05-19 21:35:44 +08:00
|
|
|
offset: '(array|string|function)',
|
2021-04-20 13:19:57 +08:00
|
|
|
popperConfig: '(null|object|function)',
|
2022-05-19 21:35:44 +08:00
|
|
|
reference: '(string|element|object)'
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-13 20:19:28 +08:00
|
|
|
* Class definition
|
2018-09-26 16:39:01 +08:00
|
|
|
*/
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2019-09-04 22:58:29 +08:00
|
|
|
class Dropdown extends BaseComponent {
|
2018-09-26 16:39:01 +08:00
|
|
|
constructor(element, config) {
|
2021-12-11 00:18:18 +08:00
|
|
|
super(element, config)
|
2019-09-04 22:58:29 +08:00
|
|
|
|
2019-02-26 19:20:34 +08:00
|
|
|
this._popper = null
|
2022-01-31 06:07:05 +08:00
|
|
|
this._parent = this._element.parentNode // dropdown wrapper
|
2023-03-30 01:49:30 +08:00
|
|
|
// TODO: v6 revert #37011 & change markup https://getbootstrap.com/docs/5.3/forms/input-group/
|
2022-09-27 15:39:11 +08:00
|
|
|
this._menu = SelectorEngine.next(this._element, SELECTOR_MENU)[0] ||
|
|
|
|
SelectorEngine.prev(this._element, SELECTOR_MENU)[0] ||
|
|
|
|
SelectorEngine.findOne(SELECTOR_MENU, this._parent)
|
2018-09-26 16:39:01 +08:00
|
|
|
this._inNavbar = this._detectNavbar()
|
2015-05-11 04:47:11 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Getters
|
|
|
|
static get Default() {
|
|
|
|
return Default
|
2017-04-14 17:25:53 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
static get DefaultType() {
|
|
|
|
return DefaultType
|
2017-04-14 17:25:53 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 15:49:30 +08:00
|
|
|
static get NAME() {
|
|
|
|
return NAME
|
2019-09-04 22:58:29 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Public
|
|
|
|
toggle() {
|
2021-07-08 22:42:09 +08:00
|
|
|
return this._isShown() ? this.hide() : this.show()
|
2019-08-23 04:02:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
2021-12-13 08:17:03 +08:00
|
|
|
if (isDisabled(this._element) || this._isShown()) {
|
2019-08-23 04:02:06 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
const relatedTarget = {
|
|
|
|
relatedTarget: this._element
|
2017-04-14 17:25:53 +08:00
|
|
|
}
|
2019-08-23 04:02:06 +08:00
|
|
|
|
2020-02-01 21:56:20 +08:00
|
|
|
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, relatedTarget)
|
2017-04-14 17:25:53 +08:00
|
|
|
|
2017-09-26 15:09:40 +08:00
|
|
|
if (showEvent.defaultPrevented) {
|
2018-09-26 16:39:01 +08:00
|
|
|
return
|
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2021-12-13 08:10:26 +08:00
|
|
|
this._createPopper()
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2018-09-26 16:39:01 +08: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
|
2021-12-13 08:10:26 +08:00
|
|
|
if ('ontouchstart' in document.documentElement && !this._parent.closest(SELECTOR_NAVBAR_NAV)) {
|
2021-10-29 15:38:35 +08:00
|
|
|
for (const element of [].concat(...document.body.children)) {
|
|
|
|
EventHandler.on(element, 'mouseover', noop)
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2015-05-14 03:48:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
this._element.focus()
|
|
|
|
this._element.setAttribute('aria-expanded', true)
|
2015-05-14 03:48:34 +08:00
|
|
|
|
2021-07-08 22:54:15 +08:00
|
|
|
this._menu.classList.add(CLASS_NAME_SHOW)
|
|
|
|
this._element.classList.add(CLASS_NAME_SHOW)
|
2021-02-04 03:51:19 +08:00
|
|
|
EventHandler.trigger(this._element, EVENT_SHOWN, relatedTarget)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-14 03:48:34 +08:00
|
|
|
|
2018-10-20 19:27:51 +08:00
|
|
|
hide() {
|
2021-12-13 08:17:03 +08:00
|
|
|
if (isDisabled(this._element) || !this._isShown()) {
|
2018-10-20 19:27:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const relatedTarget = {
|
|
|
|
relatedTarget: this._element
|
|
|
|
}
|
2018-11-14 17:16:56 +08:00
|
|
|
|
2021-03-25 02:25:00 +08:00
|
|
|
this._completeHide(relatedTarget)
|
2018-10-20 19:27:51 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
dispose() {
|
2019-08-19 04:05:20 +08:00
|
|
|
if (this._popper) {
|
2018-09-26 16:39:01 +08:00
|
|
|
this._popper.destroy()
|
2015-05-14 03:48:34 +08:00
|
|
|
}
|
2021-02-12 13:51:34 +08:00
|
|
|
|
|
|
|
super.dispose()
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-14 03:48:34 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
update() {
|
|
|
|
this._inNavbar = this._detectNavbar()
|
2019-08-19 04:05:20 +08:00
|
|
|
if (this._popper) {
|
2020-06-19 16:17:01 +08:00
|
|
|
this._popper.update()
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-14 17:25:53 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Private
|
2021-03-25 02:25:00 +08:00
|
|
|
_completeHide(relatedTarget) {
|
|
|
|
const hideEvent = EventHandler.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) {
|
2021-10-29 15:38:35 +08:00
|
|
|
for (const element of [].concat(...document.body.children)) {
|
|
|
|
EventHandler.off(element, 'mouseover', noop)
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2021-03-25 02:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._popper) {
|
|
|
|
this._popper.destroy()
|
|
|
|
}
|
|
|
|
|
2021-04-14 17:57:33 +08:00
|
|
|
this._menu.classList.remove(CLASS_NAME_SHOW)
|
|
|
|
this._element.classList.remove(CLASS_NAME_SHOW)
|
2021-03-25 02:25:00 +08:00
|
|
|
this._element.setAttribute('aria-expanded', 'false')
|
|
|
|
Manipulator.removeDataAttribute(this._menu, 'popper')
|
|
|
|
EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget)
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_getConfig(config) {
|
2021-12-11 00:18:18 +08:00
|
|
|
config = super._getConfig(config)
|
2018-09-26 16:39:01 +08:00
|
|
|
|
2020-12-21 20:32:11 +08:00
|
|
|
if (typeof config.reference === 'object' && !isElement(config.reference) &&
|
|
|
|
typeof config.reference.getBoundingClientRect !== 'function'
|
|
|
|
) {
|
|
|
|
// Popper virtual elements require a getBoundingClientRect method
|
2021-01-14 04:13:30 +08:00
|
|
|
throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`)
|
2020-12-21 20:32:11 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-12-13 08:10:26 +08:00
|
|
|
_createPopper() {
|
2021-07-08 23:23:54 +08:00
|
|
|
if (typeof Popper === 'undefined') {
|
2024-03-05 02:58:00 +08:00
|
|
|
throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org/docs/v2/)')
|
2021-07-08 23:23:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let referenceElement = this._element
|
|
|
|
|
|
|
|
if (this._config.reference === 'parent') {
|
2021-12-13 08:10:26 +08:00
|
|
|
referenceElement = this._parent
|
2021-07-08 23:23:54 +08:00
|
|
|
} 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()
|
|
|
|
this._popper = Popper.createPopper(referenceElement, this._menu, popperConfig)
|
|
|
|
}
|
|
|
|
|
2021-12-13 08:17:03 +08:00
|
|
|
_isShown() {
|
|
|
|
return this._menu.classList.contains(CLASS_NAME_SHOW)
|
2021-06-10 22:51:01 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_getPlacement() {
|
2021-12-13 08:10:26 +08:00
|
|
|
const parentDropdown = this._parent
|
2017-05-22 22:14:10 +08:00
|
|
|
|
2020-11-26 04:12:32 +08:00
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
|
|
|
|
return PLACEMENT_RIGHT
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
|
|
|
|
return PLACEMENT_LEFT
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:34:21 +08:00
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPUP_CENTER)) {
|
|
|
|
return PLACEMENT_TOPCENTER
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPDOWN_CENTER)) {
|
|
|
|
return PLACEMENT_BOTTOMCENTER
|
|
|
|
}
|
|
|
|
|
2020-11-26 04:12:32 +08:00
|
|
|
// 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 17:31:42 +08:00
|
|
|
if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
|
2020-11-26 04:12:32 +08:00
|
|
|
return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP
|
2017-05-18 04:03:11 +08:00
|
|
|
}
|
2019-02-26 19:20:34 +08:00
|
|
|
|
2020-11-26 04:12:32 +08:00
|
|
|
return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2017-05-18 04:03:11 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
_detectNavbar() {
|
2021-10-10 20:24:07 +08:00
|
|
|
return this._element.closest(SELECTOR_NAVBAR) !== null
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2017-06-04 23:56:51 +08:00
|
|
|
|
2020-12-12 08:54:29 +08:00
|
|
|
_getOffset() {
|
|
|
|
const { offset } = this._config
|
|
|
|
|
|
|
|
if (typeof offset === 'string') {
|
2021-10-29 15:38:35 +08:00
|
|
|
return offset.split(',').map(value => Number.parseInt(value, 10))
|
2020-12-12 08:54:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof offset === 'function') {
|
|
|
|
return popperData => offset(popperData, this._element)
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset
|
|
|
|
}
|
|
|
|
|
2019-02-05 16:31:18 +08:00
|
|
|
_getPopperConfig() {
|
2021-02-10 03:16:13 +08:00
|
|
|
const defaultBsPopperConfig = {
|
2018-09-26 16:39:01 +08:00
|
|
|
placement: this._getPlacement(),
|
2020-06-19 16:17:01 +08:00
|
|
|
modifiers: [{
|
|
|
|
name: 'preventOverflow',
|
|
|
|
options: {
|
2021-02-04 12:55:25 +08:00
|
|
|
boundary: this._config.boundary
|
2017-06-04 23:56:51 +08:00
|
|
|
}
|
2020-12-10 02:21:30 +08:00
|
|
|
},
|
2020-12-12 08:54:29 +08:00
|
|
|
{
|
|
|
|
name: 'offset',
|
|
|
|
options: {
|
|
|
|
offset: this._getOffset()
|
|
|
|
}
|
2020-06-19 16:17:01 +08:00
|
|
|
}]
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2017-06-04 23:56:51 +08:00
|
|
|
|
2021-10-10 20:09:57 +08:00
|
|
|
// Disable Popper if we have a static display or Dropdown is in Navbar
|
|
|
|
if (this._inNavbar || this._config.display === 'static') {
|
2023-03-30 01:49:30 +08:00
|
|
|
Manipulator.setDataAttribute(this._menu, 'popper', 'static') // TODO: v6 remove
|
2021-02-10 03:16:13 +08:00
|
|
|
defaultBsPopperConfig.modifiers = [{
|
2020-06-19 16:17:01 +08:00
|
|
|
name: 'applyStyles',
|
2018-09-26 16:39:01 +08:00
|
|
|
enabled: false
|
2020-06-19 16:17:01 +08:00
|
|
|
}]
|
2017-06-04 23:56:51 +08:00
|
|
|
}
|
2019-02-05 16:31:18 +08:00
|
|
|
|
2019-08-15 00:02:41 +08:00
|
|
|
return {
|
2021-02-10 03:16:13 +08:00
|
|
|
...defaultBsPopperConfig,
|
2024-07-19 13:05:21 +08:00
|
|
|
...execute(this._config.popperConfig, [undefined, defaultBsPopperConfig])
|
2019-08-14 23:27:58 +08:00
|
|
|
}
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2017-06-04 23:56:51 +08:00
|
|
|
|
2021-05-22 15:58:52 +08:00
|
|
|
_selectMenuItem({ key, target }) {
|
2021-10-29 15:38:35 +08:00
|
|
|
const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(element => isVisible(element))
|
2021-04-11 23:34:46 +08:00
|
|
|
|
2021-05-19 06:23:52 +08:00
|
|
|
if (!items.length) {
|
|
|
|
return
|
2021-04-11 23:34:46 +08:00
|
|
|
}
|
|
|
|
|
2021-05-22 15:58:52 +08:00
|
|
|
// 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()
|
2021-04-11 23:34:46 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
// Static
|
2021-06-10 22:56:43 +08:00
|
|
|
static jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
|
|
|
const data = Dropdown.getOrCreateInstance(this, config)
|
|
|
|
|
|
|
|
if (typeof config !== 'string') {
|
|
|
|
return
|
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2017-09-26 15:09:40 +08:00
|
|
|
if (typeof data[config] === 'undefined') {
|
2019-02-26 19:20:34 +08:00
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2019-02-26 19:20:34 +08:00
|
|
|
|
2017-09-26 15:09:40 +08:00
|
|
|
data[config]()
|
2018-09-26 16:39:01 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-28 21:24:46 +08:00
|
|
|
static clearMenus(event) {
|
2021-10-10 22:56:34 +08:00
|
|
|
if (event.button === RIGHT_MOUSE_BUTTON || (event.type === 'keyup' && event.key !== TAB_KEY)) {
|
2021-05-11 14:09:00 +08:00
|
|
|
return
|
2015-05-11 04:47:11 +08:00
|
|
|
}
|
|
|
|
|
2022-02-19 22:16:51 +08:00
|
|
|
const openToggles = SelectorEngine.find(SELECTOR_DATA_TOGGLE_SHOWN)
|
2020-03-25 22:35:02 +08:00
|
|
|
|
2022-02-19 22:16:51 +08:00
|
|
|
for (const toggle of openToggles) {
|
2021-08-10 23:07:39 +08:00
|
|
|
const context = Dropdown.getInstance(toggle)
|
2021-04-20 13:19:57 +08:00
|
|
|
if (!context || context._config.autoClose === false) {
|
2018-09-26 16:39:01 +08:00
|
|
|
continue
|
|
|
|
}
|
2017-04-14 17:25:53 +08:00
|
|
|
|
2021-10-10 22:56:34 +08:00
|
|
|
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)
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-06 09:07:43 +08:00
|
|
|
|
2021-10-10 22:56:34 +08:00
|
|
|
// Tab navigation through the dropdown menu or events from contained inputs shouldn't close the menu
|
|
|
|
if (context._menu.contains(event.target) && ((event.type === 'keyup' && event.key === TAB_KEY) || /input|select|option|textarea|form/i.test(event.target.tagName))) {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2021-11-09 21:43:02 +08:00
|
|
|
const relatedTarget = { relatedTarget: context._element }
|
|
|
|
|
2021-10-10 22:56:34 +08:00
|
|
|
if (event.type === 'click') {
|
|
|
|
relatedTarget.clickEvent = event
|
2019-08-19 04:05:20 +08:00
|
|
|
}
|
|
|
|
|
2021-03-25 02:25:00 +08:00
|
|
|
context._completeHide(relatedTarget)
|
2015-05-11 04:47:11 +08:00
|
|
|
}
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2019-07-28 21:24:46 +08:00
|
|
|
static dataApiKeydownHandler(event) {
|
2022-03-01 21:53:07 +08:00
|
|
|
// If not an UP | DOWN | ESCAPE key => not a dropdown command
|
|
|
|
// If input/textarea && if key is other than ESCAPE => not a dropdown command
|
|
|
|
|
|
|
|
const isInput = /input|textarea/i.test(event.target.tagName)
|
|
|
|
const isEscapeEvent = event.key === ESCAPE_KEY
|
|
|
|
const isUpOrDownEvent = [ARROW_UP_KEY, ARROW_DOWN_KEY].includes(event.key)
|
|
|
|
|
|
|
|
if (!isUpOrDownEvent && !isEscapeEvent) {
|
2018-09-26 16:39:01 +08:00
|
|
|
return
|
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2021-10-10 21:47:02 +08:00
|
|
|
if (isInput && !isEscapeEvent) {
|
2021-04-02 02:44:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
event.preventDefault()
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2023-03-30 01:49:30 +08:00
|
|
|
// TODO: v6 revert #37011 & change markup https://getbootstrap.com/docs/5.3/forms/input-group/
|
2022-09-27 15:39:11 +08:00
|
|
|
const getToggleButton = this.matches(SELECTOR_DATA_TOGGLE) ?
|
|
|
|
this :
|
|
|
|
(SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0] ||
|
|
|
|
SelectorEngine.next(this, SELECTOR_DATA_TOGGLE)[0] ||
|
|
|
|
SelectorEngine.findOne(SELECTOR_DATA_TOGGLE, event.delegateTarget.parentNode))
|
|
|
|
|
2021-07-08 23:44:58 +08:00
|
|
|
const instance = Dropdown.getOrCreateInstance(getToggleButton)
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2022-03-10 19:24:47 +08:00
|
|
|
if (isUpOrDownEvent) {
|
|
|
|
event.stopPropagation()
|
|
|
|
instance.show()
|
|
|
|
instance._selectMenuItem(event)
|
2020-02-01 21:56:20 +08:00
|
|
|
return
|
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2022-03-10 19:24:47 +08:00
|
|
|
if (instance._isShown()) { // else is escape and we check if it is shown
|
|
|
|
event.stopPropagation()
|
|
|
|
instance.hide()
|
|
|
|
getToggleButton.focus()
|
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
}
|
2018-09-26 16:39:01 +08:00
|
|
|
}
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2018-09-26 16:39:01 +08:00
|
|
|
/**
|
2021-10-13 20:19:28 +08:00
|
|
|
* Data API implementation
|
2018-09-26 16:39:01 +08:00
|
|
|
*/
|
2015-05-11 04:47:11 +08:00
|
|
|
|
2020-03-07 17:31:42 +08: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 15:09:40 +08:00
|
|
|
event.preventDefault()
|
2021-07-08 22:59:18 +08:00
|
|
|
Dropdown.getOrCreateInstance(this).toggle()
|
2017-09-26 15:09:40 +08:00
|
|
|
})
|
2018-09-26 16:39:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery
|
|
|
|
*/
|
2020-11-01 21:32:36 +08:00
|
|
|
|
2021-05-11 15:49:30 +08:00
|
|
|
defineJQueryPlugin(Dropdown)
|
2015-05-11 04:47:11 +08:00
|
|
|
|
|
|
|
export default Dropdown
|