From 296c6113a3c8badd102a5ad49a573a2c54bedbe5 Mon Sep 17 00:00:00 2001 From: "francois.grand" <francois.grand@irstea.fr> Date: Thu, 26 Apr 2018 17:05:40 +0200 Subject: [PATCH] =?UTF-8?q?=20#78=20ouvrages=20parall=C3=A8les=20:=20le=20?= =?UTF-8?q?bouton=20ajouter=20ins=C3=A8re=20un=20ouvrage=20juste=20apr?= =?UTF-8?q?=C3=A8s=20celui=20dont=20le=20bouton=20ajouter=20a=20=C3=A9t?= =?UTF-8?q?=C3=A9=20cliqu=C3=A9,=20pas=20=C3=A0=20la=20fin=20de=20la=20lis?= =?UTF-8?q?te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../field-set/field-set.component.ts | 2 +- .../fieldset-container.component.html | 2 +- .../fieldset-container.component.ts | 22 ++++++++++-------- .../concrete/form-parallel-structures.ts | 23 +++++++++++++++---- src/app/formulaire/fieldset-container.ts | 13 +++++++---- src/app/formulaire/fieldset-template.ts | 9 ++++++-- 6 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/app/components/field-set/field-set.component.ts b/src/app/components/field-set/field-set.component.ts index ebb0c3d6d..9674b3f3f 100644 --- a/src/app/components/field-set/field-set.component.ts +++ b/src/app/components/field-set/field-set.component.ts @@ -231,7 +231,7 @@ export class FieldSetComponent implements DoCheck { * clic sur le bouton ajouter */ private onAddClick() { - this.addFieldset.emit(); + this.addFieldset.emit(this._fieldSet); } /** diff --git a/src/app/components/fieldset-container/fieldset-container.component.html b/src/app/components/fieldset-container/fieldset-container.component.html index 9dfad64d4..a1e13d476 100644 --- a/src/app/components/fieldset-container/fieldset-container.component.html +++ b/src/app/components/fieldset-container/fieldset-container.component.html @@ -4,6 +4,6 @@ </div> <field-set *ngFor="let fs of fieldsets" [fieldSet]=fs (onRadio)=onRadioClick($event) (onValid)=onFieldsetValid() (inputChange)=onInputChange() - (addFieldset)=onAddFieldset() (removeFieldset)=onRemoveFieldset($event)> + (addFieldset)=onAddFieldset($event) (removeFieldset)=onRemoveFieldset($event)> </field-set> </div> \ No newline at end of file diff --git a/src/app/components/fieldset-container/fieldset-container.component.ts b/src/app/components/fieldset-container/fieldset-container.component.ts index a323c28eb..20f7ad5c1 100644 --- a/src/app/components/fieldset-container/fieldset-container.component.ts +++ b/src/app/components/fieldset-container/fieldset-container.component.ts @@ -34,24 +34,28 @@ export class FieldsetContainerComponent implements DoCheck, AfterViewInit { return this._container.fieldsets; } - private addStructure() { - this._container.addFromTemplate(0); + private addStructure(after?: FieldSet) { + if (after) + this._container.addFromTemplate(0, after.indexAsKid()); + else + this._container.addFromTemplate(0); } - private onFielsetListChange(val: any) { + private onFielsetListChange() { // affichage des boutons ajouter, supprimer, monter, descendre - const fs = val.last as FieldSetComponent; - fs.showButtons = true; + this._fieldsetComponents.forEach(fs => fs.showButtons = true); // désactivation du bouton supprimer s'il n'en reste qu'un - if (this._fieldsetComponents.length == 1) + if (this._fieldsetComponents.length == 1) { + const fs = this._fieldsetComponents.last as FieldSetComponent; fs.enableRemoveButton = false; + } else this._fieldsetComponents.forEach(fs => fs.enableRemoveButton = true); } public ngAfterViewInit() { - this._fieldsetComponents.changes.subscribe(value => this.onFielsetListChange(value)); + this._fieldsetComponents.changes.subscribe(_ => this.onFielsetListChange()); this.addStructure(); } @@ -142,8 +146,8 @@ export class FieldsetContainerComponent implements DoCheck, AfterViewInit { /** * réception d'un événement de demande d'ajout d'un FieldSet */ - private onAddFieldset() { - this.addStructure(); + private onAddFieldset(fs: FieldSet) { + this.addStructure(fs); } /** diff --git a/src/app/formulaire/definition/concrete/form-parallel-structures.ts b/src/app/formulaire/definition/concrete/form-parallel-structures.ts index bfd3de2c3..4b4165fe4 100644 --- a/src/app/formulaire/definition/concrete/form-parallel-structures.ts +++ b/src/app/formulaire/definition/concrete/form-parallel-structures.ts @@ -52,8 +52,13 @@ export class FormulaireParallelStructure extends FormulaireDefinition { return paramService.createSessionNub(params); } - private addStructureNub(st: Structure) { - this.parallelStructureNub.addStructure(st); + /** + * ajoute un Nub Structure + * @param st structure à ajouter + * @param after position après laquelle insérer la structure, à la fin sinon + */ + private addStructureNub(st: Structure, after?: number) { + this.parallelStructureNub.addStructure(st, after); } private get parallelStructureNub(): ParallelStructure { @@ -66,15 +71,23 @@ export class FormulaireParallelStructure extends FormulaireDefinition { public createFieldset(parent: FormulaireNode, json: {}, data?: {}): FieldSet { if (json["calcType"] === "Structure") { + // indice après lequel insérer le nouveau FieldSet + const after = data["after"]; + const res: FieldSet = new FieldSet(parent); const sn = this.createStructNub(data["template"]); - this.addStructureNub(sn.nub as Structure); + this.addStructureNub(sn.nub as Structure, after); res.setSessionNub(sn, false); - parent.kids.push(res); + + if (after !== undefined) + parent.kids.splice(after + 1, 0, res); + else + parent.kids.push(res); + return res; } else - return super.createFieldset(parent, json); + return super.createFieldset(parent, json, data); } protected initParse() { diff --git a/src/app/formulaire/fieldset-container.ts b/src/app/formulaire/fieldset-container.ts index 27ac22d03..5f2ecf308 100644 --- a/src/app/formulaire/fieldset-container.ts +++ b/src/app/formulaire/fieldset-container.ts @@ -37,10 +37,15 @@ export class FieldsetContainer extends FormulaireElement { this.kids.splice(i, 1); } - public addFromTemplate(index: number) { - const templ: FieldsetTemplate = this._templates[index]; - - const inst: FieldSet = templ.instantiateTemplate(this); + /** + * crée un FieldSet à partir d'un template + * @param templateIndex indice du template dans la liste + * @param after insère le nouveau FieldSet après cette position, à la fin sinon + */ + public addFromTemplate(templateIndex: number, after?: number) { + const templ: FieldsetTemplate = this._templates[templateIndex]; + + const inst: FieldSet = templ.instantiateTemplate(this, after); this.updateLocalisation(); diff --git a/src/app/formulaire/fieldset-template.ts b/src/app/formulaire/fieldset-template.ts index c21455761..a5d618349 100644 --- a/src/app/formulaire/fieldset-template.ts +++ b/src/app/formulaire/fieldset-template.ts @@ -38,9 +38,14 @@ export class FieldsetTemplate { return nodeType; } - public instantiateTemplate(cont: FieldsetContainer): FieldSet { + /** + * crée une instance de Fieldset et l'ajoute dans un conteneur + * @param cont conteneur + * @param at position à laquelle on ajoute le nouveau FieldSet + */ + public instantiateTemplate(cont: FieldsetContainer, after: number): FieldSet { const parentForm = cont.parent as FormulaireDefinition; - const res = parentForm.createFieldset(cont, this._jsonConfig, { "template": this }); + const res = parentForm.createFieldset(cont, this._jsonConfig, { "template": this, "after": after }); res.parseConfig(this._jsonConfig, { "parentForm": parentForm }); parentForm.afterParseFieldset(res); return res; -- GitLab