diff options
author | XhmikosR <xhmikosr@gmail.com> | 2022-07-19 18:43:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-19 18:43:58 +0300 |
commit | edf9c40956d19e6ab3f9151bfe0dfac6be06fa21 (patch) | |
tree | aa08e0842ee3260dda2a5eddc52d346bdc4c0508 /js/dist/dom/event-handler.js | |
parent | f451b4161e4e499f5d1b743802302e6b19a5dc4c (diff) |
Release v5.2.0 (#36768)v5.2.0
* Bump version to 5.2.0
* Dist
* Update masthead.html
Diffstat (limited to 'js/dist/dom/event-handler.js')
-rw-r--r-- | js/dist/dom/event-handler.js | 108 |
1 files changed, 56 insertions, 52 deletions
diff --git a/js/dist/dom/event-handler.js b/js/dist/dom/event-handler.js index 99b3b016a1..6c9d8e7bc3 100644 --- a/js/dist/dom/event-handler.js +++ b/js/dist/dom/event-handler.js @@ -1,5 +1,5 @@ /*! - * Bootstrap event-handler.js v5.2.0-beta1 (https://getbootstrap.com/) + * Bootstrap event-handler.js v5.2.0 (https://getbootstrap.com/) * Copyright 2011-2022 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors) * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) */ @@ -11,7 +11,7 @@ /** * -------------------------------------------------------------------------- - * Bootstrap (v5.2.0-beta1): dom/event-handler.js + * Bootstrap (v5.2.0): dom/event-handler.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ @@ -34,12 +34,12 @@ * Private methods */ - function getUidEvent(element, uid) { + function makeEventUid(element, uid) { return uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++; } - function getEvent(element) { - const uid = getUidEvent(element); + function getElementEvents(element) { + const uid = makeEventUid(element); element.uidEvent = uid; eventRegistry[uid] = eventRegistry[uid] || {}; return eventRegistry[uid]; @@ -47,7 +47,9 @@ function bootstrapHandler(element, fn) { return function handler(event) { - event.delegateTarget = element; + hydrateObj(event, { + delegateTarget: element + }); if (handler.oneOff) { EventHandler.off(element, event.type, fn); @@ -69,7 +71,9 @@ continue; } - event.delegateTarget = target; + hydrateObj(event, { + delegateTarget: target + }); if (handler.oneOff) { EventHandler.off(element, event.type, selector, fn); @@ -81,20 +85,21 @@ }; } - function findHandler(events, handler, delegationSelector = null) { - return Object.values(events).find(event => event.originalHandler === handler && event.delegationSelector === delegationSelector); + function findHandler(events, callable, delegationSelector = null) { + return Object.values(events).find(event => event.callable === callable && event.delegationSelector === delegationSelector); } function normalizeParameters(originalTypeEvent, handler, delegationFunction) { - const delegation = typeof handler === 'string'; - const originalHandler = delegation ? delegationFunction : handler; + const isDelegated = typeof handler === 'string'; // todo: tooltip passes `false` instead of selector, so we need to check + + const callable = isDelegated ? delegationFunction : handler || delegationFunction; let typeEvent = getTypeEvent(originalTypeEvent); if (!nativeEvents.has(typeEvent)) { typeEvent = originalTypeEvent; } - return [delegation, originalHandler, typeEvent]; + return [isDelegated, callable, typeEvent]; } function addHandler(element, originalTypeEvent, handler, delegationFunction, oneOff) { @@ -102,13 +107,9 @@ return; } - if (!handler) { - handler = delegationFunction; - delegationFunction = null; - } // in case of mouseenter or mouseleave wrap the handler within a function that checks for its DOM position + 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 - if (originalTypeEvent in customEvents) { const wrapFunction = fn => { return function (event) { @@ -118,31 +119,26 @@ }; }; - if (delegationFunction) { - delegationFunction = wrapFunction(delegationFunction); - } else { - handler = wrapFunction(handler); - } + callable = wrapFunction(callable); } - const [delegation, originalHandler, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction); - const events = getEvent(element); + const events = getElementEvents(element); const handlers = events[typeEvent] || (events[typeEvent] = {}); - const previousFunction = findHandler(handlers, originalHandler, delegation ? handler : null); + const previousFunction = findHandler(handlers, callable, isDelegated ? handler : null); if (previousFunction) { previousFunction.oneOff = previousFunction.oneOff && oneOff; return; } - const uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, '')); - const fn = delegation ? bootstrapDelegationHandler(element, handler, delegationFunction) : bootstrapHandler(element, handler); - fn.delegationSelector = delegation ? handler : null; - fn.originalHandler = originalHandler; + const uid = makeEventUid(callable, originalTypeEvent.replace(namespaceRegex, '')); + const fn = isDelegated ? bootstrapDelegationHandler(element, handler, callable) : bootstrapHandler(element, callable); + fn.delegationSelector = isDelegated ? handler : null; + fn.callable = callable; fn.oneOff = oneOff; fn.uidEvent = uid; handlers[uid] = fn; - element.addEventListener(typeEvent, fn, delegation); + element.addEventListener(typeEvent, fn, isDelegated); } function removeHandler(element, events, typeEvent, handler, delegationSelector) { @@ -162,7 +158,7 @@ for (const handlerKey of Object.keys(storeElementEvent)) { if (handlerKey.includes(namespace)) { const event = storeElementEvent[handlerKey]; - removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector); + removeHandler(element, events, typeEvent, event.callable, event.delegationSelector); } } } @@ -187,18 +183,19 @@ return; } - const [delegation, originalHandler, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction); + const [isDelegated, callable, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction); const inNamespace = typeEvent !== originalTypeEvent; - const events = getEvent(element); + const events = getElementEvents(element); + const storeElementEvent = events[typeEvent] || {}; const isNamespace = originalTypeEvent.startsWith('.'); - if (typeof originalHandler !== 'undefined') { + if (typeof callable !== 'undefined') { // Simplest case: handler is passed, remove that listener ONLY. - if (!events || !events[typeEvent]) { + if (!Object.keys(storeElementEvent).length) { return; } - removeHandler(element, events, typeEvent, originalHandler, delegation ? handler : null); + removeHandler(element, events, typeEvent, callable, isDelegated ? handler : null); return; } @@ -208,14 +205,12 @@ } } - const storeElementEvent = events[typeEvent] || {}; - for (const keyHandlers of Object.keys(storeElementEvent)) { const handlerKey = keyHandlers.replace(stripUidRegex, ''); if (!inNamespace || originalTypeEvent.includes(handlerKey)) { const event = storeElementEvent[keyHandlers]; - removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector); + removeHandler(element, events, typeEvent, event.callable, event.delegationSelector); } } }, @@ -241,21 +236,11 @@ defaultPrevented = jQueryEvent.isDefaultPrevented(); } - const evt = new Event(event, { + let evt = new Event(event, { bubbles, cancelable: true - }); // merge custom information in our event - - if (typeof args !== 'undefined') { - for (const key of Object.keys(args)) { - Object.defineProperty(evt, key, { - get() { - return args[key]; - } - - }); - } - } + }); + evt = hydrateObj(evt, args); if (defaultPrevented) { evt.preventDefault(); @@ -274,6 +259,25 @@ }; + function hydrateObj(obj, meta) { + for (const [key, value] of Object.entries(meta || {})) { + try { + obj[key] = value; + } catch (_unused) { + Object.defineProperty(obj, key, { + configurable: true, + + get() { + return value; + } + + }); + } + } + + return obj; + } + return EventHandler; })); |