2015-05-10 13:47:11 -07:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2019-02-13 18:01:40 +02:00
|
|
|
* Bootstrap (v4.3.1): dropdown.js
|
2015-05-10 13:47:11 -07:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
import {
|
2019-08-02 15:51:05 +02:00
|
|
|
getjQuery,
|
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,
|
2019-02-23 00:37:55 +02:00
|
|
|
makeArray,
|
|
|
|
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'
|
2018-11-14 10:16:56 +01:00
|
|
|
import Popper from 'popper.js'
|
2019-10-02 11:43:54 +02:00
|
|
|
import SelectorEngine from './dom/selector-engine'
|
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 VERSION = '4.3.1'
|
|
|
|
const DATA_KEY = 'bs.dropdown'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
|
|
|
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
|
2018-09-26 10:39:01 +02:00
|
|
|
const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
|
2019-02-26 13:20:34 +02:00
|
|
|
const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
const Event = {
|
2019-02-26 13:20:34 +02:00
|
|
|
HIDE: `hide${EVENT_KEY}`,
|
|
|
|
HIDDEN: `hidden${EVENT_KEY}`,
|
|
|
|
SHOW: `show${EVENT_KEY}`,
|
|
|
|
SHOWN: `shown${EVENT_KEY}`,
|
|
|
|
CLICK: `click${EVENT_KEY}`,
|
|
|
|
CLICK_DATA_API: `click${EVENT_KEY}${DATA_API_KEY}`,
|
|
|
|
KEYDOWN_DATA_API: `keydown${EVENT_KEY}${DATA_API_KEY}`,
|
|
|
|
KEYUP_DATA_API: `keyup${EVENT_KEY}${DATA_API_KEY}`
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ClassName = {
|
2019-02-26 13:20:34 +02:00
|
|
|
DISABLED: 'disabled',
|
|
|
|
SHOW: 'show',
|
|
|
|
DROPUP: 'dropup',
|
|
|
|
DROPRIGHT: 'dropright',
|
|
|
|
DROPLEFT: 'dropleft',
|
|
|
|
MENURIGHT: 'dropdown-menu-right',
|
|
|
|
POSITION_STATIC: 'position-static'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
2019-02-26 13:20:34 +02:00
|
|
|
DATA_TOGGLE: '[data-toggle="dropdown"]',
|
|
|
|
FORM_CHILD: '.dropdown form',
|
|
|
|
MENU: '.dropdown-menu',
|
|
|
|
NAVBAR_NAV: '.navbar-nav',
|
|
|
|
VISIBLE_ITEMS: '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const AttachmentMap = {
|
2019-02-26 13:20:34 +02:00
|
|
|
TOP: 'top-start',
|
|
|
|
TOPEND: 'top-end',
|
|
|
|
BOTTOM: 'bottom-start',
|
|
|
|
BOTTOMEND: 'bottom-end',
|
|
|
|
RIGHT: 'right-start',
|
|
|
|
RIGHTEND: 'right-end',
|
|
|
|
LEFT: 'left-start',
|
|
|
|
LEFTEND: 'left-end'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Default = {
|
2019-02-26 13:20:34 +02:00
|
|
|
offset: 0,
|
|
|
|
flip: true,
|
|
|
|
boundary: 'scrollParent',
|
|
|
|
reference: 'toggle',
|
2019-08-14 17:27:58 +02:00
|
|
|
display: 'dynamic',
|
|
|
|
popperConfig: null
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultType = {
|
2019-02-26 13:20:34 +02:00
|
|
|
offset: '(number|string|function)',
|
|
|
|
flip: 'boolean',
|
|
|
|
boundary: '(string|element)',
|
|
|
|
reference: '(string|element)',
|
2019-08-14 17:27:58 +02:00
|
|
|
display: 'string',
|
|
|
|
popperConfig: '(null|object)'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
class Dropdown {
|
|
|
|
constructor(element, config) {
|
2019-02-26 13:20:34 +02:00
|
|
|
this._element = element
|
|
|
|
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()
|
2018-06-24 22:02:03 +02:00
|
|
|
Data.setData(element, DATA_KEY, this)
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
|
|
|
|
|
|
|
static get VERSION() {
|
|
|
|
return VERSION
|
2017-04-17 14:26:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
|
|
|
|
|
|
|
toggle() {
|
2017-09-26 09:09:40 +02:00
|
|
|
if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
const isActive = this._menu.classList.contains(ClassName.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() {
|
|
|
|
if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || this._menu.classList.contains(ClassName.SHOW)) {
|
|
|
|
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
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
const showEvent = EventHandler.trigger(parent, 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
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Disable totally Popper.js for Dropdown in Navbar
|
|
|
|
if (!this._inNavbar) {
|
|
|
|
if (typeof Popper === 'undefined') {
|
2019-02-20 10:45:46 +02:00
|
|
|
throw new TypeError('Bootstrap\'s dropdowns require Popper.js (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
|
|
|
}
|
2017-05-22 16:14:10 +02:00
|
|
|
}
|
2017-10-27 09:51:08 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// 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') {
|
2017-09-26 09:09:40 +02:00
|
|
|
parent.classList.add(ClassName.POSITION_STATIC)
|
2017-03-18 20:41:13 -04:00
|
|
|
}
|
2019-02-26 13:20:34 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
|
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 &&
|
2019-02-23 00:37:55 +02:00
|
|
|
!makeArray(SelectorEngine.closest(parent, Selector.NAVBAR_NAV)).length) {
|
|
|
|
makeArray(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
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
Manipulator.toggleClass(this._menu, ClassName.SHOW)
|
|
|
|
Manipulator.toggleClass(parent, ClassName.SHOW)
|
|
|
|
EventHandler.trigger(parent, 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() {
|
2017-09-26 09:09:40 +02:00
|
|
|
if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || !this._menu.classList.contains(ClassName.SHOW)) {
|
2018-10-20 13:27:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
const parent = Dropdown.getParentFromElement(this._element)
|
2018-10-20 13:27:51 +02:00
|
|
|
const relatedTarget = {
|
|
|
|
relatedTarget: this._element
|
|
|
|
}
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
const hideEvent = EventHandler.trigger(parent, 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()
|
|
|
|
}
|
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
Manipulator.toggleClass(this._menu, ClassName.SHOW)
|
|
|
|
Manipulator.toggleClass(parent, ClassName.SHOW)
|
2018-07-25 11:29:16 +02:00
|
|
|
EventHandler.trigger(parent, 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
|
|
|
Data.removeData(this._element, DATA_KEY)
|
|
|
|
EventHandler.off(this._element, EVENT_KEY)
|
2018-09-26 10:39:01 +02:00
|
|
|
this._element = null
|
|
|
|
this._menu = null
|
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
|
|
|
}
|
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) {
|
2018-09-26 10:39:01 +02:00
|
|
|
this._popper.scheduleUpdate()
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 11:25:53 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_addEventListeners() {
|
2019-02-26 13:20:34 +02:00
|
|
|
EventHandler.on(this._element, Event.CLICK, event => {
|
2018-09-26 10:39:01 +02:00
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
typeCheckConfig(
|
2018-09-26 10:39:01 +02:00
|
|
|
NAME,
|
|
|
|
config,
|
|
|
|
this.constructor.DefaultType
|
|
|
|
)
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
_getMenuElement() {
|
2019-07-28 15:24:46 +02:00
|
|
|
const parent = Dropdown.getParentFromElement(this._element)
|
2019-02-26 13:20:34 +02:00
|
|
|
|
2019-04-10 10:46:50 +02:00
|
|
|
return SelectorEngine.findOne(Selector.MENU, parent)
|
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
|
2019-02-26 13:20:34 +02:00
|
|
|
let placement = AttachmentMap.BOTTOM
|
2017-05-22 16:14:10 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Handle dropup
|
2017-09-26 09:09:40 +02:00
|
|
|
if (parentDropdown.classList.contains(ClassName.DROPUP)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
placement = AttachmentMap.TOP
|
2017-09-26 09:09:40 +02:00
|
|
|
if (this._menu.classList.contains(ClassName.MENURIGHT)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
placement = AttachmentMap.TOPEND
|
2017-05-17 22:03:11 +02:00
|
|
|
}
|
2017-09-26 09:09:40 +02:00
|
|
|
} else if (parentDropdown.classList.contains(ClassName.DROPRIGHT)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
placement = AttachmentMap.RIGHT
|
2017-09-26 09:09:40 +02:00
|
|
|
} else if (parentDropdown.classList.contains(ClassName.DROPLEFT)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
placement = AttachmentMap.LEFT
|
2017-09-26 09:09:40 +02:00
|
|
|
} else if (this._menu.classList.contains(ClassName.MENURIGHT)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
placement = AttachmentMap.BOTTOMEND
|
2017-05-17 22:03:11 +02:00
|
|
|
}
|
2019-02-26 13:20:34 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
return placement
|
|
|
|
}
|
2017-05-17 22:03:11 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_detectNavbar() {
|
2018-09-14 14:27:30 +02:00
|
|
|
return Boolean(SelectorEngine.closest(this._element, '.navbar'))
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2017-06-04 17:56:51 +02:00
|
|
|
|
2019-02-05 03:31:18 -05:00
|
|
|
_getOffset() {
|
|
|
|
const offset = {}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (typeof this._config.offset === 'function') {
|
2019-02-26 13:20:34 +02:00
|
|
|
offset.fn = data => {
|
2018-09-26 10:39:01 +02:00
|
|
|
data.offsets = {
|
|
|
|
...data.offsets,
|
2019-02-05 03:31:18 -05:00
|
|
|
...this._config.offset(data.offsets, this._element) || {}
|
2017-10-03 15:34:44 +03:00
|
|
|
}
|
2019-02-05 03:31:18 -05:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
return data
|
2017-10-03 15:34:44 +03:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
} else {
|
2019-02-05 03:31:18 -05:00
|
|
|
offset.offset = this._config.offset
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2018-06-01 14:44:21 +02:00
|
|
|
|
2019-02-05 03:31:18 -05:00
|
|
|
return offset
|
|
|
|
}
|
|
|
|
|
|
|
|
_getPopperConfig() {
|
2019-08-14 18:02:41 +02:00
|
|
|
const popperConfig = {
|
2018-09-26 10:39:01 +02:00
|
|
|
placement: this._getPlacement(),
|
|
|
|
modifiers: {
|
2019-02-05 03:31:18 -05:00
|
|
|
offset: this._getOffset(),
|
2018-09-26 10:39:01 +02:00
|
|
|
flip: {
|
|
|
|
enabled: this._config.flip
|
|
|
|
},
|
|
|
|
preventOverflow: {
|
|
|
|
boundariesElement: this._config.boundary
|
2017-06-04 17:56:51 +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
|
|
|
// Disable Popper.js if we have a static display
|
|
|
|
if (this._config.display === 'static') {
|
|
|
|
popperConfig.modifiers.applyStyle = {
|
|
|
|
enabled: false
|
2018-02-12 01:45:59 +01: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 {
|
|
|
|
...popperConfig,
|
|
|
|
...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) {
|
2017-09-26 09:09:40 +02:00
|
|
|
let data = Data.getData(element, DATA_KEY)
|
|
|
|
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) {
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
|
2019-08-22 21:17:34 +02:00
|
|
|
(event.type === 'keyup' && event.which !== TAB_KEYCODE))) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
const toggles = makeArray(SelectorEngine.find(Selector.DATA_TOGGLE))
|
2018-09-26 10:39:01 +02:00
|
|
|
for (let i = 0, len = toggles.length; i < len; i++) {
|
2019-07-28 15:24:46 +02:00
|
|
|
const parent = Dropdown.getParentFromElement(toggles[i])
|
2019-02-26 13:20:34 +02:00
|
|
|
const context = Data.getData(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
|
2017-09-26 09:09:40 +02:00
|
|
|
if (!parent.classList.contains(ClassName.SHOW)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-08-22 21:17:34 +02:00
|
|
|
if (event && ((event.type === 'click' &&
|
|
|
|
/input|textarea/i.test(event.target.tagName)) ||
|
|
|
|
(event.type === 'keyup' && event.which === TAB_KEYCODE)) &&
|
2017-09-26 09:09:40 +02:00
|
|
|
parent.contains(event.target)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
const hideEvent = EventHandler.trigger(parent, Event.HIDE, relatedTarget)
|
|
|
|
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) {
|
2019-02-23 00:37:55 +02:00
|
|
|
makeArray(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()
|
|
|
|
}
|
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
dropdownMenu.classList.remove(ClassName.SHOW)
|
|
|
|
parent.classList.remove(ClassName.SHOW)
|
|
|
|
EventHandler.trigger(parent, 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) ?
|
2019-08-22 21:17:34 +02:00
|
|
|
event.which === SPACE_KEYCODE || (event.which !== ESCAPE_KEYCODE &&
|
|
|
|
((event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE) ||
|
|
|
|
SelectorEngine.closest(event.target, Selector.MENU))) :
|
2019-02-26 13:20:34 +02:00
|
|
|
!REGEXP_KEYDOWN.test(event.which)) {
|
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
|
|
|
|
2017-09-26 09:09:40 +02:00
|
|
|
if (this.disabled || this.classList.contains(ClassName.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)
|
2017-09-26 09:09:40 +02:00
|
|
|
const isActive = parent.classList.contains(ClassName.SHOW)
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2019-08-22 21:17:34 +02:00
|
|
|
if (!isActive || (isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE))) {
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event.which === ESCAPE_KEYCODE) {
|
2019-06-19 10:23:41 +01:00
|
|
|
SelectorEngine.findOne(Selector.DATA_TOGGLE, parent).focus()
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
const items = makeArray(SelectorEngine.find(Selector.VISIBLE_ITEMS, parent))
|
2019-10-17 15:01:44 +00:00
|
|
|
.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
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
let index = items.indexOf(event.target)
|
2015-08-18 19:22:46 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event.which === ARROW_UP_KEYCODE && index > 0) { // Up
|
|
|
|
index--
|
|
|
|
}
|
2015-08-18 19:22:46 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // Down
|
|
|
|
index++
|
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (index < 0) {
|
|
|
|
index = 0
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
items[index].focus()
|
2015-05-10 13:47:11 -07:00
|
|
|
}
|
2018-06-07 21:43:04 +02:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static getInstance(element) {
|
2018-06-07 21:43:04 +02:00
|
|
|
return Data.getData(element, DATA_KEY)
|
|
|
|
}
|
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
|
|
|
|
2019-07-28 15:24:46 +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)
|
2017-09-26 09:09:40 +02:00
|
|
|
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
|
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
2019-07-28 15:24:46 +02:00
|
|
|
Dropdown.dropdownInterface(this, 'toggle')
|
2017-09-26 09:09:40 +02:00
|
|
|
})
|
|
|
|
EventHandler
|
2019-02-26 13:20:34 +02:00
|
|
|
.on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, e => e.stopPropagation())
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2019-08-02 15:51:05 +02:00
|
|
|
const $ = getjQuery()
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
2017-09-26 09:09:40 +02:00
|
|
|
* add .dropdown to jQuery only if jQuery is present
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2019-04-10 10:46:50 +02:00
|
|
|
/* istanbul ignore if */
|
2019-08-02 15:51:05 +02:00
|
|
|
if ($) {
|
2017-09-26 09:09:40 +02:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
2019-07-28 15:24:46 +02:00
|
|
|
$.fn[NAME] = Dropdown.jQueryInterface
|
2019-02-26 13:20:34 +02:00
|
|
|
$.fn[NAME].Constructor = Dropdown
|
|
|
|
$.fn[NAME].noConflict = () => {
|
2017-09-26 09:09:40 +02:00
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
2019-07-28 15:24:46 +02:00
|
|
|
return Dropdown.jQueryInterface
|
2017-09-26 09:09:40 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-10 13:47:11 -07:00
|
|
|
|
|
|
|
export default Dropdown
|