UNPKG

idea-toolbox

Version:
60 lines (59 loc) 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomSectionMeta = void 0; const resource_model_1 = require("./resource.model"); const customFieldMeta_model_1 = require("./customFieldMeta.model"); const label_model_1 = require("./label.model"); /** * A custom section meta containing any number of custom fields meta. */ class CustomSectionMeta extends resource_model_1.Resource { load(x, languages) { super.load(x); if (x.name) this.name = new label_model_1.Label(x.name, languages); if (x.description) this.description = new label_model_1.Label(x.description, languages); this.fieldsLegend = this.cleanArray(x.fieldsLegend, String); this.fields = {}; x.fields = x.fields ?? {}; this.fieldsLegend.forEach(f => (this.fields[f] = new customFieldMeta_model_1.CustomFieldMeta(x.fields[f], languages))); if (x.displayTemplate) this.displayTemplate = (x.displayTemplate ?? []).map((z) => this.cleanArray(z, String) // filter out of the displayTemplate the fields which aren't in the fieldsLegend .filter(dpf => this.fieldsLegend.some(f => f === dpf))); } validate(languages) { const e = super.validate(); this.fieldsLegend.forEach(f => this.fields[f].validate(languages).forEach(ea => e.push(`${f}.${ea}`))); return e; } /** * Set the default values of the specified fields. */ setFieldsDefaultValues() { const fields = {}; this.fieldsLegend.forEach(f => (fields[f] = this.fields[f].fieldDefaultValue())); return fields; } /** * Load the values of the specified fields. * @param newFields the values to set in the fields */ loadFields(newFields) { const fields = {}; newFields = newFields ?? {}; this.fieldsLegend.forEach(f => (fields[f] = this.fields[f].loadField(newFields[f]))); return fields; } /** * Validate the fields and return an array with errors, if any. */ validateFields(fields) { fields = fields ?? {}; const e = new Array(); this.fieldsLegend.forEach(f => (!this.fields[f].validateField(fields[f]) ? e.push(f) : null)); return e; } } exports.CustomSectionMeta = CustomSectionMeta;