@ussebastian/kitdigital
Version:
Kit Digital de la Universidad San Sebastián
4 lines (3 loc) • 22.3 kB
JavaScript
/*! @ussebastian/kitdigital v0.21.0 | MIT (c) Asimov Consultores SpA - 2025 */
const t=["start","end"],e=["top","right","bottom","left"].reduce(((e,n)=>e.concat(n,n+"-"+t[0],n+"-"+t[1])),[]),n=Math.min,o=Math.max,i=Math.round,r=Math.floor,l=t=>({x:t,y:t}),s={left:"right",right:"left",bottom:"top",top:"bottom"},a={start:"end",end:"start"};function c(t,e,i){return o(t,n(e,i))}function u(t,e){return"function"==typeof t?t(e):t}function f(t){return t.split("-")[0]}function d(t){return t.split("-")[1]}function h(t){return"x"===t?"y":"x"}function p(t){return"y"===t?"height":"width"}function m(t){return["top","bottom"].includes(f(t))?"y":"x"}function g(t){return h(m(t))}function y(t,e,n){void 0===n&&(n=!1);const o=d(t),i=g(t),r=p(i);let l="x"===i?o===(n?"end":"start")?"right":"left":"start"===o?"bottom":"top";return e.reference[r]>e.floating[r]&&(l=v(l)),[l,v(l)]}function w(t){return t.replace(/start|end/g,(t=>a[t]))}function v(t){return t.replace(/left|right|bottom|top/g,(t=>s[t]))}function b(t){return"number"!=typeof t?function(t){return{top:0,right:0,bottom:0,left:0,...t}}(t):{top:t,right:t,bottom:t,left:t}}function x(t){return{...t,top:t.y,left:t.x,right:t.x+t.width,bottom:t.y+t.height}}function T(t,e,n){let{reference:o,floating:i}=t;const r=m(e),l=g(e),s=p(l),a=f(e),c="y"===r,u=o.x+o.width/2-i.width/2,h=o.y+o.height/2-i.height/2,y=o[s]/2-i[s]/2;let w;switch(a){case"top":w={x:u,y:o.y-i.height};break;case"bottom":w={x:u,y:o.y+o.height};break;case"right":w={x:o.x+o.width,y:h};break;case"left":w={x:o.x-i.width,y:h};break;default:w={x:o.x,y:o.y}}switch(d(e)){case"start":w[l]-=y*(n&&c?-1:1);break;case"end":w[l]+=y*(n&&c?-1:1)}return w}async function E(t,e){var n;void 0===e&&(e={});const{x:o,y:i,platform:r,rects:l,elements:s,strategy:a}=t,{boundary:c="clippingAncestors",rootBoundary:f="viewport",elementContext:d="floating",altBoundary:h=!1,padding:p=0}=u(e,t),m=b(p),g=s[h?"floating"===d?"reference":"floating":d],y=x(await r.getClippingRect({element:null==(n=await(null==r.isElement?void 0:r.isElement(g)))||n?g:g.contextElement||await(null==r.getDocumentElement?void 0:r.getDocumentElement(s.floating)),boundary:c,rootBoundary:f,strategy:a})),w="floating"===d?{...l.floating,x:o,y:i}:l.reference,v=await(null==r.getOffsetParent?void 0:r.getOffsetParent(s.floating)),T=await(null==r.isElement?void 0:r.isElement(v))&&await(null==r.getScale?void 0:r.getScale(v))||{x:1,y:1},E=x(r.convertOffsetParentRelativeRectToViewportRelativeRect?await r.convertOffsetParentRelativeRectToViewportRelativeRect({elements:s,rect:w,offsetParent:v,strategy:a}):w);return{top:(y.top-E.top+m.top)/T.y,bottom:(E.bottom-y.bottom+m.bottom)/T.y,left:(y.left-E.left+m.left)/T.x,right:(E.right-y.right+m.right)/T.x}}const L=function(t){return void 0===t&&(t=0),{name:"offset",options:t,async fn(e){var n,o;const{x:i,y:r,placement:l,middlewareData:s}=e,a=await async function(t,e){const{placement:n,platform:o,elements:i}=t,r=await(null==o.isRTL?void 0:o.isRTL(i.floating)),l=f(n),s=d(n),a="y"===m(n),c=["left","top"].includes(l)?-1:1,h=r&&a?-1:1,p=u(e,t);let{mainAxis:g,crossAxis:y,alignmentAxis:w}="number"==typeof p?{mainAxis:p,crossAxis:0,alignmentAxis:null}:{mainAxis:0,crossAxis:0,alignmentAxis:null,...p};return s&&"number"==typeof w&&(y="end"===s?-1*w:w),a?{x:y*h,y:g*c}:{x:g*c,y:y*h}}(e,t);return l===(null==(n=s.offset)?void 0:n.placement)&&null!=(o=s.arrow)&&o.alignmentOffset?{}:{x:i+a.x,y:r+a.y,data:{...a,placement:l}}}}};function A(t){return S(t)?(t.nodeName||"").toLowerCase():"#document"}function O(t){var e;return(null==t||null==(e=t.ownerDocument)?void 0:e.defaultView)||window}function R(t){var e;return null==(e=(S(t)?t.ownerDocument:t.document)||window.document)?void 0:e.documentElement}function S(t){return t instanceof Node||t instanceof O(t).Node}function P(t){return t instanceof Element||t instanceof O(t).Element}function k(t){return t instanceof HTMLElement||t instanceof O(t).HTMLElement}function D(t){return"undefined"!=typeof ShadowRoot&&(t instanceof ShadowRoot||t instanceof O(t).ShadowRoot)}function B(t){const{overflow:e,overflowX:n,overflowY:o,display:i}=W(t);return/auto|scroll|overlay|hidden|clip/.test(e+o+n)&&!["inline","contents"].includes(i)}function C(t){return["table","td","th"].includes(A(t))}function F(t){const e=H(),n=W(t);return"none"!==n.transform||"none"!==n.perspective||!!n.containerType&&"normal"!==n.containerType||!e&&!!n.backdropFilter&&"none"!==n.backdropFilter||!e&&!!n.filter&&"none"!==n.filter||["transform","perspective","filter"].some((t=>(n.willChange||"").includes(t)))||["paint","layout","strict","content"].some((t=>(n.contain||"").includes(t)))}function H(){return!("undefined"==typeof CSS||!CSS.supports)&&CSS.supports("-webkit-backdrop-filter","none")}function M(t){return["html","body","#document"].includes(A(t))}function W(t){return O(t).getComputedStyle(t)}function $(t){return P(t)?{scrollLeft:t.scrollLeft,scrollTop:t.scrollTop}:{scrollLeft:t.pageXOffset,scrollTop:t.pageYOffset}}function N(t){if("html"===A(t))return t;const e=t.assignedSlot||t.parentNode||D(t)&&t.host||R(t);return D(e)?e.host:e}function V(t){const e=N(t);return M(e)?t.ownerDocument?t.ownerDocument.body:t.body:k(e)&&B(e)?e:V(e)}function q(t,e,n){var o;void 0===e&&(e=[]),void 0===n&&(n=!0);const i=V(t),r=i===(null==(o=t.ownerDocument)?void 0:o.body),l=O(i);return r?e.concat(l,l.visualViewport||[],B(i)?i:[],l.frameElement&&n?q(l.frameElement):[]):e.concat(i,q(i,[],n))}function z(t){const e=W(t);let n=parseFloat(e.width)||0,o=parseFloat(e.height)||0;const r=k(t),l=r?t.offsetWidth:n,s=r?t.offsetHeight:o,a=i(n)!==l||i(o)!==s;return a&&(n=l,o=s),{width:n,height:o,$:a}}function I(t){return P(t)?t:t.contextElement}function j(t){const e=I(t);if(!k(e))return l(1);const n=e.getBoundingClientRect(),{width:o,height:r,$:s}=z(e);let a=(s?i(n.width):n.width)/o,c=(s?i(n.height):n.height)/r;return a&&Number.isFinite(a)||(a=1),c&&Number.isFinite(c)||(c=1),{x:a,y:c}}const K=l(0);function X(t){const e=O(t);return H()&&e.visualViewport?{x:e.visualViewport.offsetLeft,y:e.visualViewport.offsetTop}:K}function Y(t,e,n,o){void 0===e&&(e=!1),void 0===n&&(n=!1);const i=t.getBoundingClientRect(),r=I(t);let s=l(1);e&&(o?P(o)&&(s=j(o)):s=j(t));const a=function(t,e,n){return void 0===e&&(e=!1),!(!n||e&&n!==O(t))&&e}(r,n,o)?X(r):l(0);let c=(i.left+a.x)/s.x,u=(i.top+a.y)/s.y,f=i.width/s.x,d=i.height/s.y;if(r){const t=O(r),e=o&&P(o)?O(o):o;let n=t,i=n.frameElement;for(;i&&o&&e!==n;){const t=j(i),e=i.getBoundingClientRect(),o=W(i),r=e.left+(i.clientLeft+parseFloat(o.paddingLeft))*t.x,l=e.top+(i.clientTop+parseFloat(o.paddingTop))*t.y;c*=t.x,u*=t.y,f*=t.x,d*=t.y,c+=r,u+=l,n=O(i),i=n.frameElement}}return x({width:f,height:d,x:c,y:u})}const _=[":popover-open",":modal"];function G(t){return _.some((e=>{try{return t.matches(e)}catch(t){return!1}}))}function J(t){return Y(R(t)).left+$(t).scrollLeft}function Q(t,e,n){let i;if("viewport"===e)i=function(t,e){const n=O(t),o=R(t),i=n.visualViewport;let r=o.clientWidth,l=o.clientHeight,s=0,a=0;if(i){r=i.width,l=i.height;const t=H();(!t||t&&"fixed"===e)&&(s=i.offsetLeft,a=i.offsetTop)}return{width:r,height:l,x:s,y:a}}(t,n);else if("document"===e)i=function(t){const e=R(t),n=$(t),i=t.ownerDocument.body,r=o(e.scrollWidth,e.clientWidth,i.scrollWidth,i.clientWidth),l=o(e.scrollHeight,e.clientHeight,i.scrollHeight,i.clientHeight);let s=-n.scrollLeft+J(t);const a=-n.scrollTop;return"rtl"===W(i).direction&&(s+=o(e.clientWidth,i.clientWidth)-r),{width:r,height:l,x:s,y:a}}(R(t));else if(P(e))i=function(t,e){const n=Y(t,!0,"fixed"===e),o=n.top+t.clientTop,i=n.left+t.clientLeft,r=k(t)?j(t):l(1);return{width:t.clientWidth*r.x,height:t.clientHeight*r.y,x:i*r.x,y:o*r.y}}(e,n);else{const n=X(t);i={...e,x:e.x-n.x,y:e.y-n.y}}return x(i)}function U(t,e){const n=N(t);return!(n===e||!P(n)||M(n))&&("fixed"===W(n).position||U(n,e))}function Z(t,e,n){const o=k(e),i=R(e),r="fixed"===n,s=Y(t,!0,r,e);let a={scrollLeft:0,scrollTop:0};const c=l(0);if(o||!o&&!r)if(("body"!==A(e)||B(i))&&(a=$(e)),o){const t=Y(e,!0,r,e);c.x=t.x+e.clientLeft,c.y=t.y+e.clientTop}else i&&(c.x=J(i));return{x:s.left+a.scrollLeft-c.x,y:s.top+a.scrollTop-c.y,width:s.width,height:s.height}}function tt(t,e){return k(t)&&"fixed"!==W(t).position?e?e(t):t.offsetParent:null}function et(t,e){const n=O(t);if(!k(t)||G(t))return n;let o=tt(t,e);for(;o&&C(o)&&"static"===W(o).position;)o=tt(o,e);return o&&("html"===A(o)||"body"===A(o)&&"static"===W(o).position&&!F(o))?n:o||function(t){let e=N(t);for(;k(e)&&!M(e);){if(F(e))return e;e=N(e)}return null}(t)||n}const nt={convertOffsetParentRelativeRectToViewportRelativeRect:function(t){let{elements:e,rect:n,offsetParent:o,strategy:i}=t;const r="fixed"===i,s=R(o),a=!!e&&G(e.floating);if(o===s||a&&r)return n;let c={scrollLeft:0,scrollTop:0},u=l(1);const f=l(0),d=k(o);if((d||!d&&!r)&&(("body"!==A(o)||B(s))&&(c=$(o)),k(o))){const t=Y(o);u=j(o),f.x=t.x+o.clientLeft,f.y=t.y+o.clientTop}return{width:n.width*u.x,height:n.height*u.y,x:n.x*u.x-c.scrollLeft*u.x+f.x,y:n.y*u.y-c.scrollTop*u.y+f.y}},getDocumentElement:R,getClippingRect:function(t){let{element:e,boundary:i,rootBoundary:r,strategy:l}=t;const s=[..."clippingAncestors"===i?function(t,e){const n=e.get(t);if(n)return n;let o=q(t,[],!1).filter((t=>P(t)&&"body"!==A(t))),i=null;const r="fixed"===W(t).position;let l=r?N(t):t;for(;P(l)&&!M(l);){const e=W(l),n=F(l);n||"fixed"!==e.position||(i=null),(r?!n&&!i:!n&&"static"===e.position&&i&&["absolute","fixed"].includes(i.position)||B(l)&&!n&&U(t,l))?o=o.filter((t=>t!==l)):i=e,l=N(l)}return e.set(t,o),o}(e,this._c):[].concat(i),r],a=s[0],c=s.reduce(((t,i)=>{const r=Q(e,i,l);return t.top=o(r.top,t.top),t.right=n(r.right,t.right),t.bottom=n(r.bottom,t.bottom),t.left=o(r.left,t.left),t}),Q(e,a,l));return{width:c.right-c.left,height:c.bottom-c.top,x:c.left,y:c.top}},getOffsetParent:et,getElementRects:async function(t){const e=this.getOffsetParent||et,n=this.getDimensions;return{reference:Z(t.reference,await e(t.floating),t.strategy),floating:{x:0,y:0,...await n(t.floating)}}},getClientRects:function(t){return Array.from(t.getClientRects())},getDimensions:function(t){const{width:e,height:n}=z(t);return{width:e,height:n}},getScale:j,isElement:P,isRTL:function(t){return"rtl"===W(t).direction}};function ot(t,e,i,l){void 0===l&&(l={});const{ancestorScroll:s=!0,ancestorResize:a=!0,elementResize:c="function"==typeof ResizeObserver,layoutShift:u="function"==typeof IntersectionObserver,animationFrame:f=!1}=l,d=I(t),h=s||a?[...d?q(d):[],...q(e)]:[];h.forEach((t=>{s&&t.addEventListener("scroll",i,{passive:!0}),a&&t.addEventListener("resize",i)}));const p=d&&u?function(t,e){let i,l=null;const s=R(t);function a(){var t;clearTimeout(i),null==(t=l)||t.disconnect(),l=null}return function c(u,f){void 0===u&&(u=!1),void 0===f&&(f=1),a();const{left:d,top:h,width:p,height:m}=t.getBoundingClientRect();if(u||e(),!p||!m)return;const g={rootMargin:-r(h)+"px "+-r(s.clientWidth-(d+p))+"px "+-r(s.clientHeight-(h+m))+"px "+-r(d)+"px",threshold:o(0,n(1,f))||1};let y=!0;function w(t){const e=t[0].intersectionRatio;if(e!==f){if(!y)return c();e?c(!1,e):i=setTimeout((()=>{c(!1,1e-7)}),100)}y=!1}try{l=new IntersectionObserver(w,{...g,root:s.ownerDocument})}catch(t){l=new IntersectionObserver(w,g)}l.observe(t)}(!0),a}(d,i):null;let m,g=-1,y=null;c&&(y=new ResizeObserver((t=>{let[n]=t;n&&n.target===d&&y&&(y.unobserve(e),cancelAnimationFrame(g),g=requestAnimationFrame((()=>{var t;null==(t=y)||t.observe(e)}))),i()})),d&&!f&&y.observe(d),y.observe(e));let w=f?Y(t):null;return f&&function e(){const n=Y(t);!w||n.x===w.x&&n.y===w.y&&n.width===w.width&&n.height===w.height||i();w=n,m=requestAnimationFrame(e)}(),i(),()=>{var t;h.forEach((t=>{s&&t.removeEventListener("scroll",i),a&&t.removeEventListener("resize",i)})),null==p||p(),null==(t=y)||t.disconnect(),y=null,f&&cancelAnimationFrame(m)}}const it=function(t){return void 0===t&&(t={}),{name:"autoPlacement",options:t,async fn(n){var o,i,r;const{rects:l,middlewareData:s,placement:a,platform:c,elements:h}=n,{crossAxis:p=!1,alignment:m,allowedPlacements:g=e,autoAlignment:v=!0,...b}=u(t,n),x=void 0!==m||g===e?function(t,e,n){return(t?[...n.filter((e=>d(e)===t)),...n.filter((e=>d(e)!==t))]:n.filter((t=>f(t)===t))).filter((n=>!t||d(n)===t||!!e&&w(n)!==n))}(m||null,v,g):g,T=await E(n,b),L=(null==(o=s.autoPlacement)?void 0:o.index)||0,A=x[L];if(null==A)return{};const O=y(A,l,await(null==c.isRTL?void 0:c.isRTL(h.floating)));if(a!==A)return{reset:{placement:x[0]}};const R=[T[f(A)],T[O[0]],T[O[1]]],S=[...(null==(i=s.autoPlacement)?void 0:i.overflows)||[],{placement:A,overflows:R}],P=x[L+1];if(P)return{data:{index:L+1,overflows:S},reset:{placement:P}};const k=S.map((t=>{const e=d(t.placement);return[t.placement,e&&p?t.overflows.slice(0,2).reduce(((t,e)=>t+e),0):t.overflows[0],t.overflows]})).sort(((t,e)=>t[1]-e[1])),D=(null==(r=k.filter((t=>t[2].slice(0,d(t[0])?2:3).every((t=>t<=0))))[0])?void 0:r[0])||k[0][0];return D!==a?{data:{index:L+1,overflows:S},reset:{placement:D}}:{}}}},rt=function(t){return void 0===t&&(t={}),{name:"shift",options:t,async fn(e){const{x:n,y:o,placement:i}=e,{mainAxis:r=!0,crossAxis:l=!1,limiter:s={fn:t=>{let{x:e,y:n}=t;return{x:e,y:n}}},...a}=u(t,e),d={x:n,y:o},p=await E(e,a),g=m(f(i)),y=h(g);let w=d[y],v=d[g];if(r){const t="y"===y?"bottom":"right";w=c(w+p["y"===y?"top":"left"],w,w-p[t])}if(l){const t="y"===g?"bottom":"right";v=c(v+p["y"===g?"top":"left"],v,v-p[t])}const b=s.fn({...e,[y]:w,[g]:v});return{...b,data:{x:b.x-n,y:b.y-o}}}}},lt=function(t){return void 0===t&&(t={}),{name:"flip",options:t,async fn(e){var n,o;const{placement:i,middlewareData:r,rects:l,initialPlacement:s,platform:a,elements:c}=e,{mainAxis:h=!0,crossAxis:p=!0,fallbackPlacements:m,fallbackStrategy:g="bestFit",fallbackAxisSideDirection:b="none",flipAlignment:x=!0,...T}=u(t,e);if(null!=(n=r.arrow)&&n.alignmentOffset)return{};const L=f(i),A=f(s)===s,O=await(null==a.isRTL?void 0:a.isRTL(c.floating)),R=m||(A||!x?[v(s)]:function(t){const e=v(t);return[w(t),e,w(e)]}(s));m||"none"===b||R.push(...function(t,e,n,o){const i=d(t);let r=function(t,e,n){const o=["left","right"],i=["right","left"],r=["top","bottom"],l=["bottom","top"];switch(t){case"top":case"bottom":return n?e?i:o:e?o:i;case"left":case"right":return e?r:l;default:return[]}}(f(t),"start"===n,o);return i&&(r=r.map((t=>t+"-"+i)),e&&(r=r.concat(r.map(w)))),r}(s,x,b,O));const S=[s,...R],P=await E(e,T),k=[];let D=(null==(o=r.flip)?void 0:o.overflows)||[];if(h&&k.push(P[L]),p){const t=y(i,l,O);k.push(P[t[0]],P[t[1]])}if(D=[...D,{placement:i,overflows:k}],!k.every((t=>t<=0))){var B,C;const t=((null==(B=r.flip)?void 0:B.index)||0)+1,e=S[t];if(e)return{data:{index:t,overflows:D},reset:{placement:e}};let n=null==(C=D.filter((t=>t.overflows[0]<=0)).sort(((t,e)=>t.overflows[1]-e.overflows[1]))[0])?void 0:C.placement;if(!n)switch(g){case"bestFit":{var F;const t=null==(F=D.map((t=>[t.placement,t.overflows.filter((t=>t>0)).reduce(((t,e)=>t+e),0)])).sort(((t,e)=>t[1]-e[1]))[0])?void 0:F[0];t&&(n=t);break}case"initialPlacement":n=s}if(i!==n)return{reset:{placement:n}}}return{}}}},st=t=>({name:"arrow",options:t,async fn(e){const{x:o,y:i,placement:r,rects:l,platform:s,elements:a,middlewareData:f}=e,{element:h,padding:m=0}=u(t,e)||{};if(null==h)return{};const y=b(m),w={x:o,y:i},v=g(r),x=p(v),T=await s.getDimensions(h),E="y"===v,L=E?"top":"left",A=E?"bottom":"right",O=E?"clientHeight":"clientWidth",R=l.reference[x]+l.reference[v]-w[v]-l.floating[x],S=w[v]-l.reference[v],P=await(null==s.getOffsetParent?void 0:s.getOffsetParent(h));let k=P?P[O]:0;k&&await(null==s.isElement?void 0:s.isElement(P))||(k=a.floating[O]||l.floating[x]);const D=R/2-S/2,B=k/2-T[x]/2-1,C=n(y[L],B),F=n(y[A],B),H=C,M=k-T[x]-F,W=k/2-T[x]/2+D,$=c(H,W,M),N=!f.arrow&&null!=d(r)&&W!==$&&l.reference[x]/2-(W<H?C:F)-T[x]/2<0,V=N?W<H?W-H:W-M:0;return{[v]:w[v]+V,data:{[v]:$,centerOffset:W-$-V,...N&&{alignmentOffset:V}},reset:N}}}),at=(t,e,n)=>{const o=new Map,i={platform:nt,...n},r={...i.platform,_c:o};return(async(t,e,n)=>{const{placement:o="bottom",strategy:i="absolute",middleware:r=[],platform:l}=n,s=r.filter(Boolean),a=await(null==l.isRTL?void 0:l.isRTL(e));let c=await l.getElementRects({reference:t,floating:e,strategy:i}),{x:u,y:f}=T(c,o,a),d=o,h={},p=0;for(let n=0;n<s.length;n++){const{name:r,fn:m}=s[n],{x:g,y:y,data:w,reset:v}=await m({x:u,y:f,initialPlacement:o,placement:d,strategy:i,middlewareData:h,rects:c,platform:l,elements:{reference:t,floating:e}});u=null!=g?g:u,f=null!=y?y:f,h={...h,[r]:{...h[r],...w}},v&&p<=50&&(p++,"object"==typeof v&&(v.placement&&(d=v.placement),v.rects&&(c=!0===v.rects?await l.getElementRects({reference:t,floating:e,strategy:i}):v.rects),({x:u,y:f}=T(c,d,a))),n=-1)}return{x:u,y:f,placement:d,strategy:i,middlewareData:h}})(t,e,{...i,platform:r})};class ct{constructor(t,e){this.key=e,this.element=t,this.setupInitialState()}setupInitialState(){this.tooltip=null,this.arrow=null,this.cleanup=()=>{},this.delay=350,this.placement="top",this.isOpen=!1,this.isOverTrigger=!1,this.shiftPadding=0,this.autoPlacement=!1,this.startOpen=!1,this.validPlacements=["top","right","bottom","left","top-start","top-end","right-start","right-end","bottom-start","bottom-end","left-start","left-end"]}setAriaAttributes(){this.element.setAttribute("aria-describedby",this.key),this.tooltip.setAttribute("id",this.key),this.tooltip.setAttribute("role","tooltip")}mount(){this.handleDataSet(),this.buildTooltipElement(),this.setAriaAttributes(),this.updateTooltipPosition(),this.setEventListeners(),this.hideTooltip(!0)}handleDataSet(){const{ussTooltipDelay:t,ussTooltipPlacement:e,ussTooltipShiftPadding:n,ussTooltipAutoPlacement:o,ussTooltipStartOpen:i}=this.element.dataset,r=this.validPlacements.includes(e);if(e&&!r)throw new Error(`El valor de data-uss-tooltip-placement: ${e} no es válido.`);this.delay=t?Number(t):this.delay,this.placement=e||this.placement,this.shiftPadding=Number(n)||this.shiftPadding,this.autoPlacement=Boolean(o)||this.autoPlacement,this.startOpen=Boolean(i)||this.startOpen}buildTooltipElement(){const t=this.element.getAttribute("data-uss-tooltip-content-element-id"),e=this.element.getAttribute("data-uss-tooltip-content");this.validateContent(e,t),this.tooltip=this.findOrCreateTooltip(t,e),this.cannotHaveButtonsOrLinks(),this.buildArrow()}validateContent(t,e){if(!t&&!e)throw new Error("El Tooltip debe tener contenido o un elemento asociado.")}findOrCreateTooltip(t,e){if(t){const e=document.querySelectorAll(`[data-uss-tooltip-content-id="${t}"]`);if(1!==e.length)throw new Error(`Elemento con data-uss-tooltip-content-id: ${t} ${e.length>1?"no es único.":"no existe."}`);return e[0]}if(e.includes("<")||e.includes(">"))throw new Error("El attributo 'data-uss-tooltip-content' no puede contener HTML.");const n=document.createElement("div");return n.innerHTML=e,this.element.parentNode.insertBefore(n,this.element.nextSibling),n.setAttribute("data-uss-tooltip-content-id",this.key),this.element.removeAttribute("data-uss-tooltip-content"),n}cannotHaveButtonsOrLinks(){const t=this.tooltip.querySelectorAll("button"),e=this.tooltip.querySelectorAll("a"),n=this.tooltip.querySelectorAll("input"),o=this.tooltip.querySelectorAll("[tabindex]");if(t.length||e.length||n.length||o.length)throw new Error("El tooltip no puede contener botones, inputs, links ni nada que lleve tabindex. Evaluar otras soluciones")}buildArrow(){this.arrow=document.createElement("div"),this.arrow.setAttribute("data-uss-tooltip-arrow",this.key),this.tooltip.appendChild(this.arrow)}getAntiOverflowStrategy(){return this.autoPlacement?it:lt}updateTooltipPosition(){at(this.element,this.tooltip,{placement:this.placement,middleware:[L(12),this.getAntiOverflowStrategy()({padding:10}),rt({padding:this.shiftPadding}),st({element:this.arrow})]}).then((({x:t,y:e,placement:n,middlewareData:o})=>{this.positionTooltip({x:t,y:e}),this.positionArrow(o.arrow,n)}))}positionTooltip({x:t,y:e}){Object.assign(this.tooltip.style,{left:`${t}px`,top:`${e}px`})}positionArrow({x:t,y:e},n){const o=this.getStaticSide(n);Object.assign(this.arrow.style,this.getArrowStyle(t,e,o,n))}getStaticSide(t){return{top:"bottom",right:"left",bottom:"top",left:"right"}[t.split("-")[0]]}getArrowStyle(t,e,n,o){const i=o.split("-")[0];return{left:t?`${t}px`:"auto",top:e?`${e}px`:"auto",right:"",bottom:"",[n]:"-4px",...this.getArrowBorderStyle(i)}}getArrowBorderStyle(t){return{top:{borderTop:"none",borderRight:"inherit",borderBottom:"inherit",borderLeft:"none"},bottom:{borderTop:"inherit",borderRight:"none",borderBottom:"none",borderLeft:"inherit"},left:{borderTop:"inherit",borderRight:"inherit",borderBottom:"none",borderLeft:"none"},right:{borderTop:"none",borderRight:"none",borderBottom:"inherit",borderLeft:"inherit"}}[t]}setEventListeners(){this.element.addEventListener("mouseenter",this.handleMouseEnter.bind(this)),this.element.addEventListener("mouseleave",this.handleMouseLeave.bind(this)),this.element.addEventListener("focus",(()=>this.showTooltip(!0))),this.element.addEventListener("blur",(()=>this.hideTooltip())),this.element.addEventListener("keydown",this.handleKeydown.bind(this))}handleMouseEnter(){this.isOverTrigger=!0,setTimeout((()=>{this.isOverTrigger&&this.showTooltip()}),this.delay)}handleMouseLeave(){this.isOverTrigger=!1,setTimeout((()=>{this.isOverTrigger||this.hideTooltip()}),this.delay/2)}handleKeydown(t){["Escape","Enter"," "].includes(t.key)&&this.hideTooltip()}showTooltip(t=!1){t||(this.tooltip.addEventListener("mouseenter",(()=>{this.isOverTrigger=!0})),this.tooltip.addEventListener("mouseleave",(()=>{this.isOverTrigger=!1,setTimeout((()=>{this.isOverTrigger||this.hideTooltip()}),this.delay/4)})),setTimeout((()=>{this.isOverTrigger||this.hideTooltip()}),this.delay/4),this.tooltip.addEventListener("mouseenter",(()=>{this.isOverTrigger=!0})),this.tooltip.addEventListener("mouseleave",(()=>{this.isOverTrigger=!1})),setTimeout((()=>{this.isOverTrigger||this.hideTooltip()}),this.delay/4)),document.addEventListener("keydown",this.hideOnEscape.bind(this)),document.body.append(this.tooltip),this.cleanup=ot(this.element,this.tooltip,(()=>this.updateTooltipPosition())),this.isOpen=!0}hideTooltip(t=!1){document.removeEventListener("keydown",this.hideOnEscape.bind(this)),this.tooltip&&this.tooltip.remove(),this.isOpen=!1,t||this.cleanup()}hideOnEscape(t){"Escape"===t.key&&this.hideTooltip()}}export{ct as Tooltip};
//# sourceMappingURL=Tooltip.js.map