diff --git a/src/app/calculators/verificateur/config.json b/src/app/calculators/verificateur/config.json index 95ad28bb603de4e97a15f20770c18c30a6268448..fb1779508897b7e85db359f8cb31e81451c7d56b 100644 --- a/src/app/calculators/verificateur/config.json +++ b/src/app/calculators/verificateur/config.json @@ -21,9 +21,8 @@ "type": "fieldset", "fields": [ { - "type": "select", + "type": "select_custom", "id": "select_species_list", - "property": "speciesList", "source": "verificateur_species", "multiple": true } @@ -31,8 +30,8 @@ }, { "type": "options", - "selectIds": [ "select_pab_jet_type", "select_species_list" ], - "customSelectIds": [ "select_target_pass" ], + "selectIds": [ "select_pab_jet_type" ], + "customSelectIds": [ "select_target_pass", "select_species_list" ], "help": "verification/verificateur.html" } ] diff --git a/src/app/formulaire/definition/form-verificateur.ts b/src/app/formulaire/definition/form-verificateur.ts index 2e55695991c174dd72c825cc1473e3604f9e2cc4..7546562ae952b7d6608a7ff32a5f51bbe7d24c39 100644 --- a/src/app/formulaire/definition/form-verificateur.ts +++ b/src/app/formulaire/definition/form-verificateur.ts @@ -28,27 +28,15 @@ export class FormulaireVerificateur extends FormulaireFixedVar { console.log("> update", data, sender.constructor.name); if (sender instanceof SelectFieldCustom) { - if (data.action === "select") { + if (sender.id === "select_target_pass" && data.action === "select") { // update Verificateur property: Pass to check - let v = undefined; - if (data && data.value && data.value.value) { - v = data.value.value; - } this._currentNub.properties.setPropValue("nubToVerify", data.value); // @TODO refresh jet type selector + } else if (sender.id === "select_species_list" && data.action === "select") { + // update Verificateur property: Species list (string[]) + this._currentNub.properties.setPropValue("speciesList", data.value.map((v: any) => v.value)); } } } - public onCalculatorInit() { - // refresh species list selector on each display, because Session might have gained new Espece nubs - console.log(">>>> speciesList avant rafraîchissement :", (this.currentNub as Verificateur).speciesList); - /* const pSel = this.getFormulaireNodeById(this._speciesListSelectId) as SelectField; - if (pSel) { - pSel.clearEntries(); - pSel.parseConfig(); - // (pSel.parent as FieldSet).updateFields() - } */ - } - } diff --git a/src/app/formulaire/elements/select-field-custom.ts b/src/app/formulaire/elements/select-field-custom.ts index 6c57eba105229a228bf06798eb654df23999a7ba..de74366251b14a6b836497e929c81b3e8ffd3e61 100644 --- a/src/app/formulaire/elements/select-field-custom.ts +++ b/src/app/formulaire/elements/select-field-custom.ts @@ -1,9 +1,11 @@ import { SelectEntry } from "./select-entry"; import { ServiceFactory } from "../../services/service-factory"; +import { SelectField } from './select-field'; import { decodeHtml, arraysAreEqual } from "../../util"; -import { Session, Solveur, FishPass, CalculatorType, Verificateur, Nub } from "jalhyd"; -import { SelectField } from './select-field'; +import { FishSpecies, Session, Solveur, FishPass, CalculatorType, Verificateur, Nub } from "jalhyd"; + +import { sprintf } from 'sprintf-js'; /** * A select field that populates itself with custom stuff (ex: references to Nubs, Parameters…) @@ -38,6 +40,13 @@ export class SelectFieldCustom extends SelectField { this.setValueFromId(this._entriesBaseId + ntv.uid); } break; + + case "verificateur_species": // Vérificateur, liste d'espèces (choix multiple) + const sl = (nub as Verificateur).speciesList; + if (sl !== undefined) { + this.setValueFromId(sl.map((s) => this._entriesBaseId + s )); + } + break; } } @@ -112,6 +121,27 @@ export class SelectFieldCustom extends SelectField { } break; + case "verificateur_species": + console.log("[i] loading verif species"); + // add UIDs of all Espece type Nubs in the session + const especeNubs = Session.getInstance().getAllNubs().filter((element) => element.calcType === CalculatorType.Espece); + for (const en of especeNubs) { + this.addEntry( + new SelectEntry( + en.uid, + en.uid, + sprintf( + ServiceFactory.instance.i18nService.localizeText("INFO_VERIFICATEUR_CUSTOM_SPECIES"), + ServiceFactory.instance.formulaireService.getFormulaireFromNubId(en.uid).calculatorName + ) + ) + ); + } + // add all FishSpecies + for (let j = 0; j < Object.keys(FishSpecies).length / 2; j++) { + this.addEntry(new SelectEntry(this._entriesBaseId + j, String(j), "Groupe d'espèces " + j)); + } + break; } } @@ -157,9 +187,15 @@ export class SelectFieldCustom extends SelectField { super.setValue(undefined); } } else { - // keep previously selected entry if possible @TODO manage multiple values - if (pse && ! Array.isArray(pse) && pse.id) { - this.setValueFromId(pse.id); + // keep previously selected entry(ies) if possible + if (pse) { + if (! Array.isArray(pse) && pse.id) { + this.setValueFromId(pse.id); + } else if (Array.isArray(pse) && pse.length > 0) { + this.setValueFromId(pse.map((e) => e.id )); + } else { + this.setDefaultValue(); + } } else { this.setDefaultValue(); } @@ -172,6 +208,10 @@ export class SelectFieldCustom extends SelectField { */ public setValue(v: SelectEntry | SelectEntry[]) { const previousSelectedEntry = this._selectedEntry; + /* if (Array.isArray(v)) { + // keep selection ordered + v.sort((a,b) => (a.value > b.value) ? -1 : ((b.value > a.value) ? 1 : 0)); + } */ this._selectedEntry = v; // if value changed let valueChanged = ( @@ -184,7 +224,7 @@ export class SelectFieldCustom extends SelectField { || ( Array.isArray(previousSelectedEntry) && Array.isArray(v) - && arraysAreEqual(previousSelectedEntry, v, "id", true) + && ! arraysAreEqual(previousSelectedEntry, v, "id", true) ) ); if (valueChanged) { @@ -193,17 +233,24 @@ export class SelectFieldCustom extends SelectField { } /** - * Sets value from given ID; if it was not found, sets the + * Sets value from given ID(s); if it was not found, sets the * first available entry as selectedValue */ - public setValueFromId(id: string) { + public setValueFromId(id: string | string[]) { let found = false; - for (const e of this._entries) { - if (e.id === id) { - found = true; - if (this._multiple) { - this.setValue([ e ]); - } else { + let entries = []; + if (this._multiple && Array.isArray(id)) { + for (const e of this._entries) { + if (id.includes(e.id)) { + entries.push(e); + found = true; + } + } + this.setValue(entries); + } else { + for (const e of this._entries) { + if (e.id === id) { + found = true; this.setValue(e); } } diff --git a/src/app/formulaire/elements/select-field.ts b/src/app/formulaire/elements/select-field.ts index 4b10ec0b7e60b77680252d854a928c5477572f25..4d04e9a9a14d86e38a59449d8c254164d4966edb 100644 --- a/src/app/formulaire/elements/select-field.ts +++ b/src/app/formulaire/elements/select-field.ts @@ -5,8 +5,7 @@ import { StructureType, LoiDebit, Session, - Solveur, - CalculatorType + Solveur } from "jalhyd"; import { Field } from "./field"; @@ -15,8 +14,6 @@ import { StringMap } from "../../stringmap"; import { FormulaireNode } from "./formulaire-node"; import { FormulaireDefinition } from "../definition/form-definition"; import { ServiceFactory } from "../../services/service-factory"; -import { FishSpecies } from 'jalhyd/build/verification/fish_species'; -import { sprintf } from 'sprintf-js'; export class SelectField extends Field { @@ -201,29 +198,6 @@ export class SelectField extends Field { } break; - // possible values depend on Session @TODO does not work, because not refreshed when Session gets new Espece nubs - case "verificateur_species": - console.log("[i] loading verif species"); - // add UIDs of all Espece type Nubs in the session - const especeNubs = Session.getInstance().getAllNubs().filter((element) => element.calcType === CalculatorType.Espece); - for (const en of especeNubs) { - this.addEntry( - new SelectEntry( - en.uid, - en.uid, - sprintf( - ServiceFactory.instance.i18nService.localizeText("INFO_VERIFICATEUR_CUSTOM_SPECIES"), - ServiceFactory.instance.formulaireService.getFormulaireFromNubId(en.uid).calculatorName - ) - ) - ); - } - // add all FishSpecies - for (let j = 0; j < Object.keys(FishSpecies).length / 2; j++) { - this.addEntry(new SelectEntry(this._entriesBaseId + j, j, "Groupe d'espèces " + j)); - } - break; - // general case : property values taken from an enum default: // find enum associated to property