0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-12 00:08:59 +01:00
Bootstrap/js/dist/tab.js

372 lines
13 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap tab.js v5.0.0-beta2 (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) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/selector-engine.js'), require('./base-component.js')) :
typeof define === 'function' && define.amd ? define(['./dom/data', './dom/event-handler', './dom/selector-engine', './base-component'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Tab = factory(global.Data, global.EventHandler, global.SelectorEngine, global.Base));
}(this, (function (Data, EventHandler, SelectorEngine, BaseComponent) { 'use strict';
2018-07-24 02:51:14 +02:00
2020-09-14 17:12:06 +02:00
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
var SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
subClass.__proto__ = superClass;
}
2018-07-24 02:51:14 +02:00
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.0.0-beta2): util/index.js
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 17:31:34 +01:00
* --------------------------------------------------------------------------
*/
var MILLISECONDS_MULTIPLIER = 1000;
2019-08-27 15:03:21 +02:00
var TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-03-01 17:31:34 +01:00
2019-08-27 15:03:21 +02:00
var getSelector = function getSelector(element) {
2020-11-23 14:17:16 +01:00
var selector = element.getAttribute('data-bs-target');
2019-03-01 17:31:34 +01:00
if (!selector || selector === '#') {
var 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;
};
var getElementFromSelector = function getElementFromSelector(element) {
var selector = getSelector(element);
return selector ? document.querySelector(selector) : null;
2019-03-01 17:31:34 +01:00
};
var getTransitionDurationFromElement = function getTransitionDurationFromElement(element) {
if (!element) {
return 0;
} // Get transition-duration of the element
var _window$getComputedSt = window.getComputedStyle(element),
transitionDuration = _window$getComputedSt.transitionDuration,
transitionDelay = _window$getComputedSt.transitionDelay;
2020-11-23 14:17:16 +01:00
var floatTransitionDuration = Number.parseFloat(transitionDuration);
var floatTransitionDelay = Number.parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
2019-03-01 17:31:34 +01:00
if (!floatTransitionDuration && !floatTransitionDelay) {
return 0;
} // If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0];
transitionDelay = transitionDelay.split(',')[0];
2020-11-23 14:17:16 +01:00
return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
2019-03-01 17:31:34 +01:00
};
var triggerTransitionEnd = function triggerTransitionEnd(element) {
2020-03-28 11:29:08 +01:00
element.dispatchEvent(new Event(TRANSITION_END));
2019-03-01 17:31:34 +01:00
};
var emulateTransitionEnd = function emulateTransitionEnd(element, duration) {
var called = false;
var durationPadding = 5;
var emulatedDuration = duration + durationPadding;
function listener() {
called = true;
element.removeEventListener(TRANSITION_END, listener);
}
element.addEventListener(TRANSITION_END, listener);
setTimeout(function () {
if (!called) {
triggerTransitionEnd(element);
}
}, emulatedDuration);
};
var reflow = function reflow(element) {
return element.offsetHeight;
};
2019-08-27 15:03:21 +02:00
var getjQuery = function getjQuery() {
var _window = window,
jQuery = _window.jQuery;
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;
};
2020-11-11 18:07:37 +01:00
var onDOMContentLoaded = function onDOMContentLoaded(callback) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
};
document.documentElement.dir === 'rtl';
2020-12-03 14:08:31 +01:00
var defineJQueryPlugin = function defineJQueryPlugin(name, plugin) {
onDOMContentLoaded(function () {
var $ = getjQuery();
/* istanbul ignore if */
2020-06-14 00:40:28 +02:00
if ($) {
var JQUERY_NO_CONFLICT = $.fn[name];
$.fn[name] = plugin.jQueryInterface;
$.fn[name].Constructor = plugin;
2018-11-13 07:41:12 +01:00
$.fn[name].noConflict = function () {
$.fn[name] = JQUERY_NO_CONFLICT;
return plugin.jQueryInterface;
};
2020-12-03 15:18:59 +01:00
}
});
};
2020-12-03 15:18:59 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'tab';
2018-11-13 07:41:12 +01:00
var DATA_KEY = 'bs.tab';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
2020-03-28 11:29:08 +01:00
var EVENT_HIDE = "hide" + EVENT_KEY;
var EVENT_HIDDEN = "hidden" + EVENT_KEY;
var EVENT_SHOW = "show" + EVENT_KEY;
var EVENT_SHOWN = "shown" + EVENT_KEY;
var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
var CLASS_NAME_DROPDOWN_MENU = 'dropdown-menu';
var CLASS_NAME_ACTIVE = 'active';
var CLASS_NAME_DISABLED = 'disabled';
var CLASS_NAME_FADE = 'fade';
var CLASS_NAME_SHOW = 'show';
var SELECTOR_DROPDOWN = '.dropdown';
var SELECTOR_NAV_LIST_GROUP = '.nav, .list-group';
var SELECTOR_ACTIVE = '.active';
var SELECTOR_ACTIVE_UL = ':scope > li > .active';
2020-11-23 14:17:16 +01:00
var SELECTOR_DATA_TOGGLE = '[data-bs-toggle="tab"], [data-bs-toggle="pill"], [data-bs-toggle="list"]';
2020-03-28 11:29:08 +01:00
var SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle';
var 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
2020-12-03 15:18:59 +01:00
var Tab = /*#__PURE__*/function (_BaseComponent) {
_inheritsLoose(Tab, _BaseComponent);
2015-08-13 06:12:03 +02:00
2020-12-03 15:18:59 +01:00
function Tab() {
return _BaseComponent.apply(this, arguments) || this;
}
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
var _proto = Tab.prototype;
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
// Public
_proto.show = function show() {
var _this = this;
2015-05-11 21:29:06 +02:00
2020-03-28 11:29:08 +01:00
if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && this._element.classList.contains(CLASS_NAME_ACTIVE) || this._element.classList.contains(CLASS_NAME_DISABLED)) {
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
var previous;
2019-08-27 15:03:21 +02:00
var target = getElementFromSelector(this._element);
2020-05-13 20:53:43 +02:00
var 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) {
2020-03-28 11:29:08 +01:00
var itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? SELECTOR_ACTIVE_UL : SELECTOR_ACTIVE;
2020-09-14 17:12:06 +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
var hideEvent = previous ? EventHandler__default['default'].trigger(previous, EVENT_HIDE, {
relatedTarget: this._element
}) : null;
2020-09-14 17:12:06 +02:00
var 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);
var complete = function complete() {
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].trigger(previous, EVENT_HIDDEN, {
2018-11-13 07:41:12 +01:00
relatedTarget: _this._element
});
2020-09-14 17:12:06 +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
2018-11-13 07:41:12 +01:00
_proto._activate = function _activate(element, container, callback) {
var _this2 = this;
2015-05-11 21:29:06 +02:00
2020-09-14 17:12:06 +02:00
var activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? SelectorEngine__default['default'].find(SELECTOR_ACTIVE_UL, container) : SelectorEngine__default['default'].children(container, SELECTOR_ACTIVE);
2018-11-13 07:41:12 +01:00
var active = activeElements[0];
2020-03-28 11:29:08 +01:00
var isTransitioning = callback && active && active.classList.contains(CLASS_NAME_FADE);
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
var complete = function complete() {
return _this2._transitionComplete(element, active, callback);
2017-09-30 23:28:03 +02:00
};
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (active && isTransitioning) {
2019-03-01 17:31:34 +01:00
var transitionDuration = getTransitionDurationFromElement(active);
2020-03-28 11:29:08 +01:00
active.classList.remove(CLASS_NAME_SHOW);
EventHandler__default['default'].one(active, 'transitionend', complete);
2019-03-01 17:31:34 +01:00
emulateTransitionEnd(active, transitionDuration);
2018-11-13 07:41:12 +01:00
} else {
complete();
}
};
2017-09-06 06:05:12 +02:00
2018-11-13 07:41:12 +01:00
_proto._transitionComplete = function _transitionComplete(element, active, callback) {
if (active) {
2020-03-28 11:29:08 +01:00
active.classList.remove(CLASS_NAME_ACTIVE);
2020-09-14 17:12:06 +02:00
var 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
2020-03-28 11:29:08 +01:00
if (element.parentNode && element.parentNode.classList.contains(CLASS_NAME_DROPDOWN_MENU)) {
2020-05-13 20:53:43 +02:00
var dropdownElement = element.closest(SELECTOR_DROPDOWN);
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
if (dropdownElement) {
2020-09-14 17:12:06 +02:00
SelectorEngine__default['default'].find(SELECTOR_DROPDOWN_TOGGLE).forEach(function (dropdown) {
2020-03-28 11:29:08 +01:00
return dropdown.classList.add(CLASS_NAME_ACTIVE);
2019-03-01 17:31:34 +01:00
});
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
2019-08-27 15:03:21 +02:00
Tab.jQueryInterface = function jQueryInterface(config) {
2018-11-13 07:41:12 +01:00
return this.each(function () {
2020-09-14 17:12:06 +02:00
var data = Data__default['default'].getData(this, DATA_KEY) || new Tab(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') {
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
});
};
2017-09-30 23:28:03 +02:00
_createClass(Tab, null, [{
2020-12-03 15:18:59 +01:00
key: "DATA_KEY",
// Getters
get: function get() {
2020-12-03 15:18:59 +01:00
return DATA_KEY;
2018-11-13 07:41:12 +01:00
}
}]);
2015-05-11 21:29:06 +02:00
2018-11-13 07:41:12 +01:00
return Tab;
}(BaseComponent__default['default']);
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
2020-09-14 17:12:06 +02:00
EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
2018-11-13 07:41:12 +01:00
event.preventDefault();
2020-09-14 17:12:06 +02:00
var data = Data__default['default'].getData(this, DATA_KEY) || new Tab(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(NAME, Tab);
2015-05-11 21:29:06 +02:00
return Tab;
2018-07-24 02:51:14 +02:00
2019-11-08 09:11:23 +01:00
})));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=tab.js.map