diff --git a/src/app/calculators/cond_distri/conddistri.component.ts b/src/app/calculators/cond_distri/conddistri.component.ts index 3153398e056aadc88d605aafde7a8e6540f129d4..477ed697b4bfd02aa86756a0fe200708766bc719 100644 --- a/src/app/calculators/cond_distri/conddistri.component.ts +++ b/src/app/calculators/cond_distri/conddistri.component.ts @@ -3,13 +3,14 @@ import { Response } from '@angular/http'; import { Observable } from "rxjs/Observable"; import 'rxjs/add/operator/toPromise'; -import { ConduiteDistrib, ConduiteDistribParams } from "jalhyd"; +import { IParamsEquation, Nub, ConduiteDistrib, ConduiteDistribParams } from "jalhyd"; import { ParamService } from '../../services/param/param.service'; import { HttpService } from '../../services/http/http.service'; import { InternationalisationService } from '../../services/internationalisation/internationalisation.service'; -import { FieldSet } from '../../components/field-set/field-set.component'; -import { NgParameter, ParamRadioConfig } from '../../services/ngparam'; +import { FieldSet } from '../../calculators/generic/formulaire'; +import { NgParameter, ParamRadioConfig } from '../../calculators/generic/ngparam'; +import { GenericCalculatorComponent } from '../generic/calculator.component'; import { CalculatorResultsComponent } from '../../components/calculator-results/calculator-results.component'; @Component({ @@ -28,23 +29,23 @@ import { CalculatorResultsComponent } from '../../components/calculator-results/ ` ] }) -export class CondDistriComponent implements OnInit, DoCheck { +export class CondDistriComponent extends GenericCalculatorComponent implements OnInit, DoCheck { /** * objet JSON chargé depuis le fichier de traduction */ - private _localisation = {}; + // private _localisation = {}; /** * objet JSON chargé depuis le fichier de configuration de la calculette */ - private _config = {}; + // private _config = {}; - private _fieldSets: FieldSet[] = []; + // private _fieldSets: FieldSet[] = []; /** * symbole du paramètre à calculer par défaut (cf config "idCal") */ - private _defaultCalculatedParam: string; + // private _defaultCalculatedParam: string; /** * composant d'affichage des résultats @@ -55,12 +56,24 @@ export class CondDistriComponent implements OnInit, DoCheck { /** * flag d'affichage du composant de résultats */ - private _showResults: boolean = false; + // private _showResults: boolean = false; - constructor(private paramService: ParamService, private httpService: HttpService, private intlService: InternationalisationService) { + constructor(private paramSvc: ParamService, private httpSvc: HttpService, private intlSvc: InternationalisationService) { + super(paramSvc, httpSvc, intlSvc); } - // private loadLocalisation() { + // // private loadLocalisation() { + // // let ths = this; + // // let processData = function (s: string) { + // // // fermeture nécessaire pour capturer la valeur de this (undefined sinon) + // // ths._localisation = JSON.parse(s); + // // ths.paramService.updateLocalisation(ths._localisation); + // // } + + // // let f: string = "app/calculators/cond_distri/cond_distri." + this.intlService.languageCode + ".json" + // // this.httpService.httpGetRequest(undefined, undefined, undefined, f, processData); + // // } + // private loadLocalisation(): Promise<string> { // let ths = this; // let processData = function (s: string) { // // fermeture nécessaire pour capturer la valeur de this (undefined sinon) @@ -68,154 +81,143 @@ export class CondDistriComponent implements OnInit, DoCheck { // ths.paramService.updateLocalisation(ths._localisation); // } - // let f: string = "app/calculators/cond_distri/cond_distri." + this.intlService.languageCode + ".json" - // this.httpService.httpGetRequest(undefined, undefined, undefined, f, processData); + // let f: string = "app/calculators/cond_distri/cond_distri." + this.intlService.currentLanguage.tag + ".json" + // let resp: Observable<Response> = this.httpService.httpGetRequestResponse(undefined, undefined, undefined, f); + + // let prom = resp.map(res => res.text()).toPromise(); + // prom.then((res) => { + // processData(res); + // }) + + // return prom; // } - private loadLocalisation(): Promise<string> { - let ths = this; - let processData = function (s: string) { - // fermeture nécessaire pour capturer la valeur de this (undefined sinon) - ths._localisation = JSON.parse(s); - ths.paramService.updateLocalisation(ths._localisation); - } - - let f: string = "app/calculators/cond_distri/cond_distri." + this.intlService.currentLanguage.tag + ".json" - let resp: Observable<Response> = this.httpService.httpGetRequestResponse(undefined, undefined, undefined, f); - - let prom = resp.map(res => res.text()).toPromise(); - prom.then((res) => { - processData(res); - }) - - return prom; - } - private logObject(obj: {}, m?: string) { - // évite le message "Value below was evaluated just now" dans le debugger de Chrome - if (m == undefined) - console.log(JSON.stringify(obj)); - else - console.log(m + " " + JSON.stringify(obj)); - } + // private logObject(obj: {}, m?: string) { + // // évite le message "Value below was evaluated just now" dans le debugger de Chrome + // if (m == undefined) + // console.log(JSON.stringify(obj)); + // else + // console.log(m + " " + JSON.stringify(obj)); + // } - private parseConfig() { - this._fieldSets = []; - - for (let conf_index in this._config) { - - let conf = this._config[conf_index]; - let conf_id: string = conf["id"]; - - // field set - if (conf_id.startsWith("fs_")) { - let fieldSet: FieldSet = new FieldSet(conf_id); - - let fields = conf["fields"]; - for (let field_index in fields) { - let field = fields[field_index]; - if (field["type"] === "input") { - let input_id = field["id"]; - let param: NgParameter = this.paramService.getParameter(input_id); - if (param != undefined) { - param.unit = field["unit"]; - param.v = +field["value"]; - param.radioConfig = NgParameter.getRadioConfig(conf["option"]); - param.radioState = ParamRadioConfig.FIX; - param.isDefault = false; // malgré le fait qu'il soit initialisé dans la déclaration de la classe NgParam à false, quand on relit sa valeur, il vaut undefined (merci Microsoft) - fieldSet.params.push(param); - } - } - } - if (fieldSet.params.length > 0) { - this._fieldSets.push(fieldSet); - } - } - // options globales - else if (conf_id === "options") { - // id du paramètre à calculer par défaut - this._defaultCalculatedParam = conf["idCal"]; - let p = this.getParamFromSymbol(this._defaultCalculatedParam); - p.isDefault = true; - p.radioState = ParamRadioConfig.CAL; - } - } - } + // private parseConfig() { + // this._fieldSets = []; + + // for (let conf_index in this._config) { + + // let conf = this._config[conf_index]; + // let conf_id: string = conf["id"]; + + // // field set + // if (conf_id.startsWith("fs_")) { + // let fieldSet: FieldSet = new FieldSet(conf_id); + + // let fields = conf["fields"]; + // for (let field_index in fields) { + // let field = fields[field_index]; + // if (field["type"] === "input") { + // let input_id = field["id"]; + // let param: NgParameter = this.paramService.getParameter(input_id); + // if (param != undefined) { + // param.unit = field["unit"]; + // param.v = +field["value"]; + // param.radioConfig = NgParameter.getRadioConfig(conf["option"]); + // param.radioState = ParamRadioConfig.FIX; + // param.isDefault = false; // malgré le fait qu'il soit initialisé dans la déclaration de la classe NgParam à false, quand on relit sa valeur, il vaut undefined (merci Microsoft) + // fieldSet.addField(param); + // } + // } + // } + // if (!fieldSet.isEmpty()) { + // this._fieldSets.push(fieldSet); + // } + // } + // // options globales + // else if (conf_id === "options") { + // // id du paramètre à calculer par défaut + // this._defaultCalculatedParam = conf["idCal"]; + // let p = this.getParamFromSymbol(this._defaultCalculatedParam); + // p.isDefault = true; + // p.radioState = ParamRadioConfig.CAL; + // } + // } + // } - private getParamFromSymbol(symbol: string): NgParameter { - for (let fs of this._fieldSets) { - for (let p of fs.params) { - if (p.symbol === symbol) - return p; - } - } - return undefined; - } + // private getParamFromSymbol(symbol: string): NgParameter { + // for (let fs of this._fieldSets) { + // for (let p of fs.fields) { + // if (p.symbol === symbol) + // return p; + // } + // } + // return undefined; + // } - private getParamFromState(st: ParamRadioConfig): NgParameter { - for (let fs of this._fieldSets) { - for (let p of fs.params) { - if (p.radioState == st) - return p; - } - } - return undefined; - } + // private getParamFromState(st: ParamRadioConfig): NgParameter { + // for (let fs of this._fieldSets) { + // for (let p of fs.params) { + // if (p.radioState == st) + // return p; + // } + // } + // return undefined; + // } + + // private loadConfig() { + // let ths = this; + // let processData = function (s: string) { + // // fermeture nécessaire pour capturer la valeur de this (undefined sinon) + // ths._config = JSON.parse(s); + // ths.parseConfig(); + // } - private loadConfig() { - let ths = this; - let processData = function (s: string) { - // fermeture nécessaire pour capturer la valeur de this (undefined sinon) - ths._config = JSON.parse(s); - ths.parseConfig(); - } + // let f: string = "app/calculators/cond_distri/cond_distri.config.json" + // this.httpService.httpGetRequest(undefined, undefined, undefined, f, processData); + // } - let f: string = "app/calculators/cond_distri/cond_distri.config.json" - this.httpService.httpGetRequest(undefined, undefined, undefined, f, processData); - } + // ngOnInit() { + // this.loadLocalisation(); + // this.loadConfig(); + // // this.updateLanguage(); + // } - ngOnInit() { - this.loadLocalisation(); - this.loadConfig(); - this.updateLanguage(); - } + // private getFieldSet(id: string) { + // for (let fs of this._fieldSets) { + // if (fs.id == id) + // return fs; + // } + // return undefined; + // } - private getFieldSet(id: string) { - for (let fs of this._fieldSets) { - if (fs.id == id) - return fs; - } - return undefined; - } + // private updateLanguage() { + // for (let conf_index in this._config) { + // let conf = this._config[conf_index]; + // let conf_id: string = conf["id"]; - private updateLanguage() { - for (let conf_index in this._config) { - let conf = this._config[conf_index]; - let conf_id: string = conf["id"]; - - if (conf_id.startsWith("fs_")) { - let fieldSet: FieldSet = this.getFieldSet(conf_id); - if (fieldSet != undefined) - fieldSet.title = this._localisation[conf_id]; - } - } - } + // if (conf_id.startsWith("fs_")) { + // let fieldSet: FieldSet = this.getFieldSet(conf_id); + // if (fieldSet != undefined) + // fieldSet.title = this._localisation[conf_id]; + // } + // } + // } - ngDoCheck() { - // let q = this.getParamFromSymbol("Q"); // A VIRER !!!! - // if (q != undefined) { - // q.radioState = ParamRadioConfig.VAR; - // q.minValue = 1.5; - // q.maxValue = 6; - // q.stepValue = 0.3; - // } - - if (this.intlService.localeChanged) { - const promise = this.loadLocalisation() - .then(() => { - this.updateLanguage(); - }); - } - } + // ngDoCheck() { + // // let q = this.getParamFromSymbol("Q"); // A VIRER !!!! + // // if (q != undefined) { + // // q.radioState = ParamRadioConfig.VAR; + // // q.minValue = 1.5; + // // q.maxValue = 6; + // // q.stepValue = 0.3; + // // } + + // if (this.intlService.localeChanged) { + // const promise = this.loadLocalisation() + // .then(() => { + // this.updateLanguage(); + // }); + // } + // } /* * gestion des événements clic sur les radios : @@ -223,171 +225,191 @@ export class CondDistriComponent implements OnInit, DoCheck { * cf. https://angular.io/guide/component-interaction#parent-listens-for-child-event */ - private onRadioClick(info: string) { - this._showResults = false; - - // console.log("CondDistriComponent " + info); - let tmp: string[] = info.split("_"); - let symbol: string = tmp[0]; - - let sourceParam = this.getParamFromSymbol(symbol); - let oldState: ParamRadioConfig = sourceParam.radioState; - let newState: ParamRadioConfig = ParamRadioConfig[tmp[1].toUpperCase()]; - // console.log(sourceParam.symbol + " : " + ParamRadioConfig[oldState] + " -> " + ParamRadioConfig[newState]); - // this.logObject(sourceParam, "sourceobj1"); - - switch (oldState) { - case ParamRadioConfig.FIX: - switch (newState) { - case ParamRadioConfig.VAR: - this.resetOther(sourceParam, ParamRadioConfig.CAL); - break; - - case ParamRadioConfig.CAL: - this.resetOther(sourceParam, ParamRadioConfig.VAR); - break; - } - break; - - case ParamRadioConfig.VAR: - switch (newState) { - case ParamRadioConfig.CAL: - this.resetOther(sourceParam, ParamRadioConfig.VAR); - break; - } - break; - - case ParamRadioConfig.CAL: - switch (newState) { - case ParamRadioConfig.FIX: - this.setDefault(); - break; - - case ParamRadioConfig.VAR: - this.resetOther(sourceParam, ParamRadioConfig.CAL); - this.setDefault(); - break; - } - } - - sourceParam.radioState = newState; - - // this.appRef.tick(); - // this.changeDetectorRef.detectChanges(); // provoque une détection des changements dans les contrôles - } + // private onRadioClick(info: string) { + // this._showResults = false; + + // // console.log("CondDistriComponent " + info); + // let tmp: string[] = info.split("_"); + // let symbol: string = tmp[0]; + + // let sourceParam = this.getParamFromSymbol(symbol); + // let oldState: ParamRadioConfig = sourceParam.radioState; + // let newState: ParamRadioConfig = ParamRadioConfig[tmp[1].toUpperCase()]; + // // console.log(sourceParam.symbol + " : " + ParamRadioConfig[oldState] + " -> " + ParamRadioConfig[newState]); + // // this.logObject(sourceParam, "sourceobj1"); + + // switch (oldState) { + // case ParamRadioConfig.FIX: + // switch (newState) { + // case ParamRadioConfig.VAR: + // this.resetOther(sourceParam, ParamRadioConfig.CAL); + // break; + + // case ParamRadioConfig.CAL: + // this.resetOther(sourceParam, ParamRadioConfig.VAR); + // break; + // } + // break; + + // case ParamRadioConfig.VAR: + // switch (newState) { + // case ParamRadioConfig.CAL: + // this.resetOther(sourceParam, ParamRadioConfig.VAR); + // break; + // } + // break; + + // case ParamRadioConfig.CAL: + // switch (newState) { + // case ParamRadioConfig.FIX: + // this.setDefault(); + // break; + + // case ParamRadioConfig.VAR: + // this.resetOther(sourceParam, ParamRadioConfig.CAL); + // this.setDefault(); + // break; + // } + // } + + // sourceParam.radioState = newState; + + // // this.appRef.tick(); + // // this.changeDetectorRef.detectChanges(); // provoque une détection des changements dans les contrôles + // } /** * remet tous les paramètres à FIX sauf "me" et ceux (celui) à l'état "except" */ - private resetOther(me: NgParameter, except: ParamRadioConfig) { - // console.log("reset me=" + me.symbol + " sauf=" + ParamRadioConfig[except]) - for (let fs of this._fieldSets) { - for (let p of fs.params) { - if (p != me && p.radioState != except && p.radioConfig != ParamRadioConfig.FIX) { - // console.log("reset " + p.symbol + " st " + ParamRadioConfig[p.radioState] + " -> FIX"); - p.radioState = ParamRadioConfig.FIX; - } - } - } - } + // private resetOther(me: NgParameter, except: ParamRadioConfig) { + // // console.log("reset me=" + me.symbol + " sauf=" + ParamRadioConfig[except]) + // for (let fs of this._fieldSets) { + // for (let p of fs.params) { + // if (p != me && p.radioState != except && p.radioConfig != ParamRadioConfig.FIX) { + // // console.log("reset " + p.symbol + " st " + ParamRadioConfig[p.radioState] + " -> FIX"); + // p.radioState = ParamRadioConfig.FIX; + // } + // } + // } + // } /** * met le paramètre par défaut à CAL */ - private setDefault() { - let defaultParamCal = this.getParamFromSymbol(this._defaultCalculatedParam); - // console.log("setdefault " + defaultParamCal.symbol + " -> CAL") - defaultParamCal.radioState = ParamRadioConfig.CAL; - } + // private setDefault() { + // let defaultParamCal = this.getParamFromSymbol(this._defaultCalculatedParam); + // // console.log("setdefault " + defaultParamCal.symbol + " -> CAL") + // defaultParamCal.radioState = ParamRadioConfig.CAL; + // } - private getParameterValue(symbol: string): number { - for (let fs of this._fieldSets) { - for (let p of fs.params) { - if (p.symbol === symbol) { - switch (p.radioState) { - case ParamRadioConfig.FIX: - return p.v; - - case ParamRadioConfig.VAR: - case ParamRadioConfig.CAL: - return undefined; - } - } - } - } - } + // private getParameterValue(symbol: string): number { + // for (let fs of this._fieldSets) { + // for (let p of fs.params) { + // if (p.symbol === symbol) { + // switch (p.radioState) { + // case ParamRadioConfig.FIX: + // return p.v; + + // case ParamRadioConfig.VAR: + // case ParamRadioConfig.CAL: + // return undefined; + // } + // } + // } + // } + // } - private getComputedParameter(): NgParameter { - return this.getParamFromState(ParamRadioConfig.CAL); - } + // private getComputedParameter(): NgParameter { + // return this.getParamFromState(ParamRadioConfig.CAL); + // } - private getVariatedParameter(): NgParameter { - return this.getParamFromState(ParamRadioConfig.VAR); - } + // private getVariatedParameter(): NgParameter { + // return this.getParamFromState(ParamRadioConfig.VAR); + // } - private addFixedResults(nDigits: number) { - for (let fs of this._fieldSets) { - for (let p of fs.params) { - if (p.radioState == ParamRadioConfig.FIX && p.symbol !== "Pr") { - this.resultsComponent.addFixedResult(p, p.v, nDigits); - } - } - } - } + // private addFixedResults(nDigits: number) { + // for (let fs of this._fieldSets) { + // for (let p of fs.params) { + // if (p.radioState == ParamRadioConfig.FIX && p.symbol !== "Pr") { + // this.resultsComponent.addFixedResult(p, p.v, nDigits); + // } + // } + // } + // } - private doCompute() { - this._showResults = false; - let computedParam = this.getComputedParameter(); + // private doCompute() { + // this._showResults = false; + // let computedParam = this.getComputedParameter(); + // let Q: number = this.getParameterValue("Q"); // débit Q + // let D: number = this.getParameterValue("D"); // diamètre D + // let J: number = this.getParameterValue("J"); // perte de charge J + // let Lg: number = this.getParameterValue("Lg"); // Longueur de la conduite Lg + // let Nu: number = this.getParameterValue("Nu"); // Viscosité dynamique Nu + // let prec: number = this.getParameterValue("Pr"); // précision + // let nDigits = -Math.log10(prec); + + // let prms = new ConduiteDistribParams(Q, D, J, Lg, Nu); + // let nub = new ConduiteDistrib(prms); + + // let varParam = this.getVariatedParameter(); + // this.resultsComponent.reset(varParam == undefined); + // if (varParam == undefined) { + // // pas de paramètre à varier + + // let res = nub.Calc(computedParam.symbol, 0, prec).vCalc; + + // this.addFixedResults(nDigits); + // this.resultsComponent.addFixedResult(computedParam, res, nDigits); + // } + // else { + // // il y a un paramètre à varier + + // this.addFixedResults(nDigits); + // this.resultsComponent.setVariableParamHeader(varParam); + // this.resultsComponent.setVariableResultHeader(computedParam); + + // let min: number = +varParam.minValue; + // let max: number = +varParam.maxValue; + // let step: number = +varParam.stepValue; + + // for (let val = min; val <= max; val += step) { + // prms[varParam.symbol].v = val; + + // let res = nub.Calc(computedParam.symbol, 0, prec).vCalc; + // this.resultsComponent.addVarResult(val, res, nDigits); + // } + // // for (let vr of this._varResults) { + // // console.log(vr); + // // } + + // this.resultsComponent.setGraphTitle(computedParam.symbol + " = f( " + varParam.symbol + " )"); + // this.resultsComponent.generateGraph(); + // } + // this._showResults = true; + // } + + // private getResultsStyleDisplay() { + // return this._showResults ? "block" : "none"; + // } + + protected getConfigPathPrefix(): string { + return "app/calculators/cond_distri/cond_distri." + } + + protected getNubAndParameters(): [Nub, IParamsEquation] { let Q: number = this.getParameterValue("Q"); // débit Q let D: number = this.getParameterValue("D"); // diamètre D let J: number = this.getParameterValue("J"); // perte de charge J let Lg: number = this.getParameterValue("Lg"); // Longueur de la conduite Lg - let Nu: number = this.getParameterValue("Nu"); // Viscosité dynamique Nu - let prec: number = this.getParameterValue("Pr"); // précision - let nDigits = -Math.log10(prec); - + let Nu: number = this.getParameterValue("Nu"); // viscosité dynamique let prms = new ConduiteDistribParams(Q, D, J, Lg, Nu); let nub = new ConduiteDistrib(prms); - let varParam = this.getVariatedParameter(); - this.resultsComponent.reset(varParam == undefined); - if (varParam == undefined) { - // pas de paramètre à varier - - let res = nub.Calc(computedParam.symbol, 0, prec).vCalc; - - this.addFixedResults(nDigits); - this.resultsComponent.addFixedResult(computedParam, res, nDigits); - } - else { - // il y a un paramètre à varier - - this.addFixedResults(nDigits); - this.resultsComponent.setVariableParamHeader(varParam); - this.resultsComponent.setVariableResultHeader(computedParam); - - let min: number = +varParam.minValue; - let max: number = +varParam.maxValue; - let step: number = +varParam.stepValue; - - for (let val = min; val <= max; val += step) { - prms[varParam.symbol].v = val; - - let res = nub.Calc(computedParam.symbol, 0, prec).vCalc; - this.resultsComponent.addVarResult(val, res, nDigits); - } - // for (let vr of this._varResults) { - // console.log(vr); - // } - - this.resultsComponent.setGraphTitle(computedParam.symbol + " = f( " + varParam.symbol + " )"); - this.resultsComponent.generateGraph(); - } - this._showResults = true; + return [nub, prms]; } - private getResultsStyleDisplay() { - return this._showResults ? "block" : "none"; + protected getCalculatorResultsComponent(): CalculatorResultsComponent { + return this.resultsComponent; } }