260 lines
5.4 KiB
TypeScript
260 lines
5.4 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 date_format(
|
|
date : lib_plankton.pit.type_date
|
|
) : string
|
|
{
|
|
return lib_plankton.string.coin(
|
|
"{{year}}-{{month}}-{{day}}",
|
|
{
|
|
"year": date.year.toFixed(0).padStart(4, "0"),
|
|
"month": date.month.toFixed(0).padStart(2, "0"),
|
|
"day": date.day.toFixed(0).padStart(2, "0"),
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* @todo timezone
|
|
*/
|
|
export function time_format(
|
|
time : lib_plankton.pit.type_time
|
|
) : string
|
|
{
|
|
return lib_plankton.string.coin(
|
|
"{{hour}}:{{minute}}",
|
|
{
|
|
"hour": time.hour.toFixed(0).padStart(2, "0"),
|
|
"minute": time.minute.toFixed(0).padStart(2, "0"),
|
|
"second": time.second.toFixed(0).padStart(2, "0"),
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* @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": date_format(datetime.date),
|
|
"macro_time": (
|
|
(datetime.time === null)
|
|
?
|
|
""
|
|
:
|
|
lib_plankton.string.coin(
|
|
",{{time}}",
|
|
{
|
|
"time": time_format(datetime.time),
|
|
}
|
|
)
|
|
),
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
export function timespan_format(
|
|
from : lib_plankton.pit.type_datetime,
|
|
to : (null | lib_plankton.pit.type_datetime)
|
|
) : string
|
|
{
|
|
let result : string = "";
|
|
result += datetime_format(from);
|
|
if (to === null) {
|
|
// do nothing
|
|
}
|
|
else {
|
|
result += " - ";
|
|
if (
|
|
(from.date.year === to.date.year)
|
|
&&
|
|
(from.date.month === to.date.month)
|
|
&&
|
|
(from.date.day === to.date.day)
|
|
) {
|
|
// only time
|
|
result += time_format(to.time);
|
|
}
|
|
else {
|
|
result += datetime_format(to);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
*/
|
|
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()
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|