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

334 lines
11 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
2021-10-09 08:33:12 +02:00
* Bootstrap tab.js v5.1.3 (https://getbootstrap.com/)
* Copyright 2011-2021 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) {
2021-08-04 17:41:51 +02:00
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));
2021-10-05 17:50:18 +02:00
})(this, (function (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
/**
* --------------------------------------------------------------------------
2021-10-09 08:33:12 +02:00
* Bootstrap (v5.1.3): util/index.js
2021-08-04 17:41:51 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2021-03-23 17:26:54 +01:00
const getSelector = element => {
let selector = element.getAttribute('data-bs-target');
2019-03-01 17:31:34 +01:00
if (!selector || selector === '#') {
2021-03-23 17:26:54 +01:00
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]}`;
}
2019-08-27 15:03:21 +02:00
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
2019-03-01 17:31:34 +01:00
}
2019-08-27 15:03:21 +02:00
return selector;
};
2021-03-23 17:26:54 +01:00
const getElementFromSelector = element => {
const selector = getSelector(element);
2019-08-27 15:03:21 +02:00
return selector ? document.querySelector(selector) : null;
2019-03-01 17:31:34 +01:00
};
2021-03-23 17:26:54 +01:00
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';
2019-03-01 17:31:34 +01:00
};
2021-08-04 17:41:51 +02:00
/**
* 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
*/
2019-03-01 17:31:34 +01:00
2021-08-04 17:41:51 +02:00
const reflow = element => {
// eslint-disable-next-line no-unused-expressions
element.offsetHeight;
};
2021-03-23 17:26:54 +01:00
const getjQuery = () => {
const {
jQuery
} = window;
2019-08-27 15:03:21 +02:00
2020-11-23 14:17:16 +01:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 15:03:21 +02:00
return jQuery;
}
return null;
};
const DOMContentLoadedCallbacks = [];
2021-03-23 17:26:54 +01:00
const onDOMContentLoaded = callback => {
2020-11-11 18:07:37 +01:00
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);
2020-11-11 18:07:37 +01:00
} else {
callback();
}
};
const defineJQueryPlugin = plugin => {
2021-03-23 17:26:54 +01:00
onDOMContentLoaded(() => {
const $ = getjQuery();
/* istanbul ignore if */
2020-06-14 00:40:28 +02:00
if ($) {
const name = plugin.NAME;
2021-03-23 17:26:54 +01:00
const JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2018-11-13 07:41:12 +01:00
2021-03-23 17:26:54 +01:00
$.fn[name].noConflict = () => {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 15:18:59 +01:00
}
});
};
2020-12-03 15:18:59 +01:00
2021-03-23 17:26:54 +01:00
/**
* --------------------------------------------------------------------------
2021-10-09 08:33:12 +02:00
* Bootstrap (v5.1.3): tab.js
2021-03-23 17:26:54 +01: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 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 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 SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle';
const SELECTOR_DROPDOWN_ACTIVE_CHILD = ':scope > .dropdown-menu .active';
2019-10-08 08:39:10 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-11 21:29:06 +02:00
2021-10-05 17:50:18 +02:00
class Tab extends BaseComponent__default.default {
2021-03-23 17:26:54 +01:00
// 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() {
if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && this._element.classList.contains(CLASS_NAME_ACTIVE)) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-11 21:29:06 +02:00
2021-03-23 17:26:54 +01:00
let previous;
const target = getElementFromSelector(this._element);
2020-05-13 20:53:43 +02:00
2021-03-23 17:26:54 +01:00
const listElement = this._element.closest(SELECTOR_NAV_LIST_GROUP);
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (listElement) {
2021-03-23 17:26:54 +01:00
const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? SELECTOR_ACTIVE_UL : SELECTOR_ACTIVE;
2021-10-05 17:50:18 +02:00
previous = SelectorEngine__default.default.find(itemSelector, listElement);
2018-11-13 07:41:12 +01:00
previous = previous[previous.length - 1];
}
2015-05-11 21:29:06 +02:00
2021-10-05 17:50:18 +02:00
const hideEvent = previous ? EventHandler__default.default.trigger(previous, EVENT_HIDE, {
relatedTarget: this._element
}) : null;
2021-10-05 17:50:18 +02:00
const showEvent = EventHandler__default.default.trigger(this._element, EVENT_SHOW, {
2019-03-01 17:31:34 +01:00
relatedTarget: previous
});
2015-05-11 21:29:06 +02:00
2019-03-01 17:31:34 +01:00
if (showEvent.defaultPrevented || hideEvent !== null && hideEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
this._activate(this._element, listElement);
2021-03-23 17:26:54 +01:00
const complete = () => {
2021-10-05 17:50:18 +02:00
EventHandler__default.default.trigger(previous, EVENT_HIDDEN, {
2021-03-23 17:26:54 +01:00
relatedTarget: this._element
2018-11-13 07:41:12 +01:00
});
2021-10-05 17:50:18 +02:00
EventHandler__default.default.trigger(this._element, EVENT_SHOWN, {
2018-11-13 07:41:12 +01:00
relatedTarget: previous
});
2017-09-30 23:28:03 +02:00
};
2018-11-13 07:41:12 +01:00
if (target) {
this._activate(target, target.parentNode, complete);
} else {
complete();
}
2019-01-04 17:29:45 +01:00
} // Private
2017-08-13 21:59:27 +02:00
2015-05-11 21:29:06 +02:00
2021-03-23 17:26:54 +01:00
_activate(element, container, callback) {
2021-10-05 17:50:18 +02:00
const activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? SelectorEngine__default.default.find(SELECTOR_ACTIVE_UL, container) : SelectorEngine__default.default.children(container, SELECTOR_ACTIVE);
2021-03-23 17:26:54 +01:00
const active = activeElements[0];
const isTransitioning = callback && active && active.classList.contains(CLASS_NAME_FADE);
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
const complete = () => this._transitionComplete(element, active, callback);
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (active && isTransitioning) {
2020-03-28 11:29:08 +01:00
active.classList.remove(CLASS_NAME_SHOW);
this._queueCallback(complete, element, true);
2018-11-13 07:41:12 +01:00
} else {
complete();
}
2021-03-23 17:26:54 +01:00
}
2017-09-06 06:05:12 +02:00
2021-03-23 17:26:54 +01:00
_transitionComplete(element, active, callback) {
2018-11-13 07:41:12 +01:00
if (active) {
2020-03-28 11:29:08 +01:00
active.classList.remove(CLASS_NAME_ACTIVE);
2021-10-05 17:50:18 +02:00
const dropdownChild = SelectorEngine__default.default.findOne(SELECTOR_DROPDOWN_ACTIVE_CHILD, active.parentNode);
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (dropdownChild) {
2020-03-28 11:29:08 +01:00
dropdownChild.classList.remove(CLASS_NAME_ACTIVE);
2017-09-06 06:05:12 +02:00
}
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (active.getAttribute('role') === 'tab') {
active.setAttribute('aria-selected', false);
2018-07-24 02:51:14 +02:00
}
2018-11-13 07:41:12 +01:00
}
2015-05-11 21:29:06 +02:00
2020-03-28 11:29:08 +01:00
element.classList.add(CLASS_NAME_ACTIVE);
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (element.getAttribute('role') === 'tab') {
element.setAttribute('aria-selected', true);
}
2015-05-11 21:29:06 +02:00
2019-03-01 17:31:34 +01:00
reflow(element);
2020-03-28 11:29:08 +01:00
if (element.classList.contains(CLASS_NAME_FADE)) {
element.classList.add(CLASS_NAME_SHOW);
}
2015-05-11 21:29:06 +02:00
let parent = element.parentNode;
if (parent && parent.nodeName === 'LI') {
parent = parent.parentNode;
}
if (parent && parent.classList.contains(CLASS_NAME_DROPDOWN_MENU)) {
2021-03-23 17:26:54 +01:00
const dropdownElement = element.closest(SELECTOR_DROPDOWN);
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (dropdownElement) {
2021-10-05 17:50:18 +02:00
SelectorEngine__default.default.find(SELECTOR_DROPDOWN_TOGGLE, dropdownElement).forEach(dropdown => dropdown.classList.add(CLASS_NAME_ACTIVE));
2018-07-24 02:51:14 +02:00
}
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
element.setAttribute('aria-expanded', true);
}
2017-09-06 06:05:12 +02:00
2018-11-13 07:41:12 +01:00
if (callback) {
callback();
}
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
2018-11-13 07:41:12 +01:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
2021-03-23 17:26:54 +01:00
throw new TypeError(`No method named "${config}"`);
2017-09-06 06:05:12 +02:00
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
data[config]();
2017-09-30 23:28:03 +02:00
}
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
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
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();
}
if (isDisabled(this)) {
return;
}
const data = Tab.getOrCreateInstance(this);
2019-03-01 17:31:34 +01:00
data.show();
2018-11-13 07:41:12 +01:00
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-11 18:07:37 +01:00
* add .Tab to jQuery only if jQuery is present
2018-11-13 07:41:12 +01:00
*/
2015-05-11 21:29:06 +02:00
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