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:41:17 +01:00
|
|
|
namespace _dali.widgets
|
2025-10-02 17:00:17 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
type type_option = {
|
2025-10-17 00:10:28 +02:00
|
|
|
mode : _dali.enum_view_mode;
|
2025-10-02 17:00:17 +02:00
|
|
|
label : string,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
2025-10-13 13:29:19 +02:00
|
|
|
export class class_widget_mode_switcher implements lib_plankton.zoo_widget.interface_widget
|
2025-10-02 17:00:17 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
private options : Array<type_option>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
2025-10-17 00:10:28 +02:00
|
|
|
private initial_selection : (null | _dali.enum_view_mode);
|
2025-10-02 17:00:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
2025-10-17 00:10:28 +02:00
|
|
|
private action_change : (null | ((mode : _dali.enum_view_mode) => void));
|
2025-10-02 17:00:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
public constructor(
|
|
|
|
|
options : Array<type_option>,
|
|
|
|
|
{
|
|
|
|
|
"initial_selection": initial_selection = null,
|
|
|
|
|
"action_change": action_change = null,
|
|
|
|
|
}
|
|
|
|
|
:
|
|
|
|
|
{
|
2025-10-17 00:10:28 +02:00
|
|
|
initial_selection ?: (null | _dali.enum_view_mode),
|
|
|
|
|
action_change ?: (null | ((mode : _dali.enum_view_mode) => void))
|
2025-10-02 17:00:17 +02:00
|
|
|
}
|
|
|
|
|
=
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
this.options = options;
|
|
|
|
|
this.initial_selection = initial_selection;
|
|
|
|
|
this.action_change = action_change;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [implementation]
|
|
|
|
|
*/
|
|
|
|
|
public async load(
|
|
|
|
|
target_element : Element
|
2025-10-29 11:40:27 +01:00
|
|
|
)
|
|
|
|
|
: Promise<void>
|
2025-10-02 17:00:17 +02:00
|
|
|
{
|
|
|
|
|
target_element.innerHTML = await _dali.helpers.template_coin(
|
|
|
|
|
"widget-mode_switcher",
|
|
|
|
|
"main",
|
|
|
|
|
{
|
|
|
|
|
"options": (await lib_plankton.call.promise_condense(
|
|
|
|
|
this.options.map(
|
|
|
|
|
option => () => _dali.helpers.template_coin(
|
|
|
|
|
"widget-mode_switcher",
|
|
|
|
|
"option",
|
|
|
|
|
{
|
|
|
|
|
"rel": _dali.view_mode_encode(option.mode),
|
|
|
|
|
"value": _dali.view_mode_encode(option.mode),
|
|
|
|
|
"label": option.label,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)).join(""),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
// initial selection
|
|
|
|
|
{
|
|
|
|
|
if (this.initial_selection !== null)
|
|
|
|
|
{
|
|
|
|
|
const selector : string = lib_plankton.string.coin(
|
|
|
|
|
".widget-mode_switcher-option[rel=\"{{rel}}\"] > input",
|
|
|
|
|
{
|
|
|
|
|
"rel": _dali.view_mode_encode(this.initial_selection)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
(target_element.querySelector(selector) as HTMLInputElement).checked = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// set listeners
|
|
|
|
|
{
|
|
|
|
|
if (this.action_change !== null)
|
|
|
|
|
{
|
|
|
|
|
target_element.querySelectorAll(".widget-mode_switcher-option").forEach(
|
|
|
|
|
element => {
|
|
|
|
|
const view_mode_encoded : string = element.getAttribute("rel");
|
2025-10-17 00:10:28 +02:00
|
|
|
const mode : _dali.enum_view_mode = _dali.view_mode_decode(view_mode_encoded);
|
2025-10-02 17:00:17 +02:00
|
|
|
element.querySelector("input").addEventListener(
|
|
|
|
|
"change",
|
|
|
|
|
(event) => {
|
|
|
|
|
this.action_change(mode);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve<void>(undefined);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|