frontend-dali/source/pages/calendar_add/logic.ts

229 lines
5.6 KiB
TypeScript
Raw Normal View History

2025-09-25 17:54:20 +02:00
namespace _dali.pages
2024-09-26 16:47:01 +02:00
{
/**
*/
lib_plankton.zoo_page.register(
"calendar_add",
async (parameters, target_element) => {
target_element.innerHTML = "";
2025-09-25 17:54:20 +02:00
target_element.innerHTML = await _dali.helpers.template_coin(
2024-09-26 16:47:01 +02:00
"calendar_add",
2024-10-01 18:28:04 +02:00
"default",
2024-09-26 16:47:01 +02:00
{
"label": lib_plankton.translate.get("page.calendar_add.title")
}
);
const form : lib_plankton.zoo_form.class_form<
{
name : string;
2024-09-30 20:20:14 +02:00
access : {
2024-10-10 23:00:51 +02:00
public : boolean;
2025-09-25 17:54:20 +02:00
default_level : _dali.type.enum_access_level;
2024-09-30 20:20:14 +02:00
attributed : lib_plankton.map.type_map<
2025-09-25 17:54:20 +02:00
_dali.type.user_id,
_dali.type.enum_access_level
2024-09-30 20:20:14 +02:00
>;
};
2024-09-26 16:47:01 +02:00
resource_kind : string;
2025-10-13 13:22:53 +02:00
hue : (null | float);
},
{
name : string;
access : {
public : boolean;
default_level : _dali.type.enum_access_level;
attributed : lib_plankton.map.type_map<
_dali.type.user_id,
_dali.type.enum_access_level
>;
};
resource_kind : string;
hue : (null | float);
2024-09-26 16:47:01 +02:00
}
> = new lib_plankton.zoo_form.class_form<
{
name : string;
2024-09-30 20:20:14 +02:00
access : {
2024-10-10 23:00:51 +02:00
public : boolean;
2025-09-25 17:54:20 +02:00
default_level : _dali.type.enum_access_level;
2024-09-30 20:20:14 +02:00
attributed : lib_plankton.map.type_map<
2025-09-25 17:54:20 +02:00
_dali.type.user_id,
_dali.type.enum_access_level
2024-09-30 20:20:14 +02:00
>;
};
2024-09-26 16:47:01 +02:00
resource_kind : string;
2025-10-13 13:22:53 +02:00
hue : (null | float);
},
{
name : string;
access : {
public : boolean;
default_level : _dali.type.enum_access_level;
attributed : lib_plankton.map.type_map<
_dali.type.user_id,
_dali.type.enum_access_level
>;
};
resource_kind : string;
hue : (null | float);
2024-09-26 16:47:01 +02:00
}
>(
2025-10-13 13:22:53 +02:00
(value) => value,
(raw) => raw,
2024-09-26 16:47:01 +02:00
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")
},
2025-10-13 13:22:53 +02:00
{
"name": "hue",
"input": new lib_plankton.zoo_input.class_input_hue(
),
"label": lib_plankton.translate.get("calendar.hue"),
},
2024-09-26 16:47:01 +02:00
{
2024-09-30 20:20:14 +02:00
"name": "access",
"input": new lib_plankton.zoo_input.class_input_group(
2024-09-26 16:47:01 +02:00
[
2024-10-10 23:00:51 +02:00
{
"name": "public",
"input": new lib_plankton.zoo_input.class_input_checkbox(),
"label": lib_plankton.translate.get("calendar.access.public"),
},
2024-09-26 16:47:01 +02:00
{
2024-09-30 20:20:14 +02:00
"name": "default_level",
2025-09-25 17:54:20 +02:00
"input": _dali.helpers.input_access_level(),
2024-09-30 20:20:14 +02:00
"label": lib_plankton.translate.get("calendar.access.default_level"),
2024-09-26 16:47:01 +02:00
},
{
2024-09-30 20:20:14 +02:00
"name": "attributed",
2025-09-25 17:54:20 +02:00
"input": await _dali.helpers.input_attributed_access(),
2024-09-30 20:20:14 +02:00
"label": lib_plankton.translate.get("calendar.access.attributed"),
2024-09-26 16:47:01 +02:00
},
]
),
2024-09-30 20:20:14 +02:00
"label": lib_plankton.translate.get("calendar.access.access"),
2024-09-26 16:47:01 +02:00
},
{
"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"),
"procedure": async (get_value, get_representation) => {
const value : any = await get_value();
2025-10-13 13:22:53 +02:00
const calendar_object : _dali.type.calendar_object = {
"name": value.name,
"access": {
"public": value.access.public,
"default_level": value.access.default_level,
"attributed": value.access.attributed,
},
"resource": (() => {
switch (value.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: " + value.resource_kind));
break;
}
}
}) (),
"hue": value.hue,
};
try
{
2025-09-25 17:54:20 +02:00
await _dali.backend.calendar_add(
2025-10-13 13:22:53 +02:00
calendar_object
2024-09-26 16:47:01 +02:00
);
lib_plankton.zoo_page.set(
{
"name": "overview",
2024-09-26 16:47:01 +02:00
"parameters": {}
}
);
}
2025-10-13 13:22:53 +02:00
catch (error)
{
2024-09-26 16:47:01 +02:00
// do nothing
/*
lib_plankton.zoo_page.set(
{
"name": "event_add",
"parameters": {
}
}
);
*/
}
}
},
]
);
await form.setup(document.querySelector("#calendar_add_form"));
2025-10-13 13:22:53 +02:00
await form.input_write(
{
"name": "",
"access": {
"public": false,
"default_level": _dali.type.enum_access_level.view,
"attributed": lib_plankton.map.hashmap.implementation_map<
_dali.type.user_id,
_dali.type.enum_access_level
>(
lib_plankton.map.hashmap.make<
_dali.type.user_id,
_dali.type.enum_access_level
>(
user_id => user_id.toFixed(0),
)
),
},
"resource_kind": "local",
"hue": lib_plankton.random.generate_unit(),
}
);
2024-09-26 16:47:01 +02:00
return Promise.resolve<void>(undefined);
}
);
}