diff --git a/DEVELOPERS.md b/DEVELOPERS.md index e69e952555f21c567b08cb422a4a0cac12d3643a..908e88488f942ca08320d8c7563d30c4f975aae4 100644 --- a/DEVELOPERS.md +++ b/DEVELOPERS.md @@ -497,23 +497,23 @@ Dans chaque fichier de langue du dossier `src/locale`, ajouter les traductions p Il faut utiliser ou étendre `FormulaireParallelStructure` (ex: Cloisons, Dever…). -Dans la configuration du module, dans le "fieldset_template", ajouter un sélecteur de structure associé à la propriété "structureType" et un sélecteur de loi de débit associé à la propriété "loiDebit", noter les propriétés "calcType" (toujours "Structure" dans ce cas), "defaultStructType" et "defaultLoiDebit" : +Dans la configuration du module, dans le "fieldset_template", ajouter un sélecteur de structure et un sélecteur de loi de débit, ajouter les propriétés "calcType" (toujours "Structure" dans ce cas) et les valeurs par défaut avec "default" dans la définition du select. A noter que les ids des select sont fixés et doivent obligatoirement être "select_structure" et "select_loidebit" : ```json { "id": "fs_ouvrage", "type": "fieldset_template", "calcType": "Structure", - "defaultStructType": "VanneFondRectangulaire", - "defaultLoiDebit": "GateCem88v", "fields": [ { "id": "select_structure", "type": "select", + "default": "VanneFondRectangulaire", }, { "id": "select_loidebit", "type": "select", + "default": "GateCem88v", "help": { "Orifice_OrificeSubmerged": "structures/orifice_noye.html", "SeuilRectangulaire_WeirVillemonte": "structures/kivi.html", diff --git a/src/app/formulaire/definition/form-parallel-structures.ts b/src/app/formulaire/definition/form-parallel-structures.ts index 2fbc67186b3e6b0f11c470993b1a36c1d97ffeab..b47c4808b021bba832df9c201b65ebc4aadb76f5 100644 --- a/src/app/formulaire/definition/form-parallel-structures.ts +++ b/src/app/formulaire/definition/form-parallel-structures.ts @@ -57,11 +57,6 @@ export class FormulaireParallelStructure extends FormulaireRepeatableFieldset { } protected createChildNub(templ: FieldsetTemplate): Nub { - // !!! attention !!! - // Il doit y avoir cohérence dans le fichier de conf entre les valeurs defaultXXX et les valeurs possibles pour les select - // cad valeur par défaut du 1er select (type d'ouvrage), du 2ème (loi de débit). - // A terme, il faudrait analyser le fichier de conf (dépendances d'existence) pour déterminer automatiquement ces valeurs - const params = {}; params["calcType"] = templ.calcTypeFromConfig; params["structureType"] = templ.defaultStructTypeFromConfig; diff --git a/src/app/formulaire/elements/fieldset-container.ts b/src/app/formulaire/elements/fieldset-container.ts index 23b5488953e9c72500de7ecd8babd9bbacf86948..0f57d21fe479e6690b8eef1a7a2ac52aff1d7e5b 100644 --- a/src/app/formulaire/elements/fieldset-container.ts +++ b/src/app/formulaire/elements/fieldset-container.ts @@ -21,7 +21,7 @@ export class FieldsetContainer extends FormulaireElement { if (this._template !== undefined) { throw new Error("template already set!"); } - this._template = new FieldsetTemplate(fst); + this._template = fst; } public addFieldset(fs: FieldSet) { @@ -105,7 +105,9 @@ export class FieldsetContainer extends FormulaireElement { for (const t of templateNames) { for (const d of templates) { if (d.id === t) { - this.template = d; + const tmpl = new FieldsetTemplate(); + tmpl.parseConfig(d); + this.template = tmpl; } } } diff --git a/src/app/formulaire/elements/fieldset-template.ts b/src/app/formulaire/elements/fieldset-template.ts index ddad4008b3ea65aba067b329973a6b87948f98d2..9147d2cbb7431630e90a8b1f4b3d9d713fb922d6 100644 --- a/src/app/formulaire/elements/fieldset-template.ts +++ b/src/app/formulaire/elements/fieldset-template.ts @@ -2,13 +2,16 @@ import { FieldSet } from "./fieldset"; import { CalculatorType, LoiDebit, Nub, StructureType, SectionType } from "jalhyd"; import { FormulaireDefinition } from "../definition/form-definition"; import { FieldsetContainer } from "./fieldset-container"; +import { SelectFieldFactory } from "./select/select-field-factory"; export class FieldsetTemplate { private _jsonConfig: {}; - constructor(config: {}) { - this._jsonConfig = config; - } + // parsed default structure type from JSON configuration file (from select_structure field) + private _defaultStructureType: StructureType; + + // parsed default stage discharge law from JSON configuration file (from select_loidebit field) + private _defaultLoiDebit: LoiDebit; public get config() { return this._jsonConfig; @@ -20,16 +23,41 @@ export class FieldsetTemplate { } public get defaultStructTypeFromConfig(): StructureType { - const st: string = this._jsonConfig["defaultStructType"]; - return StructureType[st]; + return this._defaultStructureType; } public get defaultLoiDebitFromConfig(): LoiDebit { - const ld: string = this._jsonConfig["defaultLoiDebit"]; - if (LoiDebit[ld] === undefined) { - throw new Error(`FieldsetTemplate.defaultLoiDebitFromConfig: La loi de débit ${ld} n'est pas définie`); + return this._defaultLoiDebit; + } + + private findSelectField(id: string): any { + for (const f of this._jsonConfig["fields"]) { + if (f["type"] === "select" && f["id"] === id) { + return f; + } + } + } + + public parseConfig(config: {}) { + this._jsonConfig = config; + + if (this._jsonConfig["calcType"] === "Structure") { + // parse default stage discharge law + + const ld: string = this.findSelectField(SelectFieldFactory.SelectId_LoiDebit)["default"]; + if (LoiDebit[ld] === undefined) { + throw new Error(`FieldsetTemplate.defaultLoiDebitFromConfig : la loi de débit ${ld} n'est pas définie`); + } + this._defaultLoiDebit = LoiDebit[ld]; + + // parse default structure type + + const st: string = this.findSelectField(SelectFieldFactory.SelectId_StructureType)["default"]; + if (StructureType[st] === undefined) { + throw new Error(`FieldsetTemplate.defaultStructTypeFromConfig : le type de structure ${st} n'est pas défini`); + } + this._defaultStructureType = StructureType[st]; } - return LoiDebit[ld]; } /** diff --git a/src/app/formulaire/elements/select/select-field-device-loi-debit.ts b/src/app/formulaire/elements/select/select-field-device-loi-debit.ts index 8acbca1787484d4af430135800095a3702f36fb8..2c556ff0a54de8e207e0d6e077e53f3f198d9039 100644 --- a/src/app/formulaire/elements/select/select-field-device-loi-debit.ts +++ b/src/app/formulaire/elements/select/select-field-device-loi-debit.ts @@ -22,13 +22,27 @@ export class SelectFieldDeviceLoiDebit extends SelectField { this._configDefaultValue = json["default"]; } + /** + * get this select's nub + */ + protected get nub(): ParallelStructure { + return super.nub as ParallelStructure; + } + + /** + * get discharge law linked to this select's nub + */ + private get loiDebit(): LoiDebit { + const child = this.nub.getChildren()[this.parent.indexAsKid()]; + return child.properties.getPropValue("loiDebit"); + } + protected populate() { // possible values depend on CalcType // get current structure type from appropriate Nub child - const child = this.nub.getChildren()[this.parent.indexAsKid()]; - const la = (this.nub as ParallelStructure).getLoisAdmissibles(); - const loiDebit = child.properties.getPropValue("loiDebit"); + const la = this.nub.getLoisAdmissibles(); + const loiDebit: LoiDebit = this.loiDebit; const stCode = StructureProperties.findCompatibleStructure(loiDebit, this.nub as ParallelStructure); const stName = StructureType[stCode]; if (la[stName] !== undefined) { @@ -40,6 +54,7 @@ export class SelectFieldDeviceLoiDebit extends SelectField { } protected initSelectedValue() { - this.findAndSetDefaultValue(); + // current discharge law from linked nub + this.findAndSetDefaultValue(LoiDebit[this.loiDebit]); } } diff --git a/src/app/formulaire/elements/select/select-field-factory.ts b/src/app/formulaire/elements/select/select-field-factory.ts index c7ae60316a39bd06ba8fd4b964c4fe269c3356b1..49fed0c91265f84c008fff570c024693adf85591 100644 --- a/src/app/formulaire/elements/select/select-field-factory.ts +++ b/src/app/formulaire/elements/select/select-field-factory.ts @@ -13,12 +13,24 @@ import { SelectFieldTargetPass } from "./select-field-target-pass"; import { SelectFieldNubProperty } from "./select-field-nub-prop"; export class SelectFieldFactory { + /** + * Id of the select linked to structure type (JSON calculator configuration). + * Also used in fieldset template parsing. + */ + public static readonly SelectId_StructureType: string ="select_structure"; + + /** + * Id of the select linked to stage discharge law (JSON calculator configuration). + * Also used in fieldset template parsing. + */ + public static readonly SelectId_LoiDebit: string ="select_loidebit"; + public static newSelectField(json: {}, parent: FormulaireNode): SelectField { switch (json["id"]) { case "select_downstream_basin": return new SelectFieldDownstreamBasin(parent); - case "select_loidebit": + case SelectFieldFactory.SelectId_LoiDebit: return new SelectFieldDeviceLoiDebit(parent); case "select_searched_param": @@ -27,7 +39,7 @@ export class SelectFieldFactory { case "select_species_list": return new SelectFieldSpeciesList(parent); - case "select_structure": + case SelectFieldFactory.SelectId_StructureType: return new SelectFieldDeviceStructureType(parent); case "select_target": diff --git a/src/app/formulaire/elements/select/select-field.ts b/src/app/formulaire/elements/select/select-field.ts index f76690de496b84e24f0101cdf5cd732f0d07a1fd..5e490c754d4246759a7ffd9e820b50be973188cd 100644 --- a/src/app/formulaire/elements/select/select-field.ts +++ b/src/app/formulaire/elements/select/select-field.ts @@ -125,11 +125,13 @@ export abstract class SelectField extends Field { /** * try to find a default value to select */ - protected findAndSetDefaultValue() { + protected findAndSetDefaultValue(value?: string) { // default to first available entry if any if (this._entries.length > 0) { - let val; - if (this._configDefaultValue === undefined) { + let val: SelectEntry; + if (value !== undefined) { + val = this.getEntryFromValue(enumValueFromString(this._associatedProperty, value)); + } else if (this._configDefaultValue === undefined) { val = this._entries[0]; } else { val = this.getEntryFromValue(enumValueFromString(this._associatedProperty, this._configDefaultValue));