mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-09 15:16:51 +02:00
Dist (#29263)
This commit is contained in:
151
js/dist/dropdown.js
vendored
151
js/dist/dropdown.js
vendored
@@ -86,26 +86,25 @@
|
||||
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
var _window = window,
|
||||
jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
|
||||
|
||||
var toType = function toType(obj) {
|
||||
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
|
||||
};
|
||||
|
||||
var getSelectorFromElement = function getSelectorFromElement(element) {
|
||||
var getSelector = function getSelector(element) {
|
||||
var selector = element.getAttribute('data-target');
|
||||
|
||||
if (!selector || selector === '#') {
|
||||
var hrefAttr = element.getAttribute('href');
|
||||
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : '';
|
||||
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
|
||||
}
|
||||
|
||||
try {
|
||||
return document.querySelector(selector) ? selector : null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
return selector;
|
||||
};
|
||||
|
||||
var getElementFromSelector = function getElementFromSelector(element) {
|
||||
var selector = getSelector(element);
|
||||
return selector ? document.querySelector(selector) : null;
|
||||
};
|
||||
|
||||
var isElement = function isElement(obj) {
|
||||
@@ -137,6 +136,17 @@
|
||||
return function () {};
|
||||
};
|
||||
|
||||
var getjQuery = function getjQuery() {
|
||||
var _window = window,
|
||||
jQuery = _window.jQuery;
|
||||
|
||||
if (jQuery && !document.body.hasAttribute('data-no-jquery')) {
|
||||
return jQuery;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* Constants
|
||||
@@ -202,14 +212,16 @@
|
||||
flip: true,
|
||||
boundary: 'scrollParent',
|
||||
reference: 'toggle',
|
||||
display: 'dynamic'
|
||||
display: 'dynamic',
|
||||
popperConfig: null
|
||||
};
|
||||
var DefaultType = {
|
||||
offset: '(number|string|function)',
|
||||
flip: 'boolean',
|
||||
boundary: '(string|element)',
|
||||
reference: '(string|element)',
|
||||
display: 'string'
|
||||
display: 'string',
|
||||
popperConfig: '(null|object)'
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* Class Definition
|
||||
@@ -242,16 +254,23 @@
|
||||
return;
|
||||
}
|
||||
|
||||
var parent = Dropdown._getParentFromElement(this._element);
|
||||
|
||||
var isActive = this._menu.classList.contains(ClassName.SHOW);
|
||||
|
||||
Dropdown._clearMenus();
|
||||
Dropdown.clearMenus();
|
||||
|
||||
if (isActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.show();
|
||||
};
|
||||
|
||||
_proto.show = function show() {
|
||||
if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || this._menu.classList.contains(ClassName.SHOW)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var parent = Dropdown.getParentFromElement(this._element);
|
||||
var relatedTarget = {
|
||||
relatedTarget: this._element
|
||||
};
|
||||
@@ -263,10 +282,6 @@
|
||||
|
||||
|
||||
if (!this._inNavbar) {
|
||||
/**
|
||||
* Check for Popper dependency
|
||||
* Popper - https://popper.js.org
|
||||
*/
|
||||
if (typeof Popper === 'undefined') {
|
||||
throw new TypeError('Bootstrap\'s dropdowns require Popper.js (https://popper.js.org)');
|
||||
}
|
||||
@@ -312,34 +327,12 @@
|
||||
EventHandler.trigger(parent, Event.SHOWN, relatedTarget);
|
||||
};
|
||||
|
||||
_proto.show = function show() {
|
||||
if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || this._menu.classList.contains(ClassName.SHOW)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var parent = Dropdown._getParentFromElement(this._element);
|
||||
|
||||
var relatedTarget = {
|
||||
relatedTarget: this._element
|
||||
};
|
||||
var showEvent = EventHandler.trigger(parent, Event.SHOW, relatedTarget);
|
||||
|
||||
if (showEvent.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
Manipulator.toggleClass(this._menu, ClassName.SHOW);
|
||||
Manipulator.toggleClass(parent, ClassName.SHOW);
|
||||
EventHandler.trigger(parent, Event.SHOWN, relatedTarget);
|
||||
};
|
||||
|
||||
_proto.hide = function hide() {
|
||||
if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || !this._menu.classList.contains(ClassName.SHOW)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var parent = Dropdown._getParentFromElement(this._element);
|
||||
|
||||
var parent = Dropdown.getParentFromElement(this._element);
|
||||
var relatedTarget = {
|
||||
relatedTarget: this._element
|
||||
};
|
||||
@@ -349,6 +342,10 @@
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._popper) {
|
||||
this._popper.destroy();
|
||||
}
|
||||
|
||||
Manipulator.toggleClass(this._menu, ClassName.SHOW);
|
||||
Manipulator.toggleClass(parent, ClassName.SHOW);
|
||||
EventHandler.trigger(parent, Event.HIDDEN, relatedTarget);
|
||||
@@ -360,7 +357,7 @@
|
||||
this._element = null;
|
||||
this._menu = null;
|
||||
|
||||
if (this._popper !== null) {
|
||||
if (this._popper) {
|
||||
this._popper.destroy();
|
||||
|
||||
this._popper = null;
|
||||
@@ -370,7 +367,7 @@
|
||||
_proto.update = function update() {
|
||||
this._inNavbar = this._detectNavbar();
|
||||
|
||||
if (this._popper !== null) {
|
||||
if (this._popper) {
|
||||
this._popper.scheduleUpdate();
|
||||
}
|
||||
} // Private
|
||||
@@ -394,8 +391,7 @@
|
||||
};
|
||||
|
||||
_proto._getMenuElement = function _getMenuElement() {
|
||||
var parent = Dropdown._getParentFromElement(this._element);
|
||||
|
||||
var parent = Dropdown.getParentFromElement(this._element);
|
||||
return SelectorEngine.findOne(Selector.MENU, parent);
|
||||
};
|
||||
|
||||
@@ -462,11 +458,11 @@
|
||||
};
|
||||
}
|
||||
|
||||
return popperConfig;
|
||||
return _objectSpread2({}, popperConfig, {}, this._config.popperConfig);
|
||||
} // Static
|
||||
;
|
||||
|
||||
Dropdown._dropdownInterface = function _dropdownInterface(element, config) {
|
||||
Dropdown.dropdownInterface = function dropdownInterface(element, config) {
|
||||
var data = Data.getData(element, DATA_KEY);
|
||||
|
||||
var _config = typeof config === 'object' ? config : null;
|
||||
@@ -484,13 +480,13 @@
|
||||
}
|
||||
};
|
||||
|
||||
Dropdown._jQueryInterface = function _jQueryInterface(config) {
|
||||
Dropdown.jQueryInterface = function jQueryInterface(config) {
|
||||
return this.each(function () {
|
||||
Dropdown._dropdownInterface(this, config);
|
||||
Dropdown.dropdownInterface(this, config);
|
||||
});
|
||||
};
|
||||
|
||||
Dropdown._clearMenus = function _clearMenus(event) {
|
||||
Dropdown.clearMenus = function clearMenus(event) {
|
||||
if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH || event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
|
||||
return;
|
||||
}
|
||||
@@ -498,8 +494,7 @@
|
||||
var toggles = makeArray(SelectorEngine.find(Selector.DATA_TOGGLE));
|
||||
|
||||
for (var i = 0, len = toggles.length; i < len; i++) {
|
||||
var parent = Dropdown._getParentFromElement(toggles[i]);
|
||||
|
||||
var parent = Dropdown.getParentFromElement(toggles[i]);
|
||||
var context = Data.getData(toggles[i], DATA_KEY);
|
||||
var relatedTarget = {
|
||||
relatedTarget: toggles[i]
|
||||
@@ -538,24 +533,22 @@
|
||||
}
|
||||
|
||||
toggles[i].setAttribute('aria-expanded', 'false');
|
||||
|
||||
if (context._popper) {
|
||||
context._popper.destroy();
|
||||
}
|
||||
|
||||
dropdownMenu.classList.remove(ClassName.SHOW);
|
||||
parent.classList.remove(ClassName.SHOW);
|
||||
EventHandler.trigger(parent, Event.HIDDEN, relatedTarget);
|
||||
}
|
||||
};
|
||||
|
||||
Dropdown._getParentFromElement = function _getParentFromElement(element) {
|
||||
var parent;
|
||||
var selector = getSelectorFromElement(element);
|
||||
|
||||
if (selector) {
|
||||
parent = SelectorEngine.findOne(selector);
|
||||
}
|
||||
|
||||
return parent || element.parentNode;
|
||||
Dropdown.getParentFromElement = function getParentFromElement(element) {
|
||||
return getElementFromSelector(element) || element.parentNode;
|
||||
};
|
||||
|
||||
Dropdown._dataApiKeydownHandler = function _dataApiKeydownHandler(event) {
|
||||
Dropdown.dataApiKeydownHandler = function dataApiKeydownHandler(event) {
|
||||
// If not input/textarea:
|
||||
// - And not a key in REGEXP_KEYDOWN => not a dropdown command
|
||||
// If input/textarea:
|
||||
@@ -574,8 +567,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
var parent = Dropdown._getParentFromElement(this);
|
||||
|
||||
var parent = Dropdown.getParentFromElement(this);
|
||||
var isActive = parent.classList.contains(ClassName.SHOW);
|
||||
|
||||
if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
|
||||
@@ -583,8 +575,7 @@
|
||||
SelectorEngine.findOne(Selector.DATA_TOGGLE, parent).focus();
|
||||
}
|
||||
|
||||
Dropdown._clearMenus();
|
||||
|
||||
Dropdown.clearMenus();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -613,7 +604,7 @@
|
||||
items[index].focus();
|
||||
};
|
||||
|
||||
Dropdown._getInstance = function _getInstance(element) {
|
||||
Dropdown.getInstance = function getInstance(element) {
|
||||
return Data.getData(element, DATA_KEY);
|
||||
};
|
||||
|
||||
@@ -643,19 +634,19 @@
|
||||
*/
|
||||
|
||||
|
||||
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.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();
|
||||
event.stopPropagation();
|
||||
|
||||
Dropdown._dropdownInterface(this, 'toggle');
|
||||
Dropdown.dropdownInterface(this, 'toggle');
|
||||
});
|
||||
EventHandler.on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, function (e) {
|
||||
return e.stopPropagation();
|
||||
});
|
||||
var $ = getjQuery();
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* jQuery
|
||||
@@ -665,14 +656,14 @@
|
||||
|
||||
/* istanbul ignore if */
|
||||
|
||||
if (typeof jQuery !== 'undefined') {
|
||||
var JQUERY_NO_CONFLICT = jQuery.fn[NAME];
|
||||
jQuery.fn[NAME] = Dropdown._jQueryInterface;
|
||||
jQuery.fn[NAME].Constructor = Dropdown;
|
||||
if ($) {
|
||||
var JQUERY_NO_CONFLICT = $.fn[NAME];
|
||||
$.fn[NAME] = Dropdown.jQueryInterface;
|
||||
$.fn[NAME].Constructor = Dropdown;
|
||||
|
||||
jQuery.fn[NAME].noConflict = function () {
|
||||
jQuery.fn[NAME] = JQUERY_NO_CONFLICT;
|
||||
return Dropdown._jQueryInterface;
|
||||
$.fn[NAME].noConflict = function () {
|
||||
$.fn[NAME] = JQUERY_NO_CONFLICT;
|
||||
return Dropdown.jQueryInterface;
|
||||
};
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user