Files
vacuum-wall/webui/static/hoover/helpers.js
T
mteehan 633505e7dc refactor: modernize frontend with hoover framework components and docs
- Add quick modal, table, service status, and confirmation dialog components
- Refactor all pages (certs, dhcp, proxy, etc.) to use new component patterns
- Introduce refactor load utility and render guard for consistent UX
- Add hoover documentation and update AGENTS.md, architecture, overview
2026-06-21 04:29:27 +00:00

69 lines
1.6 KiB
JavaScript

/**
* Hoover — helpers.js
*
* Shared utilities: text escaping, attribute escaping, DOM value helpers,
* zone parsing, form utilities.
*/
/**
* Escape text for safe HTML output.
* Appends the string to a temporary div and reads innerHTML,
* which safely escapes all HTML special characters.
*/
export function esc(s) {
const d = document.createElement('div');
d.append(String(s ?? ''));
return d.innerHTML;
}
/**
* Escape a string for safe use in HTML attributes.
*/
export function att_esc(s) {
return String(s ?? '')
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
/**
* URL-encode a string.
*/
export const enc = encodeURIComponent;
/**
* Get the value of a DOM element by ID.
*/
export function $val(id) {
return document.getElementById(id)?.value;
}
/**
* Parse zone data from various API response shapes into a flat string array.
*/
export function parseZones(data) {
let z = data?.active || data?.zones || [];
if (typeof z === 'object' && !Array.isArray(z))
z = Object.values(z).map(i => i?.name || i);
return Array.isArray(z) ? z : [];
}
/**
* Trigger a browser file download from a Blob.
*
* @param {Blob} blob
* @param {string} filename
*/
export function downloadBlob(blob, filename) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}