2017-08-19 23:24:45 +08:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2022-10-03 15:44:02 +08:00
|
|
|
* Bootstrap (v5.2.2): dom/event-handler.js
|
2020-06-17 02:41:47 +08:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
2017-08-19 23:24:45 +08:00
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-08-02 21:51:05 +08:00
|
|
|
import { getjQuery } from '../util/index'
|
2019-02-15 04:19:10 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2017-08-23 00:47:54 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
const namespaceRegex = /[^.]*(?=\..*)\.|.*/
|
|
|
|
const stripNameRegex = /\..*/
|
2019-02-26 19:20:34 +08:00
|
|
|
const stripUidRegex = /::\d+$/
|
|
|
|
const eventRegistry = {} // Events storage
|
|
|
|
let uidEvent = 1
|
|
|
|
const customEvents = {
|
2018-09-14 20:27:30 +08:00
|
|
|
mouseenter: 'mouseover',
|
|
|
|
mouseleave: 'mouseout'
|
|
|
|
}
|
2022-04-18 06:30:56 +08:00
|
|
|
|
2020-05-02 21:56:23 +08:00
|
|
|
const nativeEvents = new Set([
|
2019-02-26 19:20:34 +08:00
|
|
|
'click',
|
|
|
|
'dblclick',
|
|
|
|
'mouseup',
|
|
|
|
'mousedown',
|
|
|
|
'contextmenu',
|
|
|
|
'mousewheel',
|
|
|
|
'DOMMouseScroll',
|
|
|
|
'mouseover',
|
|
|
|
'mouseout',
|
|
|
|
'mousemove',
|
|
|
|
'selectstart',
|
|
|
|
'selectend',
|
|
|
|
'keydown',
|
|
|
|
'keypress',
|
|
|
|
'keyup',
|
2018-09-14 20:27:30 +08:00
|
|
|
'orientationchange',
|
2019-02-26 19:20:34 +08:00
|
|
|
'touchstart',
|
|
|
|
'touchmove',
|
|
|
|
'touchend',
|
|
|
|
'touchcancel',
|
|
|
|
'pointerdown',
|
|
|
|
'pointermove',
|
|
|
|
'pointerup',
|
|
|
|
'pointerleave',
|
|
|
|
'pointercancel',
|
|
|
|
'gesturestart',
|
|
|
|
'gesturechange',
|
|
|
|
'gestureend',
|
|
|
|
'focus',
|
|
|
|
'blur',
|
|
|
|
'change',
|
|
|
|
'reset',
|
|
|
|
'select',
|
|
|
|
'submit',
|
|
|
|
'focusin',
|
|
|
|
'focusout',
|
|
|
|
'load',
|
|
|
|
'unload',
|
|
|
|
'beforeunload',
|
|
|
|
'resize',
|
|
|
|
'move',
|
|
|
|
'DOMContentLoaded',
|
|
|
|
'readystatechange',
|
|
|
|
'error',
|
|
|
|
'abort',
|
|
|
|
'scroll'
|
2020-05-02 21:56:23 +08:00
|
|
|
])
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
/**
|
|
|
|
* Private methods
|
|
|
|
*/
|
2017-08-25 04:22:02 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
function makeEventUid(element, uid) {
|
2019-08-23 03:17:34 +08:00
|
|
|
return (uid && `${uid}::${uidEvent++}`) || element.uidEvent || uidEvent++
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
function getElementEvents(element) {
|
|
|
|
const uid = makeEventUid(element)
|
2019-02-26 21:25:59 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
element.uidEvent = uid
|
2019-02-26 21:25:59 +08:00
|
|
|
eventRegistry[uid] = eventRegistry[uid] || {}
|
2018-09-14 20:27:30 +08:00
|
|
|
|
2019-02-26 21:25:59 +08:00
|
|
|
return eventRegistry[uid]
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
function bootstrapHandler(element, fn) {
|
|
|
|
return function handler(event) {
|
2022-07-01 03:52:47 +08:00
|
|
|
hydrateObj(event, { delegateTarget: element })
|
2020-06-21 00:00:53 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
if (handler.oneOff) {
|
|
|
|
EventHandler.off(element, event.type, fn)
|
2017-09-20 20:19:10 +08:00
|
|
|
}
|
2017-08-23 00:47:54 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
return fn.apply(element, [event])
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
function bootstrapDelegationHandler(element, selector, fn) {
|
|
|
|
return function handler(event) {
|
|
|
|
const domElements = element.querySelectorAll(selector)
|
2019-02-15 04:19:10 +08:00
|
|
|
|
2019-02-26 19:20:34 +08:00
|
|
|
for (let { target } = event; target && target !== this; target = target.parentNode) {
|
2021-10-29 15:38:52 +08:00
|
|
|
for (const domElement of domElements) {
|
2021-11-09 21:44:14 +08:00
|
|
|
if (domElement !== target) {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-21 00:00:53 +08:00
|
|
|
|
2022-07-01 03:52:47 +08:00
|
|
|
hydrateObj(event, { delegateTarget: target })
|
2018-09-14 20:27:30 +08:00
|
|
|
|
2021-11-09 21:44:14 +08:00
|
|
|
if (handler.oneOff) {
|
|
|
|
EventHandler.off(element, event.type, selector, fn)
|
2017-08-23 18:01:38 +08:00
|
|
|
}
|
2021-11-09 21:44:14 +08:00
|
|
|
|
|
|
|
return fn.apply(target, [event])
|
2017-08-23 18:01:38 +08:00
|
|
|
}
|
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
function findHandler(events, callable, delegationSelector = null) {
|
2021-12-16 19:43:36 +08:00
|
|
|
return Object.values(events)
|
2022-05-16 22:59:50 +08:00
|
|
|
.find(event => event.callable === callable && event.delegationSelector === delegationSelector)
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
function normalizeParameters(originalTypeEvent, handler, delegationFunction) {
|
2022-05-16 22:59:50 +08:00
|
|
|
const isDelegated = typeof handler === 'string'
|
|
|
|
// todo: tooltip passes `false` instead of selector, so we need to check
|
|
|
|
const callable = isDelegated ? delegationFunction : (handler || delegationFunction)
|
2021-04-13 11:25:58 +08:00
|
|
|
let typeEvent = getTypeEvent(originalTypeEvent)
|
2019-02-15 04:19:10 +08:00
|
|
|
|
2021-10-10 02:49:49 +08:00
|
|
|
if (!nativeEvents.has(typeEvent)) {
|
2018-09-14 20:27:30 +08:00
|
|
|
typeEvent = originalTypeEvent
|
2017-10-21 07:38:59 +08:00
|
|
|
}
|
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
return [isDelegated, callable, typeEvent]
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-10-21 07:38:59 +08:00
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
function addHandler(element, originalTypeEvent, handler, delegationFunction, oneOff) {
|
2019-02-14 03:40:15 +08:00
|
|
|
if (typeof originalTypeEvent !== 'string' || !element) {
|
2018-09-14 20:27:30 +08:00
|
|
|
return
|
|
|
|
}
|
2017-10-21 07:38:59 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
let [isDelegated, callable, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction)
|
2017-10-21 07:38:59 +08:00
|
|
|
|
2021-04-13 11:25:58 +08:00
|
|
|
// in case of mouseenter or mouseleave wrap the handler within a function that checks for its DOM position
|
|
|
|
// this prevents the handler from being dispatched the same way as mouseover or mouseout does
|
2022-04-18 06:30:56 +08:00
|
|
|
if (originalTypeEvent in customEvents) {
|
2021-10-29 15:38:35 +08:00
|
|
|
const wrapFunction = fn => {
|
2021-04-13 11:25:58 +08:00
|
|
|
return function (event) {
|
2021-04-19 13:30:33 +08:00
|
|
|
if (!event.relatedTarget || (event.relatedTarget !== event.delegateTarget && !event.delegateTarget.contains(event.relatedTarget))) {
|
2021-04-13 11:25:58 +08:00
|
|
|
return fn.call(this, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
callable = wrapFunction(callable)
|
2021-04-13 11:25:58 +08:00
|
|
|
}
|
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
const events = getElementEvents(element)
|
2019-02-26 19:20:34 +08:00
|
|
|
const handlers = events[typeEvent] || (events[typeEvent] = {})
|
2022-05-16 22:59:50 +08:00
|
|
|
const previousFunction = findHandler(handlers, callable, isDelegated ? handler : null)
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
if (previousFunction) {
|
|
|
|
previousFunction.oneOff = previousFunction.oneOff && oneOff
|
2019-02-15 04:19:10 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
return
|
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
const uid = makeEventUid(callable, originalTypeEvent.replace(namespaceRegex, ''))
|
|
|
|
const fn = isDelegated ?
|
|
|
|
bootstrapDelegationHandler(element, handler, callable) :
|
|
|
|
bootstrapHandler(element, callable)
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
fn.delegationSelector = isDelegated ? handler : null
|
|
|
|
fn.callable = callable
|
2018-09-14 20:27:30 +08:00
|
|
|
fn.oneOff = oneOff
|
|
|
|
fn.uidEvent = uid
|
|
|
|
handlers[uid] = fn
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
element.addEventListener(typeEvent, fn, isDelegated)
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
function removeHandler(element, events, typeEvent, handler, delegationSelector) {
|
|
|
|
const fn = findHandler(events[typeEvent], handler, delegationSelector)
|
2019-02-15 04:19:10 +08:00
|
|
|
|
2019-06-03 19:08:17 +08:00
|
|
|
if (!fn) {
|
2018-09-14 20:27:30 +08:00
|
|
|
return
|
2017-09-20 20:19:10 +08:00
|
|
|
}
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
element.removeEventListener(typeEvent, fn, Boolean(delegationSelector))
|
|
|
|
delete events[typeEvent][fn.uidEvent]
|
|
|
|
}
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
function removeNamespacedHandlers(element, events, typeEvent, namespace) {
|
|
|
|
const storeElementEvent = events[typeEvent] || {}
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2021-07-30 14:28:51 +08:00
|
|
|
for (const handlerKey of Object.keys(storeElementEvent)) {
|
2020-05-02 21:43:13 +08:00
|
|
|
if (handlerKey.includes(namespace)) {
|
2020-06-10 23:40:52 +08:00
|
|
|
const event = storeElementEvent[handlerKey]
|
2022-05-16 22:59:50 +08:00
|
|
|
removeHandler(element, events, typeEvent, event.callable, event.delegationSelector)
|
2020-06-10 23:40:52 +08:00
|
|
|
}
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-09-20 20:19:10 +08:00
|
|
|
|
2021-04-13 11:25:58 +08:00
|
|
|
function getTypeEvent(event) {
|
|
|
|
// allow to get the native events from namespaced events ('click.bs.button' --> 'click')
|
|
|
|
event = event.replace(stripNameRegex, '')
|
|
|
|
return customEvents[event] || event
|
|
|
|
}
|
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
const EventHandler = {
|
2021-10-29 15:38:35 +08:00
|
|
|
on(element, event, handler, delegationFunction) {
|
|
|
|
addHandler(element, event, handler, delegationFunction, false)
|
2018-09-14 20:27:30 +08:00
|
|
|
},
|
2017-08-19 23:24:45 +08:00
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
one(element, event, handler, delegationFunction) {
|
|
|
|
addHandler(element, event, handler, delegationFunction, true)
|
2018-09-14 20:27:30 +08:00
|
|
|
},
|
2017-08-19 23:24:45 +08:00
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
off(element, originalTypeEvent, handler, delegationFunction) {
|
2019-02-14 03:40:15 +08:00
|
|
|
if (typeof originalTypeEvent !== 'string' || !element) {
|
2018-09-14 20:27:30 +08:00
|
|
|
return
|
|
|
|
}
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
const [isDelegated, callable, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction)
|
2018-09-14 20:27:30 +08:00
|
|
|
const inNamespace = typeEvent !== originalTypeEvent
|
2022-05-16 22:59:50 +08:00
|
|
|
const events = getElementEvents(element)
|
|
|
|
const storeElementEvent = events[typeEvent] || {}
|
2020-05-02 21:31:51 +08:00
|
|
|
const isNamespace = originalTypeEvent.startsWith('.')
|
2017-08-21 23:43:04 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
if (typeof callable !== 'undefined') {
|
2018-09-14 20:27:30 +08:00
|
|
|
// Simplest case: handler is passed, remove that listener ONLY.
|
2022-06-14 13:33:53 +08:00
|
|
|
if (!Object.keys(storeElementEvent).length) {
|
2017-09-15 22:07:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
removeHandler(element, events, typeEvent, callable, isDelegated ? handler : null)
|
2018-09-14 20:27:30 +08:00
|
|
|
return
|
|
|
|
}
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
if (isNamespace) {
|
2021-07-30 14:28:51 +08:00
|
|
|
for (const elementEvent of Object.keys(events)) {
|
2020-06-10 23:40:52 +08:00
|
|
|
removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.slice(1))
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-08-25 04:22:02 +08:00
|
|
|
|
2021-07-30 14:28:51 +08:00
|
|
|
for (const keyHandlers of Object.keys(storeElementEvent)) {
|
2020-06-10 23:40:52 +08:00
|
|
|
const handlerKey = keyHandlers.replace(stripUidRegex, '')
|
2017-08-21 15:11:37 +08:00
|
|
|
|
2020-05-02 21:43:13 +08:00
|
|
|
if (!inNamespace || originalTypeEvent.includes(handlerKey)) {
|
2020-06-10 23:40:52 +08:00
|
|
|
const event = storeElementEvent[keyHandlers]
|
2022-05-16 22:59:50 +08:00
|
|
|
removeHandler(element, events, typeEvent, event.callable, event.delegationSelector)
|
2020-06-10 23:40:52 +08:00
|
|
|
}
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
},
|
2017-08-25 04:22:02 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
trigger(element, event, args) {
|
2019-02-14 03:40:15 +08:00
|
|
|
if (typeof event !== 'string' || !element) {
|
2018-09-14 20:27:30 +08:00
|
|
|
return null
|
|
|
|
}
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2020-11-01 21:32:36 +08:00
|
|
|
const $ = getjQuery()
|
2021-04-13 11:25:58 +08:00
|
|
|
const typeEvent = getTypeEvent(event)
|
2018-09-14 20:27:30 +08:00
|
|
|
const inNamespace = event !== typeEvent
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2021-12-16 19:25:23 +08:00
|
|
|
let jQueryEvent = null
|
2018-09-14 20:27:30 +08:00
|
|
|
let bubbles = true
|
|
|
|
let nativeDispatch = true
|
|
|
|
let defaultPrevented = false
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2019-08-02 21:51:05 +08:00
|
|
|
if (inNamespace && $) {
|
2018-09-14 20:27:30 +08:00
|
|
|
jQueryEvent = $.Event(event, args)
|
2017-08-25 04:22:02 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
$(element).trigger(jQueryEvent)
|
|
|
|
bubbles = !jQueryEvent.isPropagationStopped()
|
|
|
|
nativeDispatch = !jQueryEvent.isImmediatePropagationStopped()
|
|
|
|
defaultPrevented = jQueryEvent.isDefaultPrevented()
|
|
|
|
}
|
2017-08-25 04:22:02 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
let evt = new Event(event, { bubbles, cancelable: true })
|
|
|
|
evt = hydrateObj(evt, args)
|
2017-09-20 04:58:06 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
if (defaultPrevented) {
|
|
|
|
evt.preventDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeDispatch) {
|
|
|
|
element.dispatchEvent(evt)
|
|
|
|
}
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2021-12-16 19:25:23 +08:00
|
|
|
if (evt.defaultPrevented && jQueryEvent) {
|
2018-09-14 20:27:30 +08:00
|
|
|
jQueryEvent.preventDefault()
|
2017-09-15 22:07:24 +08:00
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
|
|
|
|
return evt
|
2017-08-19 23:24:45 +08:00
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-08-19 23:24:45 +08:00
|
|
|
|
2022-05-16 22:59:50 +08:00
|
|
|
function hydrateObj(obj, meta) {
|
|
|
|
for (const [key, value] of Object.entries(meta || {})) {
|
2022-07-01 03:52:47 +08:00
|
|
|
try {
|
|
|
|
obj[key] = value
|
|
|
|
} catch {
|
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
configurable: true,
|
|
|
|
get() {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-05-16 22:59:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2017-08-21 15:11:37 +08:00
|
|
|
export default EventHandler
|