diff options
author | Mario <mario@mariovavti.com> | 2022-08-19 13:15:48 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2022-08-19 13:15:48 +0000 |
commit | 185ddf1eaf82e08586be6c7689507ee924d9dd47 (patch) | |
tree | 218ff6da6fb1511a1b2823729607c7c4b13e30e9 /vendor/twbs/bootstrap/js/dist/tab.js | |
parent | 7dee47183d05b6e1f7d5c5588e2df9993fb294dd (diff) | |
download | volse-hubzilla-185ddf1eaf82e08586be6c7689507ee924d9dd47.tar.gz volse-hubzilla-185ddf1eaf82e08586be6c7689507ee924d9dd47.tar.bz2 volse-hubzilla-185ddf1eaf82e08586be6c7689507ee924d9dd47.zip |
update to bootstrap 5.2 and fixes
Diffstat (limited to 'vendor/twbs/bootstrap/js/dist/tab.js')
-rw-r--r-- | vendor/twbs/bootstrap/js/dist/tab.js | 429 |
1 files changed, 215 insertions, 214 deletions
diff --git a/vendor/twbs/bootstrap/js/dist/tab.js b/vendor/twbs/bootstrap/js/dist/tab.js index c3c24b75c..e36ff611a 100644 --- a/vendor/twbs/bootstrap/js/dist/tab.js +++ b/vendor/twbs/bootstrap/js/dist/tab.js @@ -1,13 +1,13 @@ /*! - * Bootstrap tab.js v5.1.3 (https://getbootstrap.com/) - * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors) + * Bootstrap tab.js v5.2.0 (https://getbootstrap.com/) + * Copyright 2011-2022 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors) * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/event-handler.js'), require('./dom/selector-engine.js'), require('./base-component.js')) : - typeof define === 'function' && define.amd ? define(['./dom/event-handler', './dom/selector-engine', './base-component'], factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Tab = factory(global.EventHandler, global.SelectorEngine, global.Base)); -})(this, (function (EventHandler, SelectorEngine, BaseComponent) { 'use strict'; + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./util/index'), require('./dom/event-handler'), require('./dom/selector-engine'), require('./base-component')) : + typeof define === 'function' && define.amd ? define(['./util/index', './dom/event-handler', './dom/selector-engine', './base-component'], factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Tab = factory(global.Index, global.EventHandler, global.SelectorEngine, global.BaseComponent)); +})(this, (function (index, EventHandler, SelectorEngine, BaseComponent) { 'use strict'; const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { default: e }; @@ -17,270 +17,266 @@ /** * -------------------------------------------------------------------------- - * Bootstrap (v5.1.3): util/index.js - * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) - * -------------------------------------------------------------------------- - */ - - const getSelector = element => { - let selector = element.getAttribute('data-bs-target'); - - if (!selector || selector === '#') { - let 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]}`; - } - - selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null; - } - - return selector; - }; - - const getElementFromSelector = element => { - const selector = getSelector(element); - return selector ? document.querySelector(selector) : null; - }; - - const isDisabled = element => { - if (!element || element.nodeType !== Node.ELEMENT_NODE) { - return true; - } - - if (element.classList.contains('disabled')) { - return true; - } - - if (typeof element.disabled !== 'undefined') { - return element.disabled; - } - - return element.hasAttribute('disabled') && element.getAttribute('disabled') !== 'false'; - }; - /** - * Trick to restart an element's animation - * - * @param {HTMLElement} element - * @return void - * - * @see https://www.charistheo.io/blog/2021/02/restart-a-css-animation-with-javascript/#restarting-a-css-animation - */ - - - const reflow = element => { - // eslint-disable-next-line no-unused-expressions - element.offsetHeight; - }; - - const getjQuery = () => { - const { - jQuery - } = window; - - if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) { - return jQuery; - } - - return null; - }; - - const DOMContentLoadedCallbacks = []; - - const onDOMContentLoaded = callback => { - if (document.readyState === 'loading') { - // add listener on the first call when the document is in loading state - if (!DOMContentLoadedCallbacks.length) { - document.addEventListener('DOMContentLoaded', () => { - DOMContentLoadedCallbacks.forEach(callback => callback()); - }); - } - - DOMContentLoadedCallbacks.push(callback); - } else { - callback(); - } - }; - - const defineJQueryPlugin = plugin => { - onDOMContentLoaded(() => { - const $ = getjQuery(); - /* istanbul ignore if */ - - if ($) { - const name = plugin.NAME; - const JQUERY_NO_CONFLICT = $.fn[name]; - $.fn[name] = plugin.jQueryInterface; - $.fn[name].Constructor = plugin; - - $.fn[name].noConflict = () => { - $.fn[name] = JQUERY_NO_CONFLICT; - return plugin.jQueryInterface; - }; - } - }); - }; - - /** - * -------------------------------------------------------------------------- - * Bootstrap (v5.1.3): tab.js + * Bootstrap (v5.2.0): tab.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ /** - * ------------------------------------------------------------------------ * Constants - * ------------------------------------------------------------------------ */ const NAME = 'tab'; const DATA_KEY = 'bs.tab'; const EVENT_KEY = `.${DATA_KEY}`; - const DATA_API_KEY = '.data-api'; 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_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`; - const CLASS_NAME_DROPDOWN_MENU = 'dropdown-menu'; + const EVENT_CLICK_DATA_API = `click${EVENT_KEY}`; + const EVENT_KEYDOWN = `keydown${EVENT_KEY}`; + const EVENT_LOAD_DATA_API = `load${EVENT_KEY}`; + const ARROW_LEFT_KEY = 'ArrowLeft'; + const ARROW_RIGHT_KEY = 'ArrowRight'; + const ARROW_UP_KEY = 'ArrowUp'; + const ARROW_DOWN_KEY = 'ArrowDown'; const CLASS_NAME_ACTIVE = 'active'; const CLASS_NAME_FADE = 'fade'; const CLASS_NAME_SHOW = 'show'; - const SELECTOR_DROPDOWN = '.dropdown'; - const SELECTOR_NAV_LIST_GROUP = '.nav, .list-group'; - const SELECTOR_ACTIVE = '.active'; - const SELECTOR_ACTIVE_UL = ':scope > li > .active'; - const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="tab"], [data-bs-toggle="pill"], [data-bs-toggle="list"]'; + const CLASS_DROPDOWN = 'dropdown'; const SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle'; - const SELECTOR_DROPDOWN_ACTIVE_CHILD = ':scope > .dropdown-menu .active'; + const SELECTOR_DROPDOWN_MENU = '.dropdown-menu'; + const SELECTOR_DROPDOWN_ITEM = '.dropdown-item'; + const NOT_SELECTOR_DROPDOWN_TOGGLE = ':not(.dropdown-toggle)'; + const SELECTOR_TAB_PANEL = '.list-group, .nav, [role="tablist"]'; + const SELECTOR_OUTER = '.nav-item, .list-group-item'; + const SELECTOR_INNER = `.nav-link${NOT_SELECTOR_DROPDOWN_TOGGLE}, .list-group-item${NOT_SELECTOR_DROPDOWN_TOGGLE}, [role="tab"]${NOT_SELECTOR_DROPDOWN_TOGGLE}`; + const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="tab"], [data-bs-toggle="pill"], [data-bs-toggle="list"]'; // todo:v6: could be only `tab` + + const SELECTOR_INNER_ELEM = `${SELECTOR_INNER}, ${SELECTOR_DATA_TOGGLE}`; + const SELECTOR_DATA_TOGGLE_ACTIVE = `.${CLASS_NAME_ACTIVE}[data-bs-toggle="tab"], .${CLASS_NAME_ACTIVE}[data-bs-toggle="pill"], .${CLASS_NAME_ACTIVE}[data-bs-toggle="list"]`; /** - * ------------------------------------------------------------------------ - * Class Definition - * ------------------------------------------------------------------------ + * Class definition */ class Tab extends BaseComponent__default.default { - // Getters + constructor(element) { + super(element); + this._parent = this._element.closest(SELECTOR_TAB_PANEL); + + if (!this._parent) { + return; // todo: should Throw exception on v6 + // throw new TypeError(`${element.outerHTML} has not a valid parent ${SELECTOR_INNER_ELEM}`) + } // Set up initial aria attributes + + + this._setInitialAttributes(this._parent, this._getChildren()); + + EventHandler__default.default.on(this._element, EVENT_KEYDOWN, event => this._keydown(event)); + } // Getters + + static get NAME() { return NAME; } // Public show() { - if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && this._element.classList.contains(CLASS_NAME_ACTIVE)) { - return; - } + // Shows this elem and deactivate the active sibling if exists + const innerElem = this._element; - let previous; - const target = getElementFromSelector(this._element); + if (this._elemIsActive(innerElem)) { + return; + } // Search for active tab on same parent to deactivate it - const listElement = this._element.closest(SELECTOR_NAV_LIST_GROUP); - if (listElement) { - const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? SELECTOR_ACTIVE_UL : SELECTOR_ACTIVE; - previous = SelectorEngine__default.default.find(itemSelector, listElement); - previous = previous[previous.length - 1]; - } + const active = this._getActiveElem(); - const hideEvent = previous ? EventHandler__default.default.trigger(previous, EVENT_HIDE, { - relatedTarget: this._element + const hideEvent = active ? EventHandler__default.default.trigger(active, EVENT_HIDE, { + relatedTarget: innerElem }) : null; - const showEvent = EventHandler__default.default.trigger(this._element, EVENT_SHOW, { - relatedTarget: previous + const showEvent = EventHandler__default.default.trigger(innerElem, EVENT_SHOW, { + relatedTarget: active }); - if (showEvent.defaultPrevented || hideEvent !== null && hideEvent.defaultPrevented) { + if (showEvent.defaultPrevented || hideEvent && hideEvent.defaultPrevented) { return; } - this._activate(this._element, listElement); + this._deactivate(active, innerElem); + + this._activate(innerElem, active); + } // Private + + + _activate(element, relatedElem) { + if (!element) { + return; + } + + element.classList.add(CLASS_NAME_ACTIVE); + + this._activate(index.getElementFromSelector(element)); // Search and activate/show the proper section + const complete = () => { - EventHandler__default.default.trigger(previous, EVENT_HIDDEN, { - relatedTarget: this._element + if (element.getAttribute('role') !== 'tab') { + element.classList.add(CLASS_NAME_SHOW); + return; + } + + element.focus(); + element.removeAttribute('tabindex'); + element.setAttribute('aria-selected', true); + + this._toggleDropDown(element, true); + + EventHandler__default.default.trigger(element, EVENT_SHOWN, { + relatedTarget: relatedElem }); - EventHandler__default.default.trigger(this._element, EVENT_SHOWN, { - relatedTarget: previous + }; + + this._queueCallback(complete, element, element.classList.contains(CLASS_NAME_FADE)); + } + + _deactivate(element, relatedElem) { + if (!element) { + return; + } + + element.classList.remove(CLASS_NAME_ACTIVE); + element.blur(); + + this._deactivate(index.getElementFromSelector(element)); // Search and deactivate the shown section too + + + const complete = () => { + if (element.getAttribute('role') !== 'tab') { + element.classList.remove(CLASS_NAME_SHOW); + return; + } + + element.setAttribute('aria-selected', false); + element.setAttribute('tabindex', '-1'); + + this._toggleDropDown(element, false); + + EventHandler__default.default.trigger(element, EVENT_HIDDEN, { + relatedTarget: relatedElem }); }; - if (target) { - this._activate(target, target.parentNode, complete); - } else { - complete(); + this._queueCallback(complete, element, element.classList.contains(CLASS_NAME_FADE)); + } + + _keydown(event) { + if (![ARROW_LEFT_KEY, ARROW_RIGHT_KEY, ARROW_UP_KEY, ARROW_DOWN_KEY].includes(event.key)) { + return; } - } // Private + event.stopPropagation(); // stopPropagation/preventDefault both added to support up/down keys without scrolling the page - _activate(element, container, callback) { - const activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? SelectorEngine__default.default.find(SELECTOR_ACTIVE_UL, container) : SelectorEngine__default.default.children(container, SELECTOR_ACTIVE); - const active = activeElements[0]; - const isTransitioning = callback && active && active.classList.contains(CLASS_NAME_FADE); + event.preventDefault(); + const isNext = [ARROW_RIGHT_KEY, ARROW_DOWN_KEY].includes(event.key); + const nextActiveElement = index.getNextActiveElement(this._getChildren().filter(element => !index.isDisabled(element)), event.target, isNext, true); - const complete = () => this._transitionComplete(element, active, callback); + if (nextActiveElement) { + Tab.getOrCreateInstance(nextActiveElement).show(); + } + } - if (active && isTransitioning) { - active.classList.remove(CLASS_NAME_SHOW); + _getChildren() { + // collection of inner elements + return SelectorEngine__default.default.find(SELECTOR_INNER_ELEM, this._parent); + } - this._queueCallback(complete, element, true); - } else { - complete(); + _getActiveElem() { + return this._getChildren().find(child => this._elemIsActive(child)) || null; + } + + _setInitialAttributes(parent, children) { + this._setAttributeIfNotExists(parent, 'role', 'tablist'); + + for (const child of children) { + this._setInitialAttributesOnChild(child); } } - _transitionComplete(element, active, callback) { - if (active) { - active.classList.remove(CLASS_NAME_ACTIVE); - const dropdownChild = SelectorEngine__default.default.findOne(SELECTOR_DROPDOWN_ACTIVE_CHILD, active.parentNode); + _setInitialAttributesOnChild(child) { + child = this._getInnerElement(child); - if (dropdownChild) { - dropdownChild.classList.remove(CLASS_NAME_ACTIVE); - } + const isActive = this._elemIsActive(child); - if (active.getAttribute('role') === 'tab') { - active.setAttribute('aria-selected', false); - } + const outerElem = this._getOuterElement(child); + + child.setAttribute('aria-selected', isActive); + + if (outerElem !== child) { + this._setAttributeIfNotExists(outerElem, 'role', 'presentation'); } - element.classList.add(CLASS_NAME_ACTIVE); + if (!isActive) { + child.setAttribute('tabindex', '-1'); + } - if (element.getAttribute('role') === 'tab') { - element.setAttribute('aria-selected', true); + this._setAttributeIfNotExists(child, 'role', 'tab'); // set attributes to the related panel too + + + this._setInitialAttributesOnTargetPanel(child); + } + + _setInitialAttributesOnTargetPanel(child) { + const target = index.getElementFromSelector(child); + + if (!target) { + return; } - reflow(element); + this._setAttributeIfNotExists(target, 'role', 'tabpanel'); - if (element.classList.contains(CLASS_NAME_FADE)) { - element.classList.add(CLASS_NAME_SHOW); + if (child.id) { + this._setAttributeIfNotExists(target, 'aria-labelledby', `#${child.id}`); } + } - let parent = element.parentNode; + _toggleDropDown(element, open) { + const outerElem = this._getOuterElement(element); - if (parent && parent.nodeName === 'LI') { - parent = parent.parentNode; + if (!outerElem.classList.contains(CLASS_DROPDOWN)) { + return; } - if (parent && parent.classList.contains(CLASS_NAME_DROPDOWN_MENU)) { - const dropdownElement = element.closest(SELECTOR_DROPDOWN); + const toggle = (selector, className) => { + const element = SelectorEngine__default.default.findOne(selector, outerElem); - if (dropdownElement) { - SelectorEngine__default.default.find(SELECTOR_DROPDOWN_TOGGLE, dropdownElement).forEach(dropdown => dropdown.classList.add(CLASS_NAME_ACTIVE)); + if (element) { + element.classList.toggle(className, open); } + }; - element.setAttribute('aria-expanded', true); - } + toggle(SELECTOR_DROPDOWN_TOGGLE, CLASS_NAME_ACTIVE); + toggle(SELECTOR_DROPDOWN_MENU, CLASS_NAME_SHOW); + toggle(SELECTOR_DROPDOWN_ITEM, CLASS_NAME_ACTIVE); + outerElem.setAttribute('aria-expanded', open); + } - if (callback) { - callback(); + _setAttributeIfNotExists(element, attribute, value) { + if (!element.hasAttribute(attribute)) { + element.setAttribute(attribute, value); } + } + + _elemIsActive(elem) { + return elem.classList.contains(CLASS_NAME_ACTIVE); + } // Try to get the inner element (usually the .nav-link) + + + _getInnerElement(elem) { + return elem.matches(SELECTOR_INNER_ELEM) ? elem : SelectorEngine__default.default.findOne(SELECTOR_INNER_ELEM, elem); + } // Try to get the outer element (usually the .nav-item) + + + _getOuterElement(elem) { + return elem.closest(SELECTOR_OUTER) || elem; } // Static @@ -288,21 +284,21 @@ return this.each(function () { const data = Tab.getOrCreateInstance(this); - if (typeof config === 'string') { - if (typeof data[config] === 'undefined') { - throw new TypeError(`No method named "${config}"`); - } + if (typeof config !== 'string') { + return; + } - data[config](); + if (data[config] === undefined || config.startsWith('_') || config === 'constructor') { + throw new TypeError(`No method named "${config}"`); } + + data[config](); }); } } /** - * ------------------------------------------------------------------------ - * Data Api implementation - * ------------------------------------------------------------------------ + * Data API implementation */ @@ -311,21 +307,26 @@ event.preventDefault(); } - if (isDisabled(this)) { + if (index.isDisabled(this)) { return; } - const data = Tab.getOrCreateInstance(this); - data.show(); + Tab.getOrCreateInstance(this).show(); + }); + /** + * Initialize on focus + */ + + EventHandler__default.default.on(window, EVENT_LOAD_DATA_API, () => { + for (const element of SelectorEngine__default.default.find(SELECTOR_DATA_TOGGLE_ACTIVE)) { + Tab.getOrCreateInstance(element); + } }); /** - * ------------------------------------------------------------------------ * jQuery - * ------------------------------------------------------------------------ - * add .Tab to jQuery only if jQuery is present */ - defineJQueryPlugin(Tab); + index.defineJQueryPlugin(Tab); return Tab; |