1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/dist/dropdown.js

664 lines
22 KiB
JavaScript
Raw Normal View History

2018-11-13 01:41:12 -05:00
/*!
2020-12-07 10:50:24 -05:00
* Bootstrap dropdown.js v5.0.0-beta1 (https://getbootstrap.com/)
2020-03-28 06:29:08 -04:00
* Copyright 2011-2020 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 01:41:12 -05:00
*/
2018-07-23 20:51:14 -04:00
(function (global, factory) {
2020-12-07 10:50:24 -05:00
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')) :
typeof define === 'function' && define.amd ? define(['@popperjs/core', './dom/data', './dom/event-handler', './dom/manipulator', './dom/selector-engine'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global.Popper, global.Data, global.EventHandler, global.Manipulator, global.SelectorEngine));
}(this, (function (Popper, Data, EventHandler, Manipulator, SelectorEngine) { 'use strict';
2019-03-01 11:31:34 -05:00
2020-09-14 11:12:06 -04:00
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
2020-12-07 10:50:24 -05: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 11:12:06 -04: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);
2018-07-23 20:51:14 -04:00
2019-03-01 11:31:34 -05:00
/**
* --------------------------------------------------------------------------
2020-12-07 10:50:24 -05:00
* Bootstrap (v5.0.0-beta1): util/index.js
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 11:31:34 -05:00
* --------------------------------------------------------------------------
*/
var toType = function toType(obj) {
2020-03-28 06:29:08 -04:00
if (obj === null || obj === undefined) {
return "" + obj;
}
2019-03-01 11:31:34 -05:00
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
};
2019-08-27 09:03:21 -04:00
var getSelector = function getSelector(element) {
2020-11-23 08:17:16 -05:00
var selector = element.getAttribute('data-bs-target');
2019-03-01 11:31:34 -05:00
if (!selector || selector === '#') {
var hrefAttr = element.getAttribute('href');
2019-08-27 09:03:21 -04:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 11:31:34 -05:00
}
2019-08-27 09:03:21 -04:00
return selector;
};
var getElementFromSelector = function getElementFromSelector(element) {
var selector = getSelector(element);
return selector ? document.querySelector(selector) : null;
2019-03-01 11:31:34 -05: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 Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
}
});
};
2019-11-08 03:11:23 -05: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 11:31:34 -05:00
var noop = function noop() {
return function () {};
};
2019-08-27 09:03:21 -04:00
var getjQuery = function getjQuery() {
var _window = window,
jQuery = _window.jQuery;
2020-11-23 08:17:16 -05:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 09:03:21 -04:00
return jQuery;
}
return null;
};
2020-11-11 12:07:37 -05:00
var onDOMContentLoaded = function onDOMContentLoaded(callback) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
};
2020-12-03 08:08:31 -05:00
var isRTL = document.documentElement.dir === 'rtl';
2020-06-13 18:40:28 -04:00
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; }
2018-11-13 01:41:12 -05:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2020-12-07 10:50:24 -05:00
var VERSION = '5.0.0-beta1';
2020-12-03 09:18:59 -05:00
var BaseComponent = /*#__PURE__*/function () {
function BaseComponent(element) {
if (!element) {
return;
}
this._element = element;
Data__default['default'].setData(element, this.constructor.DATA_KEY, this);
}
var _proto = BaseComponent.prototype;
_proto.dispose = function dispose() {
Data__default['default'].removeData(this._element, this.constructor.DATA_KEY);
this._element = null;
}
/** Static */
;
BaseComponent.getInstance = function getInstance(element) {
return Data__default['default'].getData(element, this.DATA_KEY);
};
_createClass(BaseComponent, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}]);
return BaseComponent;
}();
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 _defineProperties$1(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$1(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties$1(Constructor.prototype, protoProps); if (staticProps) _defineProperties$1(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'dropdown';
2018-11-13 01:41:12 -05:00
var DATA_KEY = 'bs.dropdown';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
2020-05-13 14:53:43 -04: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 06:29:08 -04: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 08:08:31 -05:00
var CLASS_NAME_DROPEND = 'dropend';
var CLASS_NAME_DROPSTART = 'dropstart';
2020-03-28 06:29:08 -04:00
var CLASS_NAME_NAVBAR = 'navbar';
2020-11-23 08:17:16 -05:00
var SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]';
2020-03-28 06:29:08 -04: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 08:08:31 -05: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 01:41:12 -05:00
var Default = {
offset: 0,
flip: true,
2020-12-07 10:50:24 -05:00
boundary: 'clippingParents',
2018-11-13 01:41:12 -05:00
reference: 'toggle',
2019-08-27 09:03:21 -04:00
display: 'dynamic',
popperConfig: null
2018-11-13 01:41:12 -05:00
};
var DefaultType = {
offset: '(number|string|function)',
flip: 'boolean',
boundary: '(string|element)',
reference: '(string|element)',
2019-08-27 09:03:21 -04:00
display: 'string',
popperConfig: '(null|object)'
2018-11-13 01:41:12 -05:00
};
2019-10-08 02:39:10 -04:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-13 15:48:34 -04:00
2020-12-03 09:18:59 -05:00
var Dropdown = /*#__PURE__*/function (_BaseComponent) {
_inheritsLoose(Dropdown, _BaseComponent);
2018-11-13 01:41:12 -05:00
function Dropdown(element, config) {
2020-12-03 09:18:59 -05: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 16:47:11 -04:00
2020-12-03 09:18:59 -05:00
_this._addEventListeners();
2019-03-01 11:31:34 -05:00
2020-12-03 09:18:59 -05:00
return _this;
2018-11-13 01:41:12 -05:00
} // Getters
2015-08-13 00:12:03 -04:00
2015-05-10 16:47:11 -04:00
2018-11-13 01:41:12 -05:00
var _proto = Dropdown.prototype;
2015-05-10 16:47:11 -04:00
2018-11-13 01:41:12 -05:00
// Public
_proto.toggle = function toggle() {
2020-03-28 06:29:08 -04:00
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED)) {
2018-11-13 01:41:12 -05:00
return;
}
2015-05-10 16:47:11 -04:00
2020-03-28 06:29:08 -04:00
var isActive = this._element.classList.contains(CLASS_NAME_SHOW);
2015-05-10 16:47:11 -04:00
2019-08-27 09:03:21 -04:00
Dropdown.clearMenus();
2015-05-10 16:47:11 -04:00
2018-11-13 01:41:12 -05:00
if (isActive) {
return;
}
2017-10-29 19:19:14 -04:00
2019-08-27 09:03:21 -04:00
this.show();
};
_proto.show = function show() {
2020-03-28 06:29:08 -04:00
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || this._menu.classList.contains(CLASS_NAME_SHOW)) {
2019-08-27 09:03:21 -04:00
return;
}
var parent = Dropdown.getParentFromElement(this._element);
2018-11-13 01:41:12 -05:00
var relatedTarget = {
relatedTarget: this._element
};
2020-09-14 11:12:06 -04:00
var showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW, relatedTarget);
2017-10-29 19:19:14 -04:00
2019-03-01 11:31:34 -05:00
if (showEvent.defaultPrevented) {
2018-11-13 01:41:12 -05:00
return;
2020-11-23 08:17:16 -05:00
} // Totally disable Popper for Dropdowns in Navbar
2017-09-06 00:05:12 -04:00
2017-03-19 22:03:32 -04:00
2018-11-13 01:41:12 -05:00
if (!this._inNavbar) {
2020-12-07 10:50:24 -05:00
if (typeof Popper__namespace === 'undefined') {
2020-11-23 08:17:16 -05:00
throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)');
2018-11-13 01:41:12 -05:00
}
2018-02-11 17:53:29 -05:00
2018-11-13 01:41:12 -05:00
var referenceElement = this._element;
2017-12-22 19:21:54 -05:00
2018-11-13 01:41:12 -05:00
if (this._config.reference === 'parent') {
referenceElement = parent;
2019-03-01 11:31:34 -05:00
} else if (isElement(this._config.reference)) {
2018-11-13 01:41:12 -05:00
referenceElement = this._config.reference; // Check if it's jQuery element
2017-12-22 19:21:54 -05:00
2018-11-13 01:41:12 -05:00
if (typeof this._config.reference.jquery !== 'undefined') {
referenceElement = this._config.reference[0];
2018-07-23 20:51:14 -04:00
}
}
2017-09-30 17:28:03 -04:00
2020-12-07 10:50:24 -05:00
this._popper = Popper.createPopper(referenceElement, this._menu, this._getPopperConfig());
2018-11-13 01:41:12 -05: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 17:28:03 -04:00
2020-05-13 14:53:43 -04:00
if ('ontouchstart' in document.documentElement && !parent.closest(SELECTOR_NAVBAR_NAV)) {
2020-03-28 06:29:08 -04:00
var _ref;
(_ref = []).concat.apply(_ref, document.body.children).forEach(function (elem) {
2020-09-14 11:12:06 -04:00
return EventHandler__default['default'].on(elem, 'mouseover', null, noop());
2019-03-01 11:31:34 -05:00
});
2018-11-13 01:41:12 -05:00
}
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
this._element.focus();
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
this._element.setAttribute('aria-expanded', true);
2015-05-13 15:48:34 -04:00
this._menu.classList.toggle(CLASS_NAME_SHOW);
this._element.classList.toggle(CLASS_NAME_SHOW);
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].trigger(parent, EVENT_SHOWN, relatedTarget);
2018-11-13 01:41:12 -05:00
};
_proto.hide = function hide() {
2020-03-28 06:29:08 -04:00
if (this._element.disabled || this._element.classList.contains(CLASS_NAME_DISABLED) || !this._menu.classList.contains(CLASS_NAME_SHOW)) {
2018-11-13 01:41:12 -05:00
return;
}
2017-09-30 17:28:03 -04:00
2019-08-27 09:03:21 -04:00
var parent = Dropdown.getParentFromElement(this._element);
2018-11-13 01:41:12 -05:00
var relatedTarget = {
relatedTarget: this._element
2018-07-23 20:51:14 -04:00
};
2020-09-14 11:12:06 -04:00
var hideEvent = EventHandler__default['default'].trigger(parent, EVENT_HIDE, relatedTarget);
2017-05-16 03:59:44 -04:00
2019-03-01 11:31:34 -05:00
if (hideEvent.defaultPrevented) {
2018-11-13 01:41:12 -05:00
return;
}
2017-09-30 17:28:03 -04:00
2019-08-27 09:03:21 -04:00
if (this._popper) {
this._popper.destroy();
}
this._menu.classList.toggle(CLASS_NAME_SHOW);
this._element.classList.toggle(CLASS_NAME_SHOW);
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].trigger(parent, EVENT_HIDDEN, relatedTarget);
2018-11-13 01:41:12 -05:00
};
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
_proto.dispose = function dispose() {
2020-12-03 09:18:59 -05:00
_BaseComponent.prototype.dispose.call(this);
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].off(this._element, EVENT_KEY);
2018-11-13 01:41:12 -05:00
this._menu = null;
2017-09-30 17:28:03 -04:00
2019-08-27 09:03:21 -04:00
if (this._popper) {
2018-11-13 01:41:12 -05:00
this._popper.destroy();
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
this._popper = null;
}
};
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
_proto.update = function update() {
this._inNavbar = this._detectNavbar();
2017-09-30 17:28:03 -04:00
2019-08-27 09:03:21 -04:00
if (this._popper) {
2020-12-07 10:50:24 -05:00
this._popper.update();
2018-11-13 01:41:12 -05:00
}
2019-01-04 11:29:45 -05:00
} // Private
;
2017-10-15 18:51:44 -04:00
2018-11-13 01:41:12 -05:00
_proto._addEventListeners = function _addEventListeners() {
2020-12-03 09:18:59 -05:00
var _this2 = this;
2017-10-15 18:51:44 -04:00
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].on(this._element, EVENT_CLICK, function (event) {
2018-11-13 01:41:12 -05:00
event.preventDefault();
event.stopPropagation();
2020-12-03 09:18:59 -05:00
_this2.toggle();
2018-11-13 01:41:12 -05:00
});
};
2018-02-19 17:50:56 -05:00
2018-11-13 01:41:12 -05:00
_proto._getConfig = function _getConfig(config) {
2020-09-14 11:12:06 -04:00
config = _extends({}, this.constructor.Default, Manipulator__default['default'].getDataAttributes(this._element), config);
2019-03-01 11:31:34 -05:00
typeCheckConfig(NAME, config, this.constructor.DefaultType);
2018-11-13 01:41:12 -05:00
return config;
};
2018-02-19 17:50:56 -05:00
2018-11-13 01:41:12 -05:00
_proto._getMenuElement = function _getMenuElement() {
2020-09-14 11:12:06 -04:00
return SelectorEngine__default['default'].next(this._element, SELECTOR_MENU)[0];
2018-11-13 01:41:12 -05:00
};
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
_proto._getPlacement = function _getPlacement() {
2019-03-01 11:31:34 -05:00
var parentDropdown = this._element.parentNode;
2017-09-30 17:28:03 -04:00
2020-12-07 10:50:24 -05:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
return PLACEMENT_RIGHT;
2018-11-13 01:41:12 -05:00
}
2018-07-23 20:51:14 -04:00
2020-12-07 10:50:24 -05: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 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
2020-12-07 10:50:24 -05:00
var isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end';
2018-11-13 01:41:12 -05:00
2020-12-07 10:50:24 -05:00
if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP;
2018-11-13 01:41:12 -05:00
}
2018-07-23 20:51:14 -04:00
2020-12-07 10:50:24 -05:00
return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM;
};
_proto._detectNavbar = function _detectNavbar() {
return this._element.closest("." + CLASS_NAME_NAVBAR) !== null;
};
_proto._getPopperConfig = function _getPopperConfig() {
2018-11-13 01:41:12 -05:00
var popperConfig = {
placement: this._getPlacement(),
2020-12-07 10:50:24 -05:00
modifiers: [{
name: 'preventOverflow',
options: {
altBoundary: this._config.flip,
rootBoundary: this._config.boundary
2017-09-06 00:05:12 -04:00
}
2020-12-07 10:50:24 -05:00
}]
2020-11-23 08:17:16 -05:00
}; // Disable Popper if we have a static display
2017-06-18 03:14:35 -04:00
2018-11-13 01:41:12 -05:00
if (this._config.display === 'static') {
2020-12-07 10:50:24 -05:00
popperConfig.modifiers = [{
name: 'applyStyles',
2018-11-13 01:41:12 -05:00
enabled: false
2020-12-07 10:50:24 -05:00
}];
2018-11-13 01:41:12 -05:00
}
2017-09-30 17:28:03 -04:00
2020-09-14 11:12:06 -04:00
return _extends({}, popperConfig, this._config.popperConfig);
2019-01-04 11:29:45 -05:00
} // Static
;
2015-05-10 16:47:11 -04:00
2019-08-27 09:03:21 -04:00
Dropdown.dropdownInterface = function dropdownInterface(element, config) {
2020-09-14 11:12:06 -04:00
var data = Data__default['default'].getData(element, DATA_KEY);
2015-05-10 16:47:11 -04:00
2019-03-01 11:31:34 -05:00
var _config = typeof config === 'object' ? config : null;
2015-05-10 16:47:11 -04:00
2019-03-01 11:31:34 -05:00
if (!data) {
data = new Dropdown(element, _config);
}
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
2019-03-11 11:13:30 -04:00
throw new TypeError("No method named \"" + config + "\"");
2018-11-13 01:41:12 -05:00
}
2017-05-16 03:59:44 -04:00
2019-03-01 11:31:34 -05:00
data[config]();
}
};
2015-05-10 16:47:11 -04:00
2019-08-27 09:03:21 -04:00
Dropdown.jQueryInterface = function jQueryInterface(config) {
2019-03-01 11:31:34 -05:00
return this.each(function () {
2019-08-27 09:03:21 -04:00
Dropdown.dropdownInterface(this, config);
2018-11-13 01:41:12 -05:00
});
};
2015-05-10 16:47:11 -04:00
2019-08-27 09:03:21 -04:00
Dropdown.clearMenus = function clearMenus(event) {
2020-05-13 14:53:43 -04:00
if (event && (event.button === RIGHT_MOUSE_BUTTON || event.type === 'keyup' && event.key !== TAB_KEY)) {
2018-11-13 01:41:12 -05:00
return;
}
2016-10-09 20:26:51 -04:00
2020-09-14 11:12:06 -04:00
var toggles = SelectorEngine__default['default'].find(SELECTOR_DATA_TOGGLE);
2017-03-19 22:03:32 -04:00
2018-11-13 01:41:12 -05:00
for (var i = 0, len = toggles.length; i < len; i++) {
2019-08-27 09:03:21 -04:00
var parent = Dropdown.getParentFromElement(toggles[i]);
2020-09-14 11:12:06 -04:00
var context = Data__default['default'].getData(toggles[i], DATA_KEY);
2018-11-13 01:41:12 -05:00
var relatedTarget = {
relatedTarget: toggles[i]
};
2016-10-09 20:26:51 -04:00
2018-11-13 01:41:12 -05:00
if (event && event.type === 'click') {
relatedTarget.clickEvent = event;
2017-09-06 00:05:12 -04:00
}
2017-09-30 17:28:03 -04:00
2018-11-13 01:41:12 -05:00
if (!context) {
continue;
2018-07-23 20:51:14 -04:00
}
2015-05-10 16:47:11 -04:00
2018-11-13 01:41:12 -05:00
var dropdownMenu = context._menu;
2017-09-06 00:05:12 -04:00
2020-03-28 06:29:08 -04:00
if (!toggles[i].classList.contains(CLASS_NAME_SHOW)) {
2018-11-13 01:41:12 -05:00
continue;
}
2017-12-30 22:41:36 -05:00
2020-05-13 14:53:43 -04: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 01:41:12 -05:00
continue;
2018-07-23 20:51:14 -04:00
}
2015-05-10 16:47:11 -04:00
2020-09-14 11:12:06 -04:00
var hideEvent = EventHandler__default['default'].trigger(parent, EVENT_HIDE, relatedTarget);
2018-11-13 01:41:12 -05:00
2019-03-01 11:31:34 -05:00
if (hideEvent.defaultPrevented) {
2018-11-13 01:41:12 -05:00
continue;
} // If this is a touch-enabled device we remove the extra
// empty mouseover listeners we added for iOS support
2015-05-10 16:47:11 -04:00
2018-11-13 01:41:12 -05:00
if ('ontouchstart' in document.documentElement) {
2020-03-28 06:29:08 -04:00
var _ref2;
(_ref2 = []).concat.apply(_ref2, document.body.children).forEach(function (elem) {
2020-09-14 11:12:06 -04:00
return EventHandler__default['default'].off(elem, 'mouseover', null, noop());
2019-03-01 11:31:34 -05:00
});
2018-07-23 20:51:14 -04:00
}
2015-05-10 16:47:11 -04:00
2018-11-13 01:41:12 -05:00
toggles[i].setAttribute('aria-expanded', 'false');
2019-08-27 09:03:21 -04:00
if (context._popper) {
context._popper.destroy();
}
2020-03-28 06:29:08 -04:00
dropdownMenu.classList.remove(CLASS_NAME_SHOW);
toggles[i].classList.remove(CLASS_NAME_SHOW);
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].trigger(parent, EVENT_HIDDEN, relatedTarget);
2018-11-13 01:41:12 -05:00
}
};
2015-05-10 16:47:11 -04:00
2019-08-27 09:03:21 -04:00
Dropdown.getParentFromElement = function getParentFromElement(element) {
return getElementFromSelector(element) || element.parentNode;
2019-03-01 11:31:34 -05:00
};
2015-05-10 16:47:11 -04:00
2019-08-27 09:03:21 -04:00
Dropdown.dataApiKeydownHandler = function dataApiKeydownHandler(event) {
2018-11-13 01:41:12 -05: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 14:53:43 -04: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 01:41:12 -05:00
return;
}
2015-05-10 16:47:11 -04:00
2018-11-13 01:41:12 -05:00
event.preventDefault();
event.stopPropagation();
2015-05-10 16:47:11 -04:00
2020-03-28 06:29:08 -04:00
if (this.disabled || this.classList.contains(CLASS_NAME_DISABLED)) {
2018-11-13 01:41:12 -05:00
return;
}
2015-08-18 23:28:28 -04:00
2019-08-27 09:03:21 -04:00
var parent = Dropdown.getParentFromElement(this);
2020-03-28 06:29:08 -04:00
var isActive = this.classList.contains(CLASS_NAME_SHOW);
2015-08-18 23:28:28 -04:00
2020-05-13 14:53:43 -04:00
if (event.key === ESCAPE_KEY) {
2020-09-14 11:12:06 -04:00
var button = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine__default['default'].prev(this, SELECTOR_DATA_TOGGLE)[0];
2020-03-28 06:29:08 -04:00
button.focus();
Dropdown.clearMenus();
return;
}
2015-05-10 16:47:11 -04:00
2020-05-13 14:53:43 -04:00
if (!isActive || event.key === SPACE_KEY) {
2019-08-27 09:03:21 -04:00
Dropdown.clearMenus();
2018-11-13 01:41:12 -05:00
return;
}
2017-09-30 17:28:03 -04:00
2020-09-14 11:12:06 -04:00
var items = SelectorEngine__default['default'].find(SELECTOR_VISIBLE_ITEMS, parent).filter(isVisible);
2017-09-30 17:28:03 -04:00
2019-03-01 11:31:34 -05:00
if (!items.length) {
2018-11-13 01:41:12 -05:00
return;
}
2015-05-10 16:47:11 -04:00
2020-12-03 09:18:59 -05:00
var index = items.indexOf(event.target); // Up
2015-05-10 16:47:11 -04:00
2020-05-13 14:53:43 -04:00
if (event.key === ARROW_UP_KEY && index > 0) {
2018-11-13 01:41:12 -05:00
index--;
2020-12-03 09:18:59 -05:00
} // Down
2017-09-30 17:28:03 -04:00
2020-05-13 14:53:43 -04:00
if (event.key === ARROW_DOWN_KEY && index < items.length - 1) {
2018-11-13 01:41:12 -05:00
index++;
2020-05-13 14:53:43 -04:00
} // index is -1 if the first keydown is an ArrowUp
2017-09-30 17:28:03 -04:00
2020-05-13 14:53:43 -04:00
index = index === -1 ? 0 : index;
2018-11-13 01:41:12 -05:00
items[index].focus();
2018-07-23 20:51:14 -04:00
};
2017-09-30 17:28:03 -04:00
2020-12-03 09:18:59 -05:00
_createClass$1(Dropdown, null, [{
2018-11-13 01:41:12 -05:00
key: "Default",
get: function get() {
return Default;
}
}, {
key: "DefaultType",
get: function get() {
return DefaultType;
}
2020-12-03 09:18:59 -05:00
}, {
key: "DATA_KEY",
get: function get() {
return DATA_KEY;
}
2018-11-13 01:41:12 -05:00
}]);
2018-07-23 20:51:14 -04:00
return Dropdown;
2020-12-03 09:18:59 -05:00
}(BaseComponent);
2018-11-13 01:41:12 -05:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2020-09-14 11:12:06 -04: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 01:41:12 -05:00
event.preventDefault();
event.stopPropagation();
2019-08-27 09:03:21 -04:00
Dropdown.dropdownInterface(this, 'toggle');
2019-03-01 11:31:34 -05:00
});
2020-09-14 11:12:06 -04:00
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, function (e) {
2019-03-01 11:31:34 -05:00
return e.stopPropagation();
2018-11-13 01:41:12 -05:00
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 12:07:37 -05:00
* add .Dropdown to jQuery only if jQuery is present
2018-11-13 01:41:12 -05:00
*/
2020-11-11 12:07:37 -05:00
onDOMContentLoaded(function () {
var $ = getjQuery();
/* istanbul ignore if */
2020-11-11 12:07:37 -05:00
if ($) {
var JQUERY_NO_CONFLICT = $.fn[NAME];
$.fn[NAME] = Dropdown.jQueryInterface;
$.fn[NAME].Constructor = Dropdown;
2018-11-13 01:41:12 -05:00
2020-11-11 12:07:37 -05:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Dropdown.jQueryInterface;
};
}
});
2015-05-10 16:47:11 -04:00
return Dropdown;
2018-07-23 20:51:14 -04:00
2019-11-08 03:11:23 -05:00
})));
2018-07-23 20:51:14 -04:00
//# sourceMappingURL=dropdown.js.map