aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/twbs/bootstrap/js/src/dropdown.js
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2021-06-07 12:56:27 +0200
committerMario <mario@mariovavti.com>2021-06-07 12:56:27 +0200
commitf3b4308cb59bf4b21ff186f8479c82239446d139 (patch)
tree0dcbdcffcfe0dd678958cbcc34f41cb1c470c69b /vendor/twbs/bootstrap/js/src/dropdown.js
parent67322c12643ced03bec0be70667f8b1c45de752f (diff)
downloadvolse-hubzilla-f3b4308cb59bf4b21ff186f8479c82239446d139.tar.gz
volse-hubzilla-f3b4308cb59bf4b21ff186f8479c82239446d139.tar.bz2
volse-hubzilla-f3b4308cb59bf4b21ff186f8479c82239446d139.zip
upgrade to bootstrap 5.0.1 and first batch of fixes
Diffstat (limited to 'vendor/twbs/bootstrap/js/src/dropdown.js')
-rw-r--r--vendor/twbs/bootstrap/js/src/dropdown.js531
1 files changed, 262 insertions, 269 deletions
diff --git a/vendor/twbs/bootstrap/js/src/dropdown.js b/vendor/twbs/bootstrap/js/src/dropdown.js
index 76dda8f57..565edb892 100644
--- a/vendor/twbs/bootstrap/js/src/dropdown.js
+++ b/vendor/twbs/bootstrap/js/src/dropdown.js
@@ -1,13 +1,28 @@
/**
* --------------------------------------------------------------------------
- * Bootstrap (v4.6.0): dropdown.js
+ * Bootstrap (v5.0.1): dropdown.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
-import $ from 'jquery'
-import Popper from 'popper.js'
-import Util from './util'
+import * as Popper from '@popperjs/core'
+
+import {
+ defineJQueryPlugin,
+ getElement,
+ getElementFromSelector,
+ isDisabled,
+ isElement,
+ isVisible,
+ isRTL,
+ noop,
+ typeCheckConfig
+} 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'
+import BaseComponent from './base-component'
/**
* ------------------------------------------------------------------------
@@ -16,18 +31,18 @@ import Util from './util'
*/
const NAME = 'dropdown'
-const VERSION = '4.6.0'
const DATA_KEY = 'bs.dropdown'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
-const JQUERY_NO_CONFLICT = $.fn[NAME]
-const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
-const SPACE_KEYCODE = 32 // KeyboardEvent.which value for space key
-const TAB_KEYCODE = 9 // KeyboardEvent.which value for tab key
-const ARROW_UP_KEYCODE = 38 // KeyboardEvent.which value for up arrow key
-const ARROW_DOWN_KEYCODE = 40 // KeyboardEvent.which value for down arrow key
-const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
-const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)
+
+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}`
@@ -38,43 +53,40 @@ 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'
-const CLASS_NAME_DROPRIGHT = 'dropright'
-const CLASS_NAME_DROPLEFT = 'dropleft'
-const CLASS_NAME_MENURIGHT = 'dropdown-menu-right'
-const CLASS_NAME_POSITION_STATIC = 'position-static'
+const CLASS_NAME_DROPEND = 'dropend'
+const CLASS_NAME_DROPSTART = 'dropstart'
+const CLASS_NAME_NAVBAR = 'navbar'
-const SELECTOR_DATA_TOGGLE = '[data-toggle="dropdown"]'
-const SELECTOR_FORM_CHILD = '.dropdown form'
+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 = 'top-start'
-const PLACEMENT_TOPEND = 'top-end'
-const PLACEMENT_BOTTOM = 'bottom-start'
-const PLACEMENT_BOTTOMEND = 'bottom-end'
-const PLACEMENT_RIGHT = 'right-start'
-const PLACEMENT_LEFT = 'left-start'
+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,
- flip: true,
- boundary: 'scrollParent',
+ offset: [0, 2],
+ boundary: 'clippingParents',
reference: 'toggle',
display: 'dynamic',
- popperConfig: null
+ popperConfig: null,
+ autoClose: true
}
const DefaultType = {
- offset: '(number|string|function)',
- flip: 'boolean',
+ offset: '(array|string|function)',
boundary: '(string|element)',
- reference: '(string|element)',
+ reference: '(string|element|object)',
display: 'string',
- popperConfig: '(null|object)'
+ popperConfig: '(null|object|function)',
+ autoClose: '(boolean|string)'
}
/**
@@ -83,9 +95,10 @@ const DefaultType = {
* ------------------------------------------------------------------------
*/
-class Dropdown {
+class Dropdown extends BaseComponent {
constructor(element, config) {
- this._element = element
+ super(element)
+
this._popper = null
this._config = this._getConfig(config)
this._menu = this._getMenuElement()
@@ -96,10 +109,6 @@ class Dropdown {
// Getters
- static get VERSION() {
- return VERSION
- }
-
static get Default() {
return Default
}
@@ -108,47 +117,47 @@ class Dropdown {
return DefaultType
}
+ static get NAME() {
+ return NAME
+ }
+
// Public
toggle() {
- if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED)) {
+ if (isDisabled(this._element)) {
return
}
- const isActive = $(this._menu).hasClass(CLASS_NAME_SHOW)
-
- Dropdown._clearMenus()
+ const isActive = this._element.classList.contains(CLASS_NAME_SHOW)
if (isActive) {
+ this.hide()
return
}
- this.show(true)
+ this.show()
}
- show(usePopper = false) {
- if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || $(this._menu).hasClass(CLASS_NAME_SHOW)) {
+ show() {
+ if (isDisabled(this._element) || this._menu.classList.contains(CLASS_NAME_SHOW)) {
return
}
+ const parent = Dropdown.getParentFromElement(this._element)
const relatedTarget = {
relatedTarget: this._element
}
- const showEvent = $.Event(EVENT_SHOW, relatedTarget)
- const parent = Dropdown._getParentFromElement(this._element)
- $(parent).trigger(showEvent)
+ const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, relatedTarget)
- if (showEvent.isDefaultPrevented()) {
+ if (showEvent.defaultPrevented) {
return
}
// Totally disable Popper for Dropdowns in Navbar
- if (!this._inNavbar && usePopper) {
- /**
- * Check for Popper dependency
- * Popper - https://popper.js.org
- */
+ if (this._inNavbar) {
+ Manipulator.setDataAttribute(this._menu, 'popper', 'none')
+ } else {
if (typeof Popper === 'undefined') {
throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)')
}
@@ -157,23 +166,20 @@ class Dropdown {
if (this._config.reference === 'parent') {
referenceElement = parent
- } else if (Util.isElement(this._config.reference)) {
+ } else if (isElement(this._config.reference)) {
+ referenceElement = getElement(this._config.reference)
+ } else if (typeof this._config.reference === 'object') {
referenceElement = this._config.reference
-
- // Check if it's jQuery element
- if (typeof this._config.reference.jquery !== 'undefined') {
- referenceElement = this._config.reference[0]
- }
}
- // If boundary is not `scrollParent`, then set position to `static`
- // to allow the menu to "escape" the scroll parent's boundaries
- // https://github.com/twbs/bootstrap/issues/24251
- if (this._config.boundary !== 'scrollParent') {
- $(parent).addClass(CLASS_NAME_POSITION_STATIC)
- }
+ const popperConfig = this._getPopperConfig()
+ const isDisplayStatic = popperConfig.modifiers.find(modifier => modifier.name === 'applyStyles' && modifier.enabled === false)
- this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
+ this._popper = Popper.createPopper(referenceElement, this._menu, popperConfig)
+
+ if (isDisplayStatic) {
+ Manipulator.setDataAttribute(this._menu, 'popper', 'static')
+ }
}
// If this is a touch-enabled device we add extra
@@ -181,266 +187,273 @@ class Dropdown {
// 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 &&
- $(parent).closest(SELECTOR_NAVBAR_NAV).length === 0) {
- $(document.body).children().on('mouseover', null, $.noop)
+ !parent.closest(SELECTOR_NAVBAR_NAV)) {
+ [].concat(...document.body.children)
+ .forEach(elem => EventHandler.on(elem, 'mouseover', noop))
}
this._element.focus()
this._element.setAttribute('aria-expanded', true)
- $(this._menu).toggleClass(CLASS_NAME_SHOW)
- $(parent)
- .toggleClass(CLASS_NAME_SHOW)
- .trigger($.Event(EVENT_SHOWN, relatedTarget))
+ this._menu.classList.toggle(CLASS_NAME_SHOW)
+ this._element.classList.toggle(CLASS_NAME_SHOW)
+ EventHandler.trigger(this._element, EVENT_SHOWN, relatedTarget)
}
hide() {
- if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || !$(this._menu).hasClass(CLASS_NAME_SHOW)) {
+ if (isDisabled(this._element) || !this._menu.classList.contains(CLASS_NAME_SHOW)) {
return
}
const relatedTarget = {
relatedTarget: this._element
}
- const hideEvent = $.Event(EVENT_HIDE, relatedTarget)
- const parent = Dropdown._getParentFromElement(this._element)
-
- $(parent).trigger(hideEvent)
- if (hideEvent.isDefaultPrevented()) {
- return
- }
-
- if (this._popper) {
- this._popper.destroy()
- }
-
- $(this._menu).toggleClass(CLASS_NAME_SHOW)
- $(parent)
- .toggleClass(CLASS_NAME_SHOW)
- .trigger($.Event(EVENT_HIDDEN, relatedTarget))
+ this._completeHide(relatedTarget)
}
dispose() {
- $.removeData(this._element, DATA_KEY)
- $(this._element).off(EVENT_KEY)
- this._element = null
- this._menu = null
- if (this._popper !== null) {
+ if (this._popper) {
this._popper.destroy()
- this._popper = null
}
+
+ super.dispose()
}
update() {
this._inNavbar = this._detectNavbar()
- if (this._popper !== null) {
- this._popper.scheduleUpdate()
+ if (this._popper) {
+ this._popper.update()
}
}
// Private
_addEventListeners() {
- $(this._element).on(EVENT_CLICK, event => {
+ EventHandler.on(this._element, EVENT_CLICK, event => {
event.preventDefault()
- event.stopPropagation()
this.toggle()
})
}
+ _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) {
+ [].concat(...document.body.children)
+ .forEach(elem => EventHandler.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.removeDataAttribute(this._menu, 'popper')
+ EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget)
+ }
+
_getConfig(config) {
config = {
...this.constructor.Default,
- ...$(this._element).data(),
+ ...Manipulator.getDataAttributes(this._element),
...config
}
- Util.typeCheckConfig(
- NAME,
- config,
- this.constructor.DefaultType
- )
+ 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.`)
+ }
return config
}
_getMenuElement() {
- if (!this._menu) {
- const parent = Dropdown._getParentFromElement(this._element)
+ return SelectorEngine.next(this._element, SELECTOR_MENU)[0]
+ }
- if (parent) {
- this._menu = parent.querySelector(SELECTOR_MENU)
- }
+ _getPlacement() {
+ const parentDropdown = this._element.parentNode
+
+ if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
+ return PLACEMENT_RIGHT
}
- return this._menu
- }
+ if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
+ return PLACEMENT_LEFT
+ }
- _getPlacement() {
- const $parentDropdown = $(this._element.parentNode)
- let placement = PLACEMENT_BOTTOM
-
- // Handle dropup
- if ($parentDropdown.hasClass(CLASS_NAME_DROPUP)) {
- placement = $(this._menu).hasClass(CLASS_NAME_MENURIGHT) ?
- PLACEMENT_TOPEND :
- PLACEMENT_TOP
- } else if ($parentDropdown.hasClass(CLASS_NAME_DROPRIGHT)) {
- placement = PLACEMENT_RIGHT
- } else if ($parentDropdown.hasClass(CLASS_NAME_DROPLEFT)) {
- placement = PLACEMENT_LEFT
- } else if ($(this._menu).hasClass(CLASS_NAME_MENURIGHT)) {
- placement = PLACEMENT_BOTTOMEND
- }
-
- return placement
+ // We need to trim the value because custom properties can also include spaces
+ 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
}
_detectNavbar() {
- return $(this._element).closest('.navbar').length > 0
+ return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null
}
_getOffset() {
- const offset = {}
+ const { offset } = this._config
- if (typeof this._config.offset === 'function') {
- offset.fn = data => {
- data.offsets = {
- ...data.offsets,
- ...(this._config.offset(data.offsets, this._element) || {})
- }
+ if (typeof offset === 'string') {
+ return offset.split(',').map(val => Number.parseInt(val, 10))
+ }
- return data
- }
- } else {
- offset.offset = this._config.offset
+ if (typeof offset === 'function') {
+ return popperData => offset(popperData, this._element)
}
return offset
}
_getPopperConfig() {
- const popperConfig = {
+ const defaultBsPopperConfig = {
placement: this._getPlacement(),
- modifiers: {
- offset: this._getOffset(),
- flip: {
- enabled: this._config.flip
- },
- preventOverflow: {
- boundariesElement: this._config.boundary
+ modifiers: [{
+ name: 'preventOverflow',
+ options: {
+ boundary: this._config.boundary
}
- }
+ },
+ {
+ name: 'offset',
+ options: {
+ offset: this._getOffset()
+ }
+ }]
}
// Disable Popper if we have a static display
if (this._config.display === 'static') {
- popperConfig.modifiers.applyStyle = {
+ defaultBsPopperConfig.modifiers = [{
+ name: 'applyStyles',
enabled: false
- }
+ }]
}
return {
- ...popperConfig,
- ...this._config.popperConfig
+ ...defaultBsPopperConfig,
+ ...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
}
}
+ _selectMenuItem(event) {
+ const items = SelectorEngine.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()
+ }
+
// Static
- static _jQueryInterface(config) {
- return this.each(function () {
- let data = $(this).data(DATA_KEY)
- const _config = typeof config === 'object' ? config : null
+ static dropdownInterface(element, config) {
+ let data = Data.get(element, DATA_KEY)
+ const _config = typeof config === 'object' ? config : null
+
+ if (!data) {
+ data = new Dropdown(element, _config)
+ }
- if (!data) {
- data = new Dropdown(this, _config)
- $(this).data(DATA_KEY, data)
+ if (typeof config === 'string') {
+ if (typeof data[config] === 'undefined') {
+ throw new TypeError(`No method named "${config}"`)
}
- if (typeof config === 'string') {
- if (typeof data[config] === 'undefined') {
- throw new TypeError(`No method named "${config}"`)
- }
+ data[config]()
+ }
+ }
- data[config]()
- }
+ static jQueryInterface(config) {
+ return this.each(function () {
+ Dropdown.dropdownInterface(this, config)
})
}
- static _clearMenus(event) {
- if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
- event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
+ static clearMenus(event) {
+ if (event && (event.button === RIGHT_MOUSE_BUTTON || (event.type === 'keyup' && event.key !== TAB_KEY))) {
return
}
- const toggles = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE))
+ const toggles = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
for (let i = 0, len = toggles.length; i < len; i++) {
- const parent = Dropdown._getParentFromElement(toggles[i])
- const context = $(toggles[i]).data(DATA_KEY)
- const relatedTarget = {
- relatedTarget: toggles[i]
- }
-
- if (event && event.type === 'click') {
- relatedTarget.clickEvent = event
- }
-
- if (!context) {
- continue
- }
-
- const dropdownMenu = context._menu
- if (!$(parent).hasClass(CLASS_NAME_SHOW)) {
+ const context = Data.get(toggles[i], DATA_KEY)
+ if (!context || context._config.autoClose === false) {
continue
}
- if (event && (event.type === 'click' &&
- /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) &&
- $.contains(parent, event.target)) {
+ if (!context._element.classList.contains(CLASS_NAME_SHOW)) {
continue
}
- const hideEvent = $.Event(EVENT_HIDE, relatedTarget)
- $(parent).trigger(hideEvent)
- if (hideEvent.isDefaultPrevented()) {
- continue
+ const relatedTarget = {
+ relatedTarget: context._element
}
- // If this is a touch-enabled device we remove the extra
- // empty mouseover listeners we added for iOS support
- if ('ontouchstart' in document.documentElement) {
- $(document.body).children().off('mouseover', null, $.noop)
- }
+ 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)
+ ) {
+ continue
+ }
- toggles[i].setAttribute('aria-expanded', 'false')
+ // 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
+ }
- if (context._popper) {
- context._popper.destroy()
+ if (event.type === 'click') {
+ relatedTarget.clickEvent = event
+ }
}
- $(dropdownMenu).removeClass(CLASS_NAME_SHOW)
- $(parent)
- .removeClass(CLASS_NAME_SHOW)
- .trigger($.Event(EVENT_HIDDEN, relatedTarget))
+ context._completeHide(relatedTarget)
}
}
- static _getParentFromElement(element) {
- let parent
- const selector = Util.getSelectorFromElement(element)
-
- if (selector) {
- parent = document.querySelector(selector)
- }
-
- return parent || element.parentNode
+ static getParentFromElement(element) {
+ return getElementFromSelector(element) || element.parentNode
}
- // eslint-disable-next-line complexity
- static _dataApiKeydownHandler(event) {
+ static dataApiKeydownHandler(event) {
// If not input/textarea:
// - And not a key in REGEXP_KEYDOWN => not a dropdown command
// If input/textarea:
@@ -449,57 +462,45 @@ class Dropdown {
// - If key is not up or down => not a dropdown command
// - If trigger inside the menu => not a dropdown command
if (/input|textarea/i.test(event.target.tagName) ?
- event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE &&
- (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE ||
- $(event.target).closest(SELECTOR_MENU).length) : !REGEXP_KEYDOWN.test(event.which)) {
- return
- }
-
- if (this.disabled || $(this).hasClass(CLASS_NAME_DISABLED)) {
+ 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)) {
return
}
- const parent = Dropdown._getParentFromElement(this)
- const isActive = $(parent).hasClass(CLASS_NAME_SHOW)
+ const isActive = this.classList.contains(CLASS_NAME_SHOW)
- if (!isActive && event.which === ESCAPE_KEYCODE) {
+ if (!isActive && event.key === ESCAPE_KEY) {
return
}
event.preventDefault()
event.stopPropagation()
- if (!isActive || (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
- if (event.which === ESCAPE_KEYCODE) {
- $(parent.querySelector(SELECTOR_DATA_TOGGLE)).trigger('focus')
- }
-
- $(this).trigger('click')
+ if (isDisabled(this)) {
return
}
- const items = [].slice.call(parent.querySelectorAll(SELECTOR_VISIBLE_ITEMS))
- .filter(item => $(item).is(':visible'))
+ const getToggleButton = () => this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
- if (items.length === 0) {
+ if (event.key === ESCAPE_KEY) {
+ getToggleButton().focus()
+ Dropdown.clearMenus()
return
}
- let index = items.indexOf(event.target)
-
- if (event.which === ARROW_UP_KEYCODE && index > 0) { // Up
- index--
- }
-
- if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // Down
- index++
+ if (!isActive && (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY)) {
+ getToggleButton().click()
+ return
}
- if (index < 0) {
- index = 0
+ if (!isActive || event.key === SPACE_KEY) {
+ Dropdown.clearMenus()
+ return
}
- items[index].focus()
+ Dropdown.getInstance(getToggleButton())._selectMenuItem(event)
}
}
@@ -509,30 +510,22 @@ class Dropdown {
* ------------------------------------------------------------------------
*/
-$(document)
- .on(EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown._dataApiKeydownHandler)
- .on(EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown._dataApiKeydownHandler)
- .on(`${EVENT_CLICK_DATA_API} ${EVENT_KEYUP_DATA_API}`, Dropdown._clearMenus)
- .on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
- event.preventDefault()
- event.stopPropagation()
- Dropdown._jQueryInterface.call($(this), 'toggle')
- })
- .on(EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, e => {
- e.stopPropagation()
- })
+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) {
+ event.preventDefault()
+ Dropdown.dropdownInterface(this)
+})
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
+ * add .Dropdown to jQuery only if jQuery is present
*/
-$.fn[NAME] = Dropdown._jQueryInterface
-$.fn[NAME].Constructor = Dropdown
-$.fn[NAME].noConflict = () => {
- $.fn[NAME] = JQUERY_NO_CONFLICT
- return Dropdown._jQueryInterface
-}
+defineJQueryPlugin(Dropdown)
export default Dropdown