106 lines
3.6 KiB
JavaScript
106 lines
3.6 KiB
JavaScript
(() => {
|
|
const select = document.getElementById("printer-switcher");
|
|
const openButton = document.getElementById("printer-switcher-open");
|
|
if (!select || !openButton) {
|
|
return;
|
|
}
|
|
|
|
const session = window.__KA_SESSION__ || {};
|
|
const currentPath = window.location.pathname || "/";
|
|
const currentQuery = new URLSearchParams(window.location.search || "");
|
|
|
|
function currentPrinterID() {
|
|
const printerPath = currentPath.match(/^\/printers\/([^/]+)/);
|
|
if (printerPath && printerPath[1]) {
|
|
return decodeURIComponent(printerPath[1]);
|
|
}
|
|
const queryPrinterID = (currentQuery.get("printer_id") || "").trim();
|
|
if (queryPrinterID) {
|
|
return queryPrinterID;
|
|
}
|
|
const queryPrinter = (currentQuery.get("printer") || "").trim();
|
|
if (queryPrinter) {
|
|
return queryPrinter;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function targetForPrinter(printerID) {
|
|
const encoded = encodeURIComponent(printerID);
|
|
if (currentPath === "/dashboard" || currentPath === "/" || currentPath === "/admin" || currentPath === "/account" || currentPath === "/users" || currentPath === "/audit" || currentPath === "/install") {
|
|
return `/printers/${encoded}`;
|
|
}
|
|
if (/^\/printers\/[^/]+\/settings\/?$/.test(currentPath)) {
|
|
return `/printers/${encoded}/settings`;
|
|
}
|
|
if (/^\/printers\/[^/]+\/agent\/?$/.test(currentPath)) {
|
|
return `/printers/${encoded}/agent`;
|
|
}
|
|
if (/^\/printers\/[^/]+\/ui\/?/.test(currentPath)) {
|
|
return `/printers/${encoded}/ui/`;
|
|
}
|
|
if (/^\/printers\/[^/]+$/.test(currentPath)) {
|
|
return `/printers/${encoded}`;
|
|
}
|
|
return `/printers/${encoded}`;
|
|
}
|
|
|
|
function openSelectedPrinter() {
|
|
const printerID = select.value.trim();
|
|
if (!printerID) {
|
|
return;
|
|
}
|
|
window.location.assign(targetForPrinter(printerID));
|
|
}
|
|
|
|
async function loadPrinters() {
|
|
const response = await fetch("/api/printers", { credentials: "same-origin" });
|
|
if (!response.ok) {
|
|
select.innerHTML = '<option value="">Printers unavailable</option>';
|
|
select.disabled = true;
|
|
openButton.disabled = true;
|
|
return;
|
|
}
|
|
const payload = await response.json();
|
|
const printers = Array.isArray(payload.printers) ? payload.printers : [];
|
|
if (!printers.length) {
|
|
select.innerHTML = '<option value="">No printers</option>';
|
|
select.disabled = true;
|
|
openButton.disabled = true;
|
|
return;
|
|
}
|
|
const activePrinter = currentPrinterID();
|
|
select.innerHTML = printers.map((printer) => {
|
|
const printerID = String(printer.printer_id || "");
|
|
const settings = printer.settings || {};
|
|
const label = String(settings.display_name || printerID || "Printer");
|
|
const selected = printerID === activePrinter ? " selected" : "";
|
|
return `<option value="${escapeHTML(printerID)}"${selected}>${escapeHTML(label)}</option>`;
|
|
}).join("");
|
|
if (activePrinter && printers.some((printer) => String(printer.printer_id || "") === activePrinter)) {
|
|
select.value = activePrinter;
|
|
}
|
|
select.disabled = false;
|
|
openButton.disabled = !select.value;
|
|
}
|
|
|
|
function escapeHTML(value) {
|
|
return String(value || "")
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """);
|
|
}
|
|
|
|
select.addEventListener("change", () => {
|
|
openButton.disabled = !select.value;
|
|
});
|
|
openButton.addEventListener("click", openSelectedPrinter);
|
|
|
|
loadPrinters().catch(() => {
|
|
select.innerHTML = '<option value="">Printers unavailable</option>';
|
|
select.disabled = true;
|
|
openButton.disabled = true;
|
|
});
|
|
})();
|