UNPKG

axentix

Version:

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

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