frontend-dali/source/logic/main.ts
2024-09-26 13:38:25 +02:00

598 lines
16 KiB
TypeScript

/**
*/
class class_input_datetime implements lib_plankton.zoo_input.interface_input<_zeitbild.frontend_web.helpers.type_datetime>
{
/**
*/
private core : lib_plankton.zoo_input.class_input_group<_zeitbild.frontend_web.helpers.type_datetime>;
/**
*/
public constructor(
prefix : string
)
{
this.core = new lib_plankton.zoo_input.class_input_group<_zeitbild.frontend_web.helpers.type_datetime>(
[
{
"name": "timezone_shift",
"input": new lib_plankton.zoo_input.class_input_number(
),
"label": (prefix + lib_plankton.translate.get("common.timezone_shift")),
},
{
"name": "date",
"input": new lib_plankton.zoo_input.class_input_date(
),
"label": (prefix + lib_plankton.translate.get("common.date")),
},
{
"name": "time",
"input": new lib_plankton.zoo_input.class_input_soft<type_time>(
new lib_plankton.zoo_input.class_input_time(
)
),
"label": (prefix + lib_plankton.translate.get("common.time")),
},
]
);
}
/**
*/
public async setup(
parent : HTMLElement
) : Promise<void>
{
return this.core.setup(parent);
}
/**
*/
public async read(
) : Promise<_zeitbild.frontend_web.helpers.type_datetime>
{
return this.core.read();
}
/**
*/
public async write(
value : _zeitbild.frontend_web.helpers.type_datetime
) : Promise<void>
{
return this.core.write(value);
}
}
/**
*/
namespace _zeitbild.frontend_web
{
/**
*/
export async function main(
) : Promise<void>
{
// conf
await _zeitbild.frontend_web.conf.init("conf.json");
// init
await _zeitbild.frontend_web.backend.init();
await lib_plankton.translate.initialize(
{
"verbosity": 1,
"packages": [
JSON.parse(await lib_plankton.file.read("data/localization/deu.loc.json")),
JSON.parse(await lib_plankton.file.read("data/localization/eng.loc.json")),
],
"order": ["deu", "eng"],
"autopromote": false,
}
);
lib_plankton.zoo_page.init(
document.querySelector("main"),
{
"pool": {
"login": async (parameters, target_element) => {
const preparation : {kind : string; data : any;} = await _zeitbild.frontend_web.backend.session_prepare(
);
switch (preparation.kind) {
case "internal": {
target_element.innerHTML = await _zeitbild.frontend_web.helpers.template_coin(
"login",
{
}
);
const form : lib_plankton.zoo_form.class_form<
{name : string; password : string;},
{name : string; password : string;}
> = new lib_plankton.zoo_form.class_form<
{name : string; password : string;},
{name : string; password : string;}
>(
x => x,
x => x,
new lib_plankton.zoo_input.class_input_group<
{name : string; password : string;}
>(
[
{
"name": "name",
"input": new lib_plankton.zoo_input.class_input_text(),
"label": lib_plankton.translate.get("page.login.internal.name"),
},
{
"name": "password",
"input": new lib_plankton.zoo_input.class_input_password(),
"label": lib_plankton.translate.get("page.login.internal.password"),
},
]
),
[
{
"label": lib_plankton.translate.get("page.login.internal.do"),
"target": "submit",
"procedure": async (get_value, get_representation) => {
const value : any = await get_value();
try {
await _zeitbild.frontend_web.backend.session_begin(
value.name,
value.password
);
lib_plankton.zoo_page.set(
{
"name": "events",
"parameters": {}
}
);
}
catch (error) {
lib_plankton.zoo_page.set(
{
"name": "login",
"parameters": {
"name": value.name,
}
}
);
}
}
},
]
);
await form.setup(document.querySelector("#login"));
await form.input_write(
{
"name": (parameters.name ?? ""),
"password": "",
}
);
break;
}
case "oidc": {
let element_a : HTMLElement = document.createElement("a");;
element_a.textContent = lib_plankton.string.coin(
lib_plankton.translate.get("page.login.oidc.via"),
{
"title": preparation.data.label,
}
);
element_a.setAttribute("href", preparation.data.url);
target_element.innerHTML = "";
target_element.appendChild(element_a);
break;
}
default: {
break;
}
}
},
"oidc_finish": async (parameters, target_element) => {
await _zeitbild.frontend_web.backend.set_session_key(parameters["session_key"]);
lib_plankton.zoo_page.set(
{
"name": "events",
"parameters": {}
}
);
},
"logout": async (parameters, target_element) => {
await _zeitbild.frontend_web.backend.session_end(
);
lib_plankton.zoo_page.set(
{
"name": "login",
"parameters": {
}
}
);
},
"calendar_add": async (parameters, target_element) => {
target_element.innerHTML = await _zeitbild.frontend_web.helpers.template_coin(
"calendar_add",
{
"label": lib_plankton.translate.get("page.calendar_add.title")
}
);
const form : lib_plankton.zoo_form.class_form<
_zeitbild.frontend_web.type.calendar_object,
{
name : string;
access_default_level : string;
resource_kind : string;
}
> = new lib_plankton.zoo_form.class_form<
_zeitbild.frontend_web.type.calendar_object,
{
name : string;
access_default_level : string;
resource_kind : string;
}
>(
(calendar_object) => ({
"name": calendar_object.name,
"access_default_level": (() => {
switch (calendar_object.access.default_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";
}
}) (),
"resource_kind": calendar_object.resource.kind,
}),
(raw) => ({
"name": raw.name,
"access": {
"default_level": (() => {
switch (raw.access_default_level) {
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;
}
}) (),
"attributed": lib_plankton.map.hashmap.implementation_map(
lib_plankton.map.hashmap.make(
x => x.toFixed(0)
)
),
},
"resource": (() => {
switch (raw.resource_kind) {
case "local": {
return {
"kind": "local",
"data": {
"events": [],
}
};
break;
}
case "caldav": {
return {
"kind": "caldav",
"data": {
"url": "", // TODO
"read_only": true, // TODO
}
};
break;
}
default: {
throw (new Error("invalid resource kind: " + raw.resource_kind));
break;
}
}
}) (),
}),
new lib_plankton.zoo_input.class_input_group<any>(
[
{
"name": "name",
"input": new lib_plankton.zoo_input.class_input_text(),
"label": lib_plankton.translate.get("calendar.name")
},
{
"name": "access_default_level",
"input": 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")
},
]
),
"label": lib_plankton.string.coin(
"{{default_level}}",
{
"head": lib_plankton.translate.get("calendar.access.access"),
"default_level": lib_plankton.translate.get("calendar.access.default_level"),
}
)
},
{
"name": "resource_kind",
"input": new lib_plankton.zoo_input.class_input_selection(
[
{
"value": "local",
"label": lib_plankton.translate.get("resource.kinds.local.title")
},
{
"value": "caldav",
"label": lib_plankton.translate.get("resource.kinds.caldav.title")
},
]
),
"label": lib_plankton.translate.get("resource.kind")
},
]
),
[
{
"label": lib_plankton.translate.get("page.calendar_add.actions.do"),
"target": "submit",
"procedure": async (get_value, get_representation) => {
const value : any = await get_value();
try {
await _zeitbild.frontend_web.backend.calendar_add(
value
);
lib_plankton.zoo_page.set(
{
"name": "events",
"parameters": {}
}
);
}
catch (error) {
// do nothing
/*
lib_plankton.zoo_page.set(
{
"name": "event_add",
"parameters": {
}
}
);
*/
}
}
},
]
);
await form.setup(document.querySelector("#calendar_add_form"));
},
"event_add": async (parameters, target_element) => {
const calendar_id : int = parseInt(parameters["calendar_id"]);
target_element.innerHTML = await _zeitbild.frontend_web.helpers.template_coin(
"event_add",
{
"label": lib_plankton.translate.get("page.event_add.title")
}
);
const form : lib_plankton.zoo_form.class_form<
{
calendar_id : _zeitbild.frontend_web.type.calendar_id;
event_object : _zeitbild.frontend_web.type.event_object;
},
{
calendar_id : string;
name : string;
begin : _zeitbild.frontend_web.helpers.type_datetime;
end : (null | _zeitbild.frontend_web.helpers.type_datetime);
location : (null | string);
description : (null | string);
}
> = new lib_plankton.zoo_form.class_form<
{
calendar_id : _zeitbild.frontend_web.type.calendar_id;
event_object : _zeitbild.frontend_web.type.event_object;
},
{
calendar_id : string;
name : string;
begin : _zeitbild.frontend_web.helpers.type_datetime;
end : (null | _zeitbild.frontend_web.helpers.type_datetime);
location : (null | string);
description : (null | string);
}
>(
(value) => ({
"calendar_id": value.calendar_id.toFixed(0),
"name": value.event_object.name,
"begin": value.event_object.begin,
"end": value.event_object.end,
"location": value.event_object.location,
"description": value.event_object.description,
}),
(representation) => ({
"calendar_id": parseInt(representation.calendar_id),
"event_object": {
"name": representation.name,
"begin": representation.begin,
"end": representation.end,
"location": representation.location,
"description": representation.description,
}
}),
new lib_plankton.zoo_input.class_input_group<any>(
[
{
"name": "calendar_id",
"input": new lib_plankton.zoo_input.class_input_selection(
(await _zeitbild.frontend_web.backend.calendar_list())
.filter(
(entry) => (["edit","admin"].includes(entry.access_level))
)
.map(
(entry) => ({
"value": entry.id.toFixed(0),
"label": entry.name,
})
)
),
"label": lib_plankton.translate.get("calendar.calendar")
},
{
"name": "name",
"input": new lib_plankton.zoo_input.class_input_text(
),
"label": lib_plankton.translate.get("event.name")
},
{
"name": "begin",
"input": new class_input_datetime(
""
),
"label": lib_plankton.translate.get("event.begin")
},
{
"name": "end",
"input": new lib_plankton.zoo_input.class_input_soft<_zeitbild.frontend_web.helpers.type_datetime>(
new class_input_datetime(
""
)
),
"label": lib_plankton.translate.get("event.end")
},
{
"name": "location",
"input": new lib_plankton.zoo_input.class_input_soft<string>(
new lib_plankton.zoo_input.class_input_text(
)
),
"label": lib_plankton.translate.get("event.location")
},
{
"name": "description",
"input": new lib_plankton.zoo_input.class_input_soft<string>(
new lib_plankton.zoo_input.class_input_text(
)
),
"label": lib_plankton.translate.get("event.description")
},
]
),
[
{
"label": lib_plankton.translate.get("page.event_add.actions.do"),
"target": "submit",
"procedure": async (get_value, get_representation) => {
const value : any = await get_value();
try {
await _zeitbild.frontend_web.backend.calendar_event_add(
value.calendar_id,
value.event_object
);
lib_plankton.zoo_page.set(
{
"name": "events",
"parameters": {}
}
);
}
catch (error) {
// do nothing
/*
lib_plankton.zoo_page.set(
{
"name": "event_add",
"parameters": {
}
}
);
*/
}
}
},
]
);
/*
await form.write(
{
"calendar_id": parameters["calendar_id"],
}
);
*/
await form.setup(document.querySelector("#event_add_form"));
},
"events": async (parameters, target_element) => {
const content = await _zeitbild.frontend_web.view.calendar_view_table_html(
{
"calendar_ids": null,
// TODO
"from": {
"year": 2024,
"week": 37
},
// TODO
"to": {
"year": 2024,
"week": 43
},
"timezone_shift": /*conf.timezone_shift*/0,
}
);
target_element.innerHTML = content;
},
},
"fallback": {
"name": "login",
"parameters": {}
}
}
);
lib_plankton.zoo_page.add_nav_entry(
{"name": "login", "parameters": {}},
{"label": lib_plankton.translate.get("page.login.title")}
);
lib_plankton.zoo_page.add_nav_entry(
{"name": "events", "parameters": {}},
{"label": lib_plankton.translate.get("page.events.title")}
);
lib_plankton.zoo_page.add_nav_entry(
{"name": "calendar_add", "parameters": {}},
{"label": lib_plankton.translate.get("page.calendar_add.title")}
);
lib_plankton.zoo_page.add_nav_entry(
{"name": "event_add", "parameters": {}},
{"label": lib_plankton.translate.get("page.event_add.title")}
);
lib_plankton.zoo_page.add_nav_entry(
{"name": "logout", "parameters": {}},
{"label": lib_plankton.translate.get("page.logout.title")}
);
// exec
lib_plankton.zoo_page.start();
return Promise.resolve<void>(undefined);
}
}