From da26931c77514d192e8f12589022b689fa452d35 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Thu, 19 Sep 2019 16:06:41 +0200
Subject: [PATCH] Fix #292 - custom order for Grille results

---
 .../fixed-results.component.ts                | 110 ++++++++++++++++++
 .../definition/concrete/form-grille.ts        |  12 ++
 .../definition/form-result-fixedvar.ts        |   5 +-
 src/app/results/param-calc-results.ts         |   3 +
 4 files changed, 127 insertions(+), 3 deletions(-)

diff --git a/src/app/components/fixedvar-results/fixed-results.component.ts b/src/app/components/fixedvar-results/fixed-results.component.ts
index 45d9d4183..56d481ca4 100644
--- a/src/app/components/fixedvar-results/fixed-results.component.ts
+++ b/src/app/components/fixedvar-results/fixed-results.component.ts
@@ -72,6 +72,116 @@ export class FixedResultsComponent extends ResultsComponent {
      * Returns a set of parameters and results for mat-table
      */
     public get dataSet() {
+        if (
+            this._fixedResults !== undefined
+            && Array.isArray(this._fixedResults.variablesOrder)
+            && this._fixedResults.variablesOrder.length > 0
+        ) {
+            return this.buildCustomOrderedDataset();
+        } else {
+            return this.buildDefaultOrderedDataset();
+        }
+    }
+
+    /**
+     * When this.variablesOrder is defined and not empty, builds an ordered set of
+     * data according to the given symbols order; variables whose symbol is not
+     * mentioned in this.variablesOrder are not displayed !
+     */
+    protected buildCustomOrderedDataset() {
+        const data = [];
+        for (const symbol of this._fixedResults.variablesOrder) {
+
+            // is it a fixed parameter ?
+            for (const fp of this.fixedParams) {
+                if (fp.symbol === symbol) {
+                    let label = this.formattedLabel(fp);
+                    // add structure position before label
+                    if (fp.paramDefinition.parentNub instanceof Structure) {
+                        const pos = fp.paramDefinition.parentNub.findPositionInParent();
+                        label = this.intlService.localizeText("INFO_OUVRAGE") + " n°" + (pos + 1) + ": " + label;
+                    }
+                    data.push({
+                        label: label,
+                        value: this.formattedValue(fp),
+                        isCalcResult: false // for CSS
+                    });
+                }
+            }
+
+            // is it a result or child result ?const res = this._fixedResults.result;
+            const res = this._fixedResults.result;
+            if (
+                res
+                && res.resultElements.length > 0
+                && res.resultElement
+                && res.resultElement.count() > 0
+            ) {
+                const sn = this._fixedResults.result.sourceNub;
+                let found = false;
+
+                // 2.1 all results
+                for (const k of res.resultElement.keys) {
+                    if (k === symbol) {
+                        found = true;
+                        const er: number = res.resultElement.getValue(k);
+                        // calculator type for translation
+                        let ct = sn.calcType;
+                        if (sn.parent) {
+                            ct = sn.parent.calcType;
+                        }
+                        let unit;
+                        // is k the calculated parameter ? If so, extract its unit
+                        try {
+                            const p = res.sourceNub.getParameter(k);
+                            if (p) {
+                                unit = p.unit;
+                            }
+                        } catch (e) { /* silent fail */ }
+                        const label = this.formService.expandVariableNameAndUnit(ct, k, unit);
+                        data.push({
+                            label: label,
+                            value: this.intlService.formatResult(k, er),
+                            isCalcResult: true // for CSS
+                        });
+                    }
+                }
+
+                // 2.2. children results
+                if (! found) {
+                    for (const c of sn.getChildren()) {
+                        if (c.result) {
+                            for (const k of c.result.resultElement.keys) {
+                                if (k === symbol) {
+                                    const er: number = c.result.resultElement.getValue(k);
+                                    // calculator type for translation
+                                    let ct = sn.calcType;
+                                    if (sn.parent) {
+                                        ct = sn.parent.calcType;
+                                    }
+                                    const cn = capitalize(this.intlService.childName(sn));
+                                    data.push({
+                                        label: sprintf(this.intlService.localizeText("INFO_STUFF_N"), cn)
+                                                + (c.findPositionInParent() + 1) + " : "
+                                                + this.formService.expandVariableNameAndUnit(ct, k),
+                                        value: this.intlService.formatResult(k, er),
+                                        isCalcResult: true // for CSS
+                                    });
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return data;
+    }
+
+    /**
+     * When this.variablesOrder is empty or undefined, builds an ordered set of
+     * data with fixed parameters first, then parent results, then children results
+     */
+    protected buildDefaultOrderedDataset() {
         const data = [];
         // 1. fixed parameters
         for (const fp of this.fixedParams) {
diff --git a/src/app/formulaire/definition/concrete/form-grille.ts b/src/app/formulaire/definition/concrete/form-grille.ts
index 2e47d8df5..ea732cd96 100644
--- a/src/app/formulaire/definition/concrete/form-grille.ts
+++ b/src/app/formulaire/definition/concrete/form-grille.ts
@@ -2,6 +2,7 @@ import { IObservable } from "jalhyd";
 
 import { FormulaireBase } from "./form-base";
 import { FieldSet } from "../../fieldset";
+import { FormResultFixedVar } from "../form-result-fixedvar";
 
 /**
  * Formulaire pour les Grilles (perte de charge)
@@ -14,6 +15,17 @@ export class FormulaireGrille extends FormulaireBase {
     /** id of select configuring grid type */
     private _gridTypeSelectId: string;
 
+    constructor() {
+        super();
+        // custom variables order for results tables
+        (this._formResult as FormResultFixedVar).fixedResults.variablesOrder = [
+            "QMax", "CRad", "CEau", "H", "CSomGrille", "HG", "B", "S", "SPDG", "VA", "VAPDG",
+            "Beta", "Alpha", "LG", "D", "DG", "SG", "VN",
+            "b", "p", "e", "RFB", "REEB", "OB", "O", "OEntH", "cIncl",
+            "DH00", "DH05", "DH10", "DH15", "DH20", "DH25", "DH30", "DH35", "DH40", "DH45", "DH50", "DH55", "DH60"
+        ];
+    }
+
     protected parseOptions(json: {}) {
         super.parseOptions(json);
         this._gridProfileSelectId = this.getOption(json, "gridProfileSelectId");
diff --git a/src/app/formulaire/definition/form-result-fixedvar.ts b/src/app/formulaire/definition/form-result-fixedvar.ts
index 93d6bc985..4e4dfa583 100644
--- a/src/app/formulaire/definition/form-result-fixedvar.ts
+++ b/src/app/formulaire/definition/form-result-fixedvar.ts
@@ -7,9 +7,8 @@ import { CalculatorResults } from "../../results/calculator-results";
 import { ChartType } from "../../results/chart-type";
 
 export class FormResultFixedVar extends FormResult {
-    /**
-     * résultats fixes/variables
-     */
+
+    /** résultats fixes/variables */
     protected _fixedResults: FixedResults;
     protected _varResults: VarResults;
 
diff --git a/src/app/results/param-calc-results.ts b/src/app/results/param-calc-results.ts
index 8cdf9e85e..02ec6097e 100644
--- a/src/app/results/param-calc-results.ts
+++ b/src/app/results/param-calc-results.ts
@@ -22,6 +22,9 @@ export abstract class CalculatedParamResults extends CalculatorResults {
      */
     public result: Result;
 
+    /** custom variables order for results tables  */
+    public variablesOrder: string[] = [];
+
     protected reset() {
         this._calculatedParam = undefined;
         this.calculatedParameterHeader = undefined;
-- 
GitLab