2015-05-11 12:29:06 -07:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2021-10-09 09:33:12 +03:00
|
|
|
* Bootstrap (v5.1.3): tab.js
|
2020-06-16 21:41:47 +03:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2015-05-11 12:29:06 -07:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
import {
|
2020-12-08 07:16:50 +01:00
|
|
|
defineJQueryPlugin,
|
2019-07-23 21:15:00 +02:00
|
|
|
getElementFromSelector,
|
2021-03-17 08:52:40 +00:00
|
|
|
isDisabled,
|
2019-02-23 00:37:55 +02:00
|
|
|
reflow
|
2019-10-02 11:43:54 +02:00
|
|
|
} from './util/index'
|
|
|
|
import EventHandler from './dom/event-handler'
|
|
|
|
import SelectorEngine from './dom/selector-engine'
|
2019-09-04 17:58:29 +03:00
|
|
|
import BaseComponent from './base-component'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
|
2019-02-26 13:20:34 +02:00
|
|
|
const NAME = 'tab'
|
|
|
|
const DATA_KEY = 'bs.tab'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
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'
|
2020-07-22 22:33:11 +03:00
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="tab"], [data-bs-toggle="pill"], [data-bs-toggle="list"]'
|
2020-03-07 11:31:42 +02:00
|
|
|
const SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle'
|
|
|
|
const SELECTOR_DROPDOWN_ACTIVE_CHILD = ':scope > .dropdown-menu .active'
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
2021-10-13 15:19:28 +03:00
|
|
|
* Class definition
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
|
|
|
|
2019-09-04 17:58:29 +03:00
|
|
|
class Tab extends BaseComponent {
|
2018-09-26 10:39:01 +02:00
|
|
|
// Getters
|
2021-05-11 10:49:30 +03:00
|
|
|
static get NAME() {
|
|
|
|
return NAME
|
2019-09-04 17:58:29 +03:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Public
|
|
|
|
show() {
|
2019-08-22 21:17:34 +02:00
|
|
|
if ((this._element.parentNode &&
|
2017-09-26 10:04:31 +02:00
|
|
|
this._element.parentNode.nodeType === Node.ELEMENT_NODE &&
|
2021-04-20 08:26:58 +03:00
|
|
|
this._element.classList.contains(CLASS_NAME_ACTIVE))) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
let previous
|
2019-07-23 21:15:00 +02:00
|
|
|
const target = getElementFromSelector(this._element)
|
2020-04-28 21:17:23 +02:00
|
|
|
const listElement = this._element.closest(SELECTOR_NAV_LIST_GROUP)
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (listElement) {
|
2020-03-07 11:31:42 +02:00
|
|
|
const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? SELECTOR_ACTIVE_UL : SELECTOR_ACTIVE
|
2020-03-25 15:35:02 +01:00
|
|
|
previous = SelectorEngine.find(itemSelector, listElement)
|
2018-09-26 10:39:01 +02:00
|
|
|
previous = previous[previous.length - 1]
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
|
|
|
|
2020-12-11 15:47:34 +02:00
|
|
|
const hideEvent = previous ?
|
2021-10-13 15:19:28 +03:00
|
|
|
EventHandler.trigger(previous, EVENT_HIDE, { relatedTarget: this._element }) :
|
2020-12-11 15:47:34 +02:00
|
|
|
null
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
|
2017-09-26 10:04:31 +02:00
|
|
|
relatedTarget: previous
|
|
|
|
})
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2020-12-02 06:45:15 +02:00
|
|
|
if (showEvent.defaultPrevented || (hideEvent !== null && hideEvent.defaultPrevented)) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return
|
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2020-12-02 06:45:15 +02:00
|
|
|
this._activate(this._element, listElement)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
const complete = () => {
|
2021-10-13 15:19:28 +03:00
|
|
|
EventHandler.trigger(previous, EVENT_HIDDEN, { relatedTarget: this._element })
|
|
|
|
EventHandler.trigger(this._element, EVENT_SHOWN, { relatedTarget: previous })
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (target) {
|
|
|
|
this._activate(target, target.parentNode, complete)
|
|
|
|
} else {
|
|
|
|
complete()
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Private
|
|
|
|
_activate(element, container, callback) {
|
2019-02-26 13:20:34 +02:00
|
|
|
const activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ?
|
2020-03-07 11:31:42 +02:00
|
|
|
SelectorEngine.find(SELECTOR_ACTIVE_UL, container) :
|
|
|
|
SelectorEngine.children(container, SELECTOR_ACTIVE)
|
2017-09-26 10:04:31 +02:00
|
|
|
|
2019-02-26 13:20:34 +02:00
|
|
|
const active = activeElements[0]
|
2020-12-02 06:45:15 +02:00
|
|
|
const isTransitioning = callback && (active && active.classList.contains(CLASS_NAME_FADE))
|
|
|
|
|
|
|
|
const complete = () => this._transitionComplete(element, active, callback)
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (active && isTransitioning) {
|
2020-03-07 11:31:42 +02:00
|
|
|
active.classList.remove(CLASS_NAME_SHOW)
|
2021-04-11 02:27:18 +03:00
|
|
|
this._queueCallback(complete, element, true)
|
2018-09-26 10:39:01 +02:00
|
|
|
} else {
|
|
|
|
complete()
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
_transitionComplete(element, active, callback) {
|
|
|
|
if (active) {
|
2020-03-07 11:31:42 +02:00
|
|
|
active.classList.remove(CLASS_NAME_ACTIVE)
|
2015-05-13 12:48:34 -07:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
const dropdownChild = SelectorEngine.findOne(SELECTOR_DROPDOWN_ACTIVE_CHILD, active.parentNode)
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (dropdownChild) {
|
2020-03-07 11:31:42 +02:00
|
|
|
dropdownChild.classList.remove(CLASS_NAME_ACTIVE)
|
2017-07-18 14:22:39 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (active.getAttribute('role') === 'tab') {
|
|
|
|
active.setAttribute('aria-selected', false)
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
element.classList.add(CLASS_NAME_ACTIVE)
|
2018-09-26 10:39:01 +02:00
|
|
|
if (element.getAttribute('role') === 'tab') {
|
|
|
|
element.setAttribute('aria-selected', true)
|
|
|
|
}
|
2015-08-18 22:03:34 -07:00
|
|
|
|
2019-02-23 00:37:55 +02:00
|
|
|
reflow(element)
|
2019-02-06 11:12:02 +01:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
if (element.classList.contains(CLASS_NAME_FADE)) {
|
|
|
|
element.classList.add(CLASS_NAME_SHOW)
|
2019-02-06 11:12:02 +01:00
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2021-04-21 00:30:19 -05:00
|
|
|
let parent = element.parentNode
|
|
|
|
if (parent && parent.nodeName === 'LI') {
|
|
|
|
parent = parent.parentNode
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent && parent.classList.contains(CLASS_NAME_DROPDOWN_MENU)) {
|
2020-04-28 21:17:23 +02:00
|
|
|
const dropdownElement = element.closest(SELECTOR_DROPDOWN)
|
2018-11-14 10:16:56 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (dropdownElement) {
|
2021-07-30 09:28:51 +03:00
|
|
|
for (const dropdown of SelectorEngine.find(SELECTOR_DROPDOWN_TOGGLE, dropdownElement)) {
|
|
|
|
dropdown.classList.add(CLASS_NAME_ACTIVE)
|
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
element.setAttribute('aria-expanded', true)
|
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (callback) {
|
|
|
|
callback()
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
// Static
|
2019-07-28 15:24:46 +02:00
|
|
|
static jQueryInterface(config) {
|
2018-09-26 10:39:01 +02:00
|
|
|
return this.each(function () {
|
2021-06-03 18:53:27 +03:00
|
|
|
const data = Tab.getOrCreateInstance(this)
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (typeof config === 'string') {
|
|
|
|
if (typeof data[config] === 'undefined') {
|
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
2019-02-26 13:20:34 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
})
|
2015-05-11 12:29:06 -07:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
2021-10-13 15:19:28 +03:00
|
|
|
* Data API implementation
|
2018-09-26 10:39:01 +02:00
|
|
|
*/
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2020-03-07 11:31:42 +02:00
|
|
|
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
2021-04-20 08:26:58 +03:00
|
|
|
if (['A', 'AREA'].includes(this.tagName)) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDisabled(this)) {
|
|
|
|
return
|
|
|
|
}
|
2017-09-26 10:04:31 +02:00
|
|
|
|
2021-06-03 18:53:27 +03:00
|
|
|
const data = Tab.getOrCreateInstance(this)
|
2017-09-26 10:04:31 +02:00
|
|
|
data.show()
|
|
|
|
})
|
2015-05-11 12:29:06 -07:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* jQuery
|
|
|
|
*/
|
2020-11-01 14:32:36 +01:00
|
|
|
|
2021-05-11 10:49:30 +03:00
|
|
|
defineJQueryPlugin(Tab)
|
2015-05-11 12:29:06 -07:00
|
|
|
|
|
|
|
export default Tab
|