namespace formgen.ioc { /** */ type type_field = { name : string; ioc : interface_ioc; marked : boolean; }; /** */ export class class_ioc_group implements interface_ioc> { /** */ private fields : Array>; /** */ private label : (null | string); /** */ private notification : trait_notification>; /** */ public constructor( fields : Array>, { "label": label = null, "handlers": handlers = [], } : { label ?: (null | string); handlers ?: Array>>; } = { } ) { this.fields = fields; this.label = label; this.notification = new trait_notification(handlers); } /** * [implementation] */ public async setup( element_target : Element ) : Promise { 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 = 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(undefined); } /** * [implementation] */ public async read( ) : Promise> { let result : Record = {}; for (const field of this.fields) { result[field.name] = await field.ioc.read(); } return result; } /** * [implementation] */ public async write( value : Record ) : Promise { for (const field of this.fields) { await field.ioc.write(value[field.name]); } } /** */ public listen( handler : type_handler> ) : void { this.notification.listen(handler); } } }