From c2a3bb4b96d1bd3617b8565a18c59c70eb94b2cc Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Wed, 4 Oct 2017 14:34:42 +0200
Subject: [PATCH] ajout du composant CheckFieldLineComponent

---
 src/app/app.module.ts                         |  3 +-
 src/app/calculators/generic/formulaire.ts     | 50 +++++++++++++++++--
 .../regime-uniforme.config.json               |  2 +-
 .../check-field-line.component.ts             | 32 ++++++++++++
 .../check-field-line/check-field-line.html    |  9 ++++
 src/app/components/field-set/field-set.html   |  1 +
 .../services/formulaire/formulaire.service.ts | 16 ++++--
 7 files changed, 105 insertions(+), 8 deletions(-)
 create mode 100644 src/app/components/check-field-line/check-field-line.component.ts
 create mode 100644 src/app/components/check-field-line/check-field-line.html

diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 5d6d069a2..e9ec127ae 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -11,6 +11,7 @@ import { ParamInputComponent } from './components/param-input/param-input.compon
 import { FieldSetComponent } from './components/field-set/field-set.component';
 import { ParamFieldLineComponent } from './components/param-field-line/param-field-line.component';
 import { SelectFieldLineComponent } from './components/select-field-line/select-field-line.component';
+import { CheckFieldLineComponent } from './components/check-field-line/check-field-line.component';
 import { CondDistriComponent } from './calculators/cond_distri/conddistri.component';
 import { LechaptCalmonComponent } from './calculators/lechapt-calmon/lechaptcalmon.component';
 import { SectionParametreeComponent } from './calculators/section-param/section-param.component';
@@ -38,7 +39,7 @@ import { SectionCanvasComponent } from './components/section-canvas/section-canv
     AppComponent,
     ParamInputComponent,
     FieldSetComponent,
-    ParamFieldLineComponent, SelectFieldLineComponent,
+    ParamFieldLineComponent, SelectFieldLineComponent, CheckFieldLineComponent,
     CondDistriComponent, LechaptCalmonComponent, SectionParametreeComponent, GenericCalculatorComponent, RegimeUniformeComponent,
     AlertDialog,
     CalculatorResultsComponent, SectionResultsComponent,
diff --git a/src/app/calculators/generic/formulaire.ts b/src/app/calculators/generic/formulaire.ts
index e587dbb6f..a67ceab36 100644
--- a/src/app/calculators/generic/formulaire.ts
+++ b/src/app/calculators/generic/formulaire.ts
@@ -16,8 +16,8 @@ export class FormulaireDefinition {
     private _config = {};
 
     /**
-   * symbole du paramètre à calculer par défaut (cf config "idCal")
-   */
+     * symbole du paramètre à calculer par défaut (cf config "idCal")
+     */
     private _defaultCalculatedParam: string;
 
     private _fieldSets: FieldSet[] = [];
@@ -227,6 +227,18 @@ export class FormulaireDefinition {
         }
     }
 
+    private parse_check(node_type: ComputeNodeType, field: {}): CheckField {
+        let id = field["id"];
+        let res: CheckField = new CheckField(node_type, id);
+        let value = field["value"];
+
+        res.setValue(value == "true");
+
+        this.parse_dependencies(res, field);
+
+        return res;
+    }
+
     private parse_select(node_type: ComputeNodeType, field: {}): SelectField {
         let id = field["id"];
         let res: SelectField = new SelectField(node_type, id);
@@ -279,6 +291,9 @@ export class FormulaireDefinition {
             } else if (field["type"] === "select") {
                 let param = this.parse_select(node_type, field);
                 res.addField(param);
+            } else if (field["type"] === "check") {
+                let param = this.parse_check(node_type, field);
+                res.addField(param);
             }
         }
 
@@ -445,7 +460,7 @@ export abstract class FormulaireElement {
 }
 
 export enum FieldType {
-    Input, Select
+    Input, Select, Check
 }
 
 export abstract class Field extends FormulaireElement {
@@ -461,6 +476,10 @@ export abstract class Field extends FormulaireElement {
         return this._fieldType == FieldType.Select;
     }
 
+    public get isCheck(): boolean {
+        return this._fieldType == FieldType.Check;
+    }
+
     public abstract getValue(): any;
     public abstract setValue(val: any): void;
 }
@@ -582,6 +601,31 @@ export class SelectField extends Field {
     }
 }
 
+export class CheckField extends Field {
+    private _value: boolean;
+
+    constructor(nodeType: ComputeNodeType, id: string) {
+        super(nodeType, id, FieldType.Check);
+        this._value = false;
+    }
+
+    public getValue(): boolean {
+        return this._value;
+    }
+
+    public setValue(val: boolean) {
+        this._value = val;
+    }
+
+    protected verifyDependency(d: Dependency): boolean {
+        throw "CheckField.verifyDependency() : type de condition '" + DependencyConditionType[d.masterCondition.type] + "' non pris en charge";
+    }
+
+    public updateLocalisation(loc: StringMap) {
+        this.label = loc[this.id];
+    }
+}
+
 export abstract class InputField extends Field {
     private _value: any;
 
diff --git a/src/app/calculators/regime-uniforme/regime-uniforme.config.json b/src/app/calculators/regime-uniforme/regime-uniforme.config.json
index ca8def60e..defd7d641 100644
--- a/src/app/calculators/regime-uniforme/regime-uniforme.config.json
+++ b/src/app/calculators/regime-uniforme/regime-uniforme.config.json
@@ -16,7 +16,7 @@
                         "id": "select_section_circ"
                     },
                     {
-                        "id": "select_section_para"
+                        "id": "select_section_puiss"
                     }
                 ]
             }
diff --git a/src/app/components/check-field-line/check-field-line.component.ts b/src/app/components/check-field-line/check-field-line.component.ts
new file mode 100644
index 000000000..01a3029cf
--- /dev/null
+++ b/src/app/components/check-field-line/check-field-line.component.ts
@@ -0,0 +1,32 @@
+import { Component, Input, Output, EventEmitter } from '@angular/core';
+
+import { CheckField } from '../../calculators/generic/formulaire';
+import { FormulaireService } from '../../services/formulaire/formulaire.service';
+
+@Component({
+    selector: 'check-field-line',
+    templateUrl: "./check-field-line.html",
+})
+export class CheckFieldLineComponent {
+    private _check: CheckField;
+
+    private _currentValue: boolean;
+
+    constructor(private formulaireService: FormulaireService) {
+    }
+
+    /**
+    * id input attribute
+    */
+    private _id: string;
+
+    @Input()
+    private set id(s: string) {
+        this._id = s;
+        this._check = this.formulaireService.getCheckField(this._id);
+    }
+
+    private onChange(event: any) {
+        this._check.setValue(event);
+    }
+}
diff --git a/src/app/components/check-field-line/check-field-line.html b/src/app/components/check-field-line/check-field-line.html
new file mode 100644
index 000000000..5d0d8a993
--- /dev/null
+++ b/src/app/components/check-field-line/check-field-line.html
@@ -0,0 +1,9 @@
+<tr>
+    <td align="right">{{_check.label}}</td>
+    <td colspan="3">
+        <!--
+        <input type="checkbox" >
+        -->
+        <input type="checkbox" [(ngModel)]=_currentValue (ngModelChange)="onChange($event)">
+    </td>
+</tr>
\ No newline at end of file
diff --git a/src/app/components/field-set/field-set.html b/src/app/components/field-set/field-set.html
index 325493a20..bf1aba27b 100644
--- a/src/app/components/field-set/field-set.html
+++ b/src/app/components/field-set/field-set.html
@@ -15,5 +15,6 @@
     <td colspan="5">
         <param-field-line *ngIf="p.isInput" [computeNodeType]=_fieldSet.computeNodeType [symbol]=p.symbol (onRadio)=onRadioClick($event)></param-field-line>
         <select-field-line *ngIf="p.isSelect" [id]=p.id (onSelectChange)=onSelectChanged($event)></select-field-line>
+        <check-field-line *ngIf="p.isCheck" [id]=p.id></check-field-line>
     </td>
 </tr>
diff --git a/src/app/services/formulaire/formulaire.service.ts b/src/app/services/formulaire/formulaire.service.ts
index d39981866..f8b3d8017 100644
--- a/src/app/services/formulaire/formulaire.service.ts
+++ b/src/app/services/formulaire/formulaire.service.ts
@@ -7,7 +7,7 @@ import { ParamsSectionRectang, cSnRectang, ParamsSectionCirc, cSnCirc, ParamsSec
 
 import { ParamService } from '../param/param.service';
 import { HttpService } from '../../services/http/http.service';
-import { FormulaireDefinition, FormulaireElement, CalculatorType, SelectField } from '../../calculators/generic/formulaire';
+import { FormulaireDefinition, FormulaireElement, CalculatorType, SelectField, CheckField } from '../../calculators/generic/formulaire';
 import { StringMap } from '../../stringmap';
 
 @Injectable()
@@ -44,10 +44,9 @@ export class FormulaireService {
         if (ct == undefined)
             throw "FormulaireService.getFormulaire() : invalid undefined CalculatorType"
 
-        for (let f of this._formulaires) {
+        for (let f of this._formulaires)
             if (f.type == ct)
                 return f;
-        }
 
         throw "FormulaireService.getFormulaire() : type '" + ct + "' form is not loaded";
     }
@@ -72,6 +71,17 @@ export class FormulaireService {
         return undefined;
     }
 
+    public getCheckField(id: string): CheckField {
+        for (let f of this._formulaires) {
+            let s = f.getFormulaireElementById(id);
+            if (s != undefined)
+                if (!(s instanceof CheckField))
+                    throw "Form element with id '" + id + "' is not a checkbox";
+            return <CheckField>s;
+        }
+        return undefined;
+    }
+
     public updateLocalisation(localisation: StringMap) {
         for (let loc_id in localisation) {
             let fe = this.getFormulaireElementById(loc_id);
-- 
GitLab