param-values.component.ts 8.71 KiB
import { Component, Input, Output, EventEmitter, ViewChild, DoCheck } from "@angular/core";
import { ParamDomainValue, Pair } from "jalhyd";
import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
import { NgParameter, ParamValueMode } from "../../formulaire/ngparam";
import { GenericInputComponent } from "../generic-input/generic-input.component";
import { NgParamMinComponent } from "./ngparam-min.component";
import { NgParamMaxComponent } from "./ngparam-max.component";
import { NgParamStepComponent } from "./ngparam-step.component";
@Component({
    selector: "value-list",
    templateUrl: "../generic-input/generic-input.component.html"
export class ValueListComponent extends GenericInputComponent {
    @Output()
    private onChange = new EventEmitter<string>();
    public _list: number[];
    public isInit;
    constructor(private intlService: InternationalisationService) {
        super();
        this._list = [];
    protected getModelValue(): any {
        return this._list;
    protected setModelValue(l: any) {
        if (typeof (l) == "number") {
            this._list = [];
            this._list.push(l);
        else
            this._list = l;
        this.onChange.emit("value");
    protected validateModelValue(v: any): { isValid: boolean, message: string } {
        let msg = undefined;
        let valid = false;
        if (v instanceof Array) {
            valid = true;
            for (let e of v) {
                valid = valid && (typeof (e) == "number")
                if (!valid) {
                    msg = "La valeur n'est pas une liste de nombres"
                    break;
        return { isValid: valid, message: msg };
    protected modelToUI(v: any): string {
        let res = "";
        for (let e of v) {
            if (res != "")
                res += ";";
            res += String(e);
        return res;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
protected validateUIValue(ui: string): { isValid: boolean, message: string } { let valid: boolean = false; let msg: string = undefined; let tmp: string[] = ui.split(";"); let res = true; for (let v of tmp) { let isnum = v != "" && (+v == +v); res = res && isnum; if (!res) break; } if (!res) msg = "Veuillez entrer une liste de nombres"; else valid = true; return { isValid: valid, message: msg }; } protected uiToModel(ui: string) { let tmp: string[] = ui.split(";"); let res = []; for (let v of tmp) res.push(+v); return res; } } @Component({ selector: "param-values", templateUrl: "./param-values.component.html", styles: [ `.btn-on { color:#505050; border: 3px solid #505050; background-color:white; text-transform: uppercase; font-size: 0.8em; }`, `.btn-off { color:white; border: 3px solid #505050; background-color:#505050; text-transform: uppercase; font-size: 0.8em; }` ] }) export class ParamValuesComponent implements DoCheck { @Input("param") private _param: NgParameter; /** * true si liste de valeur, false si min/max/pas */ private _list: boolean; private _valueModes = []; /** * composant de saisie du minimum */ @ViewChild(NgParamMinComponent) private _minComponent: NgParamMinComponent; /** * composant de saisie du maximum
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
*/ @ViewChild(NgParamMaxComponent) private _maxComponent: NgParamMaxComponent; /** * composant de saisie du pas de variation */ @ViewChild(NgParamStepComponent) private _stepComponent: NgParamStepComponent; /** * composant de saisie d'une liste de valeurs */ @ViewChild(ValueListComponent) private _listComponent: ValueListComponent; constructor(private intlService: InternationalisationService) { this._valueModes.push({ "value": ParamValueMode.MINMAX, "label": "Min/max" }); this._valueModes.push({ "value": ParamValueMode.LISTE, "label": "Liste" }); } private getDefaultMin(): number { switch (this._param.domain.domain) { case ParamDomainValue.ANY: case ParamDomainValue.NOT_NULL: return -10; default: return this._param.domain.minValue; } } private getDefaultMax(): number { switch (this._param.domain.domain) { case ParamDomainValue.INTERVAL: return this._param.domain.maxValue; default: return 10; } } private initMinMaxStep() { // valeur pour min (celle déjà définie ou celle déduite du domaine de définition) let min: number = this._param.minValue; let ok = min != undefined if (ok) { try { // on la vérifie this._param.checkValue(min); ok = true; } catch (e) { ok = false; } } if (!ok) min = this.getDefaultMin(); // valeur pour max (celle déjà définie ou celle déduite du domaine de définition) let max: number = this._param.maxValue; ok = max != undefined if (ok) { try { // on la vérifie this._param.checkValue(max); ok = true; } catch (e) { ok = false;
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
} } if (!ok) max = this.getDefaultMax(); this._minComponent.refValue = new Pair(this._param.domain.minValue, max); this._minComponent.model = this._param.getValue() / 2; this._maxComponent.refValue = new Pair(min, this._param.domain.maxValue); this._maxComponent.model = this._param.getValue() * 2; this.updateStepComponentRef(); this._stepComponent.model = (this._maxComponent.model - this._minComponent.model) / 20; } private onMinChanged(val: string) { this.updateStepComponentRef(); this._maxComponent.refValue = new Pair(this._minComponent.model, this._param.domain.maxValue); this._maxComponent.validateModel(); if (this._minComponent.validateModel()) this._param.minValue = this._minComponent.model; } private onMaxChanged(val: string) { this.updateStepComponentRef(); this._minComponent.refValue = new Pair(this._param.domain.minValue, this._maxComponent.model); this._minComponent.validateModel(); if (this._maxComponent.validateModel()) this._param.maxValue = this._maxComponent.model; } private onStepChanged(val: string) { if (this._stepComponent.validateModel()) this._param.stepValue = this._stepComponent.model; } private onListChanged(val: string) { if (this._listComponent.validateModel()) this._param.valueList = this._listComponent.model; } public ngDoCheck() { // initialisation des champs min/max/step à l'apparition de ces contrôles if (this._minComponent != undefined && !this._minComponent.isInit) { this._minComponent.isInit = true; this.initMinMaxStep(); } if (this._listComponent != undefined && !this._listComponent.isInit) { this._listComponent.isInit = true; this._listComponent.model = []; if (this._param.isDefined) this._listComponent.model.push(this._param.getValue()); } } /** * met à jour la valeur de référence du composant gérant le pas de variation */ private updateStepComponentRef() { this._stepComponent.refValue = new Pair(1e-9, this._maxComponent.model - this._minComponent.model); this._stepComponent.validateModel(); }
281282283284285286287288289290291292293294295296297298299300301302303304305306307308
private get uitextValeurMini() { return this.intlService.localizeText("INFO_PARAMFIELD_VALEURMINI"); } private get uitextValeurMaxi() { return this.intlService.localizeText("INFO_PARAMFIELD_VALEURMAXI"); } private get uitextPasVariation() { return this.intlService.localizeText("INFO_PARAMFIELD_PASVARIATION"); } private get isList(): boolean { return this._param.valueMode == ParamValueMode.LISTE; } /** * valeur courante affichée dans le select */ private get currentLabel(): string { return ParamValueMode[this._param.valueMode]; } private onSelectValueMode(event: any) { this._param.valueMode = event.target.value; } }