diff --git a/src/app/components/basin-fieldset-container/basin-fieldset-container.component.ts b/src/app/components/basin-fieldset-container/basin-fieldset-container.component.ts index e70818a5be87eb4daefd957c767d23949267678c..11387594e0d3f64c81b03143a77a8a592306c78f 100644 --- a/src/app/components/basin-fieldset-container/basin-fieldset-container.component.ts +++ b/src/app/components/basin-fieldset-container/basin-fieldset-container.component.ts @@ -29,7 +29,8 @@ export class BasinFieldsetContainerComponent extends FieldsetContainerComponent } protected addSubNub(after: FieldSet, clone?: boolean): void { - this.predamService.copySelectedBasin(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); + const fsIndex = this._container.kidIndex(after); + this.predamService.copyBasinByIndex(fsIndex, ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); } public onRemoveFieldset(fs: FieldSet) { diff --git a/src/app/services/prebarrage.service.ts b/src/app/services/prebarrage.service.ts index 516c14d801694a7362cd371b32ff154a31439114..915cc1c61c22714f1f34eb07a7bd678f5ea2e203 100644 --- a/src/app/services/prebarrage.service.ts +++ b/src/app/services/prebarrage.service.ts @@ -65,18 +65,8 @@ export class PrebarrageService { * @param bi basin index */ public deleteBasinByIndex(bi: number, emptyFields: boolean) { - let nthBasin: number = 0; - let i: number = 0; - for (const kid of this._model.children) { - if (kid instanceof PbBassin) { - if (nthBasin == bi) { - this.deleteByIndex(i, emptyFields); - return; - } - nthBasin++; - } - i++; - } + const i = this.nthBasinIndex(bi); + this.deleteByIndex(i, emptyFields); } private deleteByIndex(i: number, emptyFields: boolean) { @@ -100,6 +90,26 @@ export class PrebarrageService { ); } + /** + * compute index of nth basin + * @param bi nth basin + * @returns index in child list + */ + private nthBasinIndex(bi): number { + let nth: number = 0; + let res: number = 0; + for (const kid of this._model.children) { + if (kid instanceof PbBassin) { + if (nth == bi) { + return res; + } + nth++; + } + res++; + } + return -1; + } + private copyWall(wall: PbCloison, emptyFields: boolean): PbCloison { const wallCopy = new PbCloison(wall.bassinAmont, wall.bassinAval, undefined, emptyFields); wallCopy.loadObjectRepresentation(wall.objectRepresentation()); @@ -132,7 +142,19 @@ export class PrebarrageService { } public copySelectedBasin(emptyFields: boolean) { - const basin = this._selectedNub as PbBassin; + this.copyBasin(this._selectedNub as PbBassin, emptyFields); + } + + /** + * copy nth basin + * @param bi basin index + */ + public copyBasinByIndex(bi: number, emptyFields: boolean) { + const i = this.nthBasinIndex(bi); + this.copyBasin(this._model.children[i] as PbBassin, emptyFields); + } + + private copyBasin(basin: PbBassin, emptyFields: boolean) { const basinCopy = new PbBassin(new PbBassinParams(20, 99, emptyFields)); basinCopy.loadObjectRepresentation(basin.objectRepresentation()); this._model.addChild(basinCopy);