2
0
mirror of https://github.com/tenrok/bootstrap.git synced 2026-05-15 11:59:39 +03:00
Files
bootstrap/js/src/util/component-functions.js
T
2022-05-20 02:10:15 +03:00

64 lines
2.0 KiB
JavaScript

/**
* --------------------------------------------------------------------------
* Bootstrap (v5.2.0-beta1): util/component-functions.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import EventHandler from '../dom/event-handler'
import { getElementFromSelector, getSelectorFromElement, isDisabled } from './index'
import SelectorEngine from '../dom/selector-engine'
const enableDismissTrigger = (component, method = 'hide') => {
const clickEvent = `click.dismiss${component.EVENT_KEY}`
const name = component.NAME
EventHandler.on(document, clickEvent, `[data-bs-dismiss="${name}"]`, function (event) {
if (['A', 'AREA'].includes(this.tagName)) {
event.preventDefault()
}
if (isDisabled(this)) {
return
}
const target = getElementFromSelector(this) || this.closest(`.${name}`)
const instance = component.getOrCreateInstance(target)
// Method argument is left, for Alert and only, as it doesn't implement the 'hide' method
instance[method]()
})
}
const eventActionOnPlugin = (Plugin, onEvent, stringSelector, method, callback = null) => {
eventAction(`${onEvent}.${Plugin.NAME}`, stringSelector, data => {
const instances = data.targets.filter(Boolean).map(element => Plugin.getOrCreateInstance(element))
if (typeof callback === 'function') {
callback({ ...data, instances })
}
for (const instance of instances) {
instance[method]()
}
})
}
const eventAction = (onEvent, stringSelector, callback) => {
const selector = `${stringSelector}:not(.disabled):not(:disabled)`
EventHandler.on(document, onEvent, selector, function (event) {
if (['A', 'AREA'].includes(this.tagName)) {
event.preventDefault()
}
const selector = getSelectorFromElement(this)
const targets = selector ? SelectorEngine.find(selector) : [this]
callback({ targets, event })
})
}
export {
enableDismissTrigger,
eventActionOnPlugin
}