UNPKG

clickout-event

Version:

Provides universal support for `clickout` and other similar events to webpages.

6 lines 1.74 kB
/** * clickout-event v1.1.3 * (c) 2020-2025 Mu-Tsun Tsai * Released under the MIT License. */ !function(){"use strict";const t="on",e="out",n="body",o="stop",s="click",i="apply",r="target",u="prototype",c="Propagation",d="setAttribute",a="relatedTarget",h="addEventListener",p=Symbol(e),l=Object,f=document,m="undefined"!=typeof TouchEvent?TouchEvent:void 0,v=HTMLElement,y=v[u],b=Event[u],E=[s,"dbl"+s,"mousedown","mouseup","touchstart","touchend","pointerdown","pointerup"],w=new Set(E),g=new Set(E.map((n=>t+n+e))),T=(t,e)=>!!e&&t instanceof e,L=(t,e)=>t.contains(e),N=(t,e)=>{for(let n of t)e(n)},S=n=>{T(n,v)&&(n[p]?A(n):N(w,(o=>{let s=t+o+e,i=n.getAttribute(s);i&&!j(n)[s]&&n[d](s,i)})),N(n.childNodes,S))},j=t=>t[p]=t[p]||{},A=t=>{j(t),I.includes(t)||(I.push(t),q=!0)},M=(t,e)=>L(e,t)?1:L(t,e)?-1:0,O=t=>{q&&(I.sort(M),q=!1);let n=t[r];T(t,m)?(t=new m(t.type+e,t))[a]=n:t=new t.constructor(t.type+e,l.assign({},t,{[a]:n})),t[o]=[],N(I,(e=>{L(e,n)||t[o].some((t=>L(t,e)))||e.dispatchEvent(t)}))},P=e=>function(){let n=this,s=n.type;e[i](n),g.has(t+s)&&n[o].push(n[r])},k=(t,e,n)=>t[e]=n(t[e]);function H(e){let n=this[p][t+e.type];n&&n[i](this,[e])}let I=[],q=!1,x=f.createElement(n);N(w,(n=>{f[h](n,O,{passive:!0,capture:!0});let o=t+n+e;l.defineProperty(y,o,{get(){return this[p][o]},set(t){this[h](n+e,H),this[p][o]="object"==typeof t?t.handleEvent:t}})})),k(y,h,(e=>function(...n){g.has(t+n[0])&&A(this),e[i](this,n)})),k(y,d,(e=>function(n,o){g.has(n)?(e[i](x,[t+s,o]),this[n]=x[t+s]):e[i](this,[n,o])})),k(b,o+c,P),k(b,o+"Immediate"+c,P),new MutationObserver((t=>{N(t,(t=>{N(t.addedNodes,S),N(t.removedNodes,(t=>{T(t,v)&&(I=I.filter((e=>!L(t,e))))}))}))})).observe(f.documentElement,{childList:!0,subtree:!0}),S(f[n])}();