An error occurred while loading the file. Please try again.
-
Antoine Regimbeau authored5f9b1f7e
// cf. https://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
import { Component, Input, forwardRef, OnInit, DoCheck, ChangeDetectorRef } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, FormControl } from "@angular/forms";
import { ComputeNodeType, ParamDefinition, NumericalString, Message, MessageCode } from "jalhyd";
import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
import { NgParameter } from "../../formulaire/ngparam";
import { GenericInputComponent } from "../generic-input/generic-input.component";
import { Observer, IObservable } from "../../services/observer";
@Component({
selector: "ngparam-input",
templateUrl: "../generic-input/generic-input.component.html"
})
export class NgParamInputComponent extends GenericInputComponent implements Observer {
/**
* paramètre géré
*/
@Input('param')
private _paramDef: NgParameter;
/**
* valeur intermédiaire nécessitée par le fait que toutes les valeurs numériques ne sont pas légales
* pour NgParameter (l'affecter peut provoquer une exception) et qui permet de faire fonctionner la validation du modèle
*/
private _model: number;
constructor(private intlService: InternationalisationService) {
super();
}
protected getModelValue(): any {
return this._model;
}
protected setModelValue(v: any) {
this._model = v;
try {
this._paramDef.setValue(v);
}
catch (e) {
// géré par validateModelValue()
}
}
protected validateModelValue(v: any): { isValid: boolean, message: string } {
let msg = undefined;
let valid = false;
try {
this._paramDef.checkValue(v);
valid = true;
}
catch (e) {
if (e instanceof Message)
msg = this.intlService.localizeMessage(e);
else
msg = "invalid value";
}
return { isValid: valid, message: msg };
}
protected modelToUI(v: any): string {
return String(v);
}
protected validateUIValue(ui: string): { isValid: boolean, message: string } {
7172737475767778798081828384858687888990919293949596979899100101102
let valid: boolean = false;
let msg: string = undefined;
let v: NumericalString = new NumericalString(ui);
if (!v.isNumerical)
msg = "Veuillez entrer une valeur numérique";
else
valid = true;
return { isValid: valid, message: msg };
}
protected uiToModel(ui: string) {
return +ui;
}
protected afterFirstViewChecked() {
if (this._paramDef != undefined)
if (this._paramDef.isDefined)
this._model = this._paramDef.getValue();
super.afterFirstViewChecked();
this._paramDef.addObserver(this);
}
public update(sender: IObservable, data: any): void {
if (data["action"] == "value") {
this._model = data["value"];
this.updateAndValidateUI();
}
}
}