Files
espurna/code/html/src/template.mjs
Maxim Prokhorov 289fea4f3d webui: separate sources per module
* migrate to esbuild from terser, since now js source needs bundling
* (temporary?) hijack inline-source object handling to inline html files
* get rid of legacy removeIf comments in source in favour of explicit
  removal either through bundling tree-shaking and / or manual module-*
  class element removal w/ jsdom
* get rid of multi-layered custom checkboxes in favour of
  `appearance: none` directly styling the `input` elem
  also removes scripting part that was supposed to adjust for=... labels
* update to html-minifier-terser, up-to-date html-minifier fork
* update to iro.js 5.5.x, using npm to manage dependencies
* build script can now update resulting html output
  (called after bundling stage, before minification)
2024-06-22 19:49:59 +03:00

95 lines
2.5 KiB
JavaScript

// Generic parts of the HTML are placed into <template> container, which requires
// us to 'import' it into the currently loaded page to actually use it.
// (and notice that document.querySelector(...) won't be able to read inside of these)
import {
setInputValue,
setOriginalsFromValuesForNode,
setSelectValue,
setSpanValue,
initSelect,
initEnumerableSelect,
onGroupSettingsDel,
} from './settings.mjs';
import { moreElem } from './core.mjs';
function moreParent(event) {
moreElem(event.target.parentElement.parentElement);
}
/**
* @returns {HTMLElement}
*/
export function loadTemplate(name) {
let template = document.getElementById(`template-${name}`);
return document.importNode(template.content, true);
}
export function loadConfigTemplate(id) {
let template = loadTemplate(id);
for (let elem of template.querySelectorAll("input,select")) {
elem.dataset["settingsGroupElement"] = "true";
}
for (let elem of template.querySelectorAll("button.button-del-settings-group")) {
elem.addEventListener("click", onGroupSettingsDel);
}
for (let elem of template.querySelectorAll("button.button-more-parent")) {
elem.addEventListener("click", moreParent);
}
for (let elem of template.querySelectorAll("select.enumerable")) {
initEnumerableSelect(elem, initSelect);
}
return template;
}
/**
* @param {HTMLElement} line
* @param {number} id
* @param {any} cfg
*/
export function fillTemplateLineFromCfg(line, id, cfg) {
let local = {"template-id": id};
if (cfg === undefined) {
cfg = {};
}
Object.assign(local, cfg);
cfg = local;
for (let elem of line.querySelectorAll("input,select,span")) {
let key = elem.name || elem.dataset.key;
if (key && (key in cfg)) {
switch (elem.tagName) {
case "INPUT":
setInputValue(elem, cfg[key]);
break;
case "SELECT":
setSelectValue(elem, cfg[key]);
break;
case "SPAN":
setSpanValue(elem, cfg[key]);
break;
}
}
}
setOriginalsFromValuesForNode(line);
}
export function mergeTemplate(target, template) {
for (let child of Array.from(template.children)) {
target.appendChild(child);
}
}
export function addFromTemplate(container, template, cfg) {
const line = loadConfigTemplate(template);
fillTemplateLineFromCfg(line, container.childElementCount, cfg);
mergeTemplate(container, line);
}