Skip to content
Snippets Groups Projects
Commit 009f2ca5 authored by mathias.chouet's avatar mathias.chouet
Browse files

Amélioration des services

utilisation de l'injection tant que possible, plutôt que ServiceFactory
simplification de HttpService
fichier de configuration JSON pour ApplicationSetupService
parent bab3aa26
No related branches found
No related tags found
1 merge request!29Resolve "Remplacer mdbootstrap par angular-material"
......@@ -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>
......
......@@ -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;
}
}
......
{
"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 ]
}
]
}
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;
});
}
}
......@@ -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 {
......
......@@ -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 => {
......
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);
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment