formgen/source/logic/ioc/group.ts

147 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-09-30 08:38:52 +02:00
namespace formgen.ioc
2026-04-22 07:47:23 +02:00
{
/**
*/
type type_field<type_value> = {
name : string;
2026-09-30 08:38:52 +02:00
ioc : interface_ioc<type_value>;
2026-04-22 22:24:32 +02:00
marked : boolean;
2026-04-22 07:47:23 +02:00
};
/**
*/
2026-09-30 08:38:52 +02:00
export class class_ioc_group implements interface_ioc<Record<string, any>>
2026-04-22 07:47:23 +02:00
{
/**
*/
private fields : Array<type_field<any>>;
/**
*/
private label : (null | string);
2026-09-30 08:38:52 +02:00
/**
*/
private notification : trait_notification<Record<string, any>>;
2026-04-22 07:47:23 +02:00
/**
*/
public constructor(
fields : Array<type_field<any>>,
{
"label": label = null,
2026-09-30 08:38:52 +02:00
"handlers": handlers = [],
2026-04-22 07:47:23 +02:00
}
:
{
label ?: (null | string);
2026-09-30 08:38:52 +02:00
handlers ?: Array<type_handler<Record<string, any>>>;
2026-04-22 07:47:23 +02:00
}
=
{
}
)
{
this.fields = fields;
this.label = label;
2026-09-30 08:38:52 +02:00
this.notification = new trait_notification(handlers);
2026-04-22 07:47:23 +02:00
}
/**
* [implementation]
*/
public async setup(
element_target : Element
) : Promise<void>
{
const element_container : Element = document.createElement("div");
2026-09-30 08:38:52 +02:00
element_container.classList.add("formgen-ioc");
element_container.classList.add("formgen-ioc-group");
2026-04-22 22:24:32 +02:00
// label
{
if (this.label === null)
{
// do nothing
}
else
{
const element_label : Element = document.createElement("label");
element_label.textContent = this.label;
element_container.appendChild(element_label);
}
}
// fields
2026-04-22 07:47:23 +02:00
{
2026-04-22 22:24:32 +02:00
const element_fields : Element = document.createElement("div");
2026-09-30 08:38:52 +02:00
element_fields.classList.add("formgen-ioc-group-fields");
2026-04-22 22:24:32 +02:00
for (const field of this.fields)
{
const element_field : Element = document.createElement("div");
2026-09-30 08:38:52 +02:00
element_field.classList.add("formgen-ioc-group-field");
element_field.classList.toggle("formgen-ioc-group-field-marked", field.marked);
2026-04-22 22:24:32 +02:00
element_field.setAttribute("rel", field.name);
2026-09-30 08:38:52 +02:00
await field.ioc.setup(element_field);
field.ioc.listen(
async () => {
const value : Record<string, any> = await this.read();
await this.notification.notify(value);
}
);
2026-04-22 22:24:32 +02:00
element_fields.appendChild(element_field);
}
element_container.appendChild(element_fields);
2026-04-22 07:47:23 +02:00
}
element_target.appendChild(element_container);
return Promise.resolve<void>(undefined);
}
/**
* [implementation]
*/
public async read(
) : Promise<Record<string, any>>
{
let result : Record<string, any> = {};
for (const field of this.fields)
{
2026-09-30 08:38:52 +02:00
result[field.name] = await field.ioc.read();
2026-04-22 07:47:23 +02:00
}
return result;
}
/**
* [implementation]
*/
2026-09-30 08:38:52 +02:00
public async write(
2026-04-22 07:47:23 +02:00
value : Record<string, any>
) : Promise<void>
{
2026-09-30 08:38:52 +02:00
for (const field of this.fields)
{
await field.ioc.write(value[field.name]);
}
}
/**
*/
public listen(
handler : type_handler<Record<string, any>>
) : void
{
this.notification.listen(handler);
2026-04-22 07:47:23 +02:00
}
}
}