ryde/source/logic/helpers/dom.ts
2026-03-06 08:37:53 +01:00

69 lines
1.2 KiB
TypeScript

namespace lib_dom
{
/**
* @author fenris
*/
export function clone(
element : Element,
options : {
context ?: Document;
} = {}
) : Node
{
options = Object.assign(
{
"context": document,
},
options
);
return options.context.importNode(element, true);
}
/**
* @author fenris
*/
export function request(
id : string,
options : {
context ?: Document;
arguments ?: (null | Record<string, any>);
} = {}
) : DocumentFragment
{
options = Object.assign(
{
"context": document,
"arguments": null,
},
options
);
const template : (null | Element) = document.querySelector("template#" + id);
if (template === null) {
throw (new Error("template not found: " + id));
}
else {
const fragment : DocumentFragment = (
clone(
template["content"],
{
"context": options.context,
}
) as DocumentFragment
);
if (options.arguments === null) {
// do nothing
}
else {
for (let index : int = 0; index < fragment.children.length; index += 1) {
const element : Element = fragment.children[index];
element.innerHTML = lib_string.coin(element.innerHTML, options.arguments);
}
}
return fragment;
}
}
}