0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/dist/dropdown.js

733 lines
24 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap dropdown.js v5.0.0-beta2 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 07:41:12 +01:00
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@popperjs/core'), require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./dom/selector-engine.js'), require('./base-component.js')) :
typeof define === 'function' && define.amd ? define(['@popperjs/core', './dom/data', './dom/event-handler', './dom/manipulator', './dom/selector-engine', './base-component'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global.Popper, global.Data, global.EventHandler, global.Manipulator, global.SelectorEngine, global.Base));
}(this, (function (Popper, Data, EventHandler, Manipulator, SelectorEngine, BaseComponent) { 'use strict';
2019-03-01 17:31:34 +01:00
2020-09-14 17:12:06 +02:00
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
2020-12-07 16:50:24 +01:00
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var Popper__namespace = /*#__PURE__*/_interopNamespace(Popper);
2020-09-14 17:12:06 +02:00
var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
var SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
2018-07-24 02:51:14 +02:00
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.0-beta2): util/index.js
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 17:31:34 +01:00
* --------------------------------------------------------------------------
*/
var toType = function toType(obj) {
2020-03-28 11:29:08 +01:00
if (obj === null || obj === undefined) {
return "" + obj;
}
2019-03-01 17:31:34 +01:00
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
};
2019-08-27 15:03:21 +02:00
var getSelector = function getSelector(element) {
2020-11-23 14:17:16 +01:00
var selector = element.getAttribute('data-bs-target');
2019-03-01 17:31:34 +01:00
if (!selector || selector === '#') {
var hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttr || !hrefAttr.includes('#') && !hrefAttr.startsWith('.')) {
return null;
} // Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
hrefAttr = '#' + hrefAttr.split('#')[1];
}
2019-08-27 15:03:21 +02:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 17:31:34 +01:00
}
2019-08-27 15:03:21 +02:00
return selector;
};
var getElementFromSelector = function getElementFromSelector(element) {
var selector = getSelector(element);
return selector ? document.querySelector(selector) : null;
2019-03-01 17:31:34 +01:00
};
var isElement = function isElement(obj) {
return (obj[0] || obj).nodeType;
};
var typeCheckConfig = function typeCheckConfig(componentName, config, configTypes) {
Object.keys(configTypes).forEach(function (property) {
var expectedTypes = configTypes[property];
var value = config[property];
var valueType = value && isElement(value) ? 'element' : toType(value);
if (!new RegExp(expectedTypes).test(valueType)) {
throw new TypeError(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
2019-03-01 17:31:34 +01:00
}
});
};
2019-11-08 09:11:23 +01:00
var isVisible = function isVisible(element) {
if (!element) {
return false;
}
if (element.style && element.parentNode && element.parentNode.style) {
var elementStyle = getComputedStyle(element);
var parentNodeStyle = getComputedStyle(element.parentNode);
return elementStyle.display !== 'none' && parentNodeStyle.display !== 'none' && elementStyle.visibility !== 'hidden';
}
return false;
};
2019-03-01 17:31:34 +01:00
var noop = function noop() {
return function () {};
};
2019-08-27 15:03:21 +02:00
var getjQuery = function getjQuery() {
var _window = window,
jQuery = _window.jQuery;
2020-11-23 14:17:16 +01:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 15:03:21 +02:00
return jQuery;
}
return null;
};
2020-11-11 18:07:37 +01:00
var onDOMContentLoaded = function onDOMContentLoaded(callback) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
};
2020-12-03 14:08:31 +01:00
var isRTL = document.documentElement.dir === 'rtl';
var defineJQueryPlugin = function defineJQueryPlugin(name, plugin) {
onDOMContentLoaded(function () {
var $ = getjQuery();
/* istanbul ignore if */
2020-12-03 15:18:59 +01:00
if ($) {
var JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2020-12-03 15:18:59 +01:00
$.fn[name].noConflict = function () {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 15:18:59 +01:00
}
});
};
2020-12-03 15:18:59 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'dropdown';
2018-11-13 07:41:12 +01:00
var DATA_KEY = 'bs.dropdown';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
2020-05-13 20:53:43 +02:00
var ESCAPE_KEY = 'Escape';
var SPACE_KEY = 'Space';
var TAB_KEY = 'Tab';
var ARROW_UP_KEY = 'ArrowUp';
var ARROW_DOWN_KEY = 'ArrowDown';
var RIGHT_MOUSE_BUTTON = 2; // MouseEvent.button value for the secondary button, usually the right button
var REGEXP_KEYDOWN = new RegExp(ARROW_UP_KEY + "|" + ARROW_DOWN_KEY + "|" + ESCAPE_KEY);
2020-03-28 11:29:08 +01:00
var EVENT_HIDE = "hide" + EVENT_KEY;
var EVENT_HIDDEN = "hidden" + EVENT_KEY;
var EVENT_SHOW = "show" + EVENT_KEY;
var EVENT_SHOWN = "shown" + EVENT_KEY;
var EVENT_CLICK = "click" + EVENT_KEY;
var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
var EVENT_KEYDOWN_DATA_API = "keydown" + EVENT_KEY + DATA_API_KEY;
var EVENT_KEYUP_DATA_API = "keyup" + EVENT_KEY + DATA_API_KEY;
var CLASS_NAME_DISABLED = 'disabled';
var CLASS_NAME_SHOW = 'show';
var CLASS_NAME_DROPUP = 'dropup';
2020-12-03 14:08:31 +01:00
var CLASS_NAME_DROPEND = 'dropend';
var CLASS_NAME_DROPSTART = 'dropstart';
2020-03-28 11:29:08 +01:00
var CLASS_NAME_NAVBAR = 'navbar';
2020-11-23 14:17:16 +01:00
var SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]';
2020-03-28 11:29:08 +01:00
var SELECTOR_FORM_CHILD = '.dropdown form';
var SELECTOR_MENU = '.dropdown-menu';
var SELECTOR_NAVBAR_NAV = '.navbar-nav';
var SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)';
2020-12-03 14:08:31 +01:00
var PLACEMENT_TOP = isRTL ? 'top-end' : 'top-start';
var PLACEMENT_TOPEND = isRTL ? 'top-start' : 'top-end';
var PLACEMENT_BOTTOM = isRTL ? 'bottom-end' : 'bottom-start';
var PLACEMENT_BOTTOMEND = isRTL ? 'bottom-start' : 'bottom-end';
var PLACEMENT_RIGHT = isRTL ? 'left-start' : 'right-start';
var PLACEMENT_LEFT = isRTL ? 'right-start' : 'left-start';
2018-11-13 07:41:12 +01:00
var Default = {
offset: [0, 2],
2018-11-13 07:41:12 +01:00
flip: true,
2020-12-07 16:50:24 +01:00
boundary: 'clippingParents',
2018-11-13 07:41:12 +01:00
reference: 'toggle',
2019-08-27 15:03:21 +02:00
display: 'dynamic',
popperConfig: null
2018-11-13 07:41:12 +01:00
};
var DefaultType = {
offset: '(array|string|function)',
2018-11-13 07:41:12 +01:00
flip: 'boolean',
boundary: '(string|element)',
reference: '(string|element|object)',
2019-08-27 15:03:21 +02:00
display: 'string',
popperConfig: '(null|object|function)'
2018-11-13 07:41:12 +01:00
};
2019-10-08 08:39:10 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-13 21:48:34 +02:00
2020-12-03 15:18:59 +01:00
var Dropdown = /*#__PURE__*/function (_BaseComponent) {
_inheritsLoose(Dropdown, _BaseComponent);
2018-11-13 07:41:12 +01:00
function Dropdown(element, config) {
2020-12-03 15:18:59 +01:00
var _this;
_this = _BaseComponent.call(this, element) || this;
_this._popper = null;
_this._config = _this._getConfig(config);
_this._menu = _this._getMenuElement();
_this._inNavbar = _this._detectNavbar();
2015-05-10 22:47:11 +02:00
2020-12-03 15:18:59 +01:00
_this._addEventListeners();
2019-03-01 17:31:34 +01:00
2020-12-03 15:18:59 +01:00
return _this;
2018-11-13 07:41:12 +01:00
} // Getters
2015-08-13 06:12:03 +02:00
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
var _proto = Dropdown.prototype;
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
// Public
_proto.toggle = function toggle() {
2020-03-28 11:29:08 +01:00
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-10 22:47:11 +02:00
2020-03-28 11:29:08 +01:00
var isActive = this._element.classList.contains(CLASS_NAME_SHOW);
2015-05-10 22:47:11 +02:00
2019-08-27 15:03:21 +02:00
Dropdown.clearMenus();
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
if (isActive) {
return;
}
2017-10-30 00:19:14 +01:00
2019-08-27 15:03:21 +02:00
this.show();
};
_proto.show = function show() {
2020-03-28 11:29:08 +01:00
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || this._menu.classList.contains(CLASS_NAME_SHOW)) {
2019-08-27 15:03:21 +02:00
return;
}
var parent = Dropdown.getParentFromElement(this._element);
2018-11-13 07:41:12 +01:00
var relatedTarget = {
relatedTarget: this._element
};
2020-09-14 17:12:06 +02:00
var showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW, relatedTarget);
2017-10-30 00:19:14 +01:00
2019-03-01 17:31:34 +01:00
if (showEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
2020-11-23 14:17:16 +01:00
} // Totally disable Popper for Dropdowns in Navbar
2017-09-06 06:05:12 +02:00
2017-03-20 03:03:32 +01:00
if (this._inNavbar) {
Manipulator__default['default'].setDataAttribute(this._menu, 'popper', 'none');
} else {
2020-12-07 16:50:24 +01:00
if (typeof Popper__namespace === 'undefined') {
2020-11-23 14:17:16 +01:00
throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)');
2018-11-13 07:41:12 +01:00
}
2018-02-11 23:53:29 +01:00
2018-11-13 07:41:12 +01:00
var referenceElement = this._element;
2017-12-23 01:21:54 +01:00
2018-11-13 07:41:12 +01:00
if (this._config.reference === 'parent') {
referenceElement = parent;
2019-03-01 17:31:34 +01:00
} else if (isElement(this._config.reference)) {
2018-11-13 07:41:12 +01:00
referenceElement = this._config.reference; // Check if it's jQuery element
2017-12-23 01:21:54 +01:00
2018-11-13 07:41:12 +01:00
if (typeof this._config.reference.jquery !== 'undefined') {
referenceElement = this._config.reference[0];
2018-07-24 02:51:14 +02:00
}
} else if (typeof this._config.reference === 'object') {
referenceElement = this._config.reference;
2018-07-24 02:51:14 +02:00
}
2017-09-30 23:28:03 +02:00
var popperConfig = this._getPopperConfig();
var isDisplayStatic = popperConfig.modifiers.find(function (modifier) {
return modifier.name === 'applyStyles' && modifier.enabled === false;
});
this._popper = Popper.createPopper(referenceElement, this._menu, popperConfig);
if (isDisplayStatic) {
Manipulator__default['default'].setDataAttribute(this._menu, 'popper', 'static');
}
2018-11-13 07:41:12 +01: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
2017-09-30 23:28:03 +02:00
2020-05-13 20:53:43 +02:00
if ('ontouchstart' in document.documentElement && !parent.closest(SELECTOR_NAVBAR_NAV)) {
2020-03-28 11:29:08 +01:00
var _ref;
(_ref = []).concat.apply(_ref, document.body.children).forEach(function (elem) {
2020-09-14 17:12:06 +02:00
return EventHandler__default['default'].on(elem, 'mouseover', null, noop());
2019-03-01 17:31:34 +01:00
});
2018-11-13 07:41:12 +01:00
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._element.focus();
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._element.setAttribute('aria-expanded', true);
2015-05-13 21:48:34 +02:00
this._menu.classList.toggle(CLASS_NAME_SHOW);
this._element.classList.toggle(CLASS_NAME_SHOW);
EventHandler__default['default'].trigger(this._element, EVENT_SHOWN, relatedTarget);
2018-11-13 07:41:12 +01:00
};
_proto.hide = function hide() {
2020-03-28 11:29:08 +01:00
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || !this._menu.classList.contains(CLASS_NAME_SHOW)) {
2018-11-13 07:41:12 +01:00
return;
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
var relatedTarget = {
relatedTarget: this._element
2018-07-24 02:51:14 +02:00
};
var hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE, relatedTarget);
2017-05-16 09:59:44 +02:00
2019-03-01 17:31:34 +01:00
if (hideEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
2017-09-30 23:28:03 +02:00
2019-08-27 15:03:21 +02:00
if (this._popper) {
this._popper.destroy();
}
this._menu.classList.toggle(CLASS_NAME_SHOW);
this._element.classList.toggle(CLASS_NAME_SHOW);
Manipulator__default['default'].removeDataAttribute(this._menu, 'popper');
EventHandler__default['default'].trigger(this._element, EVENT_HIDDEN, relatedTarget);
2018-11-13 07:41:12 +01:00
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto.dispose = function dispose() {
2020-12-03 15:18:59 +01:00
_BaseComponent.prototype.dispose.call(this);
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].off(this._element, EVENT_KEY);
2018-11-13 07:41:12 +01:00
this._menu = null;
2017-09-30 23:28:03 +02:00
2019-08-27 15:03:21 +02:00
if (this._popper) {
2018-11-13 07:41:12 +01:00
this._popper.destroy();
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._popper = null;
}
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto.update = function update() {
this._inNavbar = this._detectNavbar();
2017-09-30 23:28:03 +02:00
2019-08-27 15:03:21 +02:00
if (this._popper) {
2020-12-07 16:50:24 +01:00
this._popper.update();
2018-11-13 07:41:12 +01:00
}
2019-01-04 17:29:45 +01:00
} // Private
;
2017-10-16 00:51:44 +02:00
2018-11-13 07:41:12 +01:00
_proto._addEventListeners = function _addEventListeners() {
2020-12-03 15:18:59 +01:00
var _this2 = this;
2017-10-16 00:51:44 +02:00
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].on(this._element, EVENT_CLICK, function (event) {
2018-11-13 07:41:12 +01:00
event.preventDefault();
event.stopPropagation();
2020-12-03 15:18:59 +01:00
_this2.toggle();
2018-11-13 07:41:12 +01:00
});
};
2018-02-19 23:50:56 +01:00
2018-11-13 07:41:12 +01:00
_proto._getConfig = function _getConfig(config) {
2020-09-14 17:12:06 +02:00
config = _extends({}, this.constructor.Default, Manipulator__default['default'].getDataAttributes(this._element), config);
2019-03-01 17:31:34 +01:00
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.");
}
2018-11-13 07:41:12 +01:00
return config;
};
2018-02-19 23:50:56 +01:00
2018-11-13 07:41:12 +01:00
_proto._getMenuElement = function _getMenuElement() {
2020-09-14 17:12:06 +02:00
return SelectorEngine__default['default'].next(this._element, SELECTOR_MENU)[0];
2018-11-13 07:41:12 +01:00
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto._getPlacement = function _getPlacement() {
2019-03-01 17:31:34 +01:00
var parentDropdown = this._element.parentNode;
2017-09-30 23:28:03 +02:00
2020-12-07 16:50:24 +01:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
return PLACEMENT_RIGHT;
2018-11-13 07:41:12 +01:00
}
2018-07-24 02:51:14 +02:00
2020-12-07 16:50:24 +01:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
return PLACEMENT_LEFT;
} // We need to trim the value because custom properties can also include spaces
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
2020-12-07 16:50:24 +01:00
var isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end';
2018-11-13 07:41:12 +01:00
2020-12-07 16:50:24 +01:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP;
2018-11-13 07:41:12 +01:00
}
2018-07-24 02:51:14 +02:00
2020-12-07 16:50:24 +01:00
return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM;
};
_proto._detectNavbar = function _detectNavbar() {
return this._element.closest("." + CLASS_NAME_NAVBAR) !== null;
};
_proto._getOffset = function _getOffset() {
var _this3 = this;
var offset = this._config.offset;
if (typeof offset === 'string') {
return offset.split(',').map(function (val) {
return Number.parseInt(val, 10);
});
}
if (typeof offset === 'function') {
return function (popperData) {
return offset(popperData, _this3._element);
};
}
return offset;
};
_proto._getPopperConfig = function _getPopperConfig() {
var defaultBsPopperConfig = {
2018-11-13 07:41:12 +01:00
placement: this._getPlacement(),
2020-12-07 16:50:24 +01:00
modifiers: [{
name: 'preventOverflow',
options: {
altBoundary: this._config.flip,
boundary: this._config.boundary
}
}, {
name: 'offset',
options: {
offset: this._getOffset()
2017-09-06 06:05:12 +02:00
}
2020-12-07 16:50:24 +01:00
}]
2020-11-23 14:17:16 +01:00
}; // Disable Popper if we have a static display
2017-06-18 09:14:35 +02:00
2018-11-13 07:41:12 +01:00
if (this._config.display === 'static') {
defaultBsPopperConfig.modifiers = [{
2020-12-07 16:50:24 +01:00
name: 'applyStyles',
2018-11-13 07:41:12 +01:00
enabled: false
2020-12-07 16:50:24 +01:00
}];
2018-11-13 07:41:12 +01:00
}
2017-09-30 23:28:03 +02:00
return _extends({}, defaultBsPopperConfig, typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig);
2019-01-04 17:29:45 +01:00
} // Static
;
2015-05-10 22:47:11 +02:00
2019-08-27 15:03:21 +02:00
Dropdown.dropdownInterface = function dropdownInterface(element, config) {
2020-09-14 17:12:06 +02:00
var data = Data__default['default'].getData(element, DATA_KEY);
2015-05-10 22:47:11 +02:00
2019-03-01 17:31:34 +01:00
var _config = typeof config === 'object' ? config : null;
2015-05-10 22:47:11 +02:00
2019-03-01 17:31:34 +01:00
if (!data) {
data = new Dropdown(element, _config);
}
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
2019-03-11 16:13:30 +01:00
throw new TypeError("No method named \"" + config + "\"");
2018-11-13 07:41:12 +01:00
}
2017-05-16 09:59:44 +02:00
2019-03-01 17:31:34 +01:00
data[config]();
}
};
2015-05-10 22:47:11 +02:00
2019-08-27 15:03:21 +02:00
Dropdown.jQueryInterface = function jQueryInterface(config) {
2019-03-01 17:31:34 +01:00
return this.each(function () {
2019-08-27 15:03:21 +02:00
Dropdown.dropdownInterface(this, config);
2018-11-13 07:41:12 +01:00
});
};
2015-05-10 22:47:11 +02:00
2019-08-27 15:03:21 +02:00
Dropdown.clearMenus = function clearMenus(event) {
2020-05-13 20:53:43 +02:00
if (event && (event.button === RIGHT_MOUSE_BUTTON || event.type === 'keyup' && event.key !== TAB_KEY)) {
2018-11-13 07:41:12 +01:00
return;
}
2016-10-10 02:26:51 +02:00
2020-09-14 17:12:06 +02:00
var toggles = SelectorEngine__default['default'].find(SELECTOR_DATA_TOGGLE);
2017-03-20 03:03:32 +01:00
2018-11-13 07:41:12 +01:00
for (var i = 0, len = toggles.length; i < len; i++) {
2020-09-14 17:12:06 +02:00
var context = Data__default['default'].getData(toggles[i], DATA_KEY);
2018-11-13 07:41:12 +01:00
var relatedTarget = {
relatedTarget: toggles[i]
};
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
if (event && event.type === 'click') {
relatedTarget.clickEvent = event;
2017-09-06 06:05:12 +02:00
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (!context) {
continue;
2018-07-24 02:51:14 +02:00
}
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
var dropdownMenu = context._menu;
2017-09-06 06:05:12 +02:00
2020-03-28 11:29:08 +01:00
if (!toggles[i].classList.contains(CLASS_NAME_SHOW)) {
2018-11-13 07:41:12 +01:00
continue;
}
2017-12-31 04:41:36 +01:00
2020-05-13 20:53:43 +02:00
if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.key === TAB_KEY) && dropdownMenu.contains(event.target)) {
2018-11-13 07:41:12 +01:00
continue;
2018-07-24 02:51:14 +02:00
}
2015-05-10 22:47:11 +02:00
var hideEvent = EventHandler__default['default'].trigger(toggles[i], EVENT_HIDE, relatedTarget);
2018-11-13 07:41:12 +01:00
2019-03-01 17:31:34 +01:00
if (hideEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
continue;
} // If this is a touch-enabled device we remove the extra
// empty mouseover listeners we added for iOS support
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
if ('ontouchstart' in document.documentElement) {
2020-03-28 11:29:08 +01:00
var _ref2;
(_ref2 = []).concat.apply(_ref2, document.body.children).forEach(function (elem) {
2020-09-14 17:12:06 +02:00
return EventHandler__default['default'].off(elem, 'mouseover', null, noop());
2019-03-01 17:31:34 +01:00
});
2018-07-24 02:51:14 +02:00
}
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
toggles[i].setAttribute('aria-expanded', 'false');
2019-08-27 15:03:21 +02:00
if (context._popper) {
context._popper.destroy();
}
2020-03-28 11:29:08 +01:00
dropdownMenu.classList.remove(CLASS_NAME_SHOW);
toggles[i].classList.remove(CLASS_NAME_SHOW);
Manipulator__default['default'].removeDataAttribute(dropdownMenu, 'popper');
EventHandler__default['default'].trigger(toggles[i], EVENT_HIDDEN, relatedTarget);
2018-11-13 07:41:12 +01:00
}
};
2015-05-10 22:47:11 +02:00
2019-08-27 15:03:21 +02:00
Dropdown.getParentFromElement = function getParentFromElement(element) {
return getElementFromSelector(element) || element.parentNode;
2019-03-01 17:31:34 +01:00
};
2015-05-10 22:47:11 +02:00
2019-08-27 15:03:21 +02:00
Dropdown.dataApiKeydownHandler = function dataApiKeydownHandler(event) {
2018-11-13 07:41:12 +01: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
2020-05-13 20:53:43 +02:00
if (/input|textarea/i.test(event.target.tagName) ? 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)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-10 22:47:11 +02:00
2018-11-13 07:41:12 +01:00
event.preventDefault();
event.stopPropagation();
2015-05-10 22:47:11 +02:00
2020-03-28 11:29:08 +01:00
if (this.disabled || this.classList.contains(CLASS_NAME_DISABLED)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-08-19 05:28:28 +02:00
2019-08-27 15:03:21 +02:00
var parent = Dropdown.getParentFromElement(this);
2020-03-28 11:29:08 +01:00
var isActive = this.classList.contains(CLASS_NAME_SHOW);
2015-08-19 05:28:28 +02:00
2020-05-13 20:53:43 +02:00
if (event.key === ESCAPE_KEY) {
2020-09-14 17:12:06 +02:00
var button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default['default'].prev(this, SELECTOR_DATA_TOGGLE)[0];
2020-03-28 11:29:08 +01:00
button.focus();
Dropdown.clearMenus();
return;
}
2015-05-10 22:47:11 +02:00
if (!isActive && (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY)) {
var _button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default['default'].prev(this, SELECTOR_DATA_TOGGLE)[0];
_button.click();
return;
}
2020-05-13 20:53:43 +02:00
if (!isActive || event.key === SPACE_KEY) {
2019-08-27 15:03:21 +02:00
Dropdown.clearMenus();
2018-11-13 07:41:12 +01:00
return;
}
2017-09-30 23:28:03 +02:00
2020-09-14 17:12:06 +02:00
var items = SelectorEngine__default['default'].find(SELECTOR_VISIBLE_ITEMS, parent).filter(isVisible);
2017-09-30 23:28:03 +02:00
2019-03-01 17:31:34 +01:00
if (!items.length) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-10 22:47:11 +02:00
2020-12-03 15:18:59 +01:00
var index = items.indexOf(event.target); // Up
2015-05-10 22:47:11 +02:00
2020-05-13 20:53:43 +02:00
if (event.key === ARROW_UP_KEY && index > 0) {
2018-11-13 07:41:12 +01:00
index--;
2020-12-03 15:18:59 +01:00
} // Down
2017-09-30 23:28:03 +02:00
2020-05-13 20:53:43 +02:00
if (event.key === ARROW_DOWN_KEY && index < items.length - 1) {
2018-11-13 07:41:12 +01:00
index++;
2020-05-13 20:53:43 +02:00
} // index is -1 if the first keydown is an ArrowUp
2017-09-30 23:28:03 +02:00
2020-05-13 20:53:43 +02:00
index = index === -1 ? 0 : index;
2018-11-13 07:41:12 +01:00
items[index].focus();
2018-07-24 02:51:14 +02:00
};
2017-09-30 23:28:03 +02:00
_createClass(Dropdown, null, [{
2018-11-13 07:41:12 +01:00
key: "Default",
get: function get() {
return Default;
}
}, {
key: "DefaultType",
get: function get() {
return DefaultType;
}
2020-12-03 15:18:59 +01:00
}, {
key: "DATA_KEY",
get: function get() {
return DATA_KEY;
}
2018-11-13 07:41:12 +01:00
}]);
2018-07-24 02:51:14 +02:00
return Dropdown;
}(BaseComponent__default['default']);
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown.dataApiKeydownHandler);
EventHandler__default['default'].on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown.dataApiKeydownHandler);
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus);
EventHandler__default['default'].on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus);
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
2018-11-13 07:41:12 +01:00
event.preventDefault();
event.stopPropagation();
2019-08-27 15:03:21 +02:00
Dropdown.dropdownInterface(this, 'toggle');
2019-03-01 17:31:34 +01:00
});
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, function (e) {
2019-03-01 17:31:34 +01:00
return e.stopPropagation();
2018-11-13 07:41:12 +01:00
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 18:07:37 +01:00
* add .Dropdown to jQuery only if jQuery is present
2018-11-13 07:41:12 +01:00
*/
defineJQueryPlugin(NAME, Dropdown);
2015-05-10 22:47:11 +02:00
return Dropdown;
2018-07-24 02:51:14 +02:00
2019-11-08 09:11:23 +01:00
})));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=dropdown.js.map