frontend-dali/source/widgets/login/logic.ts
2025-10-23 23:16:11 +02:00

216 lines
4.9 KiB
TypeScript

/*
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.login
{
/**
*/
export class class_widget_login implements lib_plankton.zoo_widget.interface_widget
{
/**
* [data]
*/
private initial_name : string;
/**
* [hook]
*/
private action_cancel : (null | (() => void));
/**
* [hook]
*/
private action_success : (null | (() => void));
/**
*/
public constructor(
{
"initial_name": initial_name = "",
"action_cancel": action_cancel = null,
"action_success": action_success = null,
}
:
{
initial_name ?: string;
action_cancel ?: (null | (() => void));
action_success ?: (null | (() => void));
}
=
{
}
)
{
this.initial_name = initial_name;
this.action_cancel = action_cancel;
this.action_success = action_success;
}
/**
* [implementation]
*/
public async load(
target_element : Element
)
: Promise<void>
{
const preparation : {kind : string; data : any;} = await _dali.backend.session_prepare(
{
"oidc_redirect_uri_template": _dali.conf.get()["misc"]["oidc_redirect_uri_template"],
}
);
switch (preparation.kind)
{
case "internal":
{
target_element.innerHTML = await _dali.helpers.template_coin(
"widget-login",
"default",
{
}
);
const form : lib_plankton.zoo_form.class_form<
{name : string; password : string;},
{name : string; password : string;}
> = new lib_plankton.zoo_form.class_form<
{name : string; password : string;},
{name : string; password : string;}
>(
x => x,
x => x,
new lib_plankton.zoo_input.class_input_group<
{name : string; password : string;}
>(
[
{
"name": "name",
"input": new lib_plankton.zoo_input.class_input_text(),
"label": lib_plankton.translate.get("widget.login.internal.name"),
},
{
"name": "password",
"input": new lib_plankton.zoo_input.class_input_password(),
"label": lib_plankton.translate.get("widget.login.internal.password"),
},
]
),
(
[]
.concat(
[
{
"label": lib_plankton.translate.get("widget.login.internal.do"),
"procedure": async (get_value, get_representation) => {
const value : any = await get_value();
try
{
await _dali.backend.session_begin(
value.name,
value.password
);
if (this.action_success !== null) this.action_success();
}
catch (error)
{
// todo
}
}
}
]
)
.concat(
(this.action_cancel === null)
?
[]
:
[
{
"label": lib_plankton.translate.get("common.cancel"),
"procedure": () => {
this.action_cancel();
}
}
]
)
)
);
await form.setup(document.querySelector(".widget-login"));
await form.input_write(
{
"name": this.initial_name,
"password": "",
}
);
break;
}
case "oidc":
{
// link
{
let element_a : HTMLElement = document.createElement("a");;
element_a.textContent = lib_plankton.string.coin(
lib_plankton.translate.get("widget.login.oidc.via"),
{
"title": preparation.data.label,
}
);
element_a.setAttribute("href", preparation.data.url);
target_element.appendChild(element_a);
}
{
let dom_br : HTMLElement = document.createElement("br");
target_element.appendChild(dom_br);
}
{
let dom_br : HTMLElement = document.createElement("br");
target_element.appendChild(dom_br);
}
// cancel
{
let dom_cancel : HTMLElement = document.createElement("button");
dom_cancel.textContent = lib_plankton.translate.get("common.cancel");
dom_cancel.addEventListener(
"click",
() => {
this.action_cancel();
}
);
target_element.appendChild(dom_cancel);
}
break;
}
default:
{
// todo
break;
}
}
}
}
}