From b95776caa5fa1cd516b64ad80bdaad1276af438e Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Wed, 2 May 2018 17:20:47 +0200
Subject: [PATCH]  #34 remplacement de l'injection de services par
 l'utilisation de ServiceFactory

---
 src/app/app.component.ts                       |  1 +
 .../calculator-list.component.ts               | 18 +++++++-----------
 .../check-field-line.component.ts              |  4 +---
 .../generic-calculator/calculator.component.ts | 12 +++++++++---
 .../param-field-line.component.ts              | 11 ++++++-----
 .../select-field-line.component.ts             |  5 -----
 .../internationalisation.service.ts            |  5 ++---
 src/app/services/service-factory.ts            |  3 +++
 8 files changed, 29 insertions(+), 30 deletions(-)

diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 938ba9907..c668d895f 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -45,6 +45,7 @@ export class AppComponent implements OnInit, OnDestroy, Observer {
     ServiceFactory.instance.applicationSetupService = appSetupService;
     ServiceFactory.instance.paramService = paramService;
     ServiceFactory.instance.internationalisationService = intlService;
+    ServiceFactory.instance.formulaireService = formulaireService;
     ServiceFactory.instance.httpService = httpService;
   }
 
diff --git a/src/app/components/calculator-list/calculator-list.component.ts b/src/app/components/calculator-list/calculator-list.component.ts
index 0d674c2e1..91b239baa 100644
--- a/src/app/components/calculator-list/calculator-list.component.ts
+++ b/src/app/components/calculator-list/calculator-list.component.ts
@@ -4,14 +4,14 @@ import { Router } from '@angular/router';
 import { CalculatorType, EnumEx } from "jalhyd";
 
 import { FormulaireDefinition } from "../../formulaire/definition/form-definition";
-import { FormulaireService } from "../../services/formulaire/formulaire.service";
+import { ServiceFactory } from "../../services/service-factory";
 import { InternationalisationService } from '../../services/internationalisation/internationalisation.service';
 
 class ListElement {
     private _label: string;
 
-    constructor(private _type: CalculatorType, formulaireService: FormulaireService) {
-        this._label = formulaireService.getLocalisedTitleFromCalculatorType(_type);
+    constructor(private _type: CalculatorType) {
+        this._label = ServiceFactory.instance.formulaireService.getLocalisedTitleFromCalculatorType(_type);
     }
 
     public get label() { return this._label }
@@ -26,23 +26,19 @@ class ListElement {
 export class CalculatorListComponent implements OnInit {
     private _items: ListElement[];
 
-    constructor(
-        private formulaireService: FormulaireService,
-        private router: Router,
-        private intlService: InternationalisationService,
-    ) {
-        this.intlService.addObserver(this);
+    constructor(private router: Router) {
+        ServiceFactory.instance.internationalisationService.addObserver(this);
     }
 
     private updateLocale() {
         this._items = [];
         for (let t of EnumEx.getValues(CalculatorType))
             if (t !== CalculatorType.Structure)
-                this._items.push(new ListElement(t, this.formulaireService));
+                this._items.push(new ListElement(t));
     }
 
     private create(t: CalculatorType) {
-        const p: Promise<FormulaireDefinition> = this.formulaireService.createFormulaire(t);
+        const p: Promise<FormulaireDefinition> = ServiceFactory.instance.formulaireService.createFormulaire(t);
         p.then(f => {
             this.router.navigate(['/calculator', f.uid]);
         });
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
index 6897543d2..94138891d 100644
--- a/src/app/components/check-field-line/check-field-line.component.ts
+++ b/src/app/components/check-field-line/check-field-line.component.ts
@@ -1,7 +1,6 @@
 import { Component, Input, Output, EventEmitter } from "@angular/core";
 
 import { CheckField } from "../../formulaire/check-field";
-import { FormulaireService } from "../../services/formulaire/formulaire.service";
 
 @Component({
     selector: "check-field-line",
@@ -13,8 +12,7 @@ export class CheckFieldLineComponent {
 
     private _currentValue: boolean;
 
-    constructor(private formulaireService: FormulaireService) {
-    }
+    constructor() { }
 
     private onChange(event: any) {
         this._check.setValue(event);
diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index d250d2523..8a837c46d 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -15,6 +15,7 @@ import { CalculatorNameComponent } from "./calc-name.component";
 import { FormulaireElement } from "../../formulaire/formulaire-element";
 import { FieldsetContainer } from "../../formulaire/fieldset-container";
 import { FieldsetContainerComponent } from "../fieldset-container/fieldset-container.component";
+import { ServiceFactory } from "../../services/service-factory";
 
 @Component({
     selector: 'hydrocalc',
@@ -87,10 +88,15 @@ export class GenericCalculatorComponent extends BaseComponent implements OnInit,
      */
     private isCalculateDisabled: boolean = true;
 
-    constructor(private intlService: InternationalisationService,
-        private formulaireService: FormulaireService,
-        private route: ActivatedRoute) {
+    // services
+
+    private intlService: InternationalisationService;
+    private formulaireService: FormulaireService;
+
+    constructor(private route: ActivatedRoute) {
         super();
+        this.intlService = ServiceFactory.instance.internationalisationService;
+        this.formulaireService = ServiceFactory.instance.formulaireService;
     }
 
     private get formElements(): FormulaireElement[] {
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 d66120394..d43df61f3 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
@@ -3,7 +3,7 @@ import { Component, ViewChild, Input, Output, EventEmitter, OnChanges } from "@a
 import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
 import { NgParameter, ParamRadioConfig } from "../../formulaire/ngparam";
 import { NgParamInputComponent } from "../ngparam-input/ngparam-input.component";
-import { FormulaireService } from "../../services/formulaire/formulaire.service";
+import { ServiceFactory } from "../../services/service-factory";
 import { ParamValueMode } from "jalhyd";
 
 @Component({
@@ -49,9 +49,10 @@ export class ParamFieldLineComponent implements OnChanges {
      */
     private _isRangeValid: boolean = true;
 
-    constructor(private intlService: InternationalisationService,
-        private formulaireService: FormulaireService
-    ) {
+    private intlService: InternationalisationService;
+
+    constructor() {
+        this.intlService = ServiceFactory.instance.internationalisationService;
         this.onValid = new EventEmitter();
         this.inputChange = new EventEmitter();
     }
@@ -292,7 +293,7 @@ export class ParamFieldLineComponent implements OnChanges {
     }
 
     private get formHasResults(): boolean {
-        return this.formulaireService.currentFormHasResults;
+        return ServiceFactory.instance.formulaireService.currentFormHasResults;
     }
 
     /**
diff --git a/src/app/components/select-field-line/select-field-line.component.ts b/src/app/components/select-field-line/select-field-line.component.ts
index 5578fe79b..a544eed7d 100644
--- a/src/app/components/select-field-line/select-field-line.component.ts
+++ b/src/app/components/select-field-line/select-field-line.component.ts
@@ -2,7 +2,6 @@ import { Component, Input, Output, EventEmitter } from "@angular/core";
 
 import { SelectField, } from "../../formulaire/select-field";
 import { SelectEntry } from "../../formulaire/select-entry";
-import { FormulaireService } from "../../services/formulaire/formulaire.service";
 import { GenericSelectComponent } from "../generic-select/generic-select.component";
 
 @Component({
@@ -13,10 +12,6 @@ export class SelectFieldLineComponent extends GenericSelectComponent<SelectEntry
     @Input("param")
     private _select: SelectField;
 
-    constructor(private formulaireService: FormulaireService) {
-        super();
-    }
-
     protected get entries(): SelectEntry[] {
         if (this._select == undefined)
             return [];
diff --git a/src/app/services/internationalisation/internationalisation.service.ts b/src/app/services/internationalisation/internationalisation.service.ts
index eac0383b5..f418a8899 100644
--- a/src/app/services/internationalisation/internationalisation.service.ts
+++ b/src/app/services/internationalisation/internationalisation.service.ts
@@ -3,7 +3,6 @@ import { Response } from '@angular/http';
 
 import { Message, MessageCode, Observable } from "jalhyd";
 
-import { HttpService } from "../http/http.service";
 import { StringMap } from "../../stringmap";
 import { ServiceFactory } from '../service-factory';
 
@@ -51,7 +50,7 @@ export class InternationalisationService extends Observable {
 
     private _languages: Language[];
 
-    public constructor(private httpService: HttpService) {
+    public constructor() {
         super();
         this._languages = [];
         this._languages.push(new Language(LanguageCode.FRENCH, "fr", "Français"));
@@ -124,7 +123,7 @@ export class InternationalisationService extends Observable {
         }
 
         let f: string = "error_messages." + l + ".json"
-        return this.httpService.httpGetRequest(undefined, undefined, undefined, "locale/" + f, processData);
+        return ServiceFactory.instance.httpService.httpGetRequest(undefined, undefined, undefined, "locale/" + f, processData);
     }
 
     private getMessageFromCode(c: MessageCode): string {
diff --git a/src/app/services/service-factory.ts b/src/app/services/service-factory.ts
index 65b08fa1e..fb5788767 100644
--- a/src/app/services/service-factory.ts
+++ b/src/app/services/service-factory.ts
@@ -1,5 +1,6 @@
 import { ApplicationSetupService } from "./app-setup/app-setup.service";
 import { ParamService } from "./param/param.service";
+import { FormulaireService } from "./formulaire/formulaire.service";
 import { InternationalisationService } from "./internationalisation/internationalisation.service";
 import { HttpService } from "./http/http.service";
 
@@ -12,6 +13,8 @@ export class ServiceFactory {
 
     public paramService: ParamService;
 
+    public formulaireService: FormulaireService;
+
     public internationalisationService: InternationalisationService;
 
     public httpService: HttpService;
-- 
GitLab