source: trip-planner-front/node_modules/bootstrap/js/dist/dom/event-handler.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*!
2 * Bootstrap event-handler.js v5.1.3 (https://getbootstrap.com/)
3 * Copyright 2011-2021 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() :
8 typeof define === 'function' && define.amd ? define(factory) :
9 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.EventHandler = factory());
10})(this, (function () { 'use strict';
11
12 /**
13 * --------------------------------------------------------------------------
14 * Bootstrap (v5.1.3): util/index.js
15 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
16 * --------------------------------------------------------------------------
17 */
18
19 const getjQuery = () => {
20 const {
21 jQuery
22 } = window;
23
24 if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
25 return jQuery;
26 }
27
28 return null;
29 };
30
31 /**
32 * --------------------------------------------------------------------------
33 * Bootstrap (v5.1.3): dom/event-handler.js
34 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
35 * --------------------------------------------------------------------------
36 */
37 /**
38 * ------------------------------------------------------------------------
39 * Constants
40 * ------------------------------------------------------------------------
41 */
42
43 const namespaceRegex = /[^.]*(?=\..*)\.|.*/;
44 const stripNameRegex = /\..*/;
45 const stripUidRegex = /::\d+$/;
46 const eventRegistry = {}; // Events storage
47
48 let uidEvent = 1;
49 const customEvents = {
50 mouseenter: 'mouseover',
51 mouseleave: 'mouseout'
52 };
53 const customEventsRegex = /^(mouseenter|mouseleave)/i;
54 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']);
55 /**
56 * ------------------------------------------------------------------------
57 * Private methods
58 * ------------------------------------------------------------------------
59 */
60
61 function getUidEvent(element, uid) {
62 return uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++;
63 }
64
65 function getEvent(element) {
66 const uid = getUidEvent(element);
67 element.uidEvent = uid;
68 eventRegistry[uid] = eventRegistry[uid] || {};
69 return eventRegistry[uid];
70 }
71
72 function bootstrapHandler(element, fn) {
73 return function handler(event) {
74 event.delegateTarget = element;
75
76 if (handler.oneOff) {
77 EventHandler.off(element, event.type, fn);
78 }
79
80 return fn.apply(element, [event]);
81 };
82 }
83
84 function bootstrapDelegationHandler(element, selector, fn) {
85 return function handler(event) {
86 const domElements = element.querySelectorAll(selector);
87
88 for (let {
89 target
90 } = event; target && target !== this; target = target.parentNode) {
91 for (let i = domElements.length; i--;) {
92 if (domElements[i] === target) {
93 event.delegateTarget = target;
94
95 if (handler.oneOff) {
96 EventHandler.off(element, event.type, selector, fn);
97 }
98
99 return fn.apply(target, [event]);
100 }
101 }
102 } // To please ESLint
103
104
105 return null;
106 };
107 }
108
109 function findHandler(events, handler, delegationSelector = null) {
110 const uidEventList = Object.keys(events);
111
112 for (let i = 0, len = uidEventList.length; i < len; i++) {
113 const event = events[uidEventList[i]];
114
115 if (event.originalHandler === handler && event.delegationSelector === delegationSelector) {
116 return event;
117 }
118 }
119
120 return null;
121 }
122
123 function normalizeParams(originalTypeEvent, handler, delegationFn) {
124 const delegation = typeof handler === 'string';
125 const originalHandler = delegation ? delegationFn : handler;
126 let typeEvent = getTypeEvent(originalTypeEvent);
127 const isNative = nativeEvents.has(typeEvent);
128
129 if (!isNative) {
130 typeEvent = originalTypeEvent;
131 }
132
133 return [delegation, originalHandler, typeEvent];
134 }
135
136 function addHandler(element, originalTypeEvent, handler, delegationFn, oneOff) {
137 if (typeof originalTypeEvent !== 'string' || !element) {
138 return;
139 }
140
141 if (!handler) {
142 handler = delegationFn;
143 delegationFn = null;
144 } // in case of mouseenter or mouseleave wrap the handler within a function that checks for its DOM position
145 // this prevents the handler from being dispatched the same way as mouseover or mouseout does
146
147
148 if (customEventsRegex.test(originalTypeEvent)) {
149 const wrapFn = fn => {
150 return function (event) {
151 if (!event.relatedTarget || event.relatedTarget !== event.delegateTarget && !event.delegateTarget.contains(event.relatedTarget)) {
152 return fn.call(this, event);
153 }
154 };
155 };
156
157 if (delegationFn) {
158 delegationFn = wrapFn(delegationFn);
159 } else {
160 handler = wrapFn(handler);
161 }
162 }
163
164 const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn);
165 const events = getEvent(element);
166 const handlers = events[typeEvent] || (events[typeEvent] = {});
167 const previousFn = findHandler(handlers, originalHandler, delegation ? handler : null);
168
169 if (previousFn) {
170 previousFn.oneOff = previousFn.oneOff && oneOff;
171 return;
172 }
173
174 const uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, ''));
175 const fn = delegation ? bootstrapDelegationHandler(element, handler, delegationFn) : bootstrapHandler(element, handler);
176 fn.delegationSelector = delegation ? handler : null;
177 fn.originalHandler = originalHandler;
178 fn.oneOff = oneOff;
179 fn.uidEvent = uid;
180 handlers[uid] = fn;
181 element.addEventListener(typeEvent, fn, delegation);
182 }
183
184 function removeHandler(element, events, typeEvent, handler, delegationSelector) {
185 const fn = findHandler(events[typeEvent], handler, delegationSelector);
186
187 if (!fn) {
188 return;
189 }
190
191 element.removeEventListener(typeEvent, fn, Boolean(delegationSelector));
192 delete events[typeEvent][fn.uidEvent];
193 }
194
195 function removeNamespacedHandlers(element, events, typeEvent, namespace) {
196 const storeElementEvent = events[typeEvent] || {};
197 Object.keys(storeElementEvent).forEach(handlerKey => {
198 if (handlerKey.includes(namespace)) {
199 const event = storeElementEvent[handlerKey];
200 removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector);
201 }
202 });
203 }
204
205 function getTypeEvent(event) {
206 // allow to get the native events from namespaced events ('click.bs.button' --> 'click')
207 event = event.replace(stripNameRegex, '');
208 return customEvents[event] || event;
209 }
210
211 const EventHandler = {
212 on(element, event, handler, delegationFn) {
213 addHandler(element, event, handler, delegationFn, false);
214 },
215
216 one(element, event, handler, delegationFn) {
217 addHandler(element, event, handler, delegationFn, true);
218 },
219
220 off(element, originalTypeEvent, handler, delegationFn) {
221 if (typeof originalTypeEvent !== 'string' || !element) {
222 return;
223 }
224
225 const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn);
226 const inNamespace = typeEvent !== originalTypeEvent;
227 const events = getEvent(element);
228 const isNamespace = originalTypeEvent.startsWith('.');
229
230 if (typeof originalHandler !== 'undefined') {
231 // Simplest case: handler is passed, remove that listener ONLY.
232 if (!events || !events[typeEvent]) {
233 return;
234 }
235
236 removeHandler(element, events, typeEvent, originalHandler, delegation ? handler : null);
237 return;
238 }
239
240 if (isNamespace) {
241 Object.keys(events).forEach(elementEvent => {
242 removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.slice(1));
243 });
244 }
245
246 const storeElementEvent = events[typeEvent] || {};
247 Object.keys(storeElementEvent).forEach(keyHandlers => {
248 const handlerKey = keyHandlers.replace(stripUidRegex, '');
249
250 if (!inNamespace || originalTypeEvent.includes(handlerKey)) {
251 const event = storeElementEvent[keyHandlers];
252 removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector);
253 }
254 });
255 },
256
257 trigger(element, event, args) {
258 if (typeof event !== 'string' || !element) {
259 return null;
260 }
261
262 const $ = getjQuery();
263 const typeEvent = getTypeEvent(event);
264 const inNamespace = event !== typeEvent;
265 const isNative = nativeEvents.has(typeEvent);
266 let jQueryEvent;
267 let bubbles = true;
268 let nativeDispatch = true;
269 let defaultPrevented = false;
270 let evt = null;
271
272 if (inNamespace && $) {
273 jQueryEvent = $.Event(event, args);
274 $(element).trigger(jQueryEvent);
275 bubbles = !jQueryEvent.isPropagationStopped();
276 nativeDispatch = !jQueryEvent.isImmediatePropagationStopped();
277 defaultPrevented = jQueryEvent.isDefaultPrevented();
278 }
279
280 if (isNative) {
281 evt = document.createEvent('HTMLEvents');
282 evt.initEvent(typeEvent, bubbles, true);
283 } else {
284 evt = new CustomEvent(event, {
285 bubbles,
286 cancelable: true
287 });
288 } // merge custom information in our event
289
290
291 if (typeof args !== 'undefined') {
292 Object.keys(args).forEach(key => {
293 Object.defineProperty(evt, key, {
294 get() {
295 return args[key];
296 }
297
298 });
299 });
300 }
301
302 if (defaultPrevented) {
303 evt.preventDefault();
304 }
305
306 if (nativeDispatch) {
307 element.dispatchEvent(evt);
308 }
309
310 if (evt.defaultPrevented && typeof jQueryEvent !== 'undefined') {
311 jQueryEvent.preventDefault();
312 }
313
314 return evt;
315 }
316
317 };
318
319 return EventHandler;
320
321}));
322//# sourceMappingURL=event-handler.js.map
Note: See TracBrowser for help on using the repository browser.