@securecall/client-component
Version:
SecureCall Core Web Component
81 lines (80 loc) • 2.81 kB
JavaScript
import { RequestField } from "./request_field";
import { FieldLevelType } from "./request_field_group";
import { MetadataField } from "./metadata_field";
export class RequestFieldList {
_fields;
_uiFields;
_level;
constructor(uiFields, fieldLevel = FieldLevelType.Instance) {
this._uiFields = uiFields;
this._level = fieldLevel;
this._fields = { metadata: new MetadataField() };
return new Proxy(this, {
get(target, prop, receiver) {
const propStr = prop.toString();
if (target.parent.fieldNames.includes(propStr)) {
if (!target.rawFields[propStr]) {
target.rawFields[propStr] = new RequestField(target, propStr);
}
return target.rawFields[propStr];
}
return Reflect.get(target, prop, receiver);
},
});
}
// Remove the _uiFields reference, otherwise we get circular references
toJSON() {
const serializableData = { ...this };
delete serializableData._uiFields;
return serializableData;
}
get parent() {
return this._uiFields;
}
get rawFields() {
return this._fields;
}
addNewField(name, config) {
if (!this.parent.fieldNames.includes(name)) {
this.parent.fieldNames.push(name);
Object.entries(config).forEach(([k, v]) => {
this[name][k] = v;
});
}
else {
throw new Error(`Field ${name} already exists in the request fields list`);
}
}
addOrUpdateField(name, config) {
if (!this.parent.fieldNames.includes(name)) {
this.parent.fieldNames.push(name);
}
Object.entries(config).forEach(([k, v]) => {
this[name][k] = v;
});
}
propagate(fieldName, prop, value) {
if (this.parent.fieldNames.includes(fieldName)) {
if (this._level === FieldLevelType.Instance) {
this.propagateValue(this.parent.session, fieldName, prop, value);
}
else if (this._level === FieldLevelType.Session) {
this.propagateValue(this.parent.transaction, fieldName, prop, value);
}
}
}
propagateValue(fieldList, fieldName, prop, value) {
if (fieldList.rawFields[fieldName]) {
fieldList[fieldName][prop] = value;
}
}
calculatedFields() {
return Object.fromEntries(Object.entries(this._fields)
.filter(([key]) => this.parent.fieldNames.includes(key))
.map(([fieldName, field]) => [
fieldName,
field.calculatedValue()
]));
}
}
//# sourceMappingURL=request_field_list.js.map