0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-02 14:24:19 +01:00
Bootstrap/js/dist/dom/event-handler.js

471 lines
14 KiB
JavaScript
Raw Normal View History

2019-03-01 17:31:34 +01:00
/*!
* Bootstrap event-handler.js v4.3.1 (https://getbootstrap.com/)
2019-03-01 17:31:34 +01:00
* Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
(function (global, factory) {
2019-04-18 13:47:52 +02:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.EventHandler = factory());
}(this, function () { 'use strict';
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): util/index.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2019-04-18 13:47:52 +02:00
var MAX_UID = 1000000;
2019-03-11 16:13:30 +01:00
var _window = window,
jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2019-04-18 13:47:52 +02:00
/**
* --------------------------------------------------------------------------
* Public Util Api
* --------------------------------------------------------------------------
*/
var getUID = function getUID(prefix) {
do {
// eslint-disable-next-line no-bitwise
prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here
} while (document.getElementById(prefix));
return prefix;
};
/* istanbul ignore file */
var _Element$prototype = Element.prototype,
matches = _Element$prototype.matches,
closest = _Element$prototype.closest;
var find = Element.prototype.querySelectorAll;
var findOne = Element.prototype.querySelector;
var createCustomEvent = function createCustomEvent(eventName, params) {
var cEvent = new CustomEvent(eventName, params);
return cEvent;
};
if (typeof window.CustomEvent !== 'function') {
createCustomEvent = function createCustomEvent(eventName, params) {
params = params || {
bubbles: false,
cancelable: false,
detail: null
};
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent(eventName, params.bubbles, params.cancelable, params.detail);
return evt;
};
}
var workingDefaultPrevented = function () {
var e = document.createEvent('CustomEvent');
e.initEvent('Bootstrap', true, true);
e.preventDefault();
return e.defaultPrevented;
}();
if (!workingDefaultPrevented) {
var origPreventDefault = Event.prototype.preventDefault;
Event.prototype.preventDefault = function () {
if (!this.cancelable) {
return;
}
origPreventDefault.call(this);
Object.defineProperty(this, 'defaultPrevented', {
get: function get() {
return true;
},
configurable: true
});
};
} // MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached
var defaultPreventedPreservedOnDispatch = function () {
var e = createCustomEvent('Bootstrap', {
cancelable: true
});
var element = document.createElement('div');
element.addEventListener('Bootstrap', function () {
return null;
});
e.preventDefault();
element.dispatchEvent(e);
return e.defaultPrevented;
}();
if (!matches) {
matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}
if (!closest) {
closest = function closest(selector) {
var element = this;
do {
if (matches.call(element, selector)) {
return element;
}
element = element.parentElement || element.parentNode;
} while (element !== null && element.nodeType === 1);
return null;
};
}
var scopeSelectorRegex = /:scope\b/;
var supportScopeQuery = function () {
var element = document.createElement('div');
try {
element.querySelectorAll(':scope *');
} catch (error) {
return false;
}
return true;
}();
if (!supportScopeQuery) {
find = function find(selector) {
if (!scopeSelectorRegex.test(selector)) {
return this.querySelectorAll(selector);
}
var hasId = Boolean(this.id);
if (!hasId) {
this.id = getUID('scope');
}
var nodeList = null;
try {
selector = selector.replace(scopeSelectorRegex, "#" + this.id);
nodeList = this.querySelectorAll(selector);
} finally {
if (!hasId) {
this.removeAttribute('id');
}
}
return nodeList;
};
findOne = function findOne(selector) {
if (!scopeSelectorRegex.test(selector)) {
return this.querySelector(selector);
}
var matches = find.call(this, selector);
if (typeof matches[0] !== 'undefined') {
return matches[0];
}
return null;
};
}
2019-03-01 17:31:34 +01:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.3.1): dom/event-handler.js
2019-03-01 17:31:34 +01:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var namespaceRegex = /[^.]*(?=\..*)\.|.*/;
var stripNameRegex = /\..*/;
var keyEventRegex = /^key/;
var stripUidRegex = /::\d+$/;
var eventRegistry = {}; // Events storage
var uidEvent = 1;
var customEvents = {
mouseenter: 'mouseover',
mouseleave: 'mouseout'
};
var nativeEvents = ['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'];
/**
* ------------------------------------------------------------------------
* Private methods
* ------------------------------------------------------------------------
*/
function getUidEvent(element, uid) {
return uid && uid + "::" + uidEvent++ || element.uidEvent || uidEvent++;
}
function getEvent(element) {
var uid = getUidEvent(element);
element.uidEvent = uid;
2019-03-11 16:13:30 +01:00
eventRegistry[uid] = eventRegistry[uid] || {};
return eventRegistry[uid];
2019-03-01 17:31:34 +01:00
}
function fixEvent(event, element) {
// Add which for key events
if (event.which === null && keyEventRegex.test(event.type)) {
2019-03-11 16:13:30 +01:00
event.which = event.charCode === null ? event.keyCode : event.charCode;
2019-03-01 17:31:34 +01:00
}
event.delegateTarget = element;
}
function bootstrapHandler(element, fn) {
return function handler(event) {
fixEvent(event, element);
if (handler.oneOff) {
EventHandler.off(element, event.type, fn);
}
return fn.apply(element, [event]);
};
}
function bootstrapDelegationHandler(element, selector, fn) {
return function handler(event) {
var domElements = element.querySelectorAll(selector);
for (var target = event.target; target && target !== this; target = target.parentNode) {
for (var i = domElements.length; i--;) {
if (domElements[i] === target) {
fixEvent(event, target);
if (handler.oneOff) {
EventHandler.off(element, event.type, fn);
}
return fn.apply(target, [event]);
}
}
} // To please ESLint
return null;
};
}
function findHandler(events, handler, delegationSelector) {
if (delegationSelector === void 0) {
delegationSelector = null;
}
2019-04-18 13:47:52 +02:00
for (var _i = 0, _Object$keys = Object.keys(events); _i < _Object$keys.length; _i++) {
var uid = _Object$keys[_i];
2019-03-01 17:31:34 +01:00
var event = events[uid];
if (event.originalHandler === handler && event.delegationSelector === delegationSelector) {
return events[uid];
}
}
return null;
}
function normalizeParams(originalTypeEvent, handler, delegationFn) {
var delegation = typeof handler === 'string';
var originalHandler = delegation ? delegationFn : handler; // allow to get the native events from namespaced events ('click.bs.button' --> 'click')
var typeEvent = originalTypeEvent.replace(stripNameRegex, '');
var custom = customEvents[typeEvent];
if (custom) {
typeEvent = custom;
}
var isNative = nativeEvents.indexOf(typeEvent) > -1;
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;
}
var _normalizeParams = normalizeParams(originalTypeEvent, handler, delegationFn),
delegation = _normalizeParams[0],
originalHandler = _normalizeParams[1],
typeEvent = _normalizeParams[2];
var events = getEvent(element);
var handlers = events[typeEvent] || (events[typeEvent] = {});
var previousFn = findHandler(handlers, originalHandler, delegation ? handler : null);
if (previousFn) {
previousFn.oneOff = previousFn.oneOff && oneOff;
return;
}
var uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, ''));
2019-03-11 16:13:30 +01:00
var fn = delegation ? bootstrapDelegationHandler(element, handler, delegationFn) : bootstrapHandler(element, handler);
2019-03-01 17:31:34 +01: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) {
var fn = findHandler(events[typeEvent], handler, delegationSelector);
if (fn === null) {
return;
}
element.removeEventListener(typeEvent, fn, Boolean(delegationSelector));
delete events[typeEvent][fn.uidEvent];
}
function removeNamespacedHandlers(element, events, typeEvent, namespace) {
var storeElementEvent = events[typeEvent] || {};
Object.keys(storeElementEvent).forEach(function (handlerKey) {
if (handlerKey.indexOf(namespace) > -1) {
var event = storeElementEvent[handlerKey];
removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector);
}
});
}
var EventHandler = {
on: function on(element, event, handler, delegationFn) {
addHandler(element, event, handler, delegationFn, false);
},
one: function one(element, event, handler, delegationFn) {
addHandler(element, event, handler, delegationFn, true);
},
off: function off(element, originalTypeEvent, handler, delegationFn) {
if (typeof originalTypeEvent !== 'string' || !element) {
return;
}
var _normalizeParams2 = normalizeParams(originalTypeEvent, handler, delegationFn),
delegation = _normalizeParams2[0],
originalHandler = _normalizeParams2[1],
typeEvent = _normalizeParams2[2];
var inNamespace = typeEvent !== originalTypeEvent;
var events = getEvent(element);
var isNamespace = originalTypeEvent.charAt(0) === '.';
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) {
Object.keys(events).forEach(function (elementEvent) {
removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.substr(1));
});
}
var storeElementEvent = events[typeEvent] || {};
Object.keys(storeElementEvent).forEach(function (keyHandlers) {
var handlerKey = keyHandlers.replace(stripUidRegex, '');
if (!inNamespace || originalTypeEvent.indexOf(handlerKey) > -1) {
var event = storeElementEvent[keyHandlers];
removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector);
}
});
},
trigger: function trigger(element, event, args) {
if (typeof event !== 'string' || !element) {
return null;
}
var typeEvent = event.replace(stripNameRegex, '');
var inNamespace = event !== typeEvent;
var isNative = nativeEvents.indexOf(typeEvent) > -1;
var jQueryEvent;
var bubbles = true;
var nativeDispatch = true;
var defaultPrevented = false;
var evt = null;
if (inNamespace && typeof jQuery !== 'undefined') {
jQueryEvent = jQuery.Event(event, args);
jQuery(element).trigger(jQueryEvent);
bubbles = !jQueryEvent.isPropagationStopped();
nativeDispatch = !jQueryEvent.isImmediatePropagationStopped();
defaultPrevented = jQueryEvent.isDefaultPrevented();
}
if (isNative) {
evt = document.createEvent('HTMLEvents');
evt.initEvent(typeEvent, bubbles, true);
} else {
2019-04-18 13:47:52 +02:00
evt = createCustomEvent(event, {
2019-03-01 17:31:34 +01:00
bubbles: bubbles,
cancelable: true
});
} // merge custom informations in our event
if (typeof args !== 'undefined') {
Object.keys(args).forEach(function (key) {
Object.defineProperty(evt, key, {
get: function get() {
return args[key];
}
});
});
}
if (defaultPrevented) {
evt.preventDefault();
2019-04-18 13:47:52 +02:00
if (!defaultPreventedPreservedOnDispatch) {
2019-03-01 17:31:34 +01:00
Object.defineProperty(evt, 'defaultPrevented', {
get: function get() {
return true;
}
});
}
}
if (nativeDispatch) {
element.dispatchEvent(evt);
}
if (evt.defaultPrevented && typeof jQueryEvent !== 'undefined') {
jQueryEvent.preventDefault();
}
return evt;
}
};
return EventHandler;
}));
//# sourceMappingURL=event-handler.js.map