From e3374ac9449adf0213ea10a383feed2fb871f730 Mon Sep 17 00:00:00 2001
From: Mathias Chouet <mathias.chouet@irstea.fr>
Date: Tue, 21 Apr 2020 15:30:23 +0200
Subject: [PATCH] Verificateur

PAB jet type is disabled when target pass is not a PAB
Species list selection is ordered
---
 .../generic-select.component.html             |  4 +--
 .../results-chart/chart-type.component.ts     |  4 +++
 .../select-field-line.component.ts            | 31 +++++++++++++++++++
 src/app/formulaire/definition/form-solveur.ts |  2 +-
 .../definition/form-verificateur.ts           | 14 +++++----
 .../elements/select-field-custom.ts           |  5 ---
 src/app/formulaire/elements/select-field.ts   |  4 ++-
 7 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/src/app/components/generic-select/generic-select.component.html b/src/app/components/generic-select/generic-select.component.html
index 4d420c820..ba4971cc0 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">
+    <mat-select [id]="selectId" [placeholder]="label" [(value)]="selectedValue" [multiple]="isMultiple" [disabled]="isDisabled">
         <mat-select-trigger *ngIf="isMultiple">
-          {{ selectedValue && selectedValue[0] ? selectedValue[0].label : '' }}
+          {{ sortedSelectedValues && sortedSelectedValues[0] ? sortedSelectedValues[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/results-chart/chart-type.component.ts b/src/app/components/results-chart/chart-type.component.ts
index 7b24c7098..a7be551c1 100644
--- a/src/app/components/results-chart/chart-type.component.ts
+++ b/src/app/components/results-chart/chart-type.component.ts
@@ -57,6 +57,10 @@ export class ChartTypeSelectComponent implements IObservable {
         return false;
     }
 
+    public get isDisabled(): boolean {
+        return false;
+    }
+
     public get showClearButton(): boolean {
         return false;
     }
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 c432c19e7..a8f76d36d 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
@@ -34,6 +34,10 @@ export class SelectFieldLineComponent implements OnInit {
         return this._select.multiple;
     }
 
+    public get isDisabled(): boolean {
+        return this._select.disabled;
+    }
+
     public get entries(): SelectEntry[] {
         if (! this._select) {
             return [];
@@ -49,6 +53,33 @@ 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-solveur.ts b/src/app/formulaire/definition/form-solveur.ts
index db1b2ef45..481ea5f6d 100644
--- a/src/app/formulaire/definition/form-solveur.ts
+++ b/src/app/formulaire/definition/form-solveur.ts
@@ -38,7 +38,7 @@ export class FormulaireSolveur extends FormulaireFixedVar {
     // interface Observer
 
     public update(sender: IObservable, data: any) {
-        // copied from FormDefinition, to avoid calling super.update()
+        // copied from FormDefinition, to avoid calling super.update() that would trigger an unwanted this.refreshFieldsets();
         if (sender instanceof Nub) {
             switch (data.action) {
                 case "resultUpdated":
diff --git a/src/app/formulaire/definition/form-verificateur.ts b/src/app/formulaire/definition/form-verificateur.ts
index 7546562ae..586daaafa 100644
--- a/src/app/formulaire/definition/form-verificateur.ts
+++ b/src/app/formulaire/definition/form-verificateur.ts
@@ -1,6 +1,7 @@
-import { IObservable, Nub, Verificateur } from "jalhyd";
+import { IObservable, Nub, Verificateur, CalculatorType } from "jalhyd";
 
 import { SelectFieldCustom } from "../elements/select-field-custom";
+import { SelectField } from '../elements/select-field';
 import { FormulaireFixedVar } from "./form-fixedvar";
 
 /**
@@ -11,7 +12,7 @@ export class FormulaireVerificateur extends FormulaireFixedVar {
     // interface Observer
 
     public update(sender: IObservable, data: any) {
-        // copied from FormDefinition, to avoid calling super.update()
+        // copied from FormDefinition, to avoid calling super.update() that would trigger an unwanted this.refreshFieldsets();
         if (sender instanceof Nub) {
             switch (data.action) {
                 case "resultUpdated":
@@ -25,13 +26,14 @@ export class FormulaireVerificateur extends FormulaireFixedVar {
             this.reset();
         }
 
-        console.log("> update", data, sender.constructor.name);
-
         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);
-                // @TODO refresh jet type selector
+                this._currentNub.properties.setPropValue("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));
diff --git a/src/app/formulaire/elements/select-field-custom.ts b/src/app/formulaire/elements/select-field-custom.ts
index de7436625..8a7fdfeed 100644
--- a/src/app/formulaire/elements/select-field-custom.ts
+++ b/src/app/formulaire/elements/select-field-custom.ts
@@ -122,7 +122,6 @@ 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) {
@@ -208,10 +207,6 @@ 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 = (
diff --git a/src/app/formulaire/elements/select-field.ts b/src/app/formulaire/elements/select-field.ts
index 4d04e9a9a..70d1bf2d1 100644
--- a/src/app/formulaire/elements/select-field.ts
+++ b/src/app/formulaire/elements/select-field.ts
@@ -33,6 +33,9 @@ export class SelectField extends Field {
     /** if true, user can select multiple values */
     protected _multiple = false;
 
+    /** if true, select box is grayed out */
+    public disabled = false;
+
     /** soruce identifier for populating with available values */
     protected source: string;
 
@@ -101,7 +104,6 @@ export class SelectField extends Field {
     }
 
     public notifyValueChanged() {
-        console.log("NOT VAL CHA", this.id, this._selectedEntry)
         this.notifyObservers({
             "action": "select",
             "value": this._selectedEntry
-- 
GitLab