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, Input, ElementRef } from "@angular/core";
francois.grand
committed
François Grand
committed
import { Message, MessageCode, Observer } from "jalhyd";
francois.grand
committed
import { I18nService } from "../../services/internationalisation.service";
import { NgParameter } from "../../formulaire/elements/ngparam";
import { GenericInputComponentDirective } 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 GenericInputComponentDirective implements Observer, OnDestroy {
@Input()
public captureTabEvents: boolean;
/**
* true : edit parameter initial value (in CALC mode) instead of single value
*/
public editInitValue: boolean;
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,
private element: ElementRef
) {
super(cdRef, intlService, appSetupService);
this.editInitValue = false;
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) {
if (this.editInitValue) {
this._tmp = this._paramDef.paramDefinition.initValue;
}
else {
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 {
if (this.editInitValue) {
this._paramDef.setInitValue(sender, v);
} else {
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) {
if (!this._paramDef.allowEmpty && v === undefined) {
François Grand
committed
throw new Message(MessageCode.ERROR_PARAMDEF_VALUE_UNDEFINED);
}
francois.grand
committed
return { isValid: valid, message: msg };
}
François Grand
committed
private undefineModel() {
if (this.getModelValue() !== undefined) {
this.setModelValue(this, undefined);
}
}
protected setModelValid(b: boolean) {
if (!b) {
this.undefineModel();
}
super.setModelValid(b);
}
protected setUIValid(b: boolean) {
if (!b) {
this.undefineModel();
}
super.setUIValid(b);
}
francois.grand
committed
public update(sender: any, data: any): void {
switch (data["action"]) {
case "ngparamAfterValue":
case "ngparamAfterInitValue":
francois.grand
committed
// 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":
if (this._tmp !== data["value"]) {
this._tmp = data["value"];
this.updateAndValidateUI();
this.updateModelFromUI()
}
francois.grand
committed
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
/**
* Renvoie l'événement au composant du dessus
*/
public onTabPressed(event, shift: boolean) {
this.tabPressed.emit({ originalEvent: event, shift: shift });
// stop event propagation ?
if (this.captureTabEvents) {
return false;
} // else let it bubble !
}
francois.grand
committed
public ngOnDestroy() {
François Grand
committed
// résoudre le conflit en supprimant le code ajouté cad ne conserver que removeObserver()
francois.grand
committed
this._paramDef.removeObserver(this);
}