UNPKG

axentix

Version:

Axentix is a framework mixing fully customizable components & utility-first classes, leaving the design choice to the developer.

3 lines (2 loc) 5.99 kB
(function(o,i){typeof exports=="object"&&typeof module!="undefined"?module.exports=i():typeof define=="function"&&define.amd?define(i):(o=typeof globalThis!="undefined"?globalThis:o||self,o.Scrollspy=i())})(this,function(){"use strict";var G=Object.defineProperty;var k=o=>{throw TypeError(o)};var J=(o,i,r)=>i in o?G(o,i,{enumerable:!0,configurable:!0,writable:!0,value:r}):o[i]=r;var C=(o,i,r)=>J(o,typeof i!="symbol"?i+"":i,r),b=(o,i,r)=>i.has(o)||k("Cannot "+r);var u=(o,i,r)=>(b(o,i,"read from private field"),r?r.call(o):i.get(o)),w=(o,i,r)=>i.has(o)?k("Cannot add the same private member more than once"):i instanceof WeakSet?i.add(o):i.set(o,r),f=(o,i,r,L)=>(b(o,i,"write to private field"),L?L.call(o,r):i.set(o,r),r),y=(o,i,r)=>(b(o,i,"access private method"),r);var m,h,x,d,a,I,T,q,M,B,S;const o=[],i={components:[],plugins:[],prefix:"ax",mode:""},r=s=>i.components.find(e=>e.name===s).class,L=()=>{const s=i.components.filter(t=>t.dataDetection),e=i.plugins.filter(t=>t.dataDetection);return[...s,...e].map(t=>t.name)},R=(s,e)=>{if(!s.name||!s.class){console.error(`[Axentix] Error registering ${e} : Missing required parameters.`);return}if(i[e].some(t=>t.name===s.name)){console.error(`[Axentix] Error registering ${e} : Already exist.`);return}s.autoInit&&(s.autoInit.selector=s.autoInit.selector+=":not(.no-axentix-init)"),i[e].push(s)},$=s=>{R(s,"components")},H=s=>s.replace(/[\w]([A-Z])/g,e=>e[0]+"-"+e[1]).toLowerCase(),N=(s,e="")=>{const t=H(s);return e?e+"-"+t:t},U=(s,e,t,n,l="")=>{const c=e[0].toUpperCase()+e.slice(1).toLowerCase();L().includes(c)&&t!=="Collapsible"&&c!=="Sidenav"&&(s[e]=r(c).getDefaultOptions());const p=l?l+"-"+e:e,g=O(s[e],t,n,p);if(!(Object.keys(g).length===0&&s.constructor===Object))return g},O=(s,e,t,n="")=>Object.keys(s).reduce((l,c)=>{if(typeof s[c]=="object"&&s[c]!==null){const p=U(s,c,e,t,n);p&&(l[c]=p)}else if(s[c]!==null){const p="data-"+e.toLowerCase()+"-"+N(c,n);if(t.hasAttribute(p)){const g=t.getAttribute(p);l[c]=typeof s[c]=="number"?Number(g):g,typeof s[c]=="boolean"&&(l[c]=g==="true")}}return l},{}),z=(s,e)=>{const t=Object.assign({},r(s).getDefaultOptions());return O(t,s,e)},F=()=>{document.querySelectorAll("[data-ax]").forEach(e=>{let t=e.dataset.ax;if(t=t[0].toUpperCase()+t.slice(1).toLowerCase(),!L().includes(t)){console.error(`[Axentix] Error: ${t} component doesn't exist. Did you forget to register him ?`,e);return}try{const n=r(t);new n(`#${e.id}`)}catch(n){console.error("[Axentix] Data: Unable to load "+t,n)}})},Y=()=>{try{new Axentix.Axentix("all")}catch(s){console.error("[Axentix] Unable to auto init.",s)}};document.addEventListener("DOMContentLoaded",()=>{document.documentElement.dataset.axentix&&Y(),F()});const D=(...s)=>s.reduce((e,t)=>{for(let n in t)e[n]=typeof t[n]=="object"&&t[n]!==null?D(e[n],t[n]):t[n];return e},{}),P=(s,e,t)=>D(r(s).getDefaultOptions(),z(s,t),e),A=(s,e,t)=>{const n=new CustomEvent("ax."+e,{detail:{},bubbles:!0});s.dispatchEvent(n)},W=s=>{const e=o.find(t=>t.type!=="Toast"&&"#"+t.instance.el.id===s);return e?e.instance:!1};class X{constructor(){C(this,"el")}removeListeners(){}setupListeners(){}setup(){}preventDbInstance(e){if(e&&W(e))throw new Error(`Instance already exist on ${e}`)}sync(){A(this.el,"component.sync"),this.removeListeners(),this.setupListeners()}reset(){A(this.el,"component.reset"),this.removeListeners(),this.setup()}destroy(){A(this.el,"component.destroy"),this.removeListeners();const e=o.findIndex(t=>t.instance.el.id===this.el.id);o.splice(e,1)}}const Z={offset:200,linkSelector:"a",classes:"active",auto:{enabled:!1,classes:"",selector:""}};class E extends X{constructor(t,n){super();w(this,a);C(this,"options");w(this,m);w(this,h);w(this,x);w(this,d);try{this.preventDbInstance(t),o.push({type:"ScrollSpy",instance:this}),this.el=document.querySelector(t),this.options=P("ScrollSpy",n,this.el),this.setup()}catch(l){console.error("[Axentix] ScrollSpy init error",l)}}setup(){A(this.el,"scrollspy.setup"),this.options.auto.enabled?y(this,a,T).call(this):y(this,a,I).call(this),typeof this.options.classes=="string"&&(this.options.classes=this.options.classes.split(" ")),f(this,m,""),this.setupListeners(),y(this,a,S).call(this)}setupListeners(){f(this,h,y(this,a,S).bind(this)),window.addEventListener("scroll",u(this,h)),window.addEventListener("resize",u(this,h))}removeListeners(){window.removeEventListener("scroll",u(this,h)),window.removeEventListener("resize",u(this,h)),f(this,h,void 0)}}return m=new WeakMap,h=new WeakMap,x=new WeakMap,d=new WeakMap,a=new WeakSet,I=function(){f(this,x,Array.from(this.el.querySelectorAll(this.options.linkSelector))),f(this,d,u(this,x).map(t=>document.querySelector(t.getAttribute("href"))))},T=function(){f(this,d,Array.from(document.querySelectorAll(this.options.auto.selector))),f(this,x,u(this,d).map(t=>{const n=document.createElement("a");return n.className=this.options.auto.classes,n.setAttribute("href","#"+t.id),n.innerHTML=t.innerHTML,this.el.appendChild(n),n}))},q=function(){const t=window.scrollY,n=window.scrollX,l=window.innerWidth,c=window.innerHeight,p=t+this.options.offset;return c+t>=document.body.offsetHeight-2?u(this,d)[u(this,d).length-1]:u(this,d).find(g=>{const v=g.getBoundingClientRect();return v.top+t>=t&&v.left+n>=n&&v.right<=l&&v.bottom<=c&&v.top+t<=p})},M=function(){u(this,m)&&this.options.classes.forEach(t=>u(this,m).classList.remove(t))},B=function(){const t=window.scrollY;return u(this,d).reduce((n,l)=>{const c=l.getBoundingClientRect().top+t,p=n.getBoundingClientRect().top+t;return c>t+this.options.offset?n:Math.abs(c-t)<Math.abs(p-t)?l:n})},S=function(){let t=y(this,a,q).call(this);t||(t=y(this,a,B).call(this));const n=u(this,x).find(l=>l.getAttribute("href").split("#")[1]===t.id);n!==u(this,m)&&(A(this.el,"scrollspy.update"),y(this,a,M).call(this),this.options.classes.forEach(l=>n.classList.add(l)),f(this,m,n))},C(E,"getDefaultOptions",()=>Z),$({class:E,name:"ScrollSpy",dataDetection:!0,autoInit:{enabled:!0,selector:".scrollspy"}}),E});