-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.min.js
9 lines (9 loc) · 2.87 KB
/
Events.min.js
1
2
3
4
5
6
7
8
9
/*
* Events.js v1.0.0
* Pequeña libreria para delegación de eventos
* Copyright (c) 2021, Emanuel Rojas Vásquez
* BSD 3-Clause License
* [Back-compatibility IE11+]
* https://github.com/erovas/Events.js
*/
Object.defineProperty(window,"Events",{value:function(e,t){Element.prototype.closest||(Element.prototype.matches||(Element.prototype.matches=Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector),Object.defineProperty(Element.prototype,"closest",{configurable:!0,enumerable:!0,writable:!0,value:function(e){let t=this;do{if(t.matches(e))return t;t=t.parentElement||t.parentNode}while(null!==t&&1===t.nodeType);return null}}));const n={click:{},input:{},touchstart:{},touchmove:{},touchend:{},mousedown:{},mousemove:{},mouseup:{},keydown:{},keyup:{}},o=function(n,o,r,l){if(!r||!l||"function"!=typeof l)return null;if(["*","window","document","document.documentElement",t,e,e.documentElement].indexOf(r)>-1)return null;const c=r instanceof Element?r:r+"";0===Object.keys(n).length&&e.addEventListener(o,function(e){const t=e.target;for(let o in n){const r=n[o].selector;if(e.Selector=n[o].origin,"string"!=typeof r){if(e.tagTarget=r,r===t||r.contains(t))n[o].callback(e,r);else if(t.shadowRoot){const l=e.path||e.composedPath(),c=l.indexOf(t);for(let i=0;i<c-1;i++)r===l[i]&&(e.customElement=t,n[o].callback(e,r))}}else{let l=t.closest(r);if(e.tagTarget=l,l)n[o].callback(e,l);else if(t.shadowRoot){const c=e.path||e.composedPath(),i=c.indexOf(t);for(let a=0;a<i-1;a++)(l=c[a].closest(r))&&(e.tagTarget=l,e.customElement=t,n[o].callback(e,l))}}}},{passive:!1});const i=Object.keys(n).length;return n[i]={selector:c,origin:r,callback:l},{callback:l,index:i}},r=function(e,t){return!!t[e]&&(delete t[e],!0)},l=!!e.documentMode,c={};for(let e in n){const t=e;c[t]="input"===t?function(e,r){return o(n[t],l?"textinput":t,e,r)}:function(e,r){return o(n[t],t,e,r)},c["remove"+t.charAt(0).toUpperCase()+t.slice(1)]=function(e){return r(e,n[t])}}return n.resize={},n.ready=[],n.load=[],c.resize=function(e){if("function"!=typeof e)return null;0===Object.keys(n.resize).length&&t.addEventListener("resize",function(e){for(let t in n.resize)n.resize[t](e,this)},!1);const o=Object.keys(n.resize).length;return n.resize[o]=e,{callback:e,index:o}},c.removeResize=function(e){return r(e,n.resize)},c.ready=function(t){return"function"!=typeof t?null:"interactive"===e.readyState||"complete"===e.readyState?(t(e),t):(0===n.ready.length&&"complete"!==e.readyState&&e.addEventListener("DOMContentLoaded",function(e){for(let e=0;e<n.ready.length;e++)n.ready[e](this);delete n.ready},{once:!0}),n.ready.push(t),t)},c.load=function(o){return"function"!=typeof o?null:"interactive"===e.readyState||"complete"===e.readyState?(o(t),o):(0===n.load.length&&t.addEventListener("load",function(e){for(let e=0;e<n.load.length;e++)n.load[e](this);delete n.load},{once:!0}),n.load.push(o),o)},c}(document,window),writable:!1});