0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-30 22:52:24 +01:00

Add event delegation + fix EventHandler.one

This commit is contained in:
Johann-S 2017-08-23 12:01:38 +02:00 committed by XhmikosR
parent 00ca781171
commit a3398fffd6
2 changed files with 19 additions and 13 deletions

View File

@ -153,12 +153,7 @@ class Alert {
* Data Api implementation * Data Api implementation
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
*/ */
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()))
$(document).on(
Event.CLICK_DATA_API,
Selector.DISMISS,
Alert._handleDismiss(new Alert())
)
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------

View File

@ -91,13 +91,27 @@ function bootstrapHandler(element, fn) {
} }
} }
function bootstrapDelegationHandler(selector, fn) {
return function (event) {
const domElements = document.querySelectorAll(selector)
for (let target = event.target; target && target !== this; target = target.parentNode) {
for (let i = domElements.length; i--;) {
if (domElements[i] === target) {
return fn.apply(target, [event])
}
}
}
}
}
const EventHandler = { const EventHandler = {
on(element, originalTypeEvent, handler) { on(element, originalTypeEvent, handler, delegationFn) {
if (typeof originalTypeEvent !== 'string' || if (typeof originalTypeEvent !== 'string' ||
(typeof element === 'undefined' || element === null)) { (typeof element === 'undefined' || element === null)) {
return return
} }
const delegation = typeof handler === 'string'
// allow to get the native events from namespaced events ('click.bs.button' --> 'click') // allow to get the native events from namespaced events ('click.bs.button' --> 'click')
let typeEvent = originalTypeEvent.replace(stripNameRegex, '') let typeEvent = originalTypeEvent.replace(stripNameRegex, '')
const isNative = nativeEvents.indexOf(typeEvent) > -1 const isNative = nativeEvents.indexOf(typeEvent) > -1
@ -107,12 +121,11 @@ const EventHandler = {
const events = getEvent(element) const events = getEvent(element)
const handlers = events[typeEvent] || (events[typeEvent] = {}) const handlers = events[typeEvent] || (events[typeEvent] = {})
const uid = getUidEvent(handler, originalTypeEvent.replace(namespaceRegex, '')) const uid = getUidEvent(handler, originalTypeEvent.replace(namespaceRegex, ''))
// TODO : Handle multi events on one element
if (handlers[uid]) { if (handlers[uid]) {
return return
} }
const fn = bootstrapHandler(element, handler) const fn = !delegation ? bootstrapHandler(element, handler) : bootstrapDelegationHandler(handler, delegationFn)
handlers[uid] = fn handlers[uid] = fn
handler.uidEvent = uid handler.uidEvent = uid
element.addEventListener(typeEvent, fn, false) element.addEventListener(typeEvent, fn, false)
@ -125,10 +138,8 @@ const EventHandler = {
if (!events || !events[typeEvent]) { if (!events || !events[typeEvent]) {
return return
} }
const uidEvent = handler.uidEvent handler.apply(element, [e])
const fn = events[typeEvent][uidEvent] EventHandler.off(element, event, complete)
fn.apply(element, [e])
EventHandler.off(element, event, handler)
} }
EventHandler.on(element, event, complete) EventHandler.on(element, event, complete)
}, },