Skip to content
Snippets Groups Projects
Commit edf16b09 authored by francois.grand's avatar francois.grand
Browse files

Transformation du service ParamService

- il servait à fournit les paramètres des formulaires quand ceux ci étaient en unique exemplaire. ParamService a été modifié pour tenir compte du fait qu'on peut avoir plusieurs formulaires du même type.
- simplification des paramètres de création des composants (en HTML, fournis au composant fils avec [param]=valeur) : ajout de l'id de formulaire à la classe FormulaireElement
parent 82d40575
No related branches found
No related tags found
No related merge requests found
Showing
with 105 additions and 240 deletions
......@@ -5,15 +5,13 @@ import { Router } from '@angular/router';
import { ComputeNodeType, ComputeNode } from 'jalhyd';
import { ParamService } from './services/param/param.service';
import { HttpService } from './services/http/http.service';
import { InternationalisationService, Language, LanguageCode } from './services/internationalisation/internationalisation.service';
import { Observer } from './services/observer';
import { ErrorService } from './services/error/error.service';
// import { AlertDialog } from './components/alert-dialog/alert-dialog.component';
import { FormulaireService } from './services/formulaire/formulaire.service';
import { FormulaireElement } from './formulaire/formulaire-element';
import { FormulaireDefinition, CalculatorType } from './formulaire/formulaire-definition';
import { OnInit, OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks';
@Component({
......@@ -21,7 +19,7 @@ import { FormulaireDefinition, CalculatorType } from './formulaire/formulaire-de
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements Observer {
export class AppComponent implements OnInit, OnDestroy, Observer {
private _currentLanguage: LanguageCode;
private _displayErrorDialog: boolean = false;
......@@ -35,7 +33,6 @@ export class AppComponent implements Observer {
private router: Router,
private formulaireService: FormulaireService
) {
this.formulaireService.addObserver(this);
}
private initLocale() {
......@@ -52,9 +49,15 @@ export class AppComponent implements Observer {
ngOnInit() {
this.initLocale();
this.formulaireService.addObserver(this);
this.subscribeErrorService();
}
ngOnDestroy() {
this.unsubscribeErrorService();
this.formulaireService.removeObserver(this);
}
private _displayedCalc: ComputeNodeType;
/**
......@@ -64,6 +67,10 @@ export class AppComponent implements Observer {
this.errorService.addObserver(this);
}
private unsubscribeErrorService() {
this.errorService.removeObserver(this);
}
private selectLang(lc: LanguageCode) {
this.intlService.setLocale(lc);
......
......@@ -3,8 +3,8 @@
<h1>{{uitextTitre}}</h1>
</div>
</div>
<field-set *ngFor="let fs of fieldSets" [style.display]="getFieldsetStyleDisplay(fs.id)" [formId]=_formulaire.uid [id]=fs.id
[fieldSet]=fs (onRadio)=onRadioClick($event) (onSelectChange)=onSelectChanged($event)></field-set>
<field-set *ngFor="let fs of fieldSets" [style.display]="getFieldsetStyleDisplay(fs.id)" [fieldSet]=fs (onRadio)=onRadioClick($event)
(onSelectChange)=onSelectChanged($event)></field-set>
<div class="row ">
<div class="col text-center">
......
import { Component, Inject, OnInit, DoCheck, ViewChild, Input } from "@angular/core";
import { ActivatedRoute, ParamMap } from '@angular/router';
import { Component, OnInit, DoCheck, OnDestroy, ViewChild } from "@angular/core";
import { ActivatedRoute } from '@angular/router';
import { ComputeNodeType } from "jalhyd";
import { ParamService } from "../../services/param/param.service";
import { HttpService } from "../../services/http/http.service";
import { FormulaireService } from "../../services/formulaire/formulaire.service";
import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
import { FieldSet } from "../../formulaire/fieldset";
import { InputField } from "../../formulaire/input-field";
import { SelectField } from "../../formulaire/select-field";
import { FormulaireDefinition } from "../../formulaire/formulaire-definition";
import { CalculatorType } from "../../formulaire/formulaire-definition";
import { Dependency } from "../../formulaire/dependency";
import { ExistenceDependency } from "../../formulaire/existence-dependency";
import { ValueDependency } from "../../formulaire/value-dependency";
import { NgParameter, ParamRadioConfig } from "../../formulaire/ngparam";
import { CalculatorResultsComponent } from "../../components/calculator-results/calculator-results.component";
import { Observer } from "../../services/observer";
import { Subscription } from "rxjs/Subscription";
......@@ -32,7 +22,7 @@ import { Subscription } from "rxjs/Subscription";
`
]
})
export class GenericCalculatorComponent implements OnInit, DoCheck, Observer {
export class GenericCalculatorComponent implements OnInit, DoCheck, OnDestroy, Observer {
/**
* composant d'affichage des résultats
*/
......@@ -44,13 +34,11 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, Observer {
*/
private _formulaire: FormulaireDefinition;
constructor(private paramService: ParamService,
private httpService: HttpService,
private intlService: InternationalisationService,
private _subscription: Subscription; // pour souscrire aux changements d'URL envoyés par le routeur
constructor(private intlService: InternationalisationService,
private formulaireService: FormulaireService,
private route: ActivatedRoute) {
this.intlService.addObserver(this);
this.formulaireService.addObserver(this);
}
private get fieldSets(): FieldSet[] {
......@@ -85,17 +73,10 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, Observer {
return this.intlService.localizeText("INFO_CALCULATOR_CALCULER");
}
private _subscription: Subscription;
ngOnInit() {
// récupération des paramètres passés avec la route (URL)
this._subscription = this.route.params.subscribe(params => {
const uid: number = +params['uid'];
this._formulaire = this.formulaireService.getFormulaireFromId(uid);
this.resultsComponent.formulaire = this._formulaire;
this._formulaire.updateNodeType();
});
this.intlService.addObserver(this);
this.formulaireService.addObserver(this);
this.subscribeRouter();
}
ngDoCheck() {
......@@ -109,6 +90,23 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, Observer {
}
ngOnDestroy() {
this.unsubscribeRouter();
this.formulaireService.removeObserver(this);
this.intlService.removeObserver(this);
}
private subscribeRouter() {
// récupération des paramètres passés avec la route (URL)
this._subscription = this.route.params.subscribe(params => {
const uid: number = +params['uid'];
this._formulaire = this.formulaireService.getFormulaireFromId(uid);
this.resultsComponent.formulaire = this._formulaire;
this._formulaire.updateNodeType();
});
}
private unsubscribeRouter() {
this._subscription.unsubscribe();
}
......@@ -123,11 +121,16 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, Observer {
let option: string = tmp[1]; // nouvel état (radio)
this._formulaire.resetRadiosAndResults(symbol, option);
// this.appRef.tick();
// this.changeDetectorRef.detectChanges(); // provoque une détection des changements dans les contrôles
}
/**
* met à jour l'interface
*/
// private updateUI() {
// this.appRef.tick();
// this.changeDetectorRef.detectChanges(); // provoque une détection des changements dans les contrôles
//}
private doCompute() {
this._formulaire.doCompute();
this.resultsComponent.updateView();
......
......@@ -28,7 +28,6 @@ export class CalculatorListComponent {
}
private create(t: CalculatorType) {
console.log("create", t);
const p: Promise<FormulaireDefinition> = this.formulaireService.createFormulaire(t);
p.then(f => {
this.router.navigate(['/calculator', f.uid]);
......
......@@ -8,6 +8,7 @@ import { FormulaireService } from "../../services/formulaire/formulaire.service"
templateUrl: "./check-field-line.component.html",
})
export class CheckFieldLineComponent {
@Input("param")
private _check: CheckField;
private _currentValue: boolean;
......@@ -15,39 +16,6 @@ export class CheckFieldLineComponent {
constructor(private formulaireService: FormulaireService) {
}
private updateField() {
if (this._formId == undefined || this._id == undefined)
this._check = undefined;
else
this._check = this.formulaireService.getCheckField(this._formId, this._id);
}
/**
* id input attribute
*/
private _id: string;
@Input()
private set id(s: string) {
this._id = s;
this.updateField();
}
/**
* form id input attribute
*/
private _formId: number;
private get formId(): number {
return this._formId;
}
@Input()
private set formId(id: number) {
this._formId = id;
this.updateField();
}
private onChange(event: any) {
this._check.setValue(event);
}
......
......@@ -14,12 +14,12 @@
</ng-template>
-->
<ng-template ngFor let-p [ngForOf]="_fieldSet.fields">
<param-field-line *ngIf="p.isInput" [formId]=formId [computeNodeType]=_fieldSet.computeNodeType [symbol]=p.symbol (onRadio)=onRadioClick($event)>
<ng-template ngFor let-p [ngForOf]="fields">
<param-field-line *ngIf="p.isInput" [param]=p (onRadio)=onRadioClick($event)>
</param-field-line>
<select-field-line *ngIf="p.isSelect" [formId]=formId [id]=p.id (onSelectChange)=onSelectChanged($event)>
<select-field-line *ngIf="p.isSelect" [param]=p (onSelectChange)=onSelectChanged($event)>
</select-field-line>
<check-field-line *ngIf="p.isCheck" [formId]=formId [id]=p.id></check-field-line>
<check-field-line *ngIf="p.isCheck" [param]=p></check-field-line>
</ng-template>
\ No newline at end of file
......@@ -30,21 +30,8 @@ export class FieldSetComponent {
this._fieldSet = fs;
}
/**
* form id input attribute
*/
private _formId: number;
private get formId(): number {
return this._formId;
}
@Input()
private set formId(id: number) {
this._formId = id;
}
constructor() {
private get fields() {
return this._fieldSet.fields;
}
private hasRadioFix(): boolean {
......@@ -110,7 +97,6 @@ export class FieldSetComponent {
* réception d'un événement d'un select
*/
private onSelectChanged(val: string) {
//console.log("fieldset " + val);
this.onSelectChange.emit(val); // on transmet au parent
}
......
......@@ -6,7 +6,7 @@
<!-- input de saisie de la valeur -->
<div class="col-sm-2">
<param-input [inputDisabled]="isInputDisabled" [computeNodeType]="nodeType" [symbol]="symbol"></param-input>
<param-input [inputDisabled]="isInputDisabled" [param]="_param"></param-input>
</div>
<!-- radio "fixé" -->
......@@ -58,4 +58,4 @@
<input type="text" class="input-alternate" name="inputstep" placeholder="step" [(ngModel)]="_param.stepValue" />
</div>
</div>
</div>
</div>
\ No newline at end of file
......@@ -2,7 +2,6 @@ import { Component, Input, Output, DoCheck, EventEmitter } from "@angular/core";
import { ComputeNodeType } from "jalhyd";
import { ParamService } from "../../services/param/param.service";
import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
import { NgParameter, ParamRadioConfig } from "../../formulaire/ngparam";
......@@ -11,9 +10,10 @@ import { NgParameter, ParamRadioConfig } from "../../formulaire/ngparam";
templateUrl: "./param-field-line.component.html"
})
export class ParamFieldLineComponent {
@Input("param")
private _param: NgParameter;
constructor(private paramService: ParamService, private intlService: InternationalisationService) {
constructor(private intlService: InternationalisationService) {
}
private get title(): string {
......@@ -49,20 +49,6 @@ export class ParamFieldLineComponent {
return this.intlService.localizeText("INFO_PARAMFIELD_PARAMCALCULER");
}
/**
* type de noeud de calcul
*/
private _computeNode: ComputeNodeType;
private get nodeType(): ComputeNodeType {
return this._computeNode;
}
@Input()
private set computeNodeType(t: ComputeNodeType) {
this._computeNode = t;
}
/**
* Parameter symbol (Q, Ks, B, ...) input attribute
*/
......@@ -70,28 +56,6 @@ export class ParamFieldLineComponent {
return this._param.symbol;
}
@Input()
private set symbol(s: string) {
this._param = this.paramService.getParameter(this._computeNode, s);
if (this._param == undefined)
throw "symbole de paramètre '" + s + "' incorrect";
}
/**
* form id input attribute
*/
private _formId: number;
private get formId(): number {
return this._formId;
}
@Input()
private set formId(id: number) {
this._formId = id;
// this.updateField();
}
/**
* calcule la présence du radio "paramètre fixé"
*/
......
......@@ -5,16 +5,11 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, FormControl } f
import { ComputeNodeType, ParamDefinition, NumericalString, Message, MessageCode } from "jalhyd";
import { ParamService } from "../../services/param/param.service";
import { InternationalisationService, LanguageCode } from "../../services/internationalisation/internationalisation.service";
import { NgParameter } from "../../formulaire/ngparam";
@Component({
selector: "param-input",
/* OK
<input placeholder="{{_paramDef.symbol}}" [ngModel]="_paramDef.v" (ngModelChange)="setValue($event)"/>
<p *ngIf="_message">{{_message}}</p>
*/
templateUrl: "./param-input.component.html",
providers: [
{
......@@ -29,23 +24,7 @@ import { NgParameter } from "../../formulaire/ngparam";
}
]
})
export class ParamInputComponent implements ControlValueAccessor, OnInit, DoCheck {
/**
* type de noeud de calcul
*/
private _computeNode: ComputeNodeType;
@Input()
private set computeNodeType(t: ComputeNodeType) {
this._computeNode = t;
}
/**
* Parameter symbol (Q, Ks, B, ...) attribute
*/
@Input('symbol')
private _paramSymbol: string;
export class ParamInputComponent implements ControlValueAccessor, DoCheck {
/**
* enable/disable input field
*/
......@@ -55,6 +34,7 @@ export class ParamInputComponent implements ControlValueAccessor, OnInit, DoChec
/**
* managed parameter
*/
@Input('param')
private _paramDef: NgParameter;
private _message: string;
......@@ -66,7 +46,6 @@ export class ParamInputComponent implements ControlValueAccessor, OnInit, DoChec
/**
* true si la modification du paramètre géré vient de l'interface utilisateur
*
* false si la modification du paramètre géré vient d'un appel du code
*/
private _fromUI: boolean;
......@@ -76,17 +55,12 @@ export class ParamInputComponent implements ControlValueAccessor, OnInit, DoChec
*/
private _uiValue: NumericalString;
constructor(private paramService: ParamService, private changeDetector: ChangeDetectorRef, private intlService: InternationalisationService) {
constructor(private changeDetector: ChangeDetectorRef, private intlService: InternationalisationService) {
this._uiValue = new NumericalString();
}
hasError(): boolean {
let res = (this._message != undefined);
// if (res)
// this.log("hasError : true " + this._message);
// else
// this.log("hasError : false");
return res;
return this._message != undefined;
}
private getSfromUI(): string {
......@@ -108,11 +82,6 @@ export class ParamInputComponent implements ControlValueAccessor, OnInit, DoChec
return this.intlService.localizeText("INFO_SAISIEPARAM_TITRE");
}
ngOnInit() {
// retrieve parameter from symbol
this._paramDef = this.paramService.getParameter(this._computeNode, this._paramSymbol);
}
/**
* fonction appelée lorsque l'utilisateur fait une saisie
* @param event valeur du contrôle
......
......@@ -9,6 +9,7 @@ import { FormulaireService } from "../../services/formulaire/formulaire.service"
templateUrl: "./select-field-line.component.html"
})
export class SelectFieldLineComponent {
@Input("param")
private _select: SelectField;
constructor(private formulaireService: FormulaireService) {
......@@ -24,39 +25,6 @@ export class SelectFieldLineComponent {
return "<no selection>";
}
private updateField() {
if (this._formId == undefined || this._id == undefined)
this._select = undefined;
else
this._select = this.formulaireService.getSelectField(this._formId, this._id);
}
/**
* id input attribute
*/
private _id: string;
@Input()
private set id(s: string) {
this._id = s;
this.updateField();
}
/**
* form id input attribute
*/
private _formId: number;
private get formId(): number {
return this._formId;
}
@Input()
private set formId(id: number) {
this._formId = id;
this.updateField();
}
/**
* selected value event
*/
......
......@@ -7,8 +7,8 @@ import { DependencyConditionType } from "./dependency-condition";
export class CheckField extends Field {
private _value: boolean;
constructor(nodeType: ComputeNodeType, id: string) {
super(nodeType, id, FieldType.Check);
constructor(nodeType: ComputeNodeType, id: string, formId: number) {
super(nodeType, id, FieldType.Check, formId);
this._value = false;
}
......
......@@ -7,8 +7,8 @@ export enum FieldType {
}
export abstract class Field extends FormulaireElement {
constructor(nodeType: ComputeNodeType, id: string, private _fieldType: FieldType) {
super(nodeType, id);
constructor(nodeType: ComputeNodeType, id: string, private _fieldType: FieldType, formId: number) {
super(nodeType, id, formId);
}
public get isInput(): boolean {
......
......@@ -10,8 +10,8 @@ import { NgParameter } from "./ngparam";
export class FieldSet extends FormulaireElement {
private _fields: Field[];
constructor(nodeType: ComputeNodeType, id: string) {
super(nodeType, id);
constructor(nodeType: ComputeNodeType, id: string, formId: number) {
super(nodeType, id, formId);
this._fields = [];
}
......
......@@ -165,7 +165,6 @@ export class FormulaireDefinition {
return undefined;
}
private removePrefix(s: string, prefix: string): string {
if (s.startsWith(prefix)) {
let l = prefix.length;
......@@ -466,7 +465,7 @@ export class FormulaireDefinition {
private parse_check(node_type: ComputeNodeType, field: {}): CheckField {
let id = field["id"];
let res: CheckField = new CheckField(node_type, id);
let res: CheckField = new CheckField(node_type, id, this._uid);
let value = field["value"];
res.setValue(value == "true");
......@@ -478,7 +477,7 @@ export class FormulaireDefinition {
private parse_select(node_type: ComputeNodeType, field: {}): SelectField {
let id = field["id"];
let res: SelectField = new SelectField(node_type, id);
let res: SelectField = new SelectField(node_type, id, this._uid);
let values = field["select"];
for (let v of values) {
......@@ -493,7 +492,7 @@ export class FormulaireDefinition {
private parse_input(node_type: ComputeNodeType, fieldset: {}, field: {}): NgParameter {
let input_id: string = field["id"];
let res: NgParameter = this.paramService.getParameter(node_type, input_id);
let res: NgParameter = this.paramService.createParameter(node_type, input_id, this._uid);
if (res == undefined)
throw "pas de paramètre '" + input_id + "' trouvé dans le noeud " + ComputeNodeType[node_type] + "(" + node_type + ") ou " + ComputeNodeType[this._defaultNodeType] + "(" + this._defaultNodeType + ")";
......@@ -515,7 +514,7 @@ export class FormulaireDefinition {
let node_type: ComputeNodeType;
node_type = nt == undefined ? this._defaultNodeType : ComputeNodeType[nt];
let res: FieldSet = new FieldSet(node_type, conf_id);
let res: FieldSet = new FieldSet(node_type, conf_id, this._uid);
this._fieldSets.push(res);
let fields = fieldset["fields"];
......@@ -547,7 +546,7 @@ export class FormulaireDefinition {
return undefined;
}
private parseNodeType() {
private parseDefaultNodeType() {
let nt: string = this.getOption("nodeType");
if (nt == undefined)
throw "l'option obligatoire 'nodeType' est absente du fichier de définition de formulaire";
......@@ -559,7 +558,7 @@ export class FormulaireDefinition {
this._dependencies = [];
this._defaultCalculatedParam = undefined;
this.parseNodeType();
this.parseDefaultNodeType();
for (let conf_index in this._config) {
let conf = this._config[conf_index];
......
......@@ -8,13 +8,15 @@ import { StringMap } from "../stringmap";
export abstract class FormulaireElement {
private _nodeType: ComputeNodeType;
private _id: string;
private _formId: number;
public isDisplayed: boolean;
public label: string;
constructor(nodeType: ComputeNodeType, id: string) {
constructor(nodeType: ComputeNodeType, id: string, formId: number) {
this._nodeType = nodeType;
this._id = id;
this.isDisplayed = true;
this._formId = formId;
}
get computeNodeType(): ComputeNodeType {
......@@ -25,6 +27,10 @@ export abstract class FormulaireElement {
return this._id;
}
get formId(): number {
return this._formId;
}
protected abstract verifyDependency(d: Dependency): boolean;
public verifiesDependency(d: Dependency): boolean {
......
......@@ -6,8 +6,8 @@ import { Field, FieldType } from "./field"
export abstract class InputField extends Field {
private _value: any;
constructor(type: ComputeNodeType, id: string) {
super(type, id, FieldType.Input);
constructor(type: ComputeNodeType, id: string, formId: number) {
super(type, id, FieldType.Input, formId);
}
public getValue() {
......
......@@ -36,8 +36,8 @@ export class NgParameter extends InputField {
public maxValue: number; // valeur max dans le cas ParamRadioConfig.VAR
public stepValue: number; // pas de progression dans le cas ParamRadioConfig.VAR
constructor(private _paramDef: ParamDefinition) {
super(_paramDef.computeNodeType, _paramDef.symbol);
constructor(private _paramDef: ParamDefinition, formId: number) {
super(_paramDef.computeNodeType, _paramDef.symbol, formId);
switch (this._paramDef.getDomain().domain) {
case ParamDomainValue.ANY:
this.minValue = -10;
......
......@@ -18,8 +18,8 @@ export class SelectField extends Field {
return this._entries;
}
constructor(nodeType: ComputeNodeType, id: string) {
super(nodeType, id, FieldType.Select);
constructor(nodeType: ComputeNodeType, id: string, formId: number) {
super(nodeType, id, FieldType.Select, formId);
this._entries = [];
}
......
......@@ -8,6 +8,7 @@ import { HttpService } from "../../services/http/http.service";
import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
import { FormulaireDefinition, CalculatorType } from "../../formulaire/formulaire-definition";
import { FormulaireElement } from "../../formulaire/formulaire-element";
import { InputField } from "../../formulaire/input-field";
import { SelectField } from "../../formulaire/select-field";
import { CheckField } from "../../formulaire/check-field";
import { StringMap } from "../../stringmap";
......@@ -118,30 +119,25 @@ export class FormulaireService extends Observable {
throw "FormulaireService.getFormulaire() : unkown form id " + uid;
}
public getInputField(formId: number, elemId: string): InputField {
let s = this.getFormulaireElementById(formId, elemId);
if (!(s instanceof InputField))
throw "Form element with id '" + elemId + "' is not an input";
return <InputField>s;
}
public getCheckField(formId: number, elemId: string): CheckField {
for (let f of this._formulaires)
if (f.uid == formId) {
let s = f.getFormulaireElementById(elemId);
if (s != undefined) {
if (!(s instanceof CheckField))
throw "Form element with id '" + elemId + "' is not a checkbox";
return <CheckField>s;
}
}
return undefined;
let s = this.getFormulaireElementById(formId, elemId);
if (!(s instanceof CheckField))
throw "Form element with id '" + elemId + "' is not a checkbox";
return <CheckField>s;
}
public getSelectField(formId: number, elemId: string): SelectField {
for (let f of this._formulaires)
if (f.uid == formId) {
let s = f.getFormulaireElementById(elemId);
if (s != undefined) {
if (!(s instanceof SelectField))
throw "Form element with id '" + elemId + "' is not a select";
return <SelectField>s;
}
}
return undefined;
let s = this.getFormulaireElementById(formId, elemId);
if (!(s instanceof SelectField))
throw "Form element with id '" + elemId + "' is not a select";
return <SelectField>s;
}
private getFormulaireElementById(formId: number, elemId: string): FormulaireElement {
......
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