Newer
Older
francois.grand
committed
// cf. https://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
import { Component, ChangeDetectorRef, OnDestroy } from "@angular/core";
francois.grand
committed
import { Message, Observer } from "jalhyd";
francois.grand
committed
import { I18nService } from "../../services/internationalisation.service";
import { NgParameter } from "../../formulaire/elements/ngparam";
francois.grand
committed
import { GenericInputComponent } from "../generic-input/generic-input.component";
import { ApplicationSetupService } from "../../services/app-setup.service";
francois.grand
committed
@Component({
selector: "ngparam-input",
templateUrl: "../generic-input/generic-input.component.html",
styleUrls: [
"./ngparam-input.component.scss"
]
francois.grand
committed
})
export class NgParamInputComponent extends GenericInputComponent implements Observer, OnDestroy {
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(
intlService: I18nService,
appSetupService: ApplicationSetupService,
cdRef: ChangeDetectorRef
) {
super(cdRef, intlService, appSetupService);
francois.grand
committed
}
* appelé avant le changement de modèle
*/
francois.grand
committed
protected beforeSetModel() {
if (this._paramDef) {
francois.grand
committed
this._paramDef.removeObserver(this);
francois.grand
committed
}
/**
* appelé après le changement de modèle
*/
protected afterSetModel() {
if (this._paramDef) {
mathias.chouet
committed
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
// géré par validateModelValue()
}
}
protected validateModelValue(v: any): { isValid: boolean, message: string } {
let msg: string;
francois.grand
committed
let valid = false;
if (! this._paramDef) {
francois.grand
committed
return { isValid: valid, message: msg };
}
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();
}
francois.grand
committed
break;
// changement de valueMode du paramètre ou de valeur à laquelle il est lié
case "valueModeChange":
case "valueLinkChange":
francois.grand
committed
if (this._tmp !== data["value"]) {
this._tmp = data["value"];
this.updateAndValidateUI();
}
francois.grand
committed
break;
francois.grand
committed
}
}
francois.grand
committed
public ngOnDestroy() {
this._paramDef.removeObserver(this);
}