An error occurred while loading the file. Please try again.
-
Le Roux Erwan authored431b97f2
import { Component, Input, Output, EventEmitter, ViewChildren, QueryList, DoCheck } from "@angular/core";
import { ParamRadioConfig } from "../../formulaire/ngparam";
import { FieldSet } from "../../formulaire/fieldset";
import { ParamFieldLineComponent } from "../param-field-line/param-field-line.component";
import { Field } from "../../formulaire/field";
import { InputField } from "../../formulaire/input-field";
import { SelectField } from "../../formulaire/select-field";
import { SelectFieldModel } from "../../formulaire/select-field-model";
import { FormulairePab } from "../../formulaire/definition/concrete/form-pab";
import { SelectModelFieldLineComponent } from "../select-model-field-line/select-model-field-line.component";
import { FieldsetContainer } from "../../formulaire/fieldset-container";
import { NotificationsService } from "../../services/notifications/notifications.service";
import { I18nService } from "../../services/internationalisation/internationalisation.service";
import { sprintf } from "sprintf-js";
@Component({
selector: "field-set",
templateUrl: "./field-set.component.html",
styleUrls: ["./field-set.component.scss"]
})
export class FieldSetComponent implements DoCheck {
/** number of children to add when clicking "add" or "clone" button */
public childrenToAdd = 1;
@Input()
private set fieldSet(fs: FieldSet) {
this._fieldSet = fs;
}
public get fields() {
return this._fieldSet.kids;
}
public get addManyOptionsList() {
return Array(20).fill(0).map((value, index) => index + 1);
}
public get title(): string {
if (! this._fieldSet) {
return "fs undefined";
}
if (! this._fieldSet.label) {
return "label undefined";
}
return this._fieldSet.label;
}
public get isValid() {
return this._isValid;
}
/** flag d'affichage des boutons ajouter, supprimer, monter, descendre */
public get showButtons() {
return (this._fieldSet.parent instanceof FieldsetContainer);
}
/** flag d'activation du bouton monter */
public get enableUpButton() {
if (this._fieldSet.parent instanceof FieldsetContainer) {
return this._fieldSet.parent.getFieldsetPosition(this._fieldSet) !== 0;
}
return false;
}
/** flag d'activation du bouton descendre */
public get enableDownButton() {
if (this._fieldSet.parent instanceof FieldsetContainer) {
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
return this._fieldSet.parent.getFieldsetPosition(this._fieldSet) < this._fieldSet.parent.fieldsets.length - 1;
}
return false;
}
/** flag d'activation du bouton supprimer */
public get enableRemoveButton() {
if (this._fieldSet.parent instanceof FieldsetContainer) {
return this._fieldSet.parent.fieldsets.length > 0;
}
return false;
}
/**
* field set attribute
*/
private _fieldSet: FieldSet;
@ViewChildren(ParamFieldLineComponent)
private _paramComponents: QueryList<ParamFieldLineComponent>;
@ViewChildren(SelectModelFieldLineComponent)
private _selectModelComponents: QueryList<SelectModelFieldLineComponent>;
/**
* événement de changement de validité
*/
@Output()
private validChange = new EventEmitter();
/**
* événement de changement de valeur d'un input
*/
@Output()
private inputChange = new EventEmitter();
/**
* événement de demande d'ajout d'un fieldset (FieldSet dans un FieldsetContainer)
*/
@Output()
private addFieldset = new EventEmitter();
/**
* événement de demande de remontée d'un fieldset dans la liste (FieldSet dans un FieldsetContainer)
*/
@Output()
private moveFieldsetUp = new EventEmitter();
/**
* événement de demande de descente d'un fieldset dans la liste (FieldSet dans un FieldsetContainer)
*/
@Output()
private moveFieldsetDown = new EventEmitter();
/**
* événement de demande de suppression d'un fieldset (FieldSet dans un FieldsetContainer)
*/
@Output()
private removeFieldset = new EventEmitter();
/**
* flag de validité de la saisie
*/
private _isValid = false;
/**
* événement de changement d'état d'un radio
*/
// tslint:disable-next-line:no-output-on-prefix
@Output()
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
private radio = new EventEmitter<any>();
/** événement signalant un appui sur TAB ou SHIFT+TAB */
@Output()
protected tabPressed = new EventEmitter<any>();
public constructor(
private notifService: NotificationsService,
private i18nService: I18nService
) { }
private hasRadioFix(): boolean {
if (this._fieldSet.hasInputs) {
switch (this._fieldSet.getInput(0).radioConfig) {
case ParamRadioConfig.FIX:
return false;
default:
return true;
}
}
return false;
}
private hasRadioVar(): boolean {
if (this._fieldSet.hasInputs) {
switch (this._fieldSet.getInput(0).radioConfig) {
case ParamRadioConfig.VAR:
case ParamRadioConfig.CAL:
return true;
default:
return false;
}
}
return false;
}
private hasRadioCal(): boolean {
if (this._fieldSet.hasInputs) {
switch (this._fieldSet.getInput(0).radioConfig) {
case ParamRadioConfig.CAL:
return true;
default:
return false;
}
}
return false;
}
/**
* détermine si un Field est du type InputField
*/
private isInputField(f: Field): boolean {
return f instanceof InputField && f.isDisplayed;
}
/**
* détermine si un Field est du type SelectField
*/
private isSelectField(f: Field): boolean {
return (
f instanceof SelectField
&& ! (f.parentForm instanceof FormulairePab)
&& f.isDisplayed
);
}
/**
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
* détermine si un Field est du type SelectFieldModel
*/
private isSelectModelField(f: Field): boolean {
return (
f instanceof SelectFieldModel
&& f.parentForm instanceof FormulairePab
&& (
f.id === (f.parentForm as FormulairePab).modeleCloisonsSelectId
|| f.id === (f.parentForm as FormulairePab).modeleCloisonAvalSelectId
)
);
}
/*
* gestion des événements clic sur les radios :
* réception d'un message du composant enfant (param-field)
* cf. https://angular.io/guide/component-interaction#parent-listens-for-child-event
*/
private onRadioClick(info: any) {
// on renvoie l'info au parent
this.radio.emit(info);
}
/**
* calcul de la validité de tous les ParamFieldLineComponent et tous les
* SelectModelFieldLineComponent de la vue
*/
private updateValidity() {
this._isValid = false;
let paramsAreValid = true;
let selectAreValid = true;
if (this._paramComponents) {
paramsAreValid = false;
paramsAreValid = this._paramComponents.reduce(
// callback
(
// accumulator (valeur précédente du résultat)
acc,
// currentValue (élément courant dans le tableau)
param,
// currentIndex (indice courant dans le tableau)
currIndex,
// array (tableau parcouru)
array
) => {
return acc && param.isValid;
}
// valeur initiale
, true);
}
if (this._selectModelComponents) {
selectAreValid = false;
selectAreValid = this._selectModelComponents.reduce(
// callback
(
// accumulator (valeur précédente du résultat)
acc,
// currentValue (élément courant dans le tableau)
select,
// currentIndex (indice courant dans le tableau)
currIndex,
// array (tableau parcouru)
array
) => {
return acc && select.isValid;
}
// valeur initiale
, true);
}
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
// global validity
this._isValid = (paramsAreValid && selectAreValid);
this.validChange.emit();
}
/**
* Renvoie l'événement au composant du dessus
*/
public onTabPressed(event) {
this.tabPressed.emit(event);
}
public ngDoCheck() {
this.updateValidity();
}
/**
* réception d'un événement de validité de ParamFieldLineComponent
*/
private onParamLineValid(event: boolean) {
this.updateValidity();
}
/**
* réception d'un événement de changement de valeur d'un input
*/
private onInputChange(event: boolean) {
this.inputChange.emit();
}
/**
* relit les valeurs dans l'interface et met à jour les NgParameter
*/
public updateParametersFromUI() {
this._paramComponents.forEach(fsc => fsc.updateParameterFromUI());
}
/**
* met à jour les paramètres liés
*/
public updateLinkedParameters() {
this._paramComponents.forEach(fsc => fsc.updateLinkedParameter());
}
/**
* clic sur le bouton ajouter
*/
private onAddClick() {
for (let i = 0; i < this.childrenToAdd; i++) {
this.addFieldset.emit({
fs: this._fieldSet,
clone: false
});
}
let msg: string;
if (this.childrenToAdd === 1) {
msg = this.i18nService.localizeText("INFO_FSC_FS_ADDED");
} else {
msg = sprintf(this.i18nService.localizeText("INFO_FSC_FS_ADDED_N_TIMES"), this.childrenToAdd);
}
this.notifService.notify(msg);
this.childrenToAdd = 1; // reinit to avoid confusion
}
/**
* clic sur le bouton copier
*/
private onCopyClick() {
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
for (let i = 0; i < this.childrenToAdd; i++) {
this.addFieldset.emit({
fs: this._fieldSet,
clone: true
});
}
const pos = (this._fieldSet.parent as FieldsetContainer).getFieldsetPosition(this._fieldSet) + 1;
let msg: string;
if (this.childrenToAdd === 1) {
msg = sprintf(this.i18nService.localizeText("INFO_FSC_FS_COPIED"), pos);
} else {
msg = sprintf(this.i18nService.localizeText("INFO_FSC_FS_COPIED_N_TIMES"), pos, this.childrenToAdd);
}
this.notifService.notify(msg);
this.childrenToAdd = 1; // reinit to avoid confusion
}
/**
* clic sur le bouton supprimer
*/
private onRemoveClick() {
const pos = (this._fieldSet.parent as FieldsetContainer).getFieldsetPosition(this._fieldSet) + 1;
this.removeFieldset.emit(this._fieldSet);
this.notifService.notify(
sprintf(this.i18nService.localizeText("INFO_FSC_FS_REMOVED"), pos)
);
}
/**
* clic sur le bouton monter
*/
private onMoveUpClick() {
const pos = (this._fieldSet.parent as FieldsetContainer).getFieldsetPosition(this._fieldSet) + 1;
this.moveFieldsetUp.emit(this._fieldSet);
this.notifService.notify(
sprintf(this.i18nService.localizeText("INFO_FSC_FS_MOVED"), pos)
);
}
/**
* clic sur le bouton descendre
*/
private onMoveDownClick() {
const pos = (this._fieldSet.parent as FieldsetContainer).getFieldsetPosition(this._fieldSet) + 1;
this.moveFieldsetDown.emit(this._fieldSet);
this.notifService.notify(
sprintf(this.i18nService.localizeText("INFO_FSC_FS_MOVED"), pos)
);
}
}