From 3d6323b80943e3439f748c0478422b2aa86b1eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr> Date: Wed, 11 Jan 2023 13:38:20 +0100 Subject: [PATCH 1/4] update jalhyd_branch to 338-optimiser-l-affichage-des-unites refs #590 --- jalhyd_branch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jalhyd_branch b/jalhyd_branch index 0bd0de843..9e2dece83 100644 --- a/jalhyd_branch +++ b/jalhyd_branch @@ -1 +1 @@ -334-restructurer-lechapt-et-calmon-pour-de-nouvelles-lois-de-pertes-de-charge +338-optimiser-l-affichage-des-unites -- GitLab From 117f1d12a214473003675821082b9d47c9a26571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr> Date: Wed, 11 Jan 2023 13:37:12 +0100 Subject: [PATCH 2/4] refactor : do not create a Nub to get results units refs #590 --- src/app/services/formulaire.service.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/app/services/formulaire.service.ts b/src/app/services/formulaire.service.ts index e54805c7d..880741539 100644 --- a/src/app/services/formulaire.service.ts +++ b/src/app/services/formulaire.service.ts @@ -58,6 +58,7 @@ import { FormulaireEspece } from "../formulaire/definition/form-espece"; import { FormulairePrebarrage } from "../formulaire/definition/form-prebarrage"; import { ServiceFactory } from "./service-factory"; import { FormulairePressureLoss } from "app/formulaire/definition/form-pressureloss"; +import { getNubResultUnit } from "jalhyd"; @Injectable() export class FormulaireService extends Observable { @@ -194,7 +195,7 @@ export class FormulaireService extends Observable { * Returns variable name and unit from symbol * @param calcType * @param symbol - * @param forceUnit if given, will be used as nuit + * @param forceUnit if given, will be used as unit */ public expandVariableNameAndUnit(calcType: CalculatorType, symbol: string, forceUnit?: string): string { let s = this.expandVariableName(calcType, symbol); @@ -217,16 +218,8 @@ export class FormulaireService extends Observable { if (forceUnit) { unit = forceUnit; } else { - // create dummy Nub from CalcType just to get results units - let dummyNub: Nub; - try { - dummyNub = Session.getInstance().createNub(new Props({ "calcType": calcType })); - } catch (e) { - // silent fail - } - if (dummyNub?.resultsUnits && dummyNub.resultsUnits[symbol]) { - unit = dummyNub.resultsUnits[symbol]; - } else { + unit = getNubResultUnit(calcType, symbol); + if (unit === undefined) { // last chance: if unit cannot be read in model, use translation files const unitKey = "UNIT_" + symbolBase; if (langCache && langCache[unitKey] !== undefined) { -- GitLab From fdaa1677d7c18f841054d4eda55e228305b7902e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr> Date: Wed, 11 Jan 2023 14:43:16 +0100 Subject: [PATCH 3/4] refactor : optimise select entry creation refs #590 --- .../elements/select/select-field-charttype.ts | 4 ++-- .../select/select-field-device-loi-debit.ts | 2 +- .../select/select-field-device-structure-type.ts | 2 +- .../select/select-field-downstream-basin.ts | 4 ++-- .../elements/select/select-field-nub-prop.ts | 2 +- .../elements/select/select-field-remous-target.ts | 4 ++-- .../elements/select/select-field-searched-param.ts | 2 +- .../select/select-field-solver-targeted-result.ts | 4 ++-- .../elements/select/select-field-solveur-target.ts | 2 +- .../elements/select/select-field-species-list.ts | 4 ++-- .../elements/select/select-field-target-pass.ts | 2 +- .../elements/select/select-field-upstream-basin.ts | 4 ++-- src/app/formulaire/elements/select/select-field.ts | 14 ++++++++++++++ 13 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/app/formulaire/elements/select/select-field-charttype.ts b/src/app/formulaire/elements/select/select-field-charttype.ts index b6f35d3f9..223dcaf39 100644 --- a/src/app/formulaire/elements/select/select-field-charttype.ts +++ b/src/app/formulaire/elements/select/select-field-charttype.ts @@ -20,7 +20,7 @@ export class SelectFieldChartType extends SelectField { protected populate() { for (const v of this._entryValues) { const id: string = ChartType[v]; - this.addEntry(new SelectEntry(id, v)); + this.addEntry(this.createOrGetEntry(id, v)); } } @@ -50,4 +50,4 @@ export class SelectFieldChartType extends SelectField { this._label = this.intlService.localizeText("INFO_PARAMFIELD_CHART_TYPE"); } -} \ No newline at end of file +} 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 2c556ff0a..07c1f555a 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 @@ -47,7 +47,7 @@ export class SelectFieldDeviceLoiDebit extends SelectField { const stName = StructureType[stCode]; if (la[stName] !== undefined) { for (const ld of la[stName]) { - const e: SelectEntry = new SelectEntry(this._entriesBaseId + LoiDebit[ld], ld); + const e: SelectEntry = this.createOrGetEntry(this._entriesBaseId + LoiDebit[ld], ld); this.addEntry(e); } } diff --git a/src/app/formulaire/elements/select/select-field-device-structure-type.ts b/src/app/formulaire/elements/select/select-field-device-structure-type.ts index aee929760..7b188d114 100644 --- a/src/app/formulaire/elements/select/select-field-device-structure-type.ts +++ b/src/app/formulaire/elements/select/select-field-device-structure-type.ts @@ -24,7 +24,7 @@ export class SelectFieldDeviceStructureType extends SelectField { protected populate() { // possible values depend on CalcType for (const st in (this.nub as ParallelStructure).getLoisAdmissibles()) { - const e: SelectEntry = new SelectEntry(this._entriesBaseId + st, StructureType[st]); + const e: SelectEntry = this.createOrGetEntry(this._entriesBaseId + st, StructureType[st]); this.addEntry(e); } } diff --git a/src/app/formulaire/elements/select/select-field-downstream-basin.ts b/src/app/formulaire/elements/select/select-field-downstream-basin.ts index dd39ce857..9c34a16e8 100644 --- a/src/app/formulaire/elements/select/select-field-downstream-basin.ts +++ b/src/app/formulaire/elements/select/select-field-downstream-basin.ts @@ -19,13 +19,13 @@ export class SelectFieldDownstreamBasin extends SelectField { for (const b of preBarrageD.bassins) { const pos = b.findPositionInParent(); if (posUb === undefined || pos > posUb) { - const e = new SelectEntry(this._entriesBaseId + b.uid, b.uid); + const e = this.createOrGetEntry(this._entriesBaseId + b.uid, b.uid); e.intlInfo = b.description; this.addEntry(e); } } // river downstream - const e = new SelectEntry(this._entriesBaseId + "none", undefined); + const e = this.createOrGetEntry(this._entriesBaseId + "none", undefined); e.intlInfo = "INFO_LIB_AVAL"; this.addEntry(e); } diff --git a/src/app/formulaire/elements/select/select-field-nub-prop.ts b/src/app/formulaire/elements/select/select-field-nub-prop.ts index df62610a8..4fa8c4e86 100644 --- a/src/app/formulaire/elements/select/select-field-nub-prop.ts +++ b/src/app/formulaire/elements/select/select-field-nub-prop.ts @@ -31,7 +31,7 @@ export class SelectFieldNubProperty extends SelectField { if (enumClass !== undefined) { // add one select entry per enum entry, in the enum order for (let j = 0; j < Object.keys(enumClass).length / 2; j++) { - this.addEntry(new SelectEntry(this._entriesBaseId + j, j)); + this.addEntry(this.createOrGetEntry(this._entriesBaseId + j, j)); } } } diff --git a/src/app/formulaire/elements/select/select-field-remous-target.ts b/src/app/formulaire/elements/select/select-field-remous-target.ts index 20b40cc47..f6eb90e6f 100644 --- a/src/app/formulaire/elements/select/select-field-remous-target.ts +++ b/src/app/formulaire/elements/select/select-field-remous-target.ts @@ -19,9 +19,9 @@ export class SelectFieldRemousTarget extends SelectField { protected populate() { // driven by string[], not enum (easier for variable names) - this.addEntry(new SelectEntry(this._entriesBaseId + "none", "")); + this.addEntry(this.createOrGetEntry(this._entriesBaseId + "none", "")); for (const at of CourbeRemous.availableTargets) { - const e: SelectEntry = new SelectEntry(this._entriesBaseId + at, at); + const e: SelectEntry = this.createOrGetEntry(this._entriesBaseId + at, at); this.addEntry(e); } } diff --git a/src/app/formulaire/elements/select/select-field-searched-param.ts b/src/app/formulaire/elements/select/select-field-searched-param.ts index 1342fb643..339acbfe5 100644 --- a/src/app/formulaire/elements/select/select-field-searched-param.ts +++ b/src/app/formulaire/elements/select/select-field-searched-param.ts @@ -28,7 +28,7 @@ export class SelectFieldSearchedParam extends SelectField { const calc = fs.getFormulaireFromId(p.originNub.uid).calculatorName; const varName = fs.expandVariableName(p.originNub.calcType, p.symbol); const label = `${p.symbol} - ${varName} (${calc})`; - this.addEntry(new SelectEntry(this._entriesBaseId + p.getParentComputeNode(false).uid + "_" + p.symbol, p, decodeHtml(label))); + this.addEntry(this.createOrGetEntry(this._entriesBaseId + p.getParentComputeNode(false).uid + "_" + p.symbol, p, decodeHtml(label))); } } } diff --git a/src/app/formulaire/elements/select/select-field-solver-targeted-result.ts b/src/app/formulaire/elements/select/select-field-solver-targeted-result.ts index a547bde3c..36094a8dd 100644 --- a/src/app/formulaire/elements/select/select-field-solver-targeted-result.ts +++ b/src/app/formulaire/elements/select/select-field-solver-targeted-result.ts @@ -24,12 +24,12 @@ export class SelectFieldSolverTargetedResult extends SelectField { // 1. calculated param const ntc = (this.nub as Solveur).nubToCalculate; if (ntc?.calculatedParam !== undefined) { // some nubs have no calculatedParam, for ex. SectionParam - this.addEntry(new SelectEntry(this._entriesBaseId + "none", "")); + this.addEntry(this.createOrGetEntry(this._entriesBaseId + "none", "")); } // 2. extra results if (ntc?.resultsFamilies !== undefined) { for (const er of Object.keys(ntc.resultsFamilies)) { - const e: SelectEntry = new SelectEntry(this._entriesBaseId + er, er); + const e: SelectEntry = this.createOrGetEntry(this._entriesBaseId + er, er); this.addEntry(e); } } diff --git a/src/app/formulaire/elements/select/select-field-solveur-target.ts b/src/app/formulaire/elements/select/select-field-solveur-target.ts index aecd238b8..b98546e22 100644 --- a/src/app/formulaire/elements/select/select-field-solveur-target.ts +++ b/src/app/formulaire/elements/select/select-field-solveur-target.ts @@ -33,7 +33,7 @@ export class SelectFieldSolverTarget extends SelectField { const varName = fs.expandVariableName(cn.calcType, cn.calculatedParam.symbol); label += ` / ${varName} (${cn.calculatedParam.symbol})`; } - this.addEntry(new SelectEntry(this._entriesBaseId + cn.uid, cn.uid, decodeHtml(label))); + this.addEntry(this.createOrGetEntry(this._entriesBaseId + cn.uid, cn.uid, decodeHtml(label))); } else { // silent fail, this Solveur nub was probably loaded before all the candidate nubs are done loading } diff --git a/src/app/formulaire/elements/select/select-field-species-list.ts b/src/app/formulaire/elements/select/select-field-species-list.ts index 9401148c3..7b94345aa 100644 --- a/src/app/formulaire/elements/select/select-field-species-list.ts +++ b/src/app/formulaire/elements/select/select-field-species-list.ts @@ -24,14 +24,14 @@ export class SelectFieldSpeciesList extends SelectField { const especeNubs = Session.getInstance().getAllNubs().filter((element) => element.calcType === CalculatorType.Espece); for (const en of especeNubs) { const form = ServiceFactory.formulaireService.getFormulaireFromNubId(en.uid); - const e = new SelectEntry(this._entriesBaseId + en.uid, en.uid); + const e = this.createOrGetEntry(this._entriesBaseId + en.uid, en.uid); e.intlInfo = { "isnub": true, "code": "INFO_VERIFICATEUR_CUSTOM_SPECIES", "id": en.uid }; this.addEntry(e); } // add all FishSpecies for (let j = 1; j < Object.keys(FishSpecies).length / 2; j++) { // exclude "0" (SPECIES_CUSTOM) const spgId = FishSpecies[j].substring(FishSpecies[j].lastIndexOf("_") + 1); - const e = new SelectEntry(this._entriesBaseId + spgId, FishSpecies[j]); + const e = this.createOrGetEntry(this._entriesBaseId + spgId, FishSpecies[j]); e.intlInfo = { "isnub": false, "code": FishSpecies[j] } this.addEntry(e); } diff --git a/src/app/formulaire/elements/select/select-field-target-pass.ts b/src/app/formulaire/elements/select/select-field-target-pass.ts index 10cd639a1..6383c02ff 100644 --- a/src/app/formulaire/elements/select/select-field-target-pass.ts +++ b/src/app/formulaire/elements/select/select-field-target-pass.ts @@ -33,7 +33,7 @@ export class SelectFieldTargetPass extends SelectField { const nub = fs.getFormulaireFromId(cn.uid); if (nub) { const label = nub.calculatorName + " (" + fs.getLocalisedTitleFromCalculatorType(nub.calculatorType) + ")"; - this.addEntry(new SelectEntry(this._entriesBaseId + cn.uid, cn.uid, decodeHtml(label))); + this.addEntry(this.createOrGetEntry(this._entriesBaseId + cn.uid, cn.uid, decodeHtml(label))); } else { // silent fail, this Verificateur nub was probably loaded before all the candidate nubs are done loading } diff --git a/src/app/formulaire/elements/select/select-field-upstream-basin.ts b/src/app/formulaire/elements/select/select-field-upstream-basin.ts index d240d097c..cc70f8a53 100644 --- a/src/app/formulaire/elements/select/select-field-upstream-basin.ts +++ b/src/app/formulaire/elements/select/select-field-upstream-basin.ts @@ -17,7 +17,7 @@ export class SelectFieldUpstreamBasin extends SelectField { const posDb = pbWallU.bassinAval?.findPositionInParent(); // river upstream - const e = new SelectEntry(this._entriesBaseId + "none", undefined); + const e = this.createOrGetEntry(this._entriesBaseId + "none", undefined); e.intlInfo = "INFO_LIB_AMONT"; this.addEntry(e); @@ -25,7 +25,7 @@ export class SelectFieldUpstreamBasin extends SelectField { for (const b of preBarrageU.bassins) { const pos = b.findPositionInParent(); if (posDb === undefined || pos < posDb) { - const e = new SelectEntry(this._entriesBaseId + b.uid, b.uid); + const e = this.createOrGetEntry(this._entriesBaseId + b.uid, b.uid); e.intlInfo = b.description; this.addEntry(e); } diff --git a/src/app/formulaire/elements/select/select-field.ts b/src/app/formulaire/elements/select/select-field.ts index 5e490c754..86a988004 100644 --- a/src/app/formulaire/elements/select/select-field.ts +++ b/src/app/formulaire/elements/select/select-field.ts @@ -36,6 +36,11 @@ export abstract class SelectField extends Field { */ protected _messageWhenEmpty: string; + /** + * map id <-> select entry + */ + protected _entryMap: { [key: string]: SelectEntry } = {}; + constructor(parent: FormulaireNode) { super(parent); this.clearEntries(); @@ -210,6 +215,15 @@ export abstract class SelectField extends Field { this._entries.push(e); } + protected createOrGetEntry(id: string, val: any, lbl?: string): SelectEntry { + let res: SelectEntry = this._entryMap[id]; + if (res === undefined) { + res = new SelectEntry(id, val, lbl); + this._entryMap[id] = res; + } + return res; + } + public getEntryFromValue(val: any): SelectEntry { for (const se of this._entries) { if (se.value === val) { -- GitLab From 1e28998663d86f2af5ea1b421c85ce62f22a6e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr> Date: Mon, 16 Jan 2023 13:34:32 +0100 Subject: [PATCH 4/4] test(e2e): temporarly disable Lechapt&Calmon test, see jalhyd#334, nghyd#585 refs #590 --- e2e/lechapt-calmon.e2e-spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/lechapt-calmon.e2e-spec.ts b/e2e/lechapt-calmon.e2e-spec.ts index f0b991c6d..4dd2d834c 100644 --- a/e2e/lechapt-calmon.e2e-spec.ts +++ b/e2e/lechapt-calmon.e2e-spec.ts @@ -36,11 +36,11 @@ describe("Lechapt&Calmon - ", () => { await browser.sleep(200); // open Lechapt-Calmon calculator - await listPage.clickMenuEntryForCalcType(1); + await listPage.clickMenuEntryForCalcType(35); await browser.sleep(200); } - it("when material is modified, results should change", async () => { + xit("when material is modified, results should change", async () => { // temporairement débranché, cf. jalhyd#334 / nghyd#585 await setup(); // select last material type -- GitLab