1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/dist/dom/event-handler.js

323 lines
10 KiB
JavaScript
Raw Normal View History

2019-03-01 11:31:34 -05:00
/*!
2021-10-09 02:33:12 -04:00
* Bootstrap event-handler.js v5.1.3 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 11:31:34 -05:00
*/
(function (global, factory) {
2020-11-11 12:07:37 -05:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.EventHandler = factory());
2021-10-05 11:50:18 -04:00
})(this, (function () { 'use strict';
2019-03-01 11:31:34 -05:00
2021-08-04 11:41:51 -04:00
/**
* --------------------------------------------------------------------------
2021-10-09 02:33:12 -04:00
* Bootstrap (v5.1.3): util/index.js
2021-08-04 11:41:51 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2021-03-23 12:26:54 -04:00
const getjQuery = () => {
const {
jQuery
} = window;
2019-08-27 09:03:21 -04:00
2020-11-23 08:17:16 -05:00
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
2019-08-27 09:03:21 -04:00
return jQuery;
}
return null;
};
2020-12-03 08:08:31 -05:00
2019-03-01 11:31:34 -05:00
/**
* --------------------------------------------------------------------------
2021-10-09 02:33:12 -04:00
* Bootstrap (v5.1.3): dom/event-handler.js
2020-06-16 14:50:01 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2019-03-01 11:31:34 -05:00
* --------------------------------------------------------------------------
*/
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2021-03-23 12:26:54 -04:00
const namespaceRegex = /[^.]*(?=\..*)\.|.*/;
const stripNameRegex = /\..*/;
const stripUidRegex = /::\d+$/;
const eventRegistry = {}; // Events storage
2019-03-01 11:31:34 -05:00
2021-03-23 12:26:54 -04:00
let uidEvent = 1;
const customEvents = {
2019-03-01 11:31:34 -05:00
mouseenter: 'mouseover',
mouseleave: 'mouseout'
};
const customEventsRegex = /^(mouseenter|mouseleave)/i;
2021-03-23 12:26:54 -04:00
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']);
2019-03-01 11:31:34 -05:00
/**
* ------------------------------------------------------------------------
* Private methods
* ------------------------------------------------------------------------
*/
function getUidEvent(element, uid) {
2021-03-23 12:26:54 -04:00
return uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++;
2019-03-01 11:31:34 -05:00
}
function getEvent(element) {
2021-03-23 12:26:54 -04:00
const uid = getUidEvent(element);
2019-03-01 11:31:34 -05:00
element.uidEvent = uid;
2019-03-11 11:13:30 -04:00
eventRegistry[uid] = eventRegistry[uid] || {};
return eventRegistry[uid];
2019-03-01 11:31:34 -05:00
}
function bootstrapHandler(element, fn) {
return function handler(event) {
2020-09-14 11:12:06 -04:00
event.delegateTarget = element;
2019-03-01 11:31:34 -05:00
if (handler.oneOff) {
EventHandler.off(element, event.type, fn);
}
return fn.apply(element, [event]);
};
}
function bootstrapDelegationHandler(element, selector, fn) {
return function handler(event) {
2021-03-23 12:26:54 -04:00
const domElements = element.querySelectorAll(selector);
2019-03-01 11:31:34 -05:00
2021-03-23 12:26:54 -04:00
for (let {
target
} = event; target && target !== this; target = target.parentNode) {
for (let i = domElements.length; i--;) {
2019-03-01 11:31:34 -05:00
if (domElements[i] === target) {
2020-09-14 11:12:06 -04:00
event.delegateTarget = target;
2019-03-01 11:31:34 -05:00
if (handler.oneOff) {
EventHandler.off(element, event.type, selector, fn);
2019-03-01 11:31:34 -05:00
}
return fn.apply(target, [event]);
}
}
} // To please ESLint
return null;
};
}
2021-03-23 12:26:54 -04:00
function findHandler(events, handler, delegationSelector = null) {
const uidEventList = Object.keys(events);
2019-07-12 17:56:26 -04:00
2021-03-23 12:26:54 -04:00
for (let i = 0, len = uidEventList.length; i < len; i++) {
const event = events[uidEventList[i]];
2019-03-01 11:31:34 -05:00
if (event.originalHandler === handler && event.delegationSelector === delegationSelector) {
2019-07-12 17:56:26 -04:00
return event;
2019-03-01 11:31:34 -05:00
}
}
return null;
}
function normalizeParams(originalTypeEvent, handler, delegationFn) {
2021-03-23 12:26:54 -04:00
const delegation = typeof handler === 'string';
const originalHandler = delegation ? delegationFn : handler;
let typeEvent = getTypeEvent(originalTypeEvent);
2021-03-23 12:26:54 -04:00
const isNative = nativeEvents.has(typeEvent);
2019-03-01 11:31:34 -05:00
if (!isNative) {
typeEvent = originalTypeEvent;
}
return [delegation, originalHandler, typeEvent];
}
function addHandler(element, originalTypeEvent, handler, delegationFn, oneOff) {
if (typeof originalTypeEvent !== 'string' || !element) {
return;
}
if (!handler) {
handler = delegationFn;
delegationFn = null;
} // 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 (customEventsRegex.test(originalTypeEvent)) {
const wrapFn = fn => {
return function (event) {
if (!event.relatedTarget || event.relatedTarget !== event.delegateTarget && !event.delegateTarget.contains(event.relatedTarget)) {
return fn.call(this, event);
}
};
};
if (delegationFn) {
delegationFn = wrapFn(delegationFn);
} else {
handler = wrapFn(handler);
}
2019-03-01 11:31:34 -05:00
}
2021-03-23 12:26:54 -04:00
const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn);
const events = getEvent(element);
const handlers = events[typeEvent] || (events[typeEvent] = {});
const previousFn = findHandler(handlers, originalHandler, delegation ? handler : null);
2019-03-01 11:31:34 -05:00
if (previousFn) {
previousFn.oneOff = previousFn.oneOff && oneOff;
return;
}
2021-03-23 12:26:54 -04:00
const uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, ''));
const fn = delegation ? bootstrapDelegationHandler(element, handler, delegationFn) : bootstrapHandler(element, handler);
2019-03-01 11:31:34 -05:00
fn.delegationSelector = delegation ? handler : null;
fn.originalHandler = originalHandler;
fn.oneOff = oneOff;
fn.uidEvent = uid;
handlers[uid] = fn;
element.addEventListener(typeEvent, fn, delegation);
}
function removeHandler(element, events, typeEvent, handler, delegationSelector) {
2021-03-23 12:26:54 -04:00
const fn = findHandler(events[typeEvent], handler, delegationSelector);
2019-03-01 11:31:34 -05:00
2019-07-12 17:56:26 -04:00
if (!fn) {
2019-03-01 11:31:34 -05:00
return;
}
element.removeEventListener(typeEvent, fn, Boolean(delegationSelector));
delete events[typeEvent][fn.uidEvent];
}
function removeNamespacedHandlers(element, events, typeEvent, namespace) {
2021-03-23 12:26:54 -04:00
const storeElementEvent = events[typeEvent] || {};
Object.keys(storeElementEvent).forEach(handlerKey => {
2020-11-23 08:17:16 -05:00
if (handlerKey.includes(namespace)) {
2021-03-23 12:26:54 -04:00
const event = storeElementEvent[handlerKey];
2019-03-01 11:31:34 -05:00
removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector);
}
});
}
function getTypeEvent(event) {
// allow to get the native events from namespaced events ('click.bs.button' --> 'click')
event = event.replace(stripNameRegex, '');
return customEvents[event] || event;
}
2021-03-23 12:26:54 -04:00
const EventHandler = {
on(element, event, handler, delegationFn) {
2019-03-01 11:31:34 -05:00
addHandler(element, event, handler, delegationFn, false);
},
2021-03-23 12:26:54 -04:00
one(element, event, handler, delegationFn) {
2019-03-01 11:31:34 -05:00
addHandler(element, event, handler, delegationFn, true);
},
2021-03-23 12:26:54 -04:00
off(element, originalTypeEvent, handler, delegationFn) {
2019-03-01 11:31:34 -05:00
if (typeof originalTypeEvent !== 'string' || !element) {
return;
}
2021-03-23 12:26:54 -04:00
const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn);
const inNamespace = typeEvent !== originalTypeEvent;
const events = getEvent(element);
const isNamespace = originalTypeEvent.startsWith('.');
2019-03-01 11:31:34 -05:00
if (typeof originalHandler !== 'undefined') {
// Simplest case: handler is passed, remove that listener ONLY.
if (!events || !events[typeEvent]) {
return;
}
removeHandler(element, events, typeEvent, originalHandler, delegation ? handler : null);
return;
}
if (isNamespace) {
2021-03-23 12:26:54 -04:00
Object.keys(events).forEach(elementEvent => {
2019-10-08 02:39:10 -04:00
removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.slice(1));
2019-03-01 11:31:34 -05:00
});
}
2021-03-23 12:26:54 -04:00
const storeElementEvent = events[typeEvent] || {};
Object.keys(storeElementEvent).forEach(keyHandlers => {
const handlerKey = keyHandlers.replace(stripUidRegex, '');
2019-03-01 11:31:34 -05:00
2020-11-23 08:17:16 -05:00
if (!inNamespace || originalTypeEvent.includes(handlerKey)) {
2021-03-23 12:26:54 -04:00
const event = storeElementEvent[keyHandlers];
2019-03-01 11:31:34 -05:00
removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector);
}
});
},
2021-03-23 12:26:54 -04:00
trigger(element, event, args) {
2019-03-01 11:31:34 -05:00
if (typeof event !== 'string' || !element) {
return null;
}
2021-03-23 12:26:54 -04:00
const $ = getjQuery();
const typeEvent = getTypeEvent(event);
2021-03-23 12:26:54 -04:00
const inNamespace = event !== typeEvent;
const isNative = nativeEvents.has(typeEvent);
let jQueryEvent;
let bubbles = true;
let nativeDispatch = true;
let defaultPrevented = false;
let evt = null;
2019-03-01 11:31:34 -05:00
2019-08-27 09:03:21 -04:00
if (inNamespace && $) {
jQueryEvent = $.Event(event, args);
$(element).trigger(jQueryEvent);
2019-03-01 11:31:34 -05:00
bubbles = !jQueryEvent.isPropagationStopped();
nativeDispatch = !jQueryEvent.isImmediatePropagationStopped();
defaultPrevented = jQueryEvent.isDefaultPrevented();
}
if (isNative) {
evt = document.createEvent('HTMLEvents');
evt.initEvent(typeEvent, bubbles, true);
} else {
2020-03-28 06:29:08 -04:00
evt = new CustomEvent(event, {
2021-03-23 12:26:54 -04:00
bubbles,
2019-03-01 11:31:34 -05:00
cancelable: true
});
2020-09-14 11:12:06 -04:00
} // merge custom information in our event
2019-03-01 11:31:34 -05:00
if (typeof args !== 'undefined') {
2021-03-23 12:26:54 -04:00
Object.keys(args).forEach(key => {
2019-03-01 11:31:34 -05:00
Object.defineProperty(evt, key, {
2021-03-23 12:26:54 -04:00
get() {
2019-03-01 11:31:34 -05:00
return args[key];
}
2021-03-23 12:26:54 -04:00
2019-03-01 11:31:34 -05:00
});
});
}
if (defaultPrevented) {
evt.preventDefault();
}
if (nativeDispatch) {
element.dispatchEvent(evt);
}
if (evt.defaultPrevented && typeof jQueryEvent !== 'undefined') {
jQueryEvent.preventDefault();
}
return evt;
}
2021-03-23 12:26:54 -04:00
2019-03-01 11:31:34 -05:00
};
return EventHandler;
2021-10-05 11:50:18 -04:00
}));
//# sourceMappingURL=event-handler.js.map