From 108fdbee4260d4d39e8bb5194dbcb7e306aa71ea Mon Sep 17 00:00:00 2001
From: Mathias Chouet <mathias.chouet@irstea.fr>
Date: Wed, 22 Apr 2020 10:31:55 +0200
Subject: [PATCH] Fix species_list select assignation and sorting

---
 .../generic-select.component.html             |  2 +-
 .../select-field-line.component.ts            | 27 -------------------
 .../definition/form-verificateur.ts           |  6 +++--
 .../elements/select-field-custom.ts           | 13 ++++++---
 src/locale/messages.en.json                   |  1 +
 src/locale/messages.fr.json                   |  1 +
 6 files changed, 17 insertions(+), 33 deletions(-)

diff --git a/src/app/components/generic-select/generic-select.component.html b/src/app/components/generic-select/generic-select.component.html
index ba4971cc0..8c352a2cf 100644
--- a/src/app/components/generic-select/generic-select.component.html
+++ b/src/app/components/generic-select/generic-select.component.html
@@ -1,7 +1,7 @@
 <mat-form-field>
     <mat-select [id]="selectId" [placeholder]="label" [(value)]="selectedValue" [multiple]="isMultiple" [disabled]="isDisabled">
         <mat-select-trigger *ngIf="isMultiple">
-          {{ sortedSelectedValues && sortedSelectedValues[0] ? sortedSelectedValues[0].label : '' }}
+          {{ selectedValue && selectedValue[0] ? selectedValue[0].label : '' }}
           <span *ngIf="selectedValue?.length > 1" class="multiple-selection-label">
             (+ {{ selectedValue.length - 1 }} {{ selectedValue?.length === 2 ? uitextAndOther : uitextAndOthers }})
           </span>
diff --git a/src/app/components/select-field-line/select-field-line.component.ts b/src/app/components/select-field-line/select-field-line.component.ts
index a8f76d36d..09d05640b 100644
--- a/src/app/components/select-field-line/select-field-line.component.ts
+++ b/src/app/components/select-field-line/select-field-line.component.ts
@@ -53,33 +53,6 @@ export class SelectFieldLineComponent implements OnInit {
         return this._select.getValue();
     }
 
-    /**
-     * Present selected values in a meaningful order, for multiple select box label (… + n others)
-     */
-    public get sortedSelectedValues(): SelectEntry[] {
-        let ssv: any[] = undefined;
-        if (Array.isArray(this.selectedValue)) {
-            ssv = JSON.parse(JSON.stringify(this.selectedValue)); // array copy
-            ssv.sort((a, b) => {
-                // numbers ?
-                if (! isNaN(Number(a._value)) && ! isNaN(Number(b._value))) {
-                    return (Number(a._value) > Number(b._value)) ? 1 : ((Number(b._value) > Number(a._value)) ? -1 : 0)
-                } else {
-                    // numbers after strings @TODO convenient for Verificateur/SpeciesList, but elsewhere ?
-                    if (! isNaN(Number(a._value))) {
-                        return 1;
-                    } else if (! isNaN(Number(b._value))) {
-                        return -1;
-                    } else {
-                        // keep original order
-                        return 0;
-                    }
-                }
-            });
-        }
-        return ssv;
-    }
-
     public get isValid(): boolean {
         return (this._select.getValue() !== undefined);
     }
diff --git a/src/app/formulaire/definition/form-verificateur.ts b/src/app/formulaire/definition/form-verificateur.ts
index 586daaafa..0c7db5328 100644
--- a/src/app/formulaire/definition/form-verificateur.ts
+++ b/src/app/formulaire/definition/form-verificateur.ts
@@ -29,14 +29,16 @@ export class FormulaireVerificateur extends FormulaireFixedVar {
         if (sender instanceof SelectFieldCustom) {
             if (sender.id === "select_target_pass" && data.action === "select") {
                 // update Verificateur property: Pass to check
-                this._currentNub.properties.setPropValue("nubToVerify", data.value ? data.value.value : undefined);
+                (this._currentNub as Verificateur).nubToVerify = data.value ? data.value.value : undefined;
                 // refresh jet type selector
                 const ntv = (this._currentNub as Verificateur).nubToVerify;
                 (this.getFormulaireNodeById("select_pab_jet_type") as SelectField).disabled = ! (ntv !== undefined && ntv.calcType === CalculatorType.Pab);
 
             } 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));
+                const caca = data.value.map((v: any) => v.value);
+                console.log("FormVerificateur.update species_list", caca);
+                (this._currentNub as Verificateur).speciesList = caca;
             }
         }
     }
diff --git a/src/app/formulaire/elements/select-field-custom.ts b/src/app/formulaire/elements/select-field-custom.ts
index 8a7fdfeed..7b5584a50 100644
--- a/src/app/formulaire/elements/select-field-custom.ts
+++ b/src/app/formulaire/elements/select-field-custom.ts
@@ -17,7 +17,7 @@ export class SelectFieldCustom extends SelectField {
      */
     protected initSelectedValue() {
         const nub = this.parentForm.currentNub;
-        console.log("[X] I S V", nub.constructor.name, this.source);
+
         switch (this.source) {
 
             case "solveur_target": // Solveur, module cible (à calculer)
@@ -137,8 +137,15 @@ export class SelectFieldCustom extends SelectField {
                     );
                 }
                 // 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));
+                for (let j = 1; j < Object.keys(FishSpecies).length / 2; j++) { // exclude "0" (SPECIES_CUSTOM)
+                    const spgId = FishSpecies[j].substring(FishSpecies[j].lastIndexOf("_") + 1);
+                    this.addEntry(
+                        new SelectEntry(
+                            this._entriesBaseId + spgId,
+                            FishSpecies[j],
+                            ServiceFactory.instance.i18nService.localizeText("INFO_VERIFICATEUR_SPECIES_GROUP") + " " + spgId
+                        )
+                    );
                 }
                 break;
         }
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index 88ab30d87..ee758e7d0 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -533,6 +533,7 @@
     "INFO_TRIGO_TITRE": "Trigonometric function",
     "INFO_TRIGO_TITRE_COURT": "Trigo. f.",
     "INFO_VERIFICATEUR_CUSTOM_SPECIES": "Custom species: %s",
+    "INFO_VERIFICATEUR_SPECIES_GROUP": "Species group",
     "INFO_VERIFICATEUR_TITRE": "Fish pass verification",
     "INFO_VERIFICATEUR_TITRE_COURT": "Verification",
     "INFO_ESPECE_TITRE": "Fish species characteristics",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index 97e80ba3f..f0a1a4d71 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -534,6 +534,7 @@
     "INFO_TRIGO_TITRE": "Fonction trigonométrique",
     "INFO_TRIGO_TITRE_COURT": "F. trigo.",
     "INFO_VERIFICATEUR_CUSTOM_SPECIES": "Espèce personnalisée : %s",
+    "INFO_VERIFICATEUR_SPECIES_GROUP": "Groupe d'espèces",
     "INFO_VERIFICATEUR_TITRE": "Vérification d'une passe",
     "INFO_VERIFICATEUR_TITRE_COURT": "Vérification",
     "INFO_ESPECE_TITRE": "Caractéristiques d'une espèce",
-- 
GitLab