147 lines
2.8 KiB
TypeScript
147 lines
2.8 KiB
TypeScript
namespace formgen.ioc
|
|
{
|
|
|
|
/**
|
|
*/
|
|
type type_field<type_value> = {
|
|
name : string;
|
|
ioc : interface_ioc<type_value>;
|
|
marked : boolean;
|
|
};
|
|
|
|
|
|
/**
|
|
*/
|
|
export class class_ioc_group implements interface_ioc<Record<string, any>>
|
|
{
|
|
|
|
/**
|
|
*/
|
|
private fields : Array<type_field<any>>;
|
|
|
|
|
|
/**
|
|
*/
|
|
private label : (null | string);
|
|
|
|
|
|
/**
|
|
*/
|
|
private notification : trait_notification<Record<string, any>>;
|
|
|
|
|
|
/**
|
|
*/
|
|
public constructor(
|
|
fields : Array<type_field<any>>,
|
|
{
|
|
"label": label = null,
|
|
"handlers": handlers = [],
|
|
}
|
|
:
|
|
{
|
|
label ?: (null | string);
|
|
handlers ?: Array<type_handler<Record<string, any>>>;
|
|
}
|
|
=
|
|
{
|
|
}
|
|
)
|
|
{
|
|
this.fields = fields;
|
|
this.label = label;
|
|
this.notification = new trait_notification(handlers);
|
|
}
|
|
|
|
|
|
/**
|
|
* [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-group");
|
|
// 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
|
|
{
|
|
const element_fields : Element = document.createElement("div");
|
|
element_fields.classList.add("formgen-ioc-group-fields");
|
|
for (const field of this.fields)
|
|
{
|
|
const element_field : Element = document.createElement("div");
|
|
element_field.classList.add("formgen-ioc-group-field");
|
|
element_field.classList.toggle("formgen-ioc-group-field-marked", field.marked);
|
|
element_field.setAttribute("rel", field.name);
|
|
await field.ioc.setup(element_field);
|
|
field.ioc.listen(
|
|
async () => {
|
|
const value : Record<string, any> = await this.read();
|
|
await this.notification.notify(value);
|
|
}
|
|
);
|
|
element_fields.appendChild(element_field);
|
|
}
|
|
element_container.appendChild(element_fields);
|
|
}
|
|
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)
|
|
{
|
|
result[field.name] = await field.ioc.read();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public async write(
|
|
value : Record<string, any>
|
|
) : Promise<void>
|
|
{
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
}
|