An error occurred while loading the file. Please try again.
-
Guillaume Perréal authoredf85f0898
// cf. https://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
import { Component, ChangeDetectorRef } from "@angular/core";
import { Message, ParamDefinition, ParamDomain, ParamDomainValue, Observable } from "jalhyd";
import { I18nService } from "../../services/internationalisation/internationalisation.service";
import { GenericInputComponent } from "../generic-input/generic-input.component";
import { ServiceFactory } from "../../services/service-factory";
import { NgParameter } from "../../formulaire/ngparam";
export class NgBaseParam extends Observable {
private _param: ParamDefinition;
constructor(symb: string, d: ParamDomain | ParamDomainValue, val: number) {
super();
this._param = new ParamDefinition(null, symb, d, val);
}
public get param() {
return this._param;
}
public get symbol() {
return this._param.symbol;
}
public get isDefined() {
return this._param.isDefined;
}
public getValue() {
return this._param.getValue();
}
public checkValue(val: number) {
return this._param.checkValue(val);
}
public checkMin(min: number): boolean {
return this._param.checkMin(min);
}
public checkMax(max: number): boolean {
return this._param.checkMax(max);
}
public checkStep(step: number): boolean {
return this._param.checkStep(step);
}
public setValue(val: number) {
this._param.setValue(val);
this.notifyObservers(val);
}
public get value() {
return this.getValue();
}
public set value(val: number) {
this.setValue(val);
}
public validateModelValue(v: any): { isValid: boolean, message: string } {
let msg: string;
let valid = false;
if (v === null || v === "") {
// NULL values are always invalid
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
msg = ServiceFactory.instance.i18nService.localizeText("ERROR_PARAM_NULL");
} else {
try {
this._param.checkValue(v);
valid = true;
} catch (e) {
if (e instanceof Message) {
// ici au début le service de localisation n'a pas encore chargé ses messages…
msg = ServiceFactory.instance.i18nService.localizeMessage(e);
} else {
msg = "invalid value";
}
}
}
return { isValid: valid, message: msg };
}
}
@Component({
selector: "base-param-input",
templateUrl: "../generic-input/generic-input.component.html",
})
export class BaseParamInputComponent extends GenericInputComponent {
constructor(private intlService: I18nService, cdRef: ChangeDetectorRef) {
super(cdRef);
}
/**
* paramètre géré
*/
private get _paramDef(): NgParameter {
return this._model as 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 _tmp: number;
protected afterSetModel() {
this._tmp = this._paramDef.getValue();
}
protected getModelValue(): any {
return this._tmp;
}
protected setModelValue(sender: any, v: any) {
this._tmp = v;
try {
this._paramDef.setValue(null, v);
} catch (e) {
// géré par validateModelValue()
}
}
protected validateModelValue(v: any): { isValid: boolean, message: string } {
if (this._model instanceof NgBaseParam) {
return this._model.validateModelValue(v);
}
}
}