formgen/source/logic/ioc/zerez.ts

246 lines
3.9 KiB
TypeScript
Raw Normal View History

2026-09-30 08:38:52 +02:00
namespace formgen.ioc
{
/**
*/
type type_data = {
manufacturer : string;
power : float;
};
/**
*/
type type_value = {
id : string;
data : (null | type_data);
};
/**
*/
type type_retrieve_data = ((id : string) => Promise<type_data>);
/**
*/
export class class_ioc_zerez implements interface_ioc<type_value>
{
/**
*/
private retrieve_data : type_retrieve_data;
/**
*/
private hint : (null | string);
/**
*/
private ioc_id : (null | interface_ioc<string>);
/**
*/
private ioc_data : (null | interface_ioc<Record<string, any>>);
/**
*/
public constructor(
retrieve : type_retrieve_data,
{
"hint": hint = null,
}
:
{
hint ?: (null | string);
}
=
{
}
)
{
this.retrieve_data = retrieve;
this.hint = hint;
this.ioc_id = null;
this.ioc_data = null;
}
/**
*/
private data_encode(
data : type_data
) : Record<string, any>
{
return {
"manufacturer": data.manufacturer,
"power": data.power,
};
}
/**
*/
private data_decode(
data_raw : Record<string, any>
) : type_data
{
return {
"manufacturer": data_raw["manufacturer"],
"power": data_raw["power"],
};
}
/**
* [implementation]
*/
public async setup(
element_target : Element
) : Promise<void>
{
const element_container : Element = document.createElement("div");
element_container.classList.add("formgen-ioc");
element_container.classList.add("formgen-ioc-zerez");
let dom_button_retrieve : HTMLButtonElement;
{
// hint
{
if (this.hint === null)
{
// do nothing
}
else
{
await (new formgen.info.class_info(this.hint)).setup(element_container);
}
}
// id ioc
{
this.ioc_id = new class_ioc_text(
{
"label": "ID",
}
);
await this.ioc_id.setup(element_container);
}
// retrieve button
{
dom_button_retrieve = document.createElement("button");
dom_button_retrieve.textContent = "ermitteln";
element_container.appendChild(dom_button_retrieve);
}
// data ioc
{
this.ioc_data = new class_ioc_group(
[
{
"name": "manufacturer",
"ioc": new class_ioc_text(
{
"label": "Hersteller",
}
),
"marked": false,
},
{
"name": "power",
"ioc": new class_ioc_number(
{
"label": "Leistung",
}
),
"marked": false,
},
]
);
await this.ioc_data.setup(element_container);
}
}
// interaction
{
dom_button_retrieve.addEventListener(
"click",
async () => {
if ((this.ioc_id === null) || (this.ioc_data == null))
{
// do nothing
}
else
{
const id : string = await this.ioc_id.read();
const data : type_data = await this.retrieve_data(id);
await this.ioc_data.write(this.data_encode(data));
}
}
);
}
element_target.appendChild(element_container);
}
/**
* [implementation]
*/
public async read(
) : Promise<type_value>
{
if (this.ioc_id === null)
{
throw (new Error("not properly initialized"));
}
else
{
return {
"id": await this.ioc_id.read(),
"data": this.data_decode(await this.ioc_data.read()),
};
}
}
/**
* [implementation]
*/
public async write(
value : type_value
) : Promise<void>
{
if (this.ioc_id === null)
{
throw (new Error("not properly initialized"));
}
else
{
if (value.data === null)
{
throw (new Error("invalid data"));
}
else
{
await this.ioc_id.write(value.id);
await this.ioc_data.write(this.data_encode(value.data));
}
}
}
/**
* [implementation]
*/
public listen(
handler : type_handler<type_value>
) : void
{
this.ioc_id.listen(handler);
this.ioc_data.listen(handler);
}
}
}