just-event.js
Version:
Modern, lightweight event utility with jQuery-like API.
3 lines (2 loc) • 4.49 kB
JavaScript
define((function(){"use strict";function t(t,e){if(!{}.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var e=0;function n(t){return"__private_"+e+++"_"+t}function r(t){return Object.prototype.toString.call(t).slice(8,-1)}function o(t){return"String"===r(t)}function s(t,e){if(!(e instanceof Function))throw new TypeError("Type must be a function");if(!Object.prototype.hasOwnProperty.call(e,"prototype"))throw new TypeError("Type is not a class");const n=e.name;return r(t)===n||Boolean(t&&t.constructor===e)}function i(t,e){if("function"==typeof e){for(let n=t;n;n=Object.getPrototypeOf(n))if(s(n,e))return!0;return!1}for(let n=t;n;n=Object.getPrototypeOf(n))if(r(n)===e)return!0;return!1}var c={normalizeElements(t){return o(t)?Array.from(document.querySelectorAll(t)):this.isDomElement(t)?[t]:i(t,"NodeList")||"Array"===r(t)?Array.from(t).filter(this.isDomElement):(console.warn("Invalid selector:",t),[])},isDomElement:t=>i(t,"HTMLElement")||i(t,"Document")||i(t,"Window"),closestUntil(t,e,n){if(!t||!e)return null;for(;t;){if(t.matches(e))return t;if(t===n||t===document.body)break;t=t.parentElement}return null}};const u=[];for(let t=0;t<256;++t)u.push((t+256).toString(16).slice(1));let l;const f=new Uint8Array(16);var a={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function h(t,e,n){if(a.randomUUID&&!t)return a.randomUUID();const r=(t=t||{}).random??t.rng?.()??function(){if(!l){if("undefined"==typeof crypto||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");l=crypto.getRandomValues.bind(crypto)}return l(f)}();if(r.length<16)throw new Error("Random bytes length must be >= 16");return r[6]=15&r[6]|64,r[8]=63&r[8]|128,function(t,e=0){return(u[t[e+0]]+u[t[e+1]]+u[t[e+2]]+u[t[e+3]]+"-"+u[t[e+4]]+u[t[e+5]]+"-"+u[t[e+6]]+u[t[e+7]]+"-"+u[t[e+8]]+u[t[e+9]]+"-"+u[t[e+10]]+u[t[e+11]]+u[t[e+12]]+u[t[e+13]]+u[t[e+14]]+u[t[e+15]]).toLowerCase()}(r)}const p=new Map;var d=n("namespaceSeparator"),m=n("eventNameKey"),y=n("getEventKey"),v=n("getRealEventType"),b=n("getEvents"),g=n("resolveArgs"),w=n("clean");class E{constructor(t){Object.defineProperty(this,w,{value:_}),Object.defineProperty(this,g,{value:j}),Object.defineProperty(this,b,{value:D}),Object.defineProperty(this,v,{value:O}),Object.defineProperty(this,y,{value:U}),Object.defineProperty(this,d,{writable:!0,value:"."}),Object.defineProperty(this,m,{writable:!0,value:"__eventName"}),this.elements=c.normalizeElements(t)}on(e,n,r){const o=t(this,g)[g](n,r);if(!o)return this;const{isDelegated:s}=o;r=o.listener;for(const o of t(this,b)[b](e))for(const e of this.elements){const i=t(this,y)[y](e,o),u=o=>{if(o.delegateTarget=e,s){const t=c.closestUntil(o.target,n,e);if(!t)return;Object.defineProperty(o,"currentTarget",{value:t})}const u=o[t(this,m)[m]];u&&!i.startsWith(u)||r.call(o.currentTarget,o)};p.has(i)||p.set(i,[]),p.get(i).push(u),e.addEventListener(t(this,v)[v](o),u)}return this}one(e,n,r){const o=t(this,g)[g](n,r);if(!o)return this;const{isDelegated:s}=o;r=o.listener;for(const o of t(this,b)[b](e)){const t=t=>{this.off(o),r.call(t.currentTarget,t)};s?this.on(o,n,t):this.on(o,t)}return this}off(e){for(const n of t(this,b)[b](e))for(const e of this.elements){const r=t(this,y)[y](e,n);if(n.startsWith(t(this,d)[d]))for(const[n,o]of p)n.endsWith(r)&&t(this,w)[w](e,n.replace(r,""),n,o);else if(n.includes(t(this,d)[d])){const o=p.get(r);o&&t(this,w)[w](e,n,r,o)}else for(const[r,o]of p)r.startsWith(n)&&t(this,w)[w](e,n,r,o)}return this}trigger(e,n=null){for(const r of t(this,b)[b](e)){const e=t(this,v)[v](r);for(const o of this.elements)if(r.includes(t(this,d)[d])){for(const[s]of p)if(s.startsWith(r)){const s=new CustomEvent(e,{bubbles:!0,detail:n});Object.defineProperty(s,t(this,m)[m],{value:r,writable:!1,configurable:!1,enumerable:!0}),o.dispatchEvent(s)}}else o.dispatchEvent(new CustomEvent(r,{bubbles:!0,detail:n}))}return this}}function U(t,e){return t.__eventListenerUUID||(t.__eventListenerUUID=h()),`${e}__${t.__eventListenerUUID}`}function O(e){return e.split(t(this,d)[d])[0]}function D(t){return o(t)?t.trim().split(/\s+/):[]}function j(t,e){const n=o(t);return"function"!=typeof(e=n?e:t)?null:{isDelegated:n,listener:e}}function _(e,n,r,o){for(const r of o)e.removeEventListener(t(this,v)[v](n),r);p.delete(r)}return t=>new E(t)}));
//# sourceMappingURL=just-event.amd.min.js.map