0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-15 12:54:23 +01:00
Bootstrap/js/src/tab.js

207 lines
5.4 KiB
JavaScript
Raw Normal View History

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
* --------------------------------------------------------------------------
*/
import {
defineJQueryPlugin,
getElementFromSelector,
isDisabled,
reflow
} 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-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
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'
2015-05-11 12:29:06 -07:00
2018-09-26 10:39:01 +02: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
static get NAME() {
return NAME
2019-09-04 17:58:29 +03:00
}
2018-09-26 10:39:01 +02:00
// Public
show() {
if ((this._element.parentNode &&
2017-09-26 10:04:31 +02:00
this._element.parentNode.nodeType === Node.ELEMENT_NODE &&
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
const target = getElementFromSelector(this._element)
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) {
const itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? SELECTOR_ACTIVE_UL : SELECTOR_ACTIVE
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
}
const hideEvent = previous ?
EventHandler.trigger(previous, EVENT_HIDE, { relatedTarget: this._element }) :
null
2015-05-11 12:29:06 -07: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 = () => {
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') ?
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) {
active.classList.remove(CLASS_NAME_SHOW)
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) {
active.classList.remove(CLASS_NAME_ACTIVE)
2015-05-13 12:48:34 -07: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) {
dropdownChild.classList.remove(CLASS_NAME_ACTIVE)
}
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
}
}
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
reflow(element)
if (element.classList.contains(CLASS_NAME_FADE)) {
element.classList.add(CLASS_NAME_SHOW)
}
2015-05-11 12:29:06 -07:00
let parent = element.parentNode
if (parent && parent.nodeName === 'LI') {
parent = parent.parentNode
}
if (parent && parent.classList.contains(CLASS_NAME_DROPDOWN_MENU)) {
const dropdownElement = element.closest(SELECTOR_DROPDOWN)
2018-09-26 10:39:01 +02:00
if (dropdownElement) {
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 () {
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
/**
* Data API implementation
2018-09-26 10:39:01 +02:00
*/
2015-05-11 12:29:06 -07:00
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
if (['A', 'AREA'].includes(this.tagName)) {
event.preventDefault()
}
if (isDisabled(this)) {
return
}
2017-09-26 10:04:31 +02: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
*/
defineJQueryPlugin(Tab)
2015-05-11 12:29:06 -07:00
export default Tab