From eab3c3afc65fd911057e102fba4b88bc164ce2a9 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 25 Aug 2020 16:46:02 +0200
Subject: [PATCH] Fix #181 - prevent setting param to Calc mode when it would
 trigger a links loop

---
 .../param-field-line.component.html           |  2 +-
 .../param-field-line.component.ts             | 24 +++++++++++++++++++
 src/locale/messages.en.json                   |  1 +
 src/locale/messages.fr.json                   |  1 +
 4 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/app/components/param-field-line/param-field-line.component.html b/src/app/components/param-field-line/param-field-line.component.html
index 07d31672d..1143ac972 100644
--- a/src/app/components/param-field-line/param-field-line.component.html
+++ b/src/app/components/param-field-line/param-field-line.component.html
@@ -38,7 +38,7 @@
             </mat-button-toggle>
 
             <mat-button-toggle class="radio_cal" value="radio_cal" *ngIf="hasRadioCal()" (click)="onRadioClick('cal')"
-                [checked]="isRadioCalChecked">
+                [checked]="isRadioCalChecked" [disabled]="isRadioCalDisabled" [title]="radioCalTitle">
                 <span fxHide.xxs>{{ uitextParamCalculer }}</span>
                 <span fxHide.gt-xxs>C</span>
             </mat-button-toggle>
diff --git a/src/app/components/param-field-line/param-field-line.component.ts b/src/app/components/param-field-line/param-field-line.component.ts
index 0fd0a4eb2..22b7a92ad 100644
--- a/src/app/components/param-field-line/param-field-line.component.ts
+++ b/src/app/components/param-field-line/param-field-line.component.ts
@@ -199,6 +199,30 @@ export class ParamFieldLineComponent implements OnChanges {
         return ret;
     }
 
+    /**
+     * Returns true if setting this parameter to Calc mode leads to a links loop
+     */
+    public get isRadioCalDisabled(): boolean {
+        // find all Nubs that have to be calculated for this one to run
+        const nub = this.param.paramDefinition.originNub;
+        const requiredNubs = nub.getRequiredNubsDeep();
+        // if one of those Nubs depends on the parameter we're about to set
+        // to Calc mode, then we shouldn't do so
+        for (const r of requiredNubs) {
+            if (r.dependsOnParameter(this.param.paramDefinition)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public get radioCalTitle(): string {
+        if (this.isRadioCalDisabled) {
+            return this.intlService.localizeText("INFO_PARAMFIELD_CANNOT_CALC_LINKS_LOOP");
+        }
+        return "";
+    }
+
     public onRadioClick(option: string) {
         const oldValue = this.param.valueMode;
         switch (option) {
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index cecb243f9..e5991f549 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -454,6 +454,7 @@
     "INFO_PARAMFIELD_BOUNDARY_CONDITIONS": "Boundary conditions",
     "INFO_PARAMFIELD_CALCULATED": "Calculated",
     "INFO_PARAMFIELD_CALCULATION_FAILED": "Calculation failed",
+    "INFO_PARAMFIELD_CANNOT_CALC_LINKS_LOOP": "This parameter cannot be calculated (links loop)",
     "INFO_PARAMFIELD_CHART_SELECT_X_AXIS": "Variable for X axis",
     "INFO_PARAMFIELD_CHART_SELECT_Y_AXIS": "Variable for Y axis",
     "INFO_PARAMFIELD_CHART_TYPE_HISTOGRAM": "Histogram",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index df635cb26..cae8286ea 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -455,6 +455,7 @@
     "INFO_PARAMFIELD_BOUNDARY_CONDITIONS": "Conditions aux limites",
     "INFO_PARAMFIELD_CALCULATED": "Calculé",
     "INFO_PARAMFIELD_CALCULATION_FAILED": "Échec du calcul",
+    "INFO_PARAMFIELD_CANNOT_CALC_LINKS_LOOP": "Ce paramètre ne peut être calculé (boucle dans les liens)",
     "INFO_PARAMFIELD_CHART_SELECT_X_AXIS": "Variable en abscisse",
     "INFO_PARAMFIELD_CHART_SELECT_Y_AXIS": "Variable en ordonnée",
     "INFO_PARAMFIELD_CHART_TYPE_HISTOGRAM": "Histogramme",
-- 
GitLab