diff options
Diffstat (limited to 'vendor/twbs/bootstrap/js/src/dropdown.js')
-rw-r--r-- | vendor/twbs/bootstrap/js/src/dropdown.js | 243 |
1 files changed, 116 insertions, 127 deletions
diff --git a/vendor/twbs/bootstrap/js/src/dropdown.js b/vendor/twbs/bootstrap/js/src/dropdown.js index d336a46d9..bfb1f668c 100644 --- a/vendor/twbs/bootstrap/js/src/dropdown.js +++ b/vendor/twbs/bootstrap/js/src/dropdown.js @@ -1,7 +1,7 @@ /** * -------------------------------------------------------------------------- - * Bootstrap (v4.3.1): dropdown.js - * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + * Bootstrap (v4.5.2): dropdown.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ @@ -16,7 +16,7 @@ import Util from './util' */ const NAME = 'dropdown' -const VERSION = '4.3.1' +const VERSION = '4.5.2' const DATA_KEY = 'bs.dropdown' const EVENT_KEY = `.${DATA_KEY}` const DATA_API_KEY = '.data-api' @@ -29,61 +29,52 @@ const ARROW_DOWN_KEYCODE = 40 // KeyboardEvent.which value for down arrow const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse) const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`) -const Event = { - 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}` -} - -const ClassName = { - DISABLED : 'disabled', - SHOW : 'show', - DROPUP : 'dropup', - DROPRIGHT : 'dropright', - DROPLEFT : 'dropleft', - MENURIGHT : 'dropdown-menu-right', - MENULEFT : 'dropdown-menu-left', - POSITION_STATIC : 'position-static' -} - -const Selector = { - 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)' -} - -const AttachmentMap = { - TOP : 'top-start', - TOPEND : 'top-end', - BOTTOM : 'bottom-start', - BOTTOMEND : 'bottom-end', - RIGHT : 'right-start', - RIGHTEND : 'right-end', - LEFT : 'left-start', - LEFTEND : 'left-end' -} +const EVENT_HIDE = `hide${EVENT_KEY}` +const EVENT_HIDDEN = `hidden${EVENT_KEY}` +const EVENT_SHOW = `show${EVENT_KEY}` +const EVENT_SHOWN = `shown${EVENT_KEY}` +const EVENT_CLICK = `click${EVENT_KEY}` +const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}` +const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}` +const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}` + +const CLASS_NAME_DISABLED = 'disabled' +const CLASS_NAME_SHOW = 'show' +const CLASS_NAME_DROPUP = 'dropup' +const CLASS_NAME_DROPRIGHT = 'dropright' +const CLASS_NAME_DROPLEFT = 'dropleft' +const CLASS_NAME_MENURIGHT = 'dropdown-menu-right' +const CLASS_NAME_POSITION_STATIC = 'position-static' + +const SELECTOR_DATA_TOGGLE = '[data-toggle="dropdown"]' +const SELECTOR_FORM_CHILD = '.dropdown form' +const SELECTOR_MENU = '.dropdown-menu' +const SELECTOR_NAVBAR_NAV = '.navbar-nav' +const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)' + +const PLACEMENT_TOP = 'top-start' +const PLACEMENT_TOPEND = 'top-end' +const PLACEMENT_BOTTOM = 'bottom-start' +const PLACEMENT_BOTTOMEND = 'bottom-end' +const PLACEMENT_RIGHT = 'right-start' +const PLACEMENT_LEFT = 'left-start' const Default = { - offset : 0, - flip : true, - boundary : 'scrollParent', - reference : 'toggle', - display : 'dynamic' + offset : 0, + flip : true, + boundary : 'scrollParent', + reference : 'toggle', + display : 'dynamic', + popperConfig : null } const DefaultType = { - offset : '(number|string|function)', - flip : 'boolean', - boundary : '(string|element)', - reference : '(string|element)', - display : 'string' + offset : '(number|string|function)', + flip : 'boolean', + boundary : '(string|element)', + reference : '(string|element)', + display : 'string', + popperConfig : '(null|object)' } /** @@ -120,12 +111,11 @@ class Dropdown { // Public toggle() { - if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) { + if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED)) { return } - const parent = Dropdown._getParentFromElement(this._element) - const isActive = $(this._menu).hasClass(ClassName.SHOW) + const isActive = $(this._menu).hasClass(CLASS_NAME_SHOW) Dropdown._clearMenus() @@ -133,10 +123,19 @@ class Dropdown { return } + this.show(true) + } + + show(usePopper = false) { + if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || $(this._menu).hasClass(CLASS_NAME_SHOW)) { + return + } + const relatedTarget = { relatedTarget: this._element } - const showEvent = $.Event(Event.SHOW, relatedTarget) + const showEvent = $.Event(EVENT_SHOW, relatedTarget) + const parent = Dropdown._getParentFromElement(this._element) $(parent).trigger(showEvent) @@ -145,7 +144,7 @@ class Dropdown { } // Disable totally Popper.js for Dropdown in Navbar - if (!this._inNavbar) { + if (!this._inNavbar && usePopper) { /** * Check for Popper dependency * Popper - https://popper.js.org @@ -171,7 +170,7 @@ class Dropdown { // to allow the menu to "escape" the scroll parent's boundaries // https://github.com/twbs/bootstrap/issues/24251 if (this._config.boundary !== 'scrollParent') { - $(parent).addClass(ClassName.POSITION_STATIC) + $(parent).addClass(CLASS_NAME_POSITION_STATIC) } this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig()) } @@ -181,51 +180,28 @@ class Dropdown { // only needed because of broken event delegation on iOS // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html if ('ontouchstart' in document.documentElement && - $(parent).closest(Selector.NAVBAR_NAV).length === 0) { + $(parent).closest(SELECTOR_NAVBAR_NAV).length === 0) { $(document.body).children().on('mouseover', null, $.noop) } this._element.focus() this._element.setAttribute('aria-expanded', true) - $(this._menu).toggleClass(ClassName.SHOW) + $(this._menu).toggleClass(CLASS_NAME_SHOW) $(parent) - .toggleClass(ClassName.SHOW) - .trigger($.Event(Event.SHOWN, relatedTarget)) - } - - show() { - if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED) || $(this._menu).hasClass(ClassName.SHOW)) { - return - } - - const relatedTarget = { - relatedTarget: this._element - } - const showEvent = $.Event(Event.SHOW, relatedTarget) - const parent = Dropdown._getParentFromElement(this._element) - - $(parent).trigger(showEvent) - - if (showEvent.isDefaultPrevented()) { - return - } - - $(this._menu).toggleClass(ClassName.SHOW) - $(parent) - .toggleClass(ClassName.SHOW) - .trigger($.Event(Event.SHOWN, relatedTarget)) + .toggleClass(CLASS_NAME_SHOW) + .trigger($.Event(EVENT_SHOWN, relatedTarget)) } hide() { - if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED) || !$(this._menu).hasClass(ClassName.SHOW)) { + if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || !$(this._menu).hasClass(CLASS_NAME_SHOW)) { return } const relatedTarget = { relatedTarget: this._element } - const hideEvent = $.Event(Event.HIDE, relatedTarget) + const hideEvent = $.Event(EVENT_HIDE, relatedTarget) const parent = Dropdown._getParentFromElement(this._element) $(parent).trigger(hideEvent) @@ -234,10 +210,14 @@ class Dropdown { return } - $(this._menu).toggleClass(ClassName.SHOW) + if (this._popper) { + this._popper.destroy() + } + + $(this._menu).toggleClass(CLASS_NAME_SHOW) $(parent) - .toggleClass(ClassName.SHOW) - .trigger($.Event(Event.HIDDEN, relatedTarget)) + .toggleClass(CLASS_NAME_SHOW) + .trigger($.Event(EVENT_HIDDEN, relatedTarget)) } dispose() { @@ -261,7 +241,7 @@ class Dropdown { // Private _addEventListeners() { - $(this._element).on(Event.CLICK, (event) => { + $(this._element).on(EVENT_CLICK, (event) => { event.preventDefault() event.stopPropagation() this.toggle() @@ -289,7 +269,7 @@ class Dropdown { const parent = Dropdown._getParentFromElement(this._element) if (parent) { - this._menu = parent.querySelector(Selector.MENU) + this._menu = parent.querySelector(SELECTOR_MENU) } } return this._menu @@ -297,20 +277,19 @@ class Dropdown { _getPlacement() { const $parentDropdown = $(this._element.parentNode) - let placement = AttachmentMap.BOTTOM + let placement = PLACEMENT_BOTTOM // Handle dropup - if ($parentDropdown.hasClass(ClassName.DROPUP)) { - placement = AttachmentMap.TOP - if ($(this._menu).hasClass(ClassName.MENURIGHT)) { - placement = AttachmentMap.TOPEND - } - } else if ($parentDropdown.hasClass(ClassName.DROPRIGHT)) { - placement = AttachmentMap.RIGHT - } else if ($parentDropdown.hasClass(ClassName.DROPLEFT)) { - placement = AttachmentMap.LEFT - } else if ($(this._menu).hasClass(ClassName.MENURIGHT)) { - placement = AttachmentMap.BOTTOMEND + if ($parentDropdown.hasClass(CLASS_NAME_DROPUP)) { + placement = $(this._menu).hasClass(CLASS_NAME_MENURIGHT) + ? PLACEMENT_TOPEND + : PLACEMENT_TOP + } else if ($parentDropdown.hasClass(CLASS_NAME_DROPRIGHT)) { + placement = PLACEMENT_RIGHT + } else if ($parentDropdown.hasClass(CLASS_NAME_DROPLEFT)) { + placement = PLACEMENT_LEFT + } else if ($(this._menu).hasClass(CLASS_NAME_MENURIGHT)) { + placement = PLACEMENT_BOTTOMEND } return placement } @@ -359,7 +338,10 @@ class Dropdown { } } - return popperConfig + return { + ...popperConfig, + ...this._config.popperConfig + } } // Static @@ -389,7 +371,7 @@ class Dropdown { return } - const toggles = [].slice.call(document.querySelectorAll(Selector.DATA_TOGGLE)) + const toggles = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE)) for (let i = 0, len = toggles.length; i < len; i++) { const parent = Dropdown._getParentFromElement(toggles[i]) @@ -407,7 +389,7 @@ class Dropdown { } const dropdownMenu = context._menu - if (!$(parent).hasClass(ClassName.SHOW)) { + if (!$(parent).hasClass(CLASS_NAME_SHOW)) { continue } @@ -417,7 +399,7 @@ class Dropdown { continue } - const hideEvent = $.Event(Event.HIDE, relatedTarget) + const hideEvent = $.Event(EVENT_HIDE, relatedTarget) $(parent).trigger(hideEvent) if (hideEvent.isDefaultPrevented()) { continue @@ -431,10 +413,14 @@ class Dropdown { toggles[i].setAttribute('aria-expanded', 'false') - $(dropdownMenu).removeClass(ClassName.SHOW) + if (context._popper) { + context._popper.destroy() + } + + $(dropdownMenu).removeClass(CLASS_NAME_SHOW) $(parent) - .removeClass(ClassName.SHOW) - .trigger($.Event(Event.HIDDEN, relatedTarget)) + .removeClass(CLASS_NAME_SHOW) + .trigger($.Event(EVENT_HIDDEN, relatedTarget)) } } @@ -461,31 +447,35 @@ class Dropdown { if (/input|textarea/i.test(event.target.tagName) ? event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE && (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE || - $(event.target).closest(Selector.MENU).length) : !REGEXP_KEYDOWN.test(event.which)) { + $(event.target).closest(SELECTOR_MENU).length) : !REGEXP_KEYDOWN.test(event.which)) { return } - event.preventDefault() - event.stopPropagation() - - if (this.disabled || $(this).hasClass(ClassName.DISABLED)) { + if (this.disabled || $(this).hasClass(CLASS_NAME_DISABLED)) { return } const parent = Dropdown._getParentFromElement(this) - const isActive = $(parent).hasClass(ClassName.SHOW) + const isActive = $(parent).hasClass(CLASS_NAME_SHOW) + + if (!isActive && event.which === ESCAPE_KEYCODE) { + return + } + + event.preventDefault() + event.stopPropagation() if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) { if (event.which === ESCAPE_KEYCODE) { - const toggle = parent.querySelector(Selector.DATA_TOGGLE) - $(toggle).trigger('focus') + $(parent.querySelector(SELECTOR_DATA_TOGGLE)).trigger('focus') } $(this).trigger('click') return } - const items = [].slice.call(parent.querySelectorAll(Selector.VISIBLE_ITEMS)) + const items = [].slice.call(parent.querySelectorAll(SELECTOR_VISIBLE_ITEMS)) + .filter((item) => $(item).is(':visible')) if (items.length === 0) { return @@ -516,15 +506,15 @@ class Dropdown { */ $(document) - .on(Event.KEYDOWN_DATA_API, Selector.DATA_TOGGLE, Dropdown._dataApiKeydownHandler) - .on(Event.KEYDOWN_DATA_API, Selector.MENU, Dropdown._dataApiKeydownHandler) - .on(`${Event.CLICK_DATA_API} ${Event.KEYUP_DATA_API}`, Dropdown._clearMenus) - .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { + .on(EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown._dataApiKeydownHandler) + .on(EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown._dataApiKeydownHandler) + .on(`${EVENT_CLICK_DATA_API} ${EVENT_KEYUP_DATA_API}`, Dropdown._clearMenus) + .on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) { event.preventDefault() event.stopPropagation() Dropdown._jQueryInterface.call($(this), 'toggle') }) - .on(Event.CLICK_DATA_API, Selector.FORM_CHILD, (e) => { + .on(EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, (e) => { e.stopPropagation() }) @@ -541,5 +531,4 @@ $.fn[NAME].noConflict = () => { return Dropdown._jQueryInterface } - export default Dropdown |