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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2025-10-28 11:38:58 +01:00
|
|
|
namespace _dali.widgets
|
2024-10-01 21:33:14 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
type type_entry = {
|
2025-10-17 00:10:28 +02:00
|
|
|
id : _dali.type_calendar_id;
|
2024-10-01 21:33:14 +02:00
|
|
|
name : string;
|
2025-10-13 13:22:53 +02:00
|
|
|
hue : float;
|
2025-10-17 00:10:28 +02:00
|
|
|
access_level : _dali.enum_access_level;
|
2024-10-01 21:33:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
2025-10-13 13:29:19 +02:00
|
|
|
export class class_widget_sources implements lib_plankton.zoo_widget.interface_widget
|
2024-10-01 21:33:14 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-14 23:04:31 +02:00
|
|
|
* [dependency]
|
2024-10-01 21:33:14 +02:00
|
|
|
*/
|
2025-10-14 23:04:31 +02:00
|
|
|
private get_entries : (() => Promise<Array<type_entry>>);
|
2024-10-01 21:33:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-14 23:04:31 +02:00
|
|
|
* [hook]
|
2024-10-01 21:33:14 +02:00
|
|
|
*/
|
2025-10-22 00:41:22 +02:00
|
|
|
private action_toggle : ((entry : type_entry, mode : boolean) => void);
|
2024-10-01 21:33:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-14 23:04:31 +02:00
|
|
|
* [hook]
|
2024-10-01 21:33:14 +02:00
|
|
|
*/
|
2025-10-22 00:41:22 +02:00
|
|
|
private action_select : ((entry : type_entry) => void);
|
2024-10-21 23:18:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-14 23:04:31 +02:00
|
|
|
* [hook]
|
2024-10-21 23:18:08 +02:00
|
|
|
*/
|
2025-10-22 00:41:22 +02:00
|
|
|
private action_add : (() => void);
|
2025-10-14 23:04:31 +02:00
|
|
|
|
|
|
|
|
|
2025-10-17 00:10:28 +02:00
|
|
|
/**
|
2025-10-20 13:16:00 +02:00
|
|
|
* [state]
|
2025-10-17 00:10:28 +02:00
|
|
|
*/
|
|
|
|
|
private priviliged : boolean;
|
|
|
|
|
|
|
|
|
|
|
2025-10-14 23:04:31 +02:00
|
|
|
/**
|
|
|
|
|
* [state]
|
|
|
|
|
*/
|
|
|
|
|
private container : (null | Element);
|
2024-10-01 21:33:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
public constructor(
|
2025-10-14 23:04:31 +02:00
|
|
|
get_entries : (() => Promise<Array<type_entry>>),
|
|
|
|
|
{
|
2025-10-22 00:41:22 +02:00
|
|
|
"action_select": action_select = ((calendar_id) => {}),
|
|
|
|
|
"action_toggle": action_toggle = ((calendar_id, mode) => {}),
|
|
|
|
|
"action_add": action_add = (() => {}),
|
2025-10-17 00:10:28 +02:00
|
|
|
"initial_priviliged": initial_priviliged = false,
|
2025-10-14 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
:
|
|
|
|
|
{
|
2025-10-22 00:41:22 +02:00
|
|
|
action_select ?: ((entry : type_entry) => void);
|
|
|
|
|
action_toggle ?: ((entry : type_entry, mode : boolean) => void);
|
|
|
|
|
action_add ?: (() => void);
|
2025-10-17 00:10:28 +02:00
|
|
|
initial_priviliged ?: boolean;
|
2025-10-14 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
=
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-10-01 21:33:14 +02:00
|
|
|
)
|
|
|
|
|
{
|
2025-10-14 23:04:31 +02:00
|
|
|
// dependencies
|
|
|
|
|
this.get_entries = get_entries;
|
|
|
|
|
|
|
|
|
|
// hooks
|
2025-10-22 00:41:22 +02:00
|
|
|
this.action_select = action_select;
|
|
|
|
|
this.action_toggle = action_toggle;
|
|
|
|
|
this.action_add = action_add;
|
2025-10-14 23:04:31 +02:00
|
|
|
|
|
|
|
|
// state
|
2025-10-17 00:10:28 +02:00
|
|
|
this.priviliged = initial_priviliged;
|
2025-10-14 23:04:31 +02:00
|
|
|
this.container = null;
|
2024-10-01 21:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
2025-10-14 23:04:31 +02:00
|
|
|
private static id_encode(
|
2025-10-17 00:10:28 +02:00
|
|
|
id : _dali.type_calendar_id
|
2025-10-14 23:04:31 +02:00
|
|
|
)
|
|
|
|
|
: string
|
|
|
|
|
{
|
|
|
|
|
return id.toFixed(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
private static id_decode(
|
|
|
|
|
representation : string
|
|
|
|
|
)
|
2025-10-17 00:10:28 +02:00
|
|
|
: _dali.type_calendar_id
|
2024-10-01 21:33:14 +02:00
|
|
|
{
|
2025-10-14 23:04:31 +02:00
|
|
|
return parseInt(representation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
public async update(
|
2025-10-17 00:10:28 +02:00
|
|
|
{
|
|
|
|
|
"priviliged": priviliged = null,
|
|
|
|
|
}
|
|
|
|
|
:
|
|
|
|
|
{
|
|
|
|
|
priviliged ?: boolean;
|
|
|
|
|
}
|
|
|
|
|
=
|
|
|
|
|
{
|
|
|
|
|
}
|
2025-10-14 23:04:31 +02:00
|
|
|
)
|
|
|
|
|
: Promise<void>
|
|
|
|
|
{
|
2025-10-17 00:10:28 +02:00
|
|
|
if (priviliged === null)
|
|
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.priviliged = priviliged;
|
|
|
|
|
}
|
|
|
|
|
const data : lib_plankton.map.type_map<_dali.type_calendar_id, type_entry> = lib_plankton.map.hashmap.implementation_map(
|
2025-10-14 23:04:31 +02:00
|
|
|
lib_plankton.map.hashmap.make(
|
|
|
|
|
calendar_id => class_widget_sources.id_encode(calendar_id),
|
|
|
|
|
{
|
|
|
|
|
"pairs": (
|
|
|
|
|
(await this.get_entries())
|
|
|
|
|
.map(
|
2025-10-17 00:10:28 +02:00
|
|
|
entry => (
|
|
|
|
|
{
|
|
|
|
|
"key": entry.id,
|
|
|
|
|
"value": entry,
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-10-14 23:04:31 +02:00
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
// structure
|
|
|
|
|
{
|
|
|
|
|
this.container.innerHTML = await _dali.helpers.template_coin(
|
|
|
|
|
"widget-sources",
|
|
|
|
|
"main",
|
|
|
|
|
{
|
|
|
|
|
"label_create": lib_plankton.translate.get("widget.sources.create"),
|
|
|
|
|
"entries": (
|
|
|
|
|
(
|
|
|
|
|
await _dali.helpers.promise_row<string>(
|
|
|
|
|
lib_plankton.map.dump(data)
|
|
|
|
|
.map(
|
|
|
|
|
(pair) => () => {
|
|
|
|
|
return _dali.helpers.template_coin(
|
|
|
|
|
"widget-sources",
|
|
|
|
|
"entry",
|
|
|
|
|
{
|
|
|
|
|
"name": pair.value.name,
|
|
|
|
|
// "access_level": entry.access_level, // TODO
|
2025-10-20 13:16:00 +02:00
|
|
|
"color": _dali.helpers.event_color(pair.value.hue),
|
2025-10-14 23:04:31 +02:00
|
|
|
"rel": class_widget_sources.id_encode(pair.key),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-10-01 21:33:14 +02:00
|
|
|
)
|
|
|
|
|
)
|
2025-10-14 23:04:31 +02:00
|
|
|
.join("")
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-10-17 00:10:28 +02:00
|
|
|
this.container.querySelector(".sources").classList.toggle("sources-priviliged", this.priviliged);
|
2025-10-14 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
// listeners
|
|
|
|
|
{
|
|
|
|
|
this.container.querySelector(".sources-create").addEventListener(
|
|
|
|
|
"click",
|
|
|
|
|
(event) => {
|
|
|
|
|
event.preventDefault();
|
2025-10-22 00:41:22 +02:00
|
|
|
this.action_add();
|
2025-10-14 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
);
|
2025-10-20 13:16:00 +02:00
|
|
|
this.container.querySelectorAll(".sources-entry-visibility").forEach(
|
2025-10-14 23:04:31 +02:00
|
|
|
(element) => {
|
|
|
|
|
element.addEventListener(
|
2025-10-20 13:16:00 +02:00
|
|
|
"change",
|
2025-10-14 23:04:31 +02:00
|
|
|
() => {
|
2025-10-20 13:16:00 +02:00
|
|
|
const mode : boolean = (element as HTMLInputElement).checked;
|
|
|
|
|
const key_encoded : string = element.parentElement.getAttribute("rel");
|
2025-10-17 00:10:28 +02:00
|
|
|
const calendar_id : _dali.type_calendar_id = class_widget_sources.id_decode(key_encoded);
|
2025-10-14 23:04:31 +02:00
|
|
|
const entry : type_entry = data.get(calendar_id);
|
2025-10-20 13:16:00 +02:00
|
|
|
element.parentElement.classList.toggle("sources-entry-hidden", (! mode));
|
2025-10-22 00:41:22 +02:00
|
|
|
this.action_toggle(entry, mode);
|
2025-10-14 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-10-20 13:16:00 +02:00
|
|
|
this.container.querySelectorAll(".sources-entry").forEach(
|
2025-10-14 23:04:31 +02:00
|
|
|
(element) => {
|
|
|
|
|
element.addEventListener(
|
|
|
|
|
"click",
|
|
|
|
|
(event) => {
|
2025-10-20 13:16:00 +02:00
|
|
|
if ((event.target as Element).classList.contains("sources-entry-visibility"))
|
|
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const key_encoded : string = element.getAttribute("rel");
|
|
|
|
|
const calendar_id : _dali.type_calendar_id = class_widget_sources.id_decode(key_encoded);
|
|
|
|
|
const entry : type_entry = data.get(calendar_id);
|
2025-10-22 00:41:22 +02:00
|
|
|
this.action_select(entry);
|
2025-10-20 13:16:00 +02:00
|
|
|
}
|
2025-10-14 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [implementation]
|
|
|
|
|
*/
|
|
|
|
|
public async load(
|
|
|
|
|
target_element : Element
|
|
|
|
|
)
|
|
|
|
|
: Promise<void>
|
|
|
|
|
{
|
|
|
|
|
this.container = target_element;
|
|
|
|
|
await this.update();
|
2024-10-01 21:33:14 +02:00
|
|
|
}
|
2025-10-17 00:10:28 +02:00
|
|
|
|
2024-10-01 21:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|