Skip to content

Commit

Permalink
EventHandler: simplify checks and make use of one variable
Browse files Browse the repository at this point in the history
Move check of falsie delegated-selector, caused by tooltip.js
  • Loading branch information
GeoSot committed Apr 14, 2022
1 parent 1a599ee commit 48c952b
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions js/src/dom/event-handler.js
Expand Up @@ -128,7 +128,9 @@ function findHandler(events, callable, delegationSelector = null) {

function normalizeParameters(originalTypeEvent, handler, delegationFunction) {
const isDelegated = typeof handler === 'string'
const originalHandler = isDelegated ? delegationFunction : handler
const originalHandler = isDelegated ?
delegationFunction :
(handler || delegationFunction) // todo: tooltip passes `false` instead of selector, so we need to check
let typeEvent = getTypeEvent(originalTypeEvent)

if (!nativeEvents.has(typeEvent)) {
Expand All @@ -143,10 +145,7 @@ function addHandler(element, originalTypeEvent, handler, delegationFunction, one
return
}

if (!handler) {
handler = delegationFunction
delegationFunction = null
}
let [isDelegated, callable, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction)

// 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
Expand All @@ -159,31 +158,26 @@ function addHandler(element, originalTypeEvent, handler, delegationFunction, one
}
}

if (delegationFunction) {
delegationFunction = wrapFunction(delegationFunction)
} else {
handler = wrapFunction(handler)
}
callable = wrapFunction(callable)
}

const [isDelegated, originalHandler, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction)
const events = getElementEvents(element)
const handlers = events[typeEvent] || (events[typeEvent] = {})
const previousFunction = findHandler(handlers, originalHandler, isDelegated ? handler : null)
const previousFunction = findHandler(handlers, callable, isDelegated ? handler : null)

if (previousFunction) {
previousFunction.oneOff = previousFunction.oneOff && oneOff

return
}

const uid = makeEventUid(originalHandler, originalTypeEvent.replace(namespaceRegex, ''))
const uid = makeEventUid(callable, originalTypeEvent.replace(namespaceRegex, ''))
const fn = isDelegated ?
bootstrapDelegationHandler(element, handler, delegationFunction) :
bootstrapHandler(element, handler)
bootstrapDelegationHandler(element, handler, callable) :
bootstrapHandler(element, callable)

fn.delegationSelector = isDelegated ? handler : null
fn.originalHandler = originalHandler
fn.originalHandler = callable
fn.oneOff = oneOff
fn.uidEvent = uid
handlers[uid] = fn
Expand Down Expand Up @@ -291,7 +285,6 @@ const EventHandler = {
let evt = new Event(event, { bubbles, cancelable: true })
evt = hydrateObj(evt, args)


if (defaultPrevented) {
evt.preventDefault()
}
Expand All @@ -316,6 +309,7 @@ function hydrateObj(obj, meta) {
}
})
}

return obj
}

Expand Down

0 comments on commit 48c952b

Please sign in to comment.