2017-08-19 23:24:45 +08:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2021-10-09 14:33:12 +08:00
|
|
|
* Bootstrap (v5.1.3): 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'
|
|
|
|
}
|
2021-04-13 11:25:58 +08:00
|
|
|
const customEventsRegex = /^(mouseenter|mouseleave)/i
|
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
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
function getUidEvent(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
|
|
|
}
|
|
|
|
|
|
|
|
function getEvent(element) {
|
|
|
|
const uid = getUidEvent(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) {
|
2020-06-18 15:02:44 +08:00
|
|
|
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
|
|
|
|
2021-11-09 21:44:14 +08:00
|
|
|
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
|
|
|
// To please ESLint
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
function findHandler(events, handler, delegationSelector = null) {
|
2021-10-10 02:49:49 +08:00
|
|
|
for (const uidEvent of Object.keys(events)) {
|
2021-08-10 23:07:39 +08:00
|
|
|
const event = events[uidEvent]
|
2019-02-15 04:19:10 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
if (event.originalHandler === handler && event.delegationSelector === delegationSelector) {
|
2019-06-03 19:08:17 +08:00
|
|
|
return event
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
}
|
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
return null
|
|
|
|
}
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
function normalizeParameters(originalTypeEvent, handler, delegationFunction) {
|
2019-02-15 04:19:10 +08:00
|
|
|
const delegation = typeof handler === 'string'
|
2021-10-29 15:38:35 +08:00
|
|
|
const originalHandler = delegation ? delegationFunction : handler
|
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
|
|
|
}
|
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
return [delegation, originalHandler, typeEvent]
|
|
|
|
}
|
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
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
if (!handler) {
|
2021-10-29 15:38:35 +08:00
|
|
|
handler = delegationFunction
|
|
|
|
delegationFunction = null
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
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
|
|
|
|
if (customEventsRegex.test(originalTypeEvent)) {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
if (delegationFunction) {
|
|
|
|
delegationFunction = wrapFunction(delegationFunction)
|
2021-04-13 11:25:58 +08:00
|
|
|
} else {
|
2021-10-29 15:38:35 +08:00
|
|
|
handler = wrapFunction(handler)
|
2021-04-13 11:25:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
const [delegation, originalHandler, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction)
|
2019-02-26 19:20:34 +08:00
|
|
|
const events = getEvent(element)
|
|
|
|
const handlers = events[typeEvent] || (events[typeEvent] = {})
|
2021-10-29 15:38:35 +08:00
|
|
|
const previousFunction = findHandler(handlers, originalHandler, delegation ? 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
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
const uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, ''))
|
2019-03-13 22:23:50 +08:00
|
|
|
const fn = delegation ?
|
2021-10-29 15:38:35 +08:00
|
|
|
bootstrapDelegationHandler(element, handler, delegationFunction) :
|
2019-03-13 22:23:50 +08:00
|
|
|
bootstrapHandler(element, handler)
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
fn.delegationSelector = delegation ? handler : null
|
|
|
|
fn.originalHandler = originalHandler
|
|
|
|
fn.oneOff = oneOff
|
|
|
|
fn.uidEvent = uid
|
|
|
|
handlers[uid] = fn
|
2017-09-22 00:04:47 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
element.addEventListener(typeEvent, fn, delegation)
|
|
|
|
}
|
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]
|
|
|
|
removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector)
|
|
|
|
}
|
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
|
|
|
|
2021-10-29 15:38:35 +08:00
|
|
|
const [delegation, originalHandler, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction)
|
2018-09-14 20:27:30 +08:00
|
|
|
const inNamespace = typeEvent !== originalTypeEvent
|
|
|
|
const events = getEvent(element)
|
2020-05-02 21:31:51 +08:00
|
|
|
const isNamespace = originalTypeEvent.startsWith('.')
|
2017-08-21 23:43:04 +08:00
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
if (typeof originalHandler !== 'undefined') {
|
|
|
|
// Simplest case: handler is passed, remove that listener ONLY.
|
|
|
|
if (!events || !events[typeEvent]) {
|
2017-09-15 22:07:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
removeHandler(element, events, typeEvent, originalHandler, delegation ? handler : null)
|
|
|
|
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
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
const storeElementEvent = events[typeEvent] || {}
|
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]
|
|
|
|
removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector)
|
|
|
|
}
|
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
|
|
|
|
2019-02-15 04:19:10 +08:00
|
|
|
let jQueryEvent
|
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
|
|
|
|
2021-12-16 19:24:16 +08:00
|
|
|
const evt = new Event(event, { bubbles, cancelable: true })
|
2017-09-15 22:07:24 +08:00
|
|
|
|
2020-06-10 23:40:52 +08:00
|
|
|
// merge custom information in our event
|
2018-09-14 20:27:30 +08:00
|
|
|
if (typeof args !== 'undefined') {
|
2021-07-30 14:28:51 +08:00
|
|
|
for (const key of Object.keys(args)) {
|
2020-06-10 23:40:52 +08:00
|
|
|
Object.defineProperty(evt, key, {
|
|
|
|
get() {
|
|
|
|
return args[key]
|
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
})
|
2021-07-30 14:28:51 +08:00
|
|
|
}
|
2018-09-14 20:27:30 +08:00
|
|
|
}
|
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
|
|
|
|
2018-09-14 20:27:30 +08:00
|
|
|
if (evt.defaultPrevented && typeof jQueryEvent !== 'undefined') {
|
|
|
|
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
|
|
|
|
2017-08-21 15:11:37 +08:00
|
|
|
export default EventHandler
|