0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-02 14:24:19 +01:00
Bootstrap/js/dist/tab.js

335 lines
10 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap tab.js v5.2.1 (https://getbootstrap.com/)
2022-05-13 08:07:23 +02:00
* Copyright 2011-2022 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) {
2022-05-13 08:07:23 +02:00
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';
2018-07-24 02:51:14 +02:00
2021-10-05 17:50:18 +02:00
const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { default: e };
2020-09-14 17:12:06 +02:00
2021-10-05 17:50:18 +02:00
const EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
const SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
const BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
2021-08-04 17:41:51 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.2.1): tab.js
2021-08-04 17:41:51 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
/**
2020-12-03 15:18:59 +01:00
* Constants
*/
2021-03-23 17:26:54 +01:00
const NAME = 'tab';
const DATA_KEY = 'bs.tab';
const EVENT_KEY = `.${DATA_KEY}`;
const EVENT_HIDE = `hide${EVENT_KEY}`;
const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
const EVENT_SHOW = `show${EVENT_KEY}`;
const EVENT_SHOWN = `shown${EVENT_KEY}`;
2022-05-13 08:07:23 +02:00
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';
2021-03-23 17:26:54 +01:00
const CLASS_NAME_ACTIVE = 'active';
const CLASS_NAME_FADE = 'fade';
const CLASS_NAME_SHOW = 'show';
2022-05-13 08:07:23 +02:00
const CLASS_DROPDOWN = 'dropdown';
2021-03-23 17:26:54 +01:00
const SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle';
2022-05-13 08:07:23 +02:00
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"]`;
2019-10-08 08:39:10 +02:00
/**
2022-05-13 08:07:23 +02:00
* Class definition
2019-10-08 08:39:10 +02:00
*/
2015-05-11 21:29:06 +02:00
2021-10-05 17:50:18 +02:00
class Tab extends BaseComponent__default.default {
2022-05-13 08:07:23 +02:00
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;
2021-03-23 17:26:54 +01:00
} // Public
2015-05-11 21:29:06 +02:00
2021-03-23 17:26:54 +01:00
show() {
2022-05-13 08:07:23 +02:00
// Shows this elem and deactivate the active sibling if exists
const innerElem = this._element;
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
if (this._elemIsActive(innerElem)) {
return;
} // Search for active tab on same parent to deactivate it
2020-05-13 20:53:43 +02:00
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
const active = this._getActiveElem();
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
const hideEvent = active ? EventHandler__default.default.trigger(active, EVENT_HIDE, {
relatedTarget: innerElem
}) : null;
2022-05-13 08:07:23 +02:00
const showEvent = EventHandler__default.default.trigger(innerElem, EVENT_SHOW, {
relatedTarget: active
2019-03-01 17:31:34 +01:00
});
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
if (showEvent.defaultPrevented || hideEvent && hideEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
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
2021-03-23 17:26:54 +01:00
const complete = () => {
2022-05-13 08:07:23 +02:00
if (element.getAttribute('role') !== 'tab') {
element.classList.add(CLASS_NAME_SHOW);
2022-05-13 08:07:23 +02:00
return;
}
element.focus();
element.removeAttribute('tabindex');
element.setAttribute('aria-selected', true);
this._toggleDropDown(element, true);
EventHandler__default.default.trigger(element, EVENT_SHOWN, {
relatedTarget: relatedElem
2018-11-13 07:41:12 +01:00
});
2022-05-13 08:07:23 +02:00
};
this._queueCallback(complete, element, element.classList.contains(CLASS_NAME_FADE));
2022-05-13 08:07:23 +02:00
}
_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);
2022-05-13 08:07:23 +02:00
return;
}
element.setAttribute('aria-selected', false);
element.setAttribute('tabindex', '-1');
this._toggleDropDown(element, false);
EventHandler__default.default.trigger(element, EVENT_HIDDEN, {
relatedTarget: relatedElem
2018-11-13 07:41:12 +01:00
});
2017-09-30 23:28:03 +02:00
};
this._queueCallback(complete, element, element.classList.contains(CLASS_NAME_FADE));
2022-05-13 08:07:23 +02:00
}
_keydown(event) {
if (![ARROW_LEFT_KEY, ARROW_RIGHT_KEY, ARROW_UP_KEY, ARROW_DOWN_KEY].includes(event.key)) {
return;
2018-11-13 07:41:12 +01:00
}
2017-08-13 21:59:27 +02:00
2022-05-13 08:07:23 +02:00
event.stopPropagation(); // stopPropagation/preventDefault both added to support up/down keys without scrolling the page
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);
if (nextActiveElement) {
Tab.getOrCreateInstance(nextActiveElement).show();
}
}
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
_getChildren() {
// collection of inner elements
return SelectorEngine__default.default.find(SELECTOR_INNER_ELEM, this._parent);
}
2016-10-10 02:26:51 +02:00
2022-05-13 08:07:23 +02:00
_getActiveElem() {
return this._getChildren().find(child => this._elemIsActive(child)) || null;
}
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
_setInitialAttributes(parent, children) {
this._setAttributeIfNotExists(parent, 'role', 'tablist');
2022-05-13 08:07:23 +02:00
for (const child of children) {
this._setInitialAttributesOnChild(child);
2018-11-13 07:41:12 +01:00
}
2021-03-23 17:26:54 +01:00
}
2017-09-06 06:05:12 +02:00
2022-05-13 08:07:23 +02:00
_setInitialAttributesOnChild(child) {
child = this._getInnerElement(child);
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
const isActive = this._elemIsActive(child);
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
const outerElem = this._getOuterElement(child);
child.setAttribute('aria-selected', isActive);
if (outerElem !== child) {
this._setAttributeIfNotExists(outerElem, 'role', 'presentation');
2018-11-13 07:41:12 +01:00
}
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
if (!isActive) {
child.setAttribute('tabindex', '-1');
}
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
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;
2018-11-13 07:41:12 +01:00
}
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
this._setAttributeIfNotExists(target, 'role', 'tabpanel');
2022-05-13 08:07:23 +02:00
if (child.id) {
this._setAttributeIfNotExists(target, 'aria-labelledby', `#${child.id}`);
}
2022-05-13 08:07:23 +02:00
}
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
_toggleDropDown(element, open) {
const outerElem = this._getOuterElement(element);
2022-05-13 08:07:23 +02:00
if (!outerElem.classList.contains(CLASS_DROPDOWN)) {
return;
}
2022-05-13 08:07:23 +02:00
const toggle = (selector, className) => {
const element = SelectorEngine__default.default.findOne(selector, outerElem);
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
if (element) {
element.classList.toggle(className, open);
2018-07-24 02:51:14 +02:00
}
2022-05-13 08:07:23 +02:00
};
2016-10-10 02:26:51 +02:00
2022-05-13 08:07:23 +02:00
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);
}
2017-09-06 06:05:12 +02:00
2022-05-13 08:07:23 +02:00
_setAttributeIfNotExists(element, attribute, value) {
if (!element.hasAttribute(attribute)) {
element.setAttribute(attribute, value);
2018-11-13 07:41:12 +01:00
}
2022-05-13 08:07:23 +02:00
}
_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;
2019-01-04 17:29:45 +01:00
} // Static
2017-09-06 06:05:12 +02:00
2021-03-23 17:26:54 +01:00
static jQueryInterface(config) {
2018-11-13 07:41:12 +01:00
return this.each(function () {
const data = Tab.getOrCreateInstance(this);
2017-09-06 06:05:12 +02:00
2022-05-13 08:07:23 +02:00
if (typeof config !== 'string') {
return;
}
2017-09-30 23:28:03 +02:00
2022-05-13 08:07:23 +02:00
if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
throw new TypeError(`No method named "${config}"`);
2017-09-30 23:28:03 +02:00
}
2022-05-13 08:07:23 +02:00
data[config]();
2018-11-13 07:41:12 +01:00
});
2021-03-23 17:26:54 +01:00
}
2015-05-11 21:29:06 +02:00
2021-03-23 17:26:54 +01:00
}
2018-11-13 07:41:12 +01:00
/**
2022-05-13 08:07:23 +02:00
* Data API implementation
2018-11-13 07:41:12 +01:00
*/
2015-05-11 21:29:06 +02:00
2017-09-30 23:28:03 +02:00
2021-10-05 17:50:18 +02:00
EventHandler__default.default.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
if (['A', 'AREA'].includes(this.tagName)) {
event.preventDefault();
}
2022-05-13 08:07:23 +02:00
if (index.isDisabled(this)) {
return;
}
2022-05-13 08:07:23 +02:00
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);
}
2018-11-13 07:41:12 +01:00
});
/**
* jQuery
*/
2015-05-11 21:29:06 +02:00
2022-05-13 08:07:23 +02:00
index.defineJQueryPlugin(Tab);
2015-05-11 21:29:06 +02:00
return Tab;
2018-07-24 02:51:14 +02:00
2021-10-05 17:50:18 +02:00
}));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=tab.js.map