33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
|
|
"use strict";
|
||
|
|
|
||
|
|
(function (global) {
|
||
|
|
function htmlEscape(value = "") {
|
||
|
|
return String(value ?? "").replace(/[&<>"']/g, ch => ({ "&": "&", "<": "<", ">": ">", "\"": """, "'": "'" }[ch] || ch));
|
||
|
|
}
|
||
|
|
|
||
|
|
function ensureStyle(id, cssText = "") {
|
||
|
|
if (!id) return null;
|
||
|
|
let style = document.getElementById(id);
|
||
|
|
if (style) return style;
|
||
|
|
style = document.createElement("style");
|
||
|
|
style.id = id;
|
||
|
|
style.textContent = cssText;
|
||
|
|
document.head.appendChild(style);
|
||
|
|
return style;
|
||
|
|
}
|
||
|
|
|
||
|
|
function createPanel({ id, className = "", html = "", after = null, parent = document.body } = {}) {
|
||
|
|
if (!id) return null;
|
||
|
|
let panel = document.getElementById(id);
|
||
|
|
if (panel) return panel;
|
||
|
|
panel = document.createElement("section");
|
||
|
|
panel.id = id;
|
||
|
|
if (className) panel.className = className;
|
||
|
|
panel.innerHTML = html;
|
||
|
|
if (after?.parentNode) after.parentNode.insertBefore(panel, after.nextSibling);
|
||
|
|
else parent.appendChild(panel);
|
||
|
|
return panel;
|
||
|
|
}
|
||
|
|
|
||
|
|
global.TarinaiUIHelpers = { htmlEscape, ensureStyle, createPanel };
|
||
|
|
})(typeof window !== "undefined" ? window : globalThis);
|