@jjwesterkamp/event-delegation
Version:
Event delegation for browser DOM events. Flexible, cross-browser compatible and Typescript-focused.
8 lines • 3.38 kB
JavaScript
/*!
* @jjwesterkamp/event-delegation
* https://jjwesterkamp.github.io/event-delegation
* (c) 2018-2021 Jeffrey Westerkamp
* This software may be freely distributed under the MIT license.
*/
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.EventDelegation=e():t.EventDelegation=e()}(self,(function(){return function(){"use strict";var t={d:function(e,n){for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:function(t,e){return Object.prototype.hasOwnProperty.call(t,e)}},e={};function n(t){return null==t}function r(t){return"string"==typeof t}function o(t){return"function"==typeof t}function i(t,e){if(!(t instanceof Element))throw new Error("cannot match a non-element against a selector");if(o(t.matches))return t.matches(e);if(o(t.matchesSelector))return t.matchesSelector(e);if(o(t.mozMatchesSelector))return t.mozMatchesSelector(e);if(o(t.msMatchesSelector))return t.msMatchesSelector(e);if(o(t.oMatchesSelector))return t.oMatchesSelector(e);if(o(t.webkitMatchesSelector))return t.webkitMatchesSelector(e);for(var n=(t.document||t.ownerDocument).querySelectorAll(e),r=n.length;--r>=0&&n.item(r)!==t;);return r>-1}t.d(e,{default:function(){return u}});var s=function(){function t(t){var e=this;this.config=t,this._isAttached=!1,this._isDestroyed=!1,this.actualListenerOptions=this.isOnceListener()?Object.assign({},t.listenerOptions,{once:!1}):t.listenerOptions,this.handler=function(r){var s=function(t,e,n){if(o(t.closest)){var r=t.closest(e);return null!==r&&n.contains(r)?r:null}if(!n.contains(t))return null;var s=t;do{if(i(s,e))return s;s=s.parentElement}while(null!==s&&s!==n&&s.nodeType===Node.ELEMENT_NODE);return null}(r.target,t.selector,t.root);n(s)||(t.listener.call(s,Object.assign(r,{delegator:s})),e.isOnceListener()&&e.remove())},t.root.addEventListener(t.eventType,this.handler,this.actualListenerOptions),this._isAttached=!0}return t.prototype.isAttached=function(){return this._isAttached},t.prototype.isDestroyed=function(){return this._isDestroyed},t.prototype.root=function(){return this.config.root},t.prototype.selector=function(){return this.config.selector},t.prototype.eventType=function(){return this.config.eventType},t.prototype.remove=function(){this._isDestroyed||(this.config.root.removeEventListener(this.config.eventType,this.handler,this.actualListenerOptions),this._isDestroyed=!0,this._isAttached=!1)},t.prototype.isOnceListener=function(){return"object"==typeof this.config.listenerOptions&&!0===this.config.listenerOptions.once},t}(),c=function(t){return{events:function(e){return{select:function(n){return{listen:function(r,o){return new s({root:t,eventType:e,selector:n,listener:r,listenerOptions:o})}}}}}}};var u={global:function(){return c(document.body)},within:function(t){return c(function(t){if(r(t)){var e=document.querySelector(t);if(n(e))throw new Error("Couldn't find any root element matching selector '"+t+"'.");return e}return t}(t))},withinMany:function(t){var e,n=r(t)?Array.from(document.querySelectorAll(t)):t;return e=n.map(c),{events:function(t){return{select:function(n){return{listen:function(r,o){return e.map((function(e){return e.events(t).select(n).listen(r,o)}))}}}}}}}};return e=e.default}()}));
//# sourceMappingURL=event-delegation.min.js.map