1 | /*!
|
---|
2 | * Bootstrap event-handler.js v5.3.3 (https://getbootstrap.com/)
|
---|
3 | * Copyright 2011-2024 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
|
---|
4 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
5 | */
|
---|
6 | (function (global, factory) {
|
---|
7 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('../util/index.js')) :
|
---|
8 | typeof define === 'function' && define.amd ? define(['../util/index'], factory) :
|
---|
9 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.EventHandler = factory(global.Index));
|
---|
10 | })(this, (function (index_js) { 'use strict';
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * --------------------------------------------------------------------------
|
---|
14 | * Bootstrap dom/event-handler.js
|
---|
15 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
16 | * --------------------------------------------------------------------------
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constants
|
---|
22 | */
|
---|
23 |
|
---|
24 | const namespaceRegex = /[^.]*(?=\..*)\.|.*/;
|
---|
25 | const stripNameRegex = /\..*/;
|
---|
26 | const stripUidRegex = /::\d+$/;
|
---|
27 | const eventRegistry = {}; // Events storage
|
---|
28 | let uidEvent = 1;
|
---|
29 | const customEvents = {
|
---|
30 | mouseenter: 'mouseover',
|
---|
31 | mouseleave: 'mouseout'
|
---|
32 | };
|
---|
33 | const nativeEvents = new Set(['click', 'dblclick', 'mouseup', 'mousedown', 'contextmenu', 'mousewheel', 'DOMMouseScroll', 'mouseover', 'mouseout', 'mousemove', 'selectstart', 'selectend', 'keydown', 'keypress', 'keyup', 'orientationchange', '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']);
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Private methods
|
---|
37 | */
|
---|
38 |
|
---|
39 | function makeEventUid(element, uid) {
|
---|
40 | return uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++;
|
---|
41 | }
|
---|
42 | function getElementEvents(element) {
|
---|
43 | const uid = makeEventUid(element);
|
---|
44 | element.uidEvent = uid;
|
---|
45 | eventRegistry[uid] = eventRegistry[uid] || {};
|
---|
46 | return eventRegistry[uid];
|
---|
47 | }
|
---|
48 | function bootstrapHandler(element, fn) {
|
---|
49 | return function handler(event) {
|
---|
50 | hydrateObj(event, {
|
---|
51 | delegateTarget: element
|
---|
52 | });
|
---|
53 | if (handler.oneOff) {
|
---|
54 | EventHandler.off(element, event.type, fn);
|
---|
55 | }
|
---|
56 | return fn.apply(element, [event]);
|
---|
57 | };
|
---|
58 | }
|
---|
59 | function bootstrapDelegationHandler(element, selector, fn) {
|
---|
60 | return function handler(event) {
|
---|
61 | const domElements = element.querySelectorAll(selector);
|
---|
62 | for (let {
|
---|
63 | target
|
---|
64 | } = event; target && target !== this; target = target.parentNode) {
|
---|
65 | for (const domElement of domElements) {
|
---|
66 | if (domElement !== target) {
|
---|
67 | continue;
|
---|
68 | }
|
---|
69 | hydrateObj(event, {
|
---|
70 | delegateTarget: target
|
---|
71 | });
|
---|
72 | if (handler.oneOff) {
|
---|
73 | EventHandler.off(element, event.type, selector, fn);
|
---|
74 | }
|
---|
75 | return fn.apply(target, [event]);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | };
|
---|
79 | }
|
---|
80 | function findHandler(events, callable, delegationSelector = null) {
|
---|
81 | return Object.values(events).find(event => event.callable === callable && event.delegationSelector === delegationSelector);
|
---|
82 | }
|
---|
83 | function normalizeParameters(originalTypeEvent, handler, delegationFunction) {
|
---|
84 | const isDelegated = typeof handler === 'string';
|
---|
85 | // TODO: tooltip passes `false` instead of selector, so we need to check
|
---|
86 | const callable = isDelegated ? delegationFunction : handler || delegationFunction;
|
---|
87 | let typeEvent = getTypeEvent(originalTypeEvent);
|
---|
88 | if (!nativeEvents.has(typeEvent)) {
|
---|
89 | typeEvent = originalTypeEvent;
|
---|
90 | }
|
---|
91 | return [isDelegated, callable, typeEvent];
|
---|
92 | }
|
---|
93 | function addHandler(element, originalTypeEvent, handler, delegationFunction, oneOff) {
|
---|
94 | if (typeof originalTypeEvent !== 'string' || !element) {
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | let [isDelegated, callable, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction);
|
---|
98 |
|
---|
99 | // in case of mouseenter or mouseleave wrap the handler within a function that checks for its DOM position
|
---|
100 | // this prevents the handler from being dispatched the same way as mouseover or mouseout does
|
---|
101 | if (originalTypeEvent in customEvents) {
|
---|
102 | const wrapFunction = fn => {
|
---|
103 | return function (event) {
|
---|
104 | if (!event.relatedTarget || event.relatedTarget !== event.delegateTarget && !event.delegateTarget.contains(event.relatedTarget)) {
|
---|
105 | return fn.call(this, event);
|
---|
106 | }
|
---|
107 | };
|
---|
108 | };
|
---|
109 | callable = wrapFunction(callable);
|
---|
110 | }
|
---|
111 | const events = getElementEvents(element);
|
---|
112 | const handlers = events[typeEvent] || (events[typeEvent] = {});
|
---|
113 | const previousFunction = findHandler(handlers, callable, isDelegated ? handler : null);
|
---|
114 | if (previousFunction) {
|
---|
115 | previousFunction.oneOff = previousFunction.oneOff && oneOff;
|
---|
116 | return;
|
---|
117 | }
|
---|
118 | const uid = makeEventUid(callable, originalTypeEvent.replace(namespaceRegex, ''));
|
---|
119 | const fn = isDelegated ? bootstrapDelegationHandler(element, handler, callable) : bootstrapHandler(element, callable);
|
---|
120 | fn.delegationSelector = isDelegated ? handler : null;
|
---|
121 | fn.callable = callable;
|
---|
122 | fn.oneOff = oneOff;
|
---|
123 | fn.uidEvent = uid;
|
---|
124 | handlers[uid] = fn;
|
---|
125 | element.addEventListener(typeEvent, fn, isDelegated);
|
---|
126 | }
|
---|
127 | function removeHandler(element, events, typeEvent, handler, delegationSelector) {
|
---|
128 | const fn = findHandler(events[typeEvent], handler, delegationSelector);
|
---|
129 | if (!fn) {
|
---|
130 | return;
|
---|
131 | }
|
---|
132 | element.removeEventListener(typeEvent, fn, Boolean(delegationSelector));
|
---|
133 | delete events[typeEvent][fn.uidEvent];
|
---|
134 | }
|
---|
135 | function removeNamespacedHandlers(element, events, typeEvent, namespace) {
|
---|
136 | const storeElementEvent = events[typeEvent] || {};
|
---|
137 | for (const [handlerKey, event] of Object.entries(storeElementEvent)) {
|
---|
138 | if (handlerKey.includes(namespace)) {
|
---|
139 | removeHandler(element, events, typeEvent, event.callable, event.delegationSelector);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | function getTypeEvent(event) {
|
---|
144 | // allow to get the native events from namespaced events ('click.bs.button' --> 'click')
|
---|
145 | event = event.replace(stripNameRegex, '');
|
---|
146 | return customEvents[event] || event;
|
---|
147 | }
|
---|
148 | const EventHandler = {
|
---|
149 | on(element, event, handler, delegationFunction) {
|
---|
150 | addHandler(element, event, handler, delegationFunction, false);
|
---|
151 | },
|
---|
152 | one(element, event, handler, delegationFunction) {
|
---|
153 | addHandler(element, event, handler, delegationFunction, true);
|
---|
154 | },
|
---|
155 | off(element, originalTypeEvent, handler, delegationFunction) {
|
---|
156 | if (typeof originalTypeEvent !== 'string' || !element) {
|
---|
157 | return;
|
---|
158 | }
|
---|
159 | const [isDelegated, callable, typeEvent] = normalizeParameters(originalTypeEvent, handler, delegationFunction);
|
---|
160 | const inNamespace = typeEvent !== originalTypeEvent;
|
---|
161 | const events = getElementEvents(element);
|
---|
162 | const storeElementEvent = events[typeEvent] || {};
|
---|
163 | const isNamespace = originalTypeEvent.startsWith('.');
|
---|
164 | if (typeof callable !== 'undefined') {
|
---|
165 | // Simplest case: handler is passed, remove that listener ONLY.
|
---|
166 | if (!Object.keys(storeElementEvent).length) {
|
---|
167 | return;
|
---|
168 | }
|
---|
169 | removeHandler(element, events, typeEvent, callable, isDelegated ? handler : null);
|
---|
170 | return;
|
---|
171 | }
|
---|
172 | if (isNamespace) {
|
---|
173 | for (const elementEvent of Object.keys(events)) {
|
---|
174 | removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.slice(1));
|
---|
175 | }
|
---|
176 | }
|
---|
177 | for (const [keyHandlers, event] of Object.entries(storeElementEvent)) {
|
---|
178 | const handlerKey = keyHandlers.replace(stripUidRegex, '');
|
---|
179 | if (!inNamespace || originalTypeEvent.includes(handlerKey)) {
|
---|
180 | removeHandler(element, events, typeEvent, event.callable, event.delegationSelector);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | },
|
---|
184 | trigger(element, event, args) {
|
---|
185 | if (typeof event !== 'string' || !element) {
|
---|
186 | return null;
|
---|
187 | }
|
---|
188 | const $ = index_js.getjQuery();
|
---|
189 | const typeEvent = getTypeEvent(event);
|
---|
190 | const inNamespace = event !== typeEvent;
|
---|
191 | let jQueryEvent = null;
|
---|
192 | let bubbles = true;
|
---|
193 | let nativeDispatch = true;
|
---|
194 | let defaultPrevented = false;
|
---|
195 | if (inNamespace && $) {
|
---|
196 | jQueryEvent = $.Event(event, args);
|
---|
197 | $(element).trigger(jQueryEvent);
|
---|
198 | bubbles = !jQueryEvent.isPropagationStopped();
|
---|
199 | nativeDispatch = !jQueryEvent.isImmediatePropagationStopped();
|
---|
200 | defaultPrevented = jQueryEvent.isDefaultPrevented();
|
---|
201 | }
|
---|
202 | const evt = hydrateObj(new Event(event, {
|
---|
203 | bubbles,
|
---|
204 | cancelable: true
|
---|
205 | }), args);
|
---|
206 | if (defaultPrevented) {
|
---|
207 | evt.preventDefault();
|
---|
208 | }
|
---|
209 | if (nativeDispatch) {
|
---|
210 | element.dispatchEvent(evt);
|
---|
211 | }
|
---|
212 | if (evt.defaultPrevented && jQueryEvent) {
|
---|
213 | jQueryEvent.preventDefault();
|
---|
214 | }
|
---|
215 | return evt;
|
---|
216 | }
|
---|
217 | };
|
---|
218 | function hydrateObj(obj, meta = {}) {
|
---|
219 | for (const [key, value] of Object.entries(meta)) {
|
---|
220 | try {
|
---|
221 | obj[key] = value;
|
---|
222 | } catch (_unused) {
|
---|
223 | Object.defineProperty(obj, key, {
|
---|
224 | configurable: true,
|
---|
225 | get() {
|
---|
226 | return value;
|
---|
227 | }
|
---|
228 | });
|
---|
229 | }
|
---|
230 | }
|
---|
231 | return obj;
|
---|
232 | }
|
---|
233 |
|
---|
234 | return EventHandler;
|
---|
235 |
|
---|
236 | }));
|
---|
237 | //# sourceMappingURL=event-handler.js.map
|
---|