frontend-dali/source/logic/helpers.ts
2024-10-02 18:26:33 +02:00

234 lines
5.1 KiB
TypeScript

/**
*/
namespace _zeitbild.frontend_web.helpers
{
/**
*/
var _template_cache : Record<string, string> = {};
/**
*/
export async function template_coin(
group : string,
name : string,
data : Record<string, string>
) : Promise<string>
{
let content : string;
const key : string = lib_plankton.string.coin(
"{{group}}/{{name}}",
{
"group": group,
"name": name,
}
);
if (! (key in _template_cache)) {
content = (
(
await lib_plankton.file.read(
lib_plankton.string.coin(
"templates/{{group}}/{{name}}.html.tpl",
{
"group": group,
"name": name,
}
)
)
)
.toString()
);
_template_cache[key] = content;
}
else {
content = _template_cache[key];
}
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);
}
/**
* @todo timezone
*/
export function datetime_format(
datetime : (null | lib_plankton.pit.type_datetime)
) : string
{
if (datetime === null) {
return "-";
}
else {
return lib_plankton.string.coin(
"{{date}}{{macro_time}}",
{
"date": lib_plankton.string.coin(
"{{year}}-{{month}}-{{day}}",
{
"year": datetime.date.year.toFixed(0).padStart(4, "0"),
"month": datetime.date.month.toFixed(0).padStart(2, "0"),
"day": datetime.date.day.toFixed(0).padStart(2, "0"),
}
),
"macro_time": (
(datetime.time === null)
?
""
:
lib_plankton.string.coin(
",{{time}}",
{
"time": lib_plankton.string.coin(
"{{hour}}:{{minute}}",
{
"hour": datetime.time.hour.toFixed(0).padStart(2, "0"),
"minute": datetime.time.minute.toFixed(0).padStart(2, "0"),
"second": datetime.time.second.toFixed(0).padStart(2, "0"),
}
),
}
)
),
}
);
}
}
/**
*/
export function timespan_format(
from : lib_plankton.pit.type_datetime,
to : (null | lib_plankton.pit.type_datetime)
) : string
{
return lib_plankton.string.coin(
"{{from}}{{macro_to}}",
{
"from": datetime_format(from),
"macro_to": (
(to === null)
?
""
:
lib_plankton.string.coin(
" - {{to}}",
{
"to": datetime_format(to),
}
)
),
}
);
}
/**
*/
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()
)
);
}
}