diff --git a/src/app/components/app-setup/app-setup.component.html b/src/app/components/app-setup/app-setup.component.html index f87d18777f7abf6238b39a1069deb4caa0b96c51..b2fd5411b3ded7512b3beadcecd66629daf8ebd6 100644 --- a/src/app/components/app-setup/app-setup.component.html +++ b/src/app/components/app-setup/app-setup.component.html @@ -23,7 +23,7 @@ <!-- nombre d'itérations max Newton --> <div class="row"> <div class="col-4 mx-auto"> - <base-param-input #newtonMaxIter title="{{uitextNewtonMaxIteration}}"></base-param-input> + <base-param-input #newtonMaxIterations title="{{uitextNewtonMaxIteration}}"></base-param-input> </div> </div> diff --git a/src/app/components/app-setup/app-setup.component.ts b/src/app/components/app-setup/app-setup.component.ts index 79973d1dd788b3c7b86a327ca0447a6274cb8493..6e927a2b5bf0ff6abb674ea5cc28d154798831f1 100644 --- a/src/app/components/app-setup/app-setup.component.ts +++ b/src/app/components/app-setup/app-setup.component.ts @@ -33,7 +33,7 @@ export class ApplicationSetupComponent extends BaseComponent implements Observer @ViewChild("computeAccuracy") private _computeAccuracyComponent: BaseParamInputComponent; - @ViewChild("newtonMaxIter") + @ViewChild("newtonMaxIterations") private _newtonMaxIterComponent: BaseParamInputComponent; constructor( @@ -79,7 +79,7 @@ export class ApplicationSetupComponent extends BaseComponent implements Observer this._computeAccuracyComponent.model = this._computePrec; // modèle du composant BaseParamInputComponent du max d'itérations pour Newton - this._newtonMaxIter = new NgBaseParam("nmi", ParamDomainValue.POS, this.appSetupService.newtonMaxIter); + this._newtonMaxIter = new NgBaseParam("nmi", ParamDomainValue.POS, this.appSetupService.newtonMaxIterations); this._newtonMaxIter.addObserver(this); this._newtonMaxIterComponent.model = this._newtonMaxIter; } @@ -101,7 +101,7 @@ export class ApplicationSetupComponent extends BaseComponent implements Observer break; case "nmi": - this.appSetupService.newtonMaxIter = +data; + this.appSetupService.newtonMaxIterations = +data; break; } } diff --git a/src/app/config.example.json b/src/app/config.example.json new file mode 100644 index 0000000000000000000000000000000000000000..71535b5dc28833296daf2550832091bdc6cdac6c --- /dev/null +++ b/src/app/config.example.json @@ -0,0 +1,30 @@ +{ + "params": { + "displayPrecision": 0.002, + "computePrecision": 0.0002, + "newtonMaxIterations": 52, + "language": "fr" + }, + "themes": [ + { + "name": "PASSE_A_BASSIN", + "calculators": [ 5, 6, 10, 9 ] + }, + { + "name": "PASSE_NATURELLE", + "calculators": [ ] + }, + { + "name": "HYDRAULIQUE_A_SURFACE_LIBRE", + "calculators": [ 2, 3, 4 ] + }, + { + "name": "HYDRAULIQUE_EN_CHARGE", + "calculators": [ 1, 0 ] + }, + { + "name": "LOIS_D_OUVRAGES", + "calculators": [ 8, 9, 10 ] + } + ] +} diff --git a/src/app/services/app-setup/app-setup.service.ts b/src/app/services/app-setup/app-setup.service.ts index 9c24f2d1131c05fdd11e14ad8d8e13fc199cf59d..8a634eca9a544920605a401bc3f32f8e32139e15 100644 --- a/src/app/services/app-setup/app-setup.service.ts +++ b/src/app/services/app-setup/app-setup.service.ts @@ -1,14 +1,43 @@ +import { HttpService } from "../http/http.service"; +import { Injectable } from "@angular/core"; + /** * Stores app preferences - * @TODO save in cookie / localStorage ? */ +@Injectable() export class ApplicationSetupService { + private CONFIG_FILE_PATH = "app/config.json"; + + // default builtin values public displayPrecision = 0.001; public computePrecision = 0.0001; - public newtonMaxIter = 50; + public newtonMaxIterations = 50; + public language = "fr"; + /** themes to group calculators, for displaying on the front page */ + public themes: any[]; + + public constructor( + private httpService: HttpService) { + this.readValuesFromConfig(); + } public get displayDigits() { return -Math.log10(this.displayPrecision); } + + // @TODO save preferences in cookie / localStorage ? + + // read default values from config + // @TODO notify (?) so that if a user starts the app on the /setup page the values are correct + // (atm the default builtin values will be visible until the user changes page and goes back to /setup) + private readValuesFromConfig() { + this.httpService.httpGetRequestPromise(this.CONFIG_FILE_PATH).then((data: any) => { + this.displayPrecision = data.params.displayPrecision; + this.computePrecision = data.params.computePrecision; + this.newtonMaxIterations = data.params.newtonMaxIterations; + this.language = data.params.language; + this.themes = data.themes; + }); + } } diff --git a/src/app/services/formulaire/formulaire.service.ts b/src/app/services/formulaire/formulaire.service.ts index 0aecac54e4a5a8a34cff52abd30aac00173bf847..8fafb0635cf636131b785fbf4e8f56b7907c67bc 100644 --- a/src/app/services/formulaire/formulaire.service.ts +++ b/src/app/services/formulaire/formulaire.service.ts @@ -6,7 +6,6 @@ import { saveAs } from "file-saver"; import { CalculatorType, EnumEx, Observable, ParamDefinition } from "jalhyd"; -import { ServiceFactory } from "../service-factory"; import { HttpService } from "../../services/http/http.service"; import { InternationalisationService } from "../../services/internationalisation/internationalisation.service"; import { FormulaireDefinition } from "../../formulaire/definition/form-definition"; @@ -29,17 +28,20 @@ export class FormulaireService extends Observable { private _currentFormId: string = null; - constructor() { + constructor( + private internationalisationService: InternationalisationService, + private httpService: HttpService) { + super(); this._formulaires = []; } private get _intlService(): InternationalisationService { - return ServiceFactory.instance.internationalisationService; + return this.internationalisationService; } private get _httpService(): HttpService { - return ServiceFactory.instance.httpService; + return this.httpService; } public get formulaires(): FormulaireDefinition[] { @@ -48,7 +50,7 @@ export class FormulaireService extends Observable { private loadLocalisation(calc: CalculatorType): Promise<any> { const f: string = this.getConfigPathPrefix(calc) + this._intlService.currentLanguage.tag + ".json"; - const prom = this._httpService.httpGetRequestPromise(undefined, undefined, undefined, f); + const prom = this._httpService.httpGetRequestPromise(f); return prom.then((j) => { return j as StringMap; @@ -101,7 +103,7 @@ export class FormulaireService extends Observable { public loadConfig(ct: CalculatorType): Promise<any> { const f: string = this.getConfigPathPrefix(ct) + "config.json"; - return this._httpService.httpGetRequestPromise(undefined, undefined, undefined, f); + return this._httpService.httpGetRequestPromise(f); } private newFormulaire(ct: CalculatorType, jsonState?: {}): FormulaireDefinition { diff --git a/src/app/services/http/http.service.ts b/src/app/services/http/http.service.ts index 613c8fd3de71d5e92493f2ee06974aa18db08aa0..580ffee36e0cc66d25d3c35202180008b424fa45 100644 --- a/src/app/services/http/http.service.ts +++ b/src/app/services/http/http.service.ts @@ -10,52 +10,25 @@ export class HttpService { /** * Lance une requête GET (version standard Observable) - * @param protocol ex: "https" - * @param host ex: "domaine.fr" - * @param port ex: 8080 * @param path ex: /mon-service/toto * @param headers ex: new HttpHeaders { "Authorization": "bla" } */ - public httpGetRequest(protocol: string, host: string, port: number, path: string, - headers?: HttpHeaders): Observable<Object> { - - let url = ""; - // 1. construire URL - if (host) { - url = host; - } // else throw Error ? - if (protocol) { - url = protocol + "://" + url; - } - if (port) { - url += ":" + String(port); - } - const p = (path === "/") ? "" : path; - if (url === "") { - url = p; - } else { - url += "/" + p; - } - url = encodeURI(url); - - // 2. ajout entêtes + public httpGetRequest(path: string, headers?: HttpHeaders): Observable<Object> { + // ajout entêtes const opts = {}; if (headers) { opts["headers"] = headers; } - - // 3. lancement requête - return this.http.get(url, opts); + // lancement requête + return this.http.get(encodeURI(path), opts); } /** * Lance une requête GET (version Promise) * @see httpGetRequest */ - public httpGetRequestPromise(protocol: string, host: string, port: number, path: string, - headers?: HttpHeaders): Promise<Object> { - - const res$: Observable<Object> = this.httpGetRequest(protocol, host, port, path, headers); + public httpGetRequestPromise(path: string, headers?: HttpHeaders): Promise<Object> { + const res$: Observable<Object> = this.httpGetRequest(path, headers); return res$.toPromise(); } @@ -65,12 +38,12 @@ export class HttpService { * @param errorCallback callback en cas d'erreur * @see httpGetRequest */ - public httpGetRequestCallbacks(protocol: string, host: string, port: number, path: string, + public httpGetRequestCallbacks(path: string, processDataCallback: (s: any) => void, errorCallback?: (err: any) => void, headers?: HttpHeaders): Subscription { - const res$: Observable<Object> = this.httpGetRequest(protocol, host, port, path, headers); + const res$: Observable<Object> = this.httpGetRequest(path, headers); const annulable = res$.subscribe( data => { diff --git a/src/app/services/internationalisation/internationalisation.service.ts b/src/app/services/internationalisation/internationalisation.service.ts index 3d571aa73dfef737f65f017f8389b1fb96fb9eb1..ebb9df3a1e784a6ce818c560e2d1043d224528db 100644 --- a/src/app/services/internationalisation/internationalisation.service.ts +++ b/src/app/services/internationalisation/internationalisation.service.ts @@ -1,10 +1,10 @@ import { Injectable } from "@angular/core"; -import { Response } from "@angular/http"; import { Message, MessageCode, Observable } from "jalhyd"; import { StringMap } from "../../stringmap"; -import { ServiceFactory } from "../service-factory"; +import { ApplicationSetupService } from "../app-setup/app-setup.service"; +import { HttpService } from "../http/http.service"; /* language tag : fr-FR @@ -44,13 +44,15 @@ export class Language { @Injectable() export class InternationalisationService extends Observable { + private _currLang: Language; - private _sLang: string; private _Messages: StringMap; - private _languages: Language[]; - public constructor() { + constructor( + private applicationSetupService: ApplicationSetupService, + private httpService: HttpService) { + super(); this._languages = []; this._languages.push(new Language(LanguageCode.FRENCH, "fr", "Français")); @@ -131,7 +133,7 @@ export class InternationalisationService extends Observable { } const f: string = "messages." + l + ".json"; - return ServiceFactory.instance.httpService.httpGetRequestPromise(undefined, undefined, undefined, "locale/" + f).then( + return this.httpService.httpGetRequestPromise("locale/" + f).then( (res: any) => { is._Messages = res; } ); } @@ -222,7 +224,7 @@ export class InternationalisationService extends Observable { if (match > -1) { return this.localizeText(`INFO_EXTRARES_${label.substring(match).toUpperCase()}_${value}`); } - const nDigits = ServiceFactory.instance.applicationSetupService.displayDigits; + const nDigits = this.applicationSetupService.displayDigits; return value.toFixed(nDigits); } diff --git a/src/app/services/param/param.service.ts b/src/app/services/param/param.service.ts index 7f3ef4039a349d0262f1b77a944c5d906dea55e6..be0abf19ef81bdb1b3b15b19b2f7f26dfeafe2a1 100644 --- a/src/app/services/param/param.service.ts +++ b/src/app/services/param/param.service.ts @@ -2,19 +2,24 @@ import { ParamDomain, ParamDefinition, ParamDomainValue, ParamCalculability, Ses import { NgParameter } from "../../formulaire/ngparam"; import { Injectable } from "@angular/core"; -import { ServiceFactory } from "../service-factory"; import { InternationalisationService } from "../internationalisation/internationalisation.service"; import { ApplicationSetupService } from "../app-setup/app-setup.service"; import { FormulaireNode } from "../../formulaire/formulaire-node"; @Injectable() export class ParamService { + + constructor( + private internationalisationService: InternationalisationService, + private applicationSetupService: ApplicationSetupService + ) { } + private get _intlService(): InternationalisationService { - return ServiceFactory.instance.internationalisationService; + return this.internationalisationService; } private get _appSetupService(): ApplicationSetupService { - return ServiceFactory.instance.applicationSetupService; + return this.applicationSetupService; } private createAccuracyParameter(): ParamDefinition {