frontend-dali/source/logic/helpers.ts
2024-09-30 20:20:14 +02:00

152 lines
3.5 KiB
TypeScript

/**
*/
namespace _zeitbild.frontend_web.helpers
{
/**
*/
var _template_cache : Record<string, string> = {};
/**
* @todo caching
*/
export async function template_coin(
name : string,
data : Record<string, string>
) : Promise<string>
{
let content : string;
if (! (name in _template_cache)) {
content = (
(
await lib_plankton.file.read(
lib_plankton.string.coin(
"templates/{{name}}.html.tpl",
{
"name": name,
}
)
)
)
.toString()
);
_template_cache[name] = content;
}
else {
content = _template_cache[name];
}
return Promise.resolve<string>(
lib_plankton.string.coin(
content,
data
)
);
}
/**
* @todo outsource
*/
export async function promise_row<type_result>(
members : Array<
() => Promise<type_result>
>
) : Promise<
Array<
type_result
>
>
{
let results : Array<type_result> = [];
for await (const member of members) {
results.push(await member());
}
return Promise.resolve<Array<type_result>>(results);
}
/**
*/
export function input_access_level(
) : lib_plankton.zoo_input.interface_input<_zeitbild.frontend_web.type.enum_access_level>
{
return (
new lib_plankton.zoo_input.class_input_wrapped<
/*("none" | "view" | "edit" | "admin")*/string,
_zeitbild.frontend_web.type.enum_access_level
>(
new lib_plankton.zoo_input.class_input_selection(
[
{
"value": "none",
"label": lib_plankton.translate.get("access_level.none"),
},
{
"value": "view",
"label": lib_plankton.translate.get("access_level.view")
},
{
"value": "edit",
"label": lib_plankton.translate.get("access_level.edit")
},
{
"value": "admin",
"label": lib_plankton.translate.get("access_level.admin")
},
]
),
(raw) => {
switch (raw) {
case "none": return _zeitbild.frontend_web.type.enum_access_level.none;
case "view": return _zeitbild.frontend_web.type.enum_access_level.view;
case "edit": return _zeitbild.frontend_web.type.enum_access_level.edit;
case "admin": return _zeitbild.frontend_web.type.enum_access_level.admin;
}
},
(access_level) => {
switch (access_level) {
case _zeitbild.frontend_web.type.enum_access_level.none: return "none";
case _zeitbild.frontend_web.type.enum_access_level.view: return "view";
case _zeitbild.frontend_web.type.enum_access_level.edit: return "edit";
case _zeitbild.frontend_web.type.enum_access_level.admin: return "admin";
}
},
)
);
}
/**
*/
export async function input_attributed_access(
) : Promise<lib_plankton.zoo_input.class_input_hashmap<_zeitbild.frontend_web.type.user_id, _zeitbild.frontend_web.type.enum_access_level>>
{
const users : Array<{id : _zeitbild.frontend_web.type.user_id; name : string;}> = await _zeitbild.frontend_web.backend.user_list(
);
return Promise.resolve(
new lib_plankton.zoo_input.class_input_hashmap<_zeitbild.frontend_web.type.user_id, _zeitbild.frontend_web.type.enum_access_level>(
// hash_key
(user_id) => user_id.toFixed(0),
// key_input_factory
() => new lib_plankton.zoo_input.class_input_wrapped<string, int>(
new lib_plankton.zoo_input.class_input_selection(
users
.map(
(user) => ({
"value": user.id.toFixed(0),
"label": user.name,
})
)
),
x => parseInt(x),
x => x.toFixed(0)
),
// value_input_factory
() => input_access_level()
)
);
}
}