Skip to content
Snippets Groups Projects
Commit fdaa1677 authored by François Grand's avatar François Grand
Browse files

refactor : optimise select entry creation

refs #590
parent 117f1d12
No related branches found
No related tags found
2 merge requests!225Release v4.17.0,!195Resolve "Optimiser l'affichage des unités"
Pipeline #140121 passed
Showing
with 32 additions and 18 deletions
......@@ -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
}
......@@ -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);
}
}
......
......@@ -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);
}
}
......
......@@ -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);
}
......
......@@ -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));
}
}
}
......
......@@ -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);
}
}
......
......@@ -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)));
}
}
}
......
......@@ -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);
}
}
......
......@@ -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
}
......
......@@ -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);
}
......
......@@ -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
}
......
......@@ -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);
}
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment