1 line
1.7 KiB
JavaScript
1 line
1.7 KiB
JavaScript
"use strict";(()=>{const t=(e,o,n,i)=>{o.onclick=null;const a=parseInt(o.getAttribute("data-cap-floating-offset"))||8,s=o.getAttribute("data-cap-floating-position")||"top",c=o.getBoundingClientRect();Object.assign(n.style,{display:"block",position:"fixed",zIndex:"99999"});const l=c.left+(c.width-n.offsetWidth)/2,r=Math.min(l,window.innerWidth-n.offsetWidth);n.style.top="top"===s?`${Math.max(0,c.top-n.offsetHeight-a)}px`:`${Math.min(c.bottom+a,window.innerHeight-n.offsetHeight)}px`,n.style.left=`${Math.max(r,2)}px`,n.solve(),n.addEventListener("solve",(({detail:a})=>{o.setAttribute("data-cap-token",a.token),o.setAttribute("data-cap-progress","done"),setTimeout((()=>{i.forEach((t=>{o.addEventListener("click",t),t.call(o,e)})),setTimeout((()=>{o.onclick=null,i.forEach((t=>o.removeEventListener("click",t))),o.onclick=e=>t(e,o,n,i)}),50)}),500),setTimeout((()=>{n.style.display="none"}),700)}))},e=e=>{const o=e.getAttribute("data-cap-floating");if(!o)return;const n=document.querySelector(o);if(!document.contains(n)&&!n.solve)throw new Error(`[Cap floating] Element "${o}" doesn't exist or isn't a Cap widget`);n.style.display="none";const i=[e.onclick].filter(Boolean);"function"==typeof getEventListeners&&i.push(...(getEventListeners(e).click||[]).map((t=>t.listener))),i.length&&(e.onclick=null,i.forEach((t=>e.removeEventListener("click",t)))),e.addEventListener("click",(o=>{o.stopImmediatePropagation(),o.preventDefault(),t(o,e,n,i)}))},o=t=>{e(t),t.querySelectorAll("[data-cap-floating]").forEach(e)};o(document.body),new MutationObserver((t=>t.forEach((t=>t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&o(t)})))))).observe(document.body,{childList:!0,subtree:!0})})(); |