From 2ab769a2f62f427d48cadd8b3ed3479ae9969b31 Mon Sep 17 00:00:00 2001
From: Mathias Chouet <mathias.chouet@irstea.fr>
Date: Tue, 14 Apr 2020 16:23:57 +0200
Subject: [PATCH 1/2] Fix #385 - dialog-edit-param-values

interdependent min/max validation
better step validation
quicker estimation of number of values
---
 .../dialog-edit-param-values.component.html   |  7 +++++--
 .../dialog-edit-param-values.component.ts     | 21 ++++++++++++++++---
 .../param-values/param-values.component.ts    |  2 +-
 .../jalhyd-model-validation.directive.ts      |  8 +++++--
 4 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.html b/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.html
index 3ab502bb5..594a6de2c 100644
--- a/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.html
+++ b/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.html
@@ -17,10 +17,11 @@
     </mat-form-field>
 
     <div *ngIf="isMinMax" class="min-max-step-container">
-        <form>
+        <form #minMaxForm="ngForm">
             <mat-form-field>
                 <input matInput class="form-control" type="number" inputmode="numeric" name="min-value" step="0.01"
                     [placeholder]="uitextValeurMini" [(ngModel)]="minValue" #min="ngModel" name="min"
+                    (input)="minMaxForm.controls.max.updateValueAndValidity()"
                     [appJalhydModelValidationMin]="param" required pattern="^-?([0-9]*\.)?([0-9]+[Ee]-?)?[0-9]+$">
 
                 <mat-error *ngIf="min.errors">
@@ -36,6 +37,7 @@
             <mat-form-field>
                 <input matInput class="form-control" type="number" inputmode="numeric" name="max-value" step="0.01"
                     [placeholder]="uitextValeurMaxi" [(ngModel)]="maxValue" #max="ngModel" name="max"
+                    (input)="minMaxForm.controls.min.updateValueAndValidity()"
                     [appJalhydModelValidationMax]="param" required pattern="^-?([0-9]*\.)?([0-9]+[Ee]-?)?[0-9]+$">
 
                     <mat-error *ngIf="max.errors">
@@ -51,6 +53,7 @@
             <mat-form-field>
                 <input matInput class="form-control" type="number" inputmode="numeric" name="step-value" step="0.01"
                     [placeholder]="uitextPasVariation" [(ngModel)]="stepValue" #step="ngModel" name="step"
+                    [appJalhydModelValidationStep]="param"
                     required pattern="^([0-9]*\.)?([0-9]+[Ee]-?)?[0-9]+$">
 
                     <mat-error *ngIf="step.errors">
@@ -118,7 +121,7 @@
 
   <div mat-dialog-actions [attr.align]="'end'">
     <div *ngIf="isMinMax || viewChart">
-        <button mat-raised-button [mat-dialog-close]="true" cdkFocusInitial>
+        <button mat-raised-button [mat-dialog-close]="true" [disabled]="minMaxFormInvalid" cdkFocusInitial>
             {{ uitextClose }}
         </button>
     </div>
diff --git a/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts b/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts
index b1e6bb84d..7da1e632c 100644
--- a/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts
+++ b/src/app/components/dialog-edit-param-values/dialog-edit-param-values.component.ts
@@ -1,6 +1,6 @@
 import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";
-import { Inject, Component, OnInit } from "@angular/core";
-import { FormBuilder, FormGroup, Validators } from "@angular/forms";
+import { Inject, Component, OnInit, ViewChild } from "@angular/core";
+import { FormBuilder, FormGroup, Validators, NgForm } from "@angular/forms";
 
 import { I18nService } from "../../services/internationalisation.service";
 import { NgParameter } from "../../formulaire/elements/ngparam";
@@ -34,6 +34,7 @@ export class DialogEditParamValuesComponent implements OnInit {
     /** current decimal separator */
     public decimalSeparator: string;
 
+    /** form for values list */
     public valuesListForm: FormGroup;
 
     /** when true, shows the values chart instead of the edit form */
@@ -42,6 +43,10 @@ export class DialogEditParamValuesComponent implements OnInit {
     public chartData = {};
     public chartOptions;
 
+    /** form for min/max/step values */
+    @ViewChild("minMaxForm", { static: false })
+    public minMaxForm: NgForm;
+
     constructor(
         public dialogRef: MatDialogRef<DialogEditParamValuesComponent>,
         private intlService: I18nService,
@@ -206,7 +211,12 @@ export class DialogEditParamValuesComponent implements OnInit {
     public get numberOfValues(): number {
         if (this.isMinMax) {
             try {
-                return this.param.paramDefinition.getInferredValuesList().length;
+                // .getInferredValuesList().length is too slow when nb values > 1000
+                if (this.param.paramDefinition.step === 0) {
+                    return 0;
+                } else {
+                    return Math.max(0, Math.ceil((this.param.paramDefinition.max - this.param.paramDefinition.min) / this.param.paramDefinition.step) + 1);
+                }
             } catch (e) {
                 // min > max or something, silent fail
                 return 0;
@@ -232,6 +242,11 @@ export class DialogEditParamValuesComponent implements OnInit {
         this.param.setValueList(this, vals);
     }
 
+    /** returns true if min/max/step form is invalid */
+    public get minMaxFormInvalid(): boolean {
+        return this.minMaxForm === undefined || ! this.minMaxForm.valid;
+    }
+
     public toggleViewChart() {
         // validate list values before switching views ?
         if (! this.viewChart && this.param.valueMode === ParamValueMode.LISTE) {
diff --git a/src/app/components/param-values/param-values.component.ts b/src/app/components/param-values/param-values.component.ts
index 69655fd54..2a0ce5d3c 100644
--- a/src/app/components/param-values/param-values.component.ts
+++ b/src/app/components/param-values/param-values.component.ts
@@ -58,7 +58,7 @@ export class ParamValuesComponent implements AfterViewInit, Observer {
         this.editValuesDialog.open(
             DialogEditParamValuesComponent,
             {
-                disableClose: false,
+                disableClose: true,
                 data: {
                     param: this.param
                 },
diff --git a/src/app/directives/jalhyd-model-validation.directive.ts b/src/app/directives/jalhyd-model-validation.directive.ts
index 90a3719a6..71445d72f 100644
--- a/src/app/directives/jalhyd-model-validation.directive.ts
+++ b/src/app/directives/jalhyd-model-validation.directive.ts
@@ -144,13 +144,17 @@ export function jalhydModelValidatorMax(ngParam: NgParameter): ValidatorFn {
 export function jalhydModelValidatorStep(ngParam: NgParameter): ValidatorFn {
     return (control: AbstractControl): { [key: string]: any } | null => {
         let errorReturn = null; // no error, everything OK
-        const result = ngParam.checkMinMaxStep(control.value);
+        // @WARNING remplacement du contrôle complet de min/max/step par une
+        // simple vérification que le pas est positif
+        // const result = ngParam.checkMinMaxStep(control.value);
+        const result = (control.value > 0);
         if (! result) {
             errorReturn = {
                 "jalhydModelStep": {
                     value: control.value,
                     message: sprintf(
-                        ServiceFactory.instance.i18nService.localizeText("ERROR_MINMAXSTEP_STEP"),
+                        // ServiceFactory.instance.i18nService.localizeText("ERROR_MINMAXSTEP_STEP"),
+                        ServiceFactory.instance.i18nService.localizeText("ERROR_PARAM_MUST_BE_POSITIVE"),
                         ngParam.stepRefValue.toString()
                     )
                 }
-- 
GitLab


From 59f1012ea1d06f442aec7d91c3a017512b476d33 Mon Sep 17 00:00:00 2001
From: Mathias Chouet <mathias.chouet@irstea.fr>
Date: Tue, 14 Apr 2020 16:24:53 +0200
Subject: [PATCH 2/2] Update jalhyd_branch

---
 jalhyd_branch | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/jalhyd_branch b/jalhyd_branch
index b2cd21bbf..1f7391f92 100644
--- a/jalhyd_branch
+++ b/jalhyd_branch
@@ -1 +1 @@
-188-solveur-pouvoir-cibler-un-resultat-complementaire-eventuellement-sur-un-seul-nub
+master
-- 
GitLab