frontend-dali/source/widgets/calendar_edit/logic.ts

264 lines
6.5 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/>.
*/
namespace _dali.widgets
2025-10-14 00:16:22 +02:00
{
/**
*/
export class class_widget_calendar_edit
implements lib_plankton.zoo_widget.interface_widget
{
2025-10-23 11:35:57 +02:00
/**
*/
private groups : Array<{id : _dali.type_group_id; object : _dali.type_group_object;}>;
2025-10-17 00:10:28 +02:00
/**
*/
private users : Array<{id : _dali.type_user_id; name : string;}>;
2025-10-14 00:16:22 +02:00
/**
*/
private read_only : boolean;
/**
*/
private action_cancel ?: (null | (() => void));
/**
*/
2025-10-17 00:10:28 +02:00
private action_add ?: (null | ((value : _dali.type_calendar_object) => void));
2025-10-14 00:16:22 +02:00
/**
*/
2025-10-17 00:10:28 +02:00
private action_change ?: (null | ((value : _dali.type_calendar_object) => void));
2025-10-14 00:16:22 +02:00
/**
*/
2025-10-17 00:10:28 +02:00
private action_remove ?: (null | ((value : _dali.type_calendar_object) => void));
2025-10-14 00:16:22 +02:00
/**
*/
2025-10-22 00:41:22 +02:00
private initial_value : _dali.type_calendar_object;
2025-10-14 00:16:22 +02:00
/**
*/
public constructor(
2025-10-23 11:35:57 +02:00
groups : Array<{id : _dali.type_group_id; object : _dali.type_group_object;}>,
2025-10-17 00:10:28 +02:00
users : Array<{id : _dali.type_user_id; name : string;}>,
2025-10-22 00:41:22 +02:00
initial_value : _dali.type_calendar_object,
2025-10-14 00:16:22 +02:00
{
"read_only": read_only = false,
"action_cancel": action_cancel = null,
"action_add": action_add = null,
2025-10-14 00:16:22 +02:00
"action_change": action_change = null,
"action_remove": action_remove = null,
}
:
{
read_only ?: boolean;
action_cancel ?: (null | (() => void));
2025-10-17 00:10:28 +02:00
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));
2025-10-14 00:16:22 +02:00
}
=
{
}
)
{
2025-10-23 11:35:57 +02:00
this.groups = groups;
2025-10-17 00:10:28 +02:00
this.users = users;
2025-10-22 00:41:22 +02:00
this.initial_value = initial_value;
2025-10-14 00:16:22 +02:00
this.read_only = read_only;
this.action_cancel = action_cancel;
this.action_add = action_add;
2025-10-14 00:16:22 +02:00
this.action_change = action_change;
this.action_remove = action_remove;
}
/**
* [implementation]
*/
public async load(
target_element : HTMLElement
) : Promise<void>
{
const dom_root = await _dali.helpers.element_from_template(
"widget-calendar_edit",
"main",
{
}
);
2025-10-14 00:16:22 +02:00
const form : lib_plankton.zoo_form.class_form<
2025-10-17 00:10:28 +02:00
_dali.type_calendar_object,
_dali.type_calendar_object
2025-10-14 00:16:22 +02:00
> = new lib_plankton.zoo_form.class_form<
2025-10-17 00:10:28 +02:00
_dali.type_calendar_object,
_dali.type_calendar_object
2025-10-14 00:16:22 +02:00
>(
(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"),
},
{
2025-10-23 11:35:57 +02:00
"name": "attributed_group",
"input": _dali.helpers.input_attributed_access_group(this.groups),
"label": lib_plankton.translate.get("calendar.access.attributed_group"),
},
{
"name": "attributed_user",
"input": _dali.helpers.input_attributed_access_user(this.users),
"label": lib_plankton.translate.get("calendar.access.attributed_user"),
2025-10-14 00:16:22 +02:00
},
]
),
"label": lib_plankton.translate.get("calendar.access.access"),
},
2025-10-17 00:10:28 +02:00
{
"name": "resource",
"input": new lib_plankton.zoo_input.class_input_hidden(
),
"label": lib_plankton.translate.get("calendar.resource"),
},
2025-10-14 00:16:22 +02:00
]
),
(
[]
// 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) => {
2025-10-17 00:10:28 +02:00
const value : _dali.type_calendar_object = await get_value();
this.action_add(value);
}
},
]
:
[]
)
2025-10-14 00:16:22 +02:00
// 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) => {
2025-10-17 00:10:28 +02:00
const value : _dali.type_calendar_object = await get_value();
2025-10-14 00:16:22 +02:00
this.action_change(value);
}
},
]
:
[]
)
// remove
.concat(
((! this.read_only) && (! (this.action_remove === null)))
2025-10-14 00:16:22 +02:00
?
[
{
"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);
}
2025-10-14 00:16:22 +02:00
}
},
]
:
[]
)
2025-10-17 00:10:28 +02:00
// cancel
.concat(
(! (this.action_cancel === null))
?
[
{
"label": lib_plankton.translate.get("common.cancel"),
"procedure": async () => {
this.action_cancel();
}
},
]
:
[]
)
2025-10-14 00:16:22 +02:00
)
);
await form.setup(dom_root);
2025-10-22 00:41:22 +02:00
await form.input_lock(this.read_only);
await form.input_write(this.initial_value);
target_element.appendChild(dom_root);
2025-10-29 13:24:41 +01:00
form.input_focus();
2025-10-14 00:16:22 +02:00
}
}
}