From 96beadc2e52126b783e59e388dd912f5e53bbba0 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 12 Feb 2019 15:06:28 +0100
Subject: [PATCH] Branchement des FormControl Angular sur le validateur maison

---
 .../dialog-load-session.component.html               |  4 ----
 .../fixedvar-results/fixed-results.component.html    |  3 ---
 .../generic-calculator/calculator.component.scss     |  4 ++++
 .../generic-input/generic-input.component.html       |  6 +++---
 .../generic-input/generic-input.component.ts         | 12 ++++++++++--
 .../ngparam-input/ngparam-input.component.ts         |  2 +-
 .../remous-results/remous-results.component.ts       |  5 +++++
 7 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/src/app/components/dialog-load-session/dialog-load-session.component.html b/src/app/components/dialog-load-session/dialog-load-session.component.html
index 75e143061..b9f411fb7 100644
--- a/src/app/components/dialog-load-session/dialog-load-session.component.html
+++ b/src/app/components/dialog-load-session/dialog-load-session.component.html
@@ -2,10 +2,6 @@
 
 <form [formGroup]="loadSessionForm">
 
-    <!-- <input type="file" required [(ngModel)]="file"
-      name="fileSelection" #fileSelection="ngModel"> -->
-
-
   <div mat-dialog-content>
 
     <mat-form-field>
diff --git a/src/app/components/fixedvar-results/fixed-results.component.html b/src/app/components/fixedvar-results/fixed-results.component.html
index 5e779313a..b35f932d4 100644
--- a/src/app/components/fixedvar-results/fixed-results.component.html
+++ b/src/app/components/fixedvar-results/fixed-results.component.html
@@ -1,5 +1,4 @@
 <div class="fixed-results-container" *ngIf="hasFixedParameters">
-
     <!-- table des résultats fixés -->
     <table mat-table [dataSource]="dataSet">
 
@@ -14,7 +13,5 @@
 
         <tr mat-header-row *matHeaderRowDef="tableColumns"></tr>
         <tr mat-row *matRowDef="let row; columns: tableColumns;"></tr>
-
-        <!-- <tr *ngIf="hasParameterResult" vertical-result-element [result-element]=resultElement [_label]=resultLabel> -->
     </table>
 </div>
diff --git a/src/app/components/generic-calculator/calculator.component.scss b/src/app/components/generic-calculator/calculator.component.scss
index 0226d4398..b9275137b 100644
--- a/src/app/components/generic-calculator/calculator.component.scss
+++ b/src/app/components/generic-calculator/calculator.component.scss
@@ -44,6 +44,10 @@ mat-card {
     &#calc-card-results {
         margin-left: 1em;
         margin-right: 1em;
+
+        mat-card-header {
+            margin-bottom: 1em;
+        }
     }
 
     // @WARNING ::ng-deep est déprécié, mais y a rien d'autre pour
diff --git a/src/app/components/generic-input/generic-input.component.html b/src/app/components/generic-input/generic-input.component.html
index 28d062b05..af97d0505 100644
--- a/src/app/components/generic-input/generic-input.component.html
+++ b/src/app/components/generic-input/generic-input.component.html
@@ -1,7 +1,7 @@
 <mat-form-field>
-    <input matInput class="form-control" type="text" inputmode="numeric"
-        [id]="inputId" [disabled]="isDisabled" [(ngModel)]="uiValue" [placeholder]="title"
-        required>
+    <input matInput #inputControl="ngModel" class="form-control" type="text" inputmode="numeric"
+        [id]="inputId" [name]="inputId" [disabled]="isDisabled" [(ngModel)]="uiValue" [placeholder]="title"
+        pattern="-?([0-9]+\.)?[0-9]+" required>
 
     <mat-error>{{ errorMessage }}</mat-error>
 </mat-form-field>
diff --git a/src/app/components/generic-input/generic-input.component.ts b/src/app/components/generic-input/generic-input.component.ts
index 15c56f546..a7cd9064c 100644
--- a/src/app/components/generic-input/generic-input.component.ts
+++ b/src/app/components/generic-input/generic-input.component.ts
@@ -1,5 +1,5 @@
-import { Input, Output, EventEmitter, ChangeDetectorRef, OnChanges } from "@angular/core";
-
+import { Input, Output, EventEmitter, ChangeDetectorRef, OnChanges, ViewChild } from "@angular/core";
+import { NgModel } from "@angular/forms";
 import { BaseComponent } from "../base/base.component";
 import { isNumeric } from "jalhyd";
 
@@ -75,6 +75,8 @@ export abstract class GenericInputComponent extends BaseComponent implements OnC
      */
     private _errorMessageModel: string;
 
+    @ViewChild("inputControl") inputField: NgModel;
+
     constructor(private cdRef: ChangeDetectorRef) {
         super();
         // generate "unique" input id
@@ -132,6 +134,12 @@ export abstract class GenericInputComponent extends BaseComponent implements OnC
         if (this.isValid !== old) {
             this.emitValidChanged();
         }
+        // répercussion des erreurs sur le Form angular, pour faire apparaître/disparaître les mat-error
+        if (b) {
+            this.inputField.control.setErrors(null);
+        } else {
+            this.inputField.control.setErrors({ "incorrect": true });
+        }
     }
 
     private validateModel() {
diff --git a/src/app/components/ngparam-input/ngparam-input.component.ts b/src/app/components/ngparam-input/ngparam-input.component.ts
index 69be65ac3..7e1af55ce 100644
--- a/src/app/components/ngparam-input/ngparam-input.component.ts
+++ b/src/app/components/ngparam-input/ngparam-input.component.ts
@@ -2,7 +2,7 @@
 
 import { Component, ChangeDetectorRef, OnDestroy } from "@angular/core";
 
-import { isNumeric, Message, Observer } from "jalhyd";
+import { Message, Observer } from "jalhyd";
 
 import { I18nService } from "../../services/internationalisation/internationalisation.service";
 import { NgParameter } from "../../formulaire/ngparam";
diff --git a/src/app/components/remous-results/remous-results.component.ts b/src/app/components/remous-results/remous-results.component.ts
index 21d0cab67..a6ff0306b 100644
--- a/src/app/components/remous-results/remous-results.component.ts
+++ b/src/app/components/remous-results/remous-results.component.ts
@@ -7,6 +7,8 @@ import { LogComponent } from "../../components/log/log.component";
 import { RemousResults } from "../../results/remous-results";
 import { CalculatorResults } from "../../results/calculator-results";
 import { VarResultsComponent } from "../fixedvar-results/var-results.component";
+import { ApplicationSetupService } from "../../services/app-setup/app-setup.service";
+import { ServiceFactory } from "../../services/service-factory";
 
 /**
  * données pour une ligne dans le graphe
@@ -32,6 +34,8 @@ class LineData {
      */
     private _data = {};
 
+    private appSetup: ApplicationSetupService;
+
     /**
      * profondeur à laquelle est dessinée la ligne
      * les profondeurs les plus petites sont dessinées derrière les profondeurs les plus grandes
@@ -44,6 +48,7 @@ class LineData {
         for (let i = this._tx.length - 1; i >= 0; i--) {
             this._ty.push(null);
         }
+        this.appSetup = ServiceFactory.instance.applicationSetupService;
     }
 
     public getYat(x: number) {
-- 
GitLab