leitfaden-digitalisierung/source/helpers.ts
2025-05-19 21:18:37 +00:00

127 lines
2.2 KiB
TypeScript

namespace leitfaden
{
/**
*/
export async function promise_all_stable(
promises : Array<Promise<any>>
) : Promise<Array<any>>
{
const result : Array<any> = [];
for (const promise of promises) {
result.push(await promise);
}
return result;
}
/**
*/
let template_cache : lib_plankton.cache.type_subject<string>;
/**
*/
export async function render(
template_name : string,
arguments_ : Record<string, string>
) : Promise<string>
{
const template_content : string = await lib_plankton.cache.get<string>(
template_cache,
template_name,
null,
() => lib_plankton.file.read(
lib_plankton.string.coin(
"templates/{{name}}.html.tpl",
{
"name": template_name,
}
)
)
);
return lib_plankton.string.coin(
template_content,
arguments_
);
}
/**
*/
export async function templates_init(
): Promise<void>
{
template_cache = await lib_plankton.cache.make();
}
/**
*/
let tabs_count : int = 0;
/**
*/
export function tabs(
sections : Array<
{
name : string;
title : string;
content : string;
}
>
) : Promise<string>
{
tabs_count += 1;
const identifier : string = tabs_count.toFixed(0).padStart(4, "0");
return render(
"tabs",
{
"id": lib_plankton.string.coin(
"tabs-{{identifier}}",
{
"identifier": identifier,
}
),
"head": (
sections
.map(
(section, index) => lib_plankton.string.coin(
"<button class=\"tabs-head-entry\" onClick=\"tab_select('{{identifier}}',{{index}})\">{{title}}</buttom>",
{
"identifier": identifier,
"index": index.toFixed(0),
"title": section.title,
}
)
)
.join("\n")
),
"body": (
sections
.map(
(section, index) => lib_plankton.string.coin(
"<div class=\"tabs-body-entry\" id=\"tabs-body-entry-{{identifier}}-{{index}}\">{{content}}</div>",
{
"identifier": identifier,
"index": index.toFixed(0),
"content": section.content,
}
)
)
.join("\n")
),
"script": lib_plankton.string.coin(
"tab_select('{{identifier}}',0);",
{
"identifier": identifier,
}
),
}
);
}
}