Newer
Older
francois.grand
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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";
@Component({
selector: "ngparam-input",
templateUrl: "../generic-input/generic-input.component.html"
})
export class NgParamInputComponent extends GenericInputComponent {
/**
* managed parameter
*/
@Input('param')
private _paramDef: NgParameter;
constructor(private intlService: InternationalisationService) {
super();
}
protected getModelValue(): any {
return this._paramDef.getValue();
}
protected setModelValue(v: any) {
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 } {
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;
}
}