frontend-dali/source/widgets/calendar_edit/logic.ts
2025-10-22 00:41:22 +02:00

222 lines
5.2 KiB
TypeScript

namespace _dali.widgets.calendar_edit
{
/**
*/
export class class_widget_calendar_edit
implements lib_plankton.zoo_widget.interface_widget
{
/**
*/
private users : Array<{id : _dali.type_user_id; name : string;}>;
/**
*/
private read_only : boolean;
/**
*/
private action_cancel ?: (null | (() => void));
/**
*/
private action_add ?: (null | ((value : _dali.type_calendar_object) => void));
/**
*/
private action_change ?: (null | ((value : _dali.type_calendar_object) => void));
/**
*/
private action_remove ?: (null | ((value : _dali.type_calendar_object) => void));
/**
*/
private initial_value : _dali.type_calendar_object;
/**
*/
public constructor(
users : Array<{id : _dali.type_user_id; name : string;}>,
initial_value : _dali.type_calendar_object,
{
"read_only": read_only = false,
"action_cancel": action_cancel = null,
"action_add": action_add = null,
"action_change": action_change = null,
"action_remove": action_remove = null,
}
:
{
read_only ?: boolean;
action_cancel ?: (null | (() => void));
action_add ?: (null | ((value : _dali.type_calendar_object) => void))
action_change ?: (null | ((value : _dali.type_calendar_object) => void));
action_remove ?: (null | ((value : _dali.type_calendar_object) => void));
}
=
{
}
)
{
this.users = users;
this.initial_value = initial_value;
this.read_only = read_only;
this.action_cancel = action_cancel;
this.action_add = action_add;
this.action_change = action_change;
this.action_remove = action_remove;
}
/**
* [implementation]
*/
public async load(
target_element : HTMLElement
) : Promise<void>
{
const form : lib_plankton.zoo_form.class_form<
_dali.type_calendar_object,
_dali.type_calendar_object
> = new lib_plankton.zoo_form.class_form<
_dali.type_calendar_object,
_dali.type_calendar_object
>(
(value) => value,
(raw) => raw,
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": "hue",
"input": new lib_plankton.zoo_input.class_input_hue(
),
"label": lib_plankton.translate.get("calendar.hue"),
},
{
"name": "access",
"input": new lib_plankton.zoo_input.class_input_group(
[
{
"name": "public",
"input": new lib_plankton.zoo_input.class_input_checkbox(),
"label": lib_plankton.translate.get("calendar.access.public"),
},
{
"name": "default_level",
"input": _dali.helpers.input_access_level(),
"label": lib_plankton.translate.get("calendar.access.default_level"),
},
{
"name": "attributed",
"input": _dali.helpers.input_attributed_access(this.users),
"label": lib_plankton.translate.get("calendar.access.attributed"),
},
]
),
"label": lib_plankton.translate.get("calendar.access.access"),
},
{
"name": "resource",
"input": new lib_plankton.zoo_input.class_input_hidden(
),
"label": lib_plankton.translate.get("calendar.resource"),
},
]
),
(
[]
// add
.concat(
((! this.read_only) && (! (this.action_add === null)))
?
[
{
"label": lib_plankton.translate.get("widget.calendar_edit.actions.add"),
"procedure": async (get_value, get_representation) => {
const value : _dali.type_calendar_object = await get_value();
this.action_add(value);
}
},
]
:
[]
)
// change
.concat(
((! this.read_only) && (! (this.action_change === null)))
?
[
{
"label": lib_plankton.translate.get("widget.calendar_edit.actions.change"),
"procedure": async (get_value, get_representation) => {
const value : _dali.type_calendar_object = await get_value();
this.action_change(value);
}
},
]
:
[]
)
// remove
.concat(
((! this.read_only) && (! (this.action_remove === null)))
?
[
{
"label": lib_plankton.translate.get("widget.calendar_edit.actions.remove"),
"procedure": async (get_value, get_representation) => {
if (! window.confirm(lib_plankton.translate.get("common.confirm_deletion")))
{
// do nothing
}
else
{
const value : _dali.type_calendar_object = await get_value();
this.action_remove(value);
}
}
},
]
:
[]
)
// cancel
.concat(
(! (this.action_cancel === null))
?
[
{
"label": lib_plankton.translate.get("common.cancel"),
"procedure": async () => {
this.action_cancel();
}
},
]
:
[]
)
)
);
await form.setup(target_element);
await form.input_lock(this.read_only);
await form.input_write(this.initial_value);
}
}
}