Files
jinniu-daikuan/public/assets/fusion.js
T

159 lines
6.1 KiB
JavaScript

(function () {
var THEME_KEY = "jinniu-theme";
var root = function () { return document.querySelector("[data-variant-root]"); };
function currentTheme() {
var r = root();
if (r && r.getAttribute("data-theme") === "night") return "night";
return "day";
}
function applyLogos(theme) {
var src = theme === "night" ? "assets/logo-night.png" : "assets/logo-day.png";
document.querySelectorAll("img[data-brand-logo]").forEach(function (img) { img.src = src; });
}
window.setFusionTheme = function (next, animate) {
if (next !== "day" && next !== "night") return;
var r = root();
if (!r) return;
var reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (animate !== false && !reduce) {
r.setAttribute("data-theme-anim", "");
setTimeout(function () { r.removeAttribute("data-theme-anim"); }, 240);
}
if (next === "night") r.setAttribute("data-theme", "night");
else r.removeAttribute("data-theme");
try { localStorage.setItem(THEME_KEY, next); } catch (e) {}
r.querySelectorAll("[data-theme-btn]").forEach(function (b) {
b.setAttribute("aria-pressed", String(b.getAttribute("data-theme-btn") === next));
});
applyLogos(next);
};
document.addEventListener("click", function (e) {
var btn = e.target.closest("[data-theme-btn]");
if (btn) {
e.preventDefault();
window.setFusionTheme(btn.getAttribute("data-theme-btn"), true);
return;
}
var sw = e.target.closest("[data-switch]");
if (sw && !sw.disabled && sw.getAttribute("aria-disabled") !== "true") {
var on = sw.getAttribute("aria-checked") !== "true";
sw.setAttribute("aria-checked", on ? "true" : "false");
var bind = sw.getAttribute("data-bind-check");
if (bind) {
var cb = document.getElementById(bind);
if (cb) cb.checked = on;
}
}
});
var toastWrap = document.getElementById("fusion-toast-wrap");
window.fusionToast = function (msg) {
if (!toastWrap) return;
toastWrap.innerHTML = '<div class="toast" data-hidden role="status"><span class="tick" aria-hidden="true">✓</span><span></span></div>';
var t = toastWrap.querySelector(".toast");
t.querySelector("span:last-child").textContent = msg;
requestAnimationFrame(function () {
requestAnimationFrame(function () { t.removeAttribute("data-hidden"); });
});
clearTimeout(window.fusionToast._t);
window.fusionToast._t = setTimeout(function () { t.setAttribute("data-hidden", ""); }, 2600);
t.addEventListener("mouseenter", function () { clearTimeout(window.fusionToast._t); });
t.addEventListener("mouseleave", function () {
window.fusionToast._t = setTimeout(function () { t.setAttribute("data-hidden", ""); }, 2600);
});
};
function rowChecks(scope) {
var table = scope || document;
return Array.prototype.slice.call(table.querySelectorAll("tbody input.cid, tbody input[data-row-check]"));
}
window.syncCheckAll = function (table) {
table = table || document.querySelector(".table-card table, .table-wrap table");
if (!table) return;
var all = table.querySelector("[data-check-all]");
if (!all) return;
var boxes = rowChecks(table);
var n = boxes.filter(function (x) { return x.checked; }).length;
all.checked = boxes.length > 0 && n === boxes.length;
all.indeterminate = n > 0 && n < boxes.length;
boxes.forEach(function (x) {
var tr = x.closest("tr");
if (tr) {
if (x.checked) tr.setAttribute("data-checked", "");
else tr.removeAttribute("data-checked");
}
});
var bar = document.querySelector("[data-batch-slot]");
if (bar) bar.hidden = n === 0;
var countEl = document.querySelector("[data-batch-count]");
if (countEl) countEl.textContent = String(n);
};
document.addEventListener("change", function (e) {
var all = e.target.closest("[data-check-all]");
if (all) {
var table = all.closest("table");
rowChecks(table).forEach(function (x) { x.checked = all.checked; });
window.syncCheckAll(table);
return;
}
if (e.target.matches("input.cid, input[data-row-check]")) {
window.syncCheckAll(e.target.closest("table"));
}
});
document.addEventListener("pointerdown", function (e) {
var hold = e.target.closest(".hold-btn");
if (!hold || hold.disabled) return;
hold.setAttribute("data-holding", "");
hold._holdTimer = setTimeout(function () {
hold.removeAttribute("data-holding");
var href = hold.getAttribute("data-hold-href");
var form = hold.closest("form");
if (href) window.location.href = href;
else if (form) form.submit();
else hold.dispatchEvent(new CustomEvent("holdconfirm", { bubbles: true }));
}, 1400);
});
["pointerup", "pointerleave", "pointercancel"].forEach(function (ev) {
document.addEventListener(ev, function (e) {
var hold = e.target.closest ? e.target.closest(".hold-btn") : null;
document.querySelectorAll(".hold-btn[data-holding]").forEach(function (h) {
if (hold && h !== hold && ev === "pointerup") return;
h.removeAttribute("data-holding");
if (h._holdTimer) clearTimeout(h._holdTimer);
});
});
});
document.addEventListener("click", function (e) {
var openModal = e.target.closest("[data-open-modal]");
if (openModal) {
e.preventDefault();
var r = root();
if (r) r.setAttribute("data-modal-open", openModal.getAttribute("data-open-modal") || "import");
}
if (e.target.closest("[data-close-modal]")) {
var r2 = root();
if (r2) r2.removeAttribute("data-modal-open");
}
});
var shell = document.querySelector(".content-shell");
if (shell && !window.history.state) shell.scrollTop = 0;
document.addEventListener("DOMContentLoaded", function () {
window.setFusionTheme(currentTheme(), false);
document.querySelectorAll("table").forEach(function (t) { window.syncCheckAll(t); });
var flash = document.getElementById("flash-banner");
if (flash && flash.classList.contains("flash-ok")) {
window.fusionToast(flash.textContent.trim());
}
});
})();