Newer
Older
francois.grand
committed
// 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 { NumericalString, Message, Observer } from "jalhyd";
francois.grand
committed
import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
import { NgParameter } from "../../formulaire/ngparam";
import { GenericInputComponent } from "../generic-input/generic-input.component";
@Component({
selector: "ngparam-input",
templateUrl: "../generic-input/generic-input.component.html"
})
francois.grand
committed
export class NgParamInputComponent extends GenericInputComponent implements Observer {
francois.grand
committed
/**
francois.grand
committed
* paramètre géré
francois.grand
committed
*/
private get _paramDef(): NgParameter {
return this.model;
}
francois.grand
committed
francois.grand
committed
/**
* 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
*/
francois.grand
committed
constructor(private intlService: InternationalisationService, cdRef: ChangeDetectorRef) {
super(cdRef);
francois.grand
committed
}
* appelé avant le changement de modèle
*/
francois.grand
committed
protected beforeSetModel() {
if (this._paramDef != undefined)
this._paramDef.removeObserver(this);
}
/**
* appelé après le changement de modèle
*/
protected afterSetModel() {
if (this._paramDef != undefined) {
if (this._paramDef.isDefined)
this._tmp = this._paramDef.getValue();
francois.grand
committed
this._paramDef.addObserver(this);
francois.grand
committed
protected getModelValue(): any {
francois.grand
committed
}
francois.grand
committed
protected setModelValue(sender: any, v: any) {
francois.grand
committed
try {
francois.grand
committed
this._paramDef.setValue(sender, v);
francois.grand
committed
}
catch (e) {
// géré par validateModelValue()
}
}
protected validateModelValue(v: any): { isValid: boolean, message: string } {
let msg = undefined;
let valid = false;
if (this._paramDef == undefined)
msg = "internal error, model undefined";
else
try {
this._paramDef.checkValue(v);
valid = true;
}
catch (e) {
if (e instanceof Message)
msg = this.intlService.localizeMessage(e);
else
msg = "invalid value";
}
francois.grand
committed
return { isValid: valid, message: msg };
}
protected modelToUI(v: any): string {
return String(v);
}
protected validateUIValue(ui: string): { isValid: boolean, message: string } {
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;
}
francois.grand
committed
francois.grand
committed
public update(sender: any, data: any): void {
switch (data["action"]) {
case "ngparamAfterValue":
// on ne fait rien au cas où la modif vient de l'interface (on ne remet pas à jour _uiValue ce qui permet
// de garder par ex le '.' si on supprime le '2' de '1.2')
if (sender !== this) {
this._tmp = data["value"];
this.updateAndValidateUI();
}
}
}