frontend-dali/source/helpers.ts

373 lines
7.3 KiB
TypeScript
Raw Normal View History

2025-10-23 23:16:11 +02:00
/*
This file is part of »dali«.
Copyright 2025 'kcf' <fenris@folksprak.org>
»dali« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»dali« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »dali«. If not, see <http://www.gnu.org/licenses/>.
*/
2024-09-12 00:02:12 +02:00
/**
*/
2025-09-25 17:54:20 +02:00
namespace _dali.helpers
2024-09-12 00:02:12 +02:00
{
/**
* @todo outsource
*/
function is_touch_device(
)
: boolean
{
return (
("ontouchstart" in window)
||
(navigator.maxTouchPoints > 0)
||
(navigator["msMaxTouchPoints"] > 0)
);
}
2024-09-12 00:02:12 +02:00
/**
*/
var _template_cache : Record<string, string> = {};
2025-10-17 00:10:28 +02:00
/**
*/
export function view_mode_determine(
mode_descriptor : string
)
: _dali.enum_view_mode
2025-10-17 00:10:28 +02:00
{
if (mode_descriptor === "auto")
{
return (
(window.innerWidth >= 1000)
2025-10-17 00:10:28 +02:00
?
_dali.enum_view_mode.week
:
_dali.enum_view_mode.list
);
}
else
{
return view_mode_decode(mode_descriptor);
}
}
/**
*/
export function view_kind_determine(
mode_descriptor : string
)
: _dali.enum_view_kind
{
if (mode_descriptor === "auto")
{
return (
is_touch_device()
?
_dali.enum_view_kind.touch
:
_dali.enum_view_kind.regular
);
}
else
{
return view_kind_decode(mode_descriptor);
}
}
2024-09-12 00:02:12 +02:00
/**
*/
export async function template_coin(
2024-10-01 18:28:04 +02:00
group : string,
2024-09-12 00:02:12 +02:00
name : string,
data : Record<string, string>
) : Promise<string>
{
let content : string;
2024-10-01 18:28:04 +02:00
const key : string = lib_plankton.string.coin(
"{{group}}/{{name}}",
{
"group": group,
"name": name,
}
);
if (! (key in _template_cache)) {
2024-09-12 00:02:12 +02:00
content = (
(
await lib_plankton.file.read(
lib_plankton.string.coin(
2024-10-01 18:28:04 +02:00
"templates/{{group}}/{{name}}.html.tpl",
2024-09-12 00:02:12 +02:00
{
2024-10-01 18:28:04 +02:00
"group": group,
2024-09-12 00:02:12 +02:00
"name": name,
}
)
)
)
.toString()
);
2024-10-01 18:28:04 +02:00
_template_cache[key] = content;
2024-09-12 00:02:12 +02:00
}
else {
2024-10-01 18:28:04 +02:00
content = _template_cache[key];
2024-09-12 00:02:12 +02:00
}
return Promise.resolve<string>(
lib_plankton.string.coin(
content,
data
)
);
}
2024-09-12 00:02:12 +02:00
/**
* @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);
}
2024-09-30 20:20:14 +02:00
/**
*/
export function input_access_level(
2025-10-17 00:10:28 +02:00
) : lib_plankton.zoo_input.interface_input<_dali.enum_access_level>
2024-09-30 20:20:14 +02:00
{
return (
new lib_plankton.zoo_input.class_input_wrapped<
/*("none" | "view" | "edit" | "admin")*/string,
2025-10-17 00:10:28 +02:00
_dali.enum_access_level
2024-09-30 20:20:14 +02:00
>(
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) {
2025-10-17 00:10:28 +02:00
case "none": return _dali.enum_access_level.none;
case "view": return _dali.enum_access_level.view;
case "edit": return _dali.enum_access_level.edit;
case "admin": return _dali.enum_access_level.admin;
2024-09-30 20:20:14 +02:00
}
},
(access_level) => {
switch (access_level) {
2025-10-17 00:10:28 +02:00
case _dali.enum_access_level.none: return "none";
case _dali.enum_access_level.view: return "view";
case _dali.enum_access_level.edit: return "edit";
case _dali.enum_access_level.admin: return "admin";
2024-09-30 20:20:14 +02:00
}
},
)
);
}
/**
*/
2025-10-23 11:35:57 +02:00
export function input_attributed_access_group(
groups : Array<{id : _dali.type_group_id; object : _dali.type_group_object;}>
)
: lib_plankton.zoo_input.class_input_hashmap<
_dali.type_group_id,
_dali.enum_access_level
>
{
return (
new lib_plankton.zoo_input.class_input_hashmap<_dali.type_group_id, _dali.enum_access_level>(
// hash_key
(group_id) => group_id.toFixed(0),
// key_input_factory
() => new lib_plankton.zoo_input.class_input_wrapped<string, int>(
new lib_plankton.zoo_input.class_input_selection(
groups
.map(
(group) => ({
"value": group.id.toFixed(0),
"label": group.object.label,
})
)
),
x => parseInt(x),
x => x.toFixed(0)
),
// value_input_factory
() => input_access_level()
)
);
}
/**
*/
export function input_attributed_access_user(
2025-10-17 00:10:28 +02:00
users : Array<{id : _dali.type_user_id; name : string;}>
)
: lib_plankton.zoo_input.class_input_hashmap<
_dali.type_user_id,
_dali.enum_access_level
>
2024-09-30 20:20:14 +02:00
{
2025-10-17 00:10:28 +02:00
return (
new lib_plankton.zoo_input.class_input_hashmap<_dali.type_user_id, _dali.enum_access_level>(
2024-09-30 20:20:14 +02:00
// 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()
)
);
}
/**
*/
export function datetime_input(
) : lib_plankton.zoo_input.interface_input<lib_plankton.pit.type_datetime>
{
return (
2025-09-25 17:54:20 +02:00
_dali.conf.get().misc.use_central_europe_specific_datetime_inputs
?
new lib_plankton.zoo_input.class_input_datetime_central_europe(
{
"label_date": lib_plankton.translate.get("common.date"),
"label_time": lib_plankton.translate.get("common.time"),
}
)
:
new lib_plankton.zoo_input.class_input_datetime(
{
"label_timezone_shift": lib_plankton.translate.get("common.timezone_shift"),
"label_date": lib_plankton.translate.get("common.date"),
"label_time": lib_plankton.translate.get("common.time"),
}
)
);
}
/**
*/
export function event_color(
hue : float
)
:
string
{
return lib_plankton.color.output_hex(
lib_plankton.color.make_hsv(
{
"hue": hue,
"saturation": 0.375,
"value": 0.375,
}
),
);
}
/**
*/
export function month_name(
month : int
)
: string
{
const keys : Array<string> = [
"common.monthname.january",
"common.monthname.february",
"common.monthname.march",
"common.monthname.april",
"common.monthname.may",
"common.monthname.june",
"common.monthname.july",
"common.monthname.august",
"common.monthname.september",
"common.monthname.october",
"common.monthname.november",
"common.monthname.december",
];
return lib_plankton.translate.get(keys[month-1]);
}
/**
*/
export function loading(
mode : boolean
)
: void
{
if (! mode)
{
_dali.overlay.toggle({"mode": false});
}
else
{
_dali.overlay.get_content_element().innerHTML = "[...]";
_dali.overlay.toggle({"mode": true});
}
}
2024-09-12 00:02:12 +02:00
}