UNPKG

@finsweet/attributes-sliderdots

Version:

Create custom Slider Dots for a Slider.

2 lines (1 loc) 5.83 kB
"use strict";(()=>{var m="fs-attributes";var D="cmsattribute";var U="cmsslider";var d="sliderdots";var h=async(...t)=>{var r;let e=[];for(let o of t){let s=await((r=window.fsAttributes[o])==null?void 0:r.loading);e.push(s)}return e};var E=()=>{};function I(t,e,r,o){return t?(t.addEventListener(e,r,o),()=>t.removeEventListener(e,r,o)):E}var u={slider:"w-slider",slide:"w-slide",sliderMask:"w-slider-mask",sliderNav:"w-slider-nav",sliderDot:"w-slider-dot",activeSliderDot:"w-active"};var w=(t,e)=>(Array.isArray(e)||(e=[e]),e.map(o=>t.dispatchEvent(new Event(o,{bubbles:!0}))).every(o=>o));var v=(t,e=!0)=>t.cloneNode(e);var C=t=>{let e=t.split("-"),r=parseInt(e[e.length-1]);if(!isNaN(r))return r};var L=t=>t!=null;function B(t,e,r){var s;let o=window.fsAttributes[t];return o.destroy=r||E,(s=o.resolve)==null||s.call(o,e),e}var Q=`${m}-support`,J="https://cdn.jsdelivr.net/npm/@finsweet/attributes-support@1/support.js",N=async()=>{let{fsAttributes:t,location:e}=window,{host:r,searchParams:o}=new URL(e.href);t.support||(t.support={});let{support:s}=t;if(!r.includes("webflow.io")||!o.has(Q))return!1;if(s.import)return s.import;try{s.import=new Promise((n,c)=>{let i=document.createElement("script");i.src=J,i.onload=()=>n(!0),i.onerror=c,document.head.append(i)})}catch(n){return!1}return s.import};var f=t=>e=>`${t}${e?`-${e}`:""}`,b=t=>{let e=(o,s,n)=>{let c=t[o],{key:i,values:a}=c,l;if(!s)return`[${i}]`;let p=a==null?void 0:a[s];typeof p=="string"?l=p:l=p(n&&"instanceIndex"in n?n.instanceIndex:void 0);let T=n&&"caseInsensitive"in n&&n.caseInsensitive?"i":"";if(!(n!=null&&n.operator))return`[${i}="${l}"${T}]`;switch(n.operator){case"prefixed":return`[${i}^="${l}"${T}]`;case"suffixed":return`[${i}$="${l}"${T}]`;case"contains":return`[${i}*="${l}"${T}]`}};function r(o,s){let n=e("element",o,s),c=(s==null?void 0:s.scope)||document;return s!=null&&s.all?[...c.querySelectorAll(n)]:c.querySelector(n)}return[e,r]};var A={preventLoad:{key:`${m}-preventload`},debugMode:{key:`${m}-debug`},src:{key:"src",values:{finsweet:"@finsweet/attributes"}},dev:{key:`${m}-dev`}},[g,$t]=b(A);var K=t=>{let{currentScript:e}=document,r={};if(!e)return{attributes:r,preventsLoad:!1};let s={preventsLoad:typeof e.getAttribute(A.preventLoad.key)=="string",attributes:r};for(let n in t){let c=e.getAttribute(t[n]);s.attributes[n]=c}return s};var M=({scriptAttributes:t,attributeKey:e,version:r,init:o})=>{var i;Z(),(i=window.fsAttributes)[e]||(i[e]={});let{preventsLoad:s,attributes:n}=K(t),c=window.fsAttributes[e];c.version=r,c.init=o,s||(window.Webflow||(window.Webflow=[]),window.Webflow.push(()=>o(n)))},Z=()=>{let t=tt();if(window.fsAttributes&&!Array.isArray(window.fsAttributes)){V(window.fsAttributes,t);return}let e={cms:{},push(...r){var o,s;for(let[n,c]of r)(s=(o=this[n])==null?void 0:o.loading)==null||s.then(c)},destroy(){var r,o;for(let s of t)(o=(r=window.fsAttributes[s])==null?void 0:r.destroy)==null||o.call(r)}};V(e,t),et(e),window.fsAttributes=e,window.FsAttributes=window.fsAttributes,N()},tt=()=>{let t=g("src","finsweet",{operator:"contains"}),e=g("dev");return[...document.querySelectorAll(`script${t}, script${e}`)].reduce((s,n)=>{var i;let c=n.getAttribute(A.dev.key)||((i=n.src.match(/[\w-. ]+(?=(\.js)$)/))==null?void 0:i[0]);return c&&!s.includes(c)&&s.push(c),s},[])},V=(t,e)=>{for(let r of e){if(t[r])continue;t[r]={};let o=t[r];o.loading=new Promise(s=>{o.resolve=n=>{s(n),delete o.resolve}})}},et=t=>{let e=Array.isArray(window.fsAttributes)?window.fsAttributes:[];t.push(...e)};var O="1.8.0";var k=(t,e)=>{let r=t.getAttribute(e);return r?C(r):void 0};var x=`fs-${d}`,rt="slider",st="content",nt="slider-nav",it="active",ct="remove",S={element:{key:`${x}-element`,values:{slider:f(rt),content:f(st),sliderNav:f(nt)}},active:{key:`${x}-${it}`},remove:{key:`${x}-${ct}`,values:{true:"true"}}},[_,R]=b(S),P=`${x}_active`;var $=(t,e)=>I(t,"click",({target:o})=>{if(!(o instanceof Element))return;let s=o.closest(_("element","content",{operator:"prefixed"}));if(!s)return;let{dot:n}=e.find(({customDot:c})=>c===s)||{};n&&w(n,"click")});var F="role";var Y="tabindex",H="aria-label";var q="aria-pressed";var y=({dot:t,customDot:e},r)=>{let o=t.classList.contains(u.activeSliderDot);e.classList[o?"add":"remove"](r);for(let s of[H,q,F,Y]){let n=t.getAttribute(s);n?e.setAttribute(s,n):e.removeAttribute(s)}};var j=(t,e,r)=>{let o=c=>{for(let{target:i}of c){if(!(i instanceof HTMLElement))continue;let a=e.find(({dot:l})=>l===i);!a||y(a,r)}},s={subtree:!0,attributes:!0,attributeFilter:["class"]},n=new MutationObserver(o);return n.observe(t,s),n};var{remove:{key:at,values:lt}}=S,{slide:ut,sliderDot:pt}=u,G=(t,e)=>{let r=[],o=t.querySelectorAll(`.${ut}`),s=t.querySelectorAll(`.${pt}`);return o.forEach((n,c)=>{let i=s[c];if(!i)return;i.style.display="none";let a=R("content",{operator:"prefixed",scope:n});if(!a)return;let p=a.getAttribute(at)===lt.true?a:v(a);e.appendChild(p),r.push({dot:i,customDot:p})}),r};var X=t=>new Promise(e=>{let r=s=>{for(let{addedNodes:n}of s)for(let c of n)if(!!(c instanceof Element&&c.closest(`.${u.sliderDot}`))){e(void 0),o.disconnect();return}},o=new MutationObserver(r);o.observe(t,{childList:!0})});var{element:{key:mt},remove:{key:dt,values:Et},active:{key:At}}=S,W=async t=>{let e=k(t,mt),r=t.querySelector(`.${u.sliderNav}`),o=R("sliderNav",{instanceIndex:e})||r;if(!r||!o)return;let s=window.fsAttributes[U];s&&await Promise.all([s.loading,X(r)]);let n=t.getAttribute(At)||P;o.getAttribute(dt)===Et.true&&(o.innerHTML="");let i=G(t,o);for(let p of i)y(p,n);let a=j(r,i,n),l=$(o,i);return()=>{a.disconnect(),l()}};var z=async()=>{await h(D);let t=[...document.querySelectorAll(`.${u.slider}${_("element","slider",{operator:"prefixed"})}`)],e=(await Promise.all(t.map(W))).filter(L);return B(d,t,()=>{for(let r of e)r()})};M({init:z,version:O,attributeKey:d});})();