(() => {
"use strict";
const ATTR = "data-vars";
const DENSITY_KEY = "makeover-density";
const DENSITY_CHOICE = "data-density-choice";
const DENSITY = "data-density";
const DENSITIES = ["pointer", "touch"];
const chosen = () => {
try {
const value = localStorage.getItem(DENSITY_KEY) ?? "";
return DENSITIES.includes(value) ? value : "";
} catch {
return "";
}
};
const wear = (value) => {
const root = document.documentElement;
if (value) root.setAttribute(DENSITY, value);
else root.removeAttribute(DENSITY);
};
const show = (picker) => {
picker.value = chosen();
};
wear(chosen());
document.addEventListener("change", (event) => {
const picker = event.target;
if (!(picker instanceof Element) || !picker.hasAttribute(DENSITY_CHOICE)) return;
const value = DENSITIES.includes(picker.value) ? picker.value : "";
try {
if (value) localStorage.setItem(DENSITY_KEY, value);
else localStorage.removeItem(DENSITY_KEY);
} catch {
}
wear(value);
});
const set = new WeakMap();
const pairs = (text) => {
const found = new Map();
for (const part of text.split(";")) {
const at = part.indexOf(":");
if (at < 0) continue;
const name = part.slice(0, at).trim();
if (!name.startsWith("--")) continue;
found.set(name, part.slice(at + 1).trim());
}
return found;
};
const apply = (el) => {
const next = pairs(el.getAttribute(ATTR) ?? "");
for (const name of set.get(el) ?? []) {
if (!next.has(name)) el.style.removeProperty(name);
}
for (const [name, value] of next) el.style.setProperty(name, value);
set.set(el, [...next.keys()]);
};
const within = (node) => {
if (node.nodeType !== Node.ELEMENT_NODE) return;
if (node.hasAttribute(ATTR)) apply(node);
for (const el of node.querySelectorAll(`[${ATTR}]`)) apply(el);
if (node.hasAttribute(DENSITY_CHOICE)) show(node);
for (const el of node.querySelectorAll(`[${DENSITY_CHOICE}]`)) show(el);
};
new MutationObserver((records) => {
for (const record of records) {
if (record.type === "attributes") apply(record.target);
else record.addedNodes.forEach(within);
}
}).observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: [ATTR],
});
within(document.documentElement);
})();