param-values.component.ts 9.47 KiB
import { Component, Input, Output, EventEmitter, ViewChild, AfterViewChecked } from "@angular/core";
import { ParamValueMode } from "jalhyd";
import { InternationalisationService } from "../../services/internationalisation/internationalisation.service";
import { NgParameter } from "../../formulaire/ngparam";
import { NgParamMinComponent } from "./ngparam-min.component";
import { NgParamMaxComponent } from "./ngparam-max.component";
import { NgParamStepComponent } from "./ngparam-step.component";
import { BaseComponent } from "../base/base.component";
import { ValueListComponent } from "./value-list.component";
@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 extends BaseComponent implements AfterViewChecked {
    @Input("param")
    private _param: NgParameter;
    private _valueModes = [];
    /**
     * true quand les champs du composant et de ses enfants sont initialisés
    private _initCompleted = false;
    /**
     * true si la valeur min est valide
    private _validMin: boolean = false;
    /**
     * true si la valeur max est valide
    private _validMax: boolean = false;
    /**
     * true si la valeur du pas est valide
    private _validStep: boolean = false;
    /**
     * true si la liste de valeurs est valide
    private _validList: boolean = false;
    /**
     * flag signalant qu'il faut initialiser le composant ValueListComponent (par ex quand on a sélectionné le mode "liste")
    private _doInitList: boolean = false;
    /**
     * flag signalant qu'il faut initialiser les composants min/max/pas
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
*/ private _doInitMinmax: boolean = false; /** * composant de saisie du minimum */ @ViewChild(NgParamMinComponent) private _minComponent: NgParamMinComponent; /** * composant de saisie du maximum */ @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; @Output() private onValid: EventEmitter<boolean>; constructor(private intlService: InternationalisationService) { super(); this._valueModes.push({ "value": ParamValueMode.MINMAX, "label": "Min/max" }); this._valueModes.push({ "value": ParamValueMode.LISTE, "label": "Liste" }); this.onValid = new EventEmitter(); } /** * init des champs min/max/pas */ private initMinMaxStep() { if (this.isMinMax && this._doInitMinmax) { this._doInitMinmax = false; // valeur pour min : celle déjà définie ou celle déduite de la valeur saisie let min: number = this._param.minValue; if (min == undefined) min = this._param.getValue() / 2; // valeur pour max : celle déjà définie ou celle déduite de la valeur saisie let max: number = this._param.maxValue; if (max == undefined) max = this._param.getValue() * 2; this._param.minValue = min; this._minComponent.model = this._param; this._param.maxValue = max; this._maxComponent.model = this._param; // valeur du pas let step = this._param.stepValue; if (step == undefined) step = (max - min) / 20; this._param.stepValue = step; this._stepComponent.model = this._param; this.validateAll(); this._validMin = this._minComponent.isValid;
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
this._validMax = this._maxComponent.isValid; this._validStep = this._stepComponent.isValid; this.emitValidity(); this._initCompleted = true; } } /** * initialisation de la liste de valeurs avec celle du paramètre géré */ private initList() { if (this._doInitList && this._listComponent != undefined) { this._doInitList = false; let l = this._param.valueList; if (l == undefined) { if (this._param.isDefined) l = [this._param.getValue()]; else l = []; } this._param.valueList = l; this._listComponent.model = this._param; } } /** * revalidation de tous les composants enfants */ private validateAll() { if (this._minComponent != undefined) this._minComponent.validate(); if (this._maxComponent != undefined) this._maxComponent.validate(); if (this._stepComponent != undefined) this._stepComponent.validate(); if (this._listComponent != undefined) this._listComponent.validate(); } /** * envoi d'un événement de validité */ private emitValidity() { switch (this._param.valueMode) { case ParamValueMode.LISTE: this.onValid.emit(this._validList); break; case ParamValueMode.MINMAX: this.onValid.emit(this._validMin && this._validMax && this._validStep); break; } } /** * réception d'un événement de NgParamMinComponent */ private onMinChanged(event: any) { if (this._initCompleted) switch (event.action) { case "model": this.validateAll(); break; case "valid": this._validMin = event.value; this.emitValidity(); break;
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
} } /** * réception d'un événement de NgParamMaxComponent */ private onMaxChanged(event: any) { if (this._initCompleted) switch (event.action) { case "model": this.validateAll(); break; case "valid": this._validMax = event.value; this.emitValidity(); break; } } /** * réception d'un événement de NgParamStepComponent */ private onStepChanged(event: any) { if (this._initCompleted) switch (event.action) { case "model": this.validateAll(); break; case "valid": this._validStep = event.value; this.emitValidity(); break; } } /** * réception d'un événement de ValueListComponent */ private onListChanged(event: any) { if (this._initCompleted) switch (event.action) { case "model": this.validateAll(); break; case "valid": this._validList = event.value; this.emitValidity(); break; } } 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"); } /** * true si mode "liste de valeurs" */ private get isList(): boolean {
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
return this._param.valueMode == ParamValueMode.LISTE; } /** * true si mode "lié" */ private get isLink(): boolean { return this._param.valueMode == ParamValueMode.LINK; } /** * true si mode "min/max/pas" */ private get isMinMax(): boolean { return this._param.valueMode == ParamValueMode.MINMAX; } /** * valeur courante affichée dans le select min-max/list */ private get currentModeSelectLabel(): string { return ParamValueMode[this._param.valueMode]; } /** * réception d'un événement du menu "min/max/liste" */ private onSelectValueMode(event: any) { const next = event.target.value; switch (next) { // on a sélectionné "min/max" ? case ParamValueMode.MINMAX: this._doInitMinmax = true; break; // on a sélectionné "liste" ? case ParamValueMode.LISTE: this._doInitList = true; break; default: throw "valeur " + next + " de ParamValueMode non prise en charge"; } this._param.valueMode = next; } /** * appelé quand les @Input changent */ ngOnChanges() { if (this.isMinMax) this._doInitMinmax = true; else this._doInitList = true; } ngAfterViewChecked() { super.ngAfterViewChecked(); this.initMinMaxStep(); this.initList(); } }