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 82a8e845a96b40e4a650ae514dc9f52b003ad7f7..dd8b8f85450295ec6552bb7be8c74d14b8a6e709 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 @@ -3,6 +3,9 @@ import { Component } from "@angular/core"; import { I18nService } from "../../services/internationalisation.service"; import { ApplicationSetupService } from "../../services/app-setup.service"; import { FieldsetContainerComponent } from "../fieldset-container/fieldset-container.component"; +import { PrebarrageService } from "app/services/prebarrage.service"; +import { ServiceFactory } from "app/services/service-factory"; +import { FieldSet } from "app/formulaire/elements/fieldset"; @Component({ selector: "basin-fieldset-container", @@ -13,7 +16,7 @@ import { FieldsetContainerComponent } from "../fieldset-container/fieldset-conta }) export class BasinFieldsetContainerComponent extends FieldsetContainerComponent { - constructor(i18nService: I18nService, appSetupService: ApplicationSetupService) { + constructor(i18nService: I18nService, appSetupService: ApplicationSetupService, private predamService: PrebarrageService) { super(i18nService, appSetupService); } @@ -24,4 +27,8 @@ export class BasinFieldsetContainerComponent extends FieldsetContainerComponent fs.showMoveArrows = false; }); } + + protected addSubNub(after: FieldSet, clone?: boolean): void { + this.predamService.copySelectedBasin(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); + } } diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts index 2cf618100ec5a19b326b1568f0110291e8b13ab5..c258b98783f353aae60b26edbc0ecae2ed2df155 100644 --- a/src/app/components/pb-schema/pb-schema.component.ts +++ b/src/app/components/pb-schema/pb-schema.component.ts @@ -21,7 +21,7 @@ import { AppComponent } from "../../app.component"; import { fv } from "app/util"; import { ServiceFactory } from "app/services/service-factory"; import { DefinedBoolean } from "app/definedvalue/definedboolean"; -import { PrebarrageService } from "app/services/prebarrage.service"; +import { PrebarrageService, PrebarrageServiceEvents } from "app/services/prebarrage.service"; /** * The interactive schema for calculator type "PreBarrage" (component) @@ -70,6 +70,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni ) { this.hotkeysService.add(new Hotkey("del", AppComponent.onHotkey(this.removeOnHotkey, this))); this._isValid = new DefinedBoolean(); + this.predamService.changeEventEmitter.subscribe(event => this.onPredamServiceEvent(event)); } /** tracks the fullscreen state */ @@ -482,16 +483,12 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni /** Copies a wall or a basin */ public onCopyClick() { - let copy: PbCloison | PbBassin; if (this._selectedItem instanceof PbCloison) { - copy = this.predamService.copySelectedWall(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); + this.predamService.copySelectedWall(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); } else { - copy = this.predamService.copySelectedBasin(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); + this.predamService.copySelectedBasin(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); } - this.clearResults(); - this.refreshWithSelection(copy.uid); - this.calculatorComponent.showPBInputData = true; } public get uitextCopy() { @@ -500,10 +497,22 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni /** Adds a new lone basin */ public onAddBasinClick() { - const newBasin = this.predamService.addBasin(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); - this.clearResults(); - this.refreshWithSelection(newBasin.uid); - this.calculatorComponent.showPBInputData = true; + this.predamService.addBasin(ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); + } + + /** + * process events from PrebarrageService + */ + private onPredamServiceEvent(event: any) { + const nub = event.data; + switch (event.id) { + case PrebarrageServiceEvents.BASIN_WALL_ADDED: + case PrebarrageServiceEvents.BASIN_WALL_COPIED: + this.clearResults(); + this.refreshWithSelection(nub.uid); + this.calculatorComponent.showPBInputData = true; + break; + } } public get uitextAddBasin() { @@ -529,10 +538,7 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni // apply modifications dialogRef.afterClosed().subscribe(result => { if (result.up !== undefined && result.down !== undefined) { - const wall = this.predamService.addWall(result.up, result.down, ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); - this.clearResults(); - this.refreshWithSelection(wall.uid); - this.calculatorComponent.showPBInputData = true; + this.predamService.addWall(result.up, result.down, ServiceFactory.applicationSetupService.enableEmptyFieldsOnFormInit); } }); } diff --git a/src/app/services/prebarrage.service.ts b/src/app/services/prebarrage.service.ts index 41672a034bfd851329911e9af71f26cf492cc79e..da22ff2f995fb22ee25512c6276ef02b615e3d4b 100644 --- a/src/app/services/prebarrage.service.ts +++ b/src/app/services/prebarrage.service.ts @@ -1,6 +1,10 @@ -import { Injectable } from "@angular/core"; +import { EventEmitter, Injectable } from "@angular/core"; import { MermaidUtil, PbBassin, PbBassinParams, PbCloison, PreBarrage } from "jalhyd"; +export enum PrebarrageServiceEvents { + BASIN_WALL_ADDED, // a wall has been added + BASIN_WALL_COPIED, // a basin or wall has been copied +} /** * service relatif au schéma de prébarrage */ @@ -16,6 +20,8 @@ export class PrebarrageService { /** Latest clicked item: a PbCloison, a PbBassin or undefined if river "Upstream" or "Downstream" was clicked */ private _selectedNub: PbCloison | PbBassin; + private _changeEventEmitter = new EventEmitter(); + public get model(): PreBarrage { return this._model; } @@ -24,6 +30,10 @@ export class PrebarrageService { this._model = pb; } + public get changeEventEmitter() { + return this._changeEventEmitter; + } + /** * return upstream (and downstream) basin object */ @@ -66,26 +76,41 @@ export class PrebarrageService { return wallCopy; } - public copySelectedWall(emptyFields: boolean): PbCloison { + public copySelectedWall(emptyFields: boolean) { const wall = this._selectedNub as PbCloison; const wallCopy = new PbCloison(wall.bassinAmont, wall.bassinAval, undefined, emptyFields); wallCopy.loadObjectRepresentation(wall.objectRepresentation()); this._model.addChild(wallCopy); - return wallCopy; + this._changeEventEmitter.emit( + { + id: PrebarrageServiceEvents.BASIN_WALL_COPIED, + data: wallCopy + } + ); } - public addBasin(emptyFields: boolean): PbBassin { + public addBasin(emptyFields: boolean) { const newBasin = new PbBassin(new PbBassinParams(20, 99, emptyFields)); this._model.addChild(newBasin); - return newBasin; + this._changeEventEmitter.emit( + { + id: PrebarrageServiceEvents.BASIN_WALL_ADDED, + data: newBasin + } + ); } - public copySelectedBasin(emptyFields: boolean): PbBassin { + public copySelectedBasin(emptyFields: boolean) { const basin = this._selectedNub as PbBassin; const basinCopy = new PbBassin(new PbBassinParams(20, 99, emptyFields)); basinCopy.loadObjectRepresentation(basin.objectRepresentation()); this._model.addChild(basinCopy); - return basinCopy; + this._changeEventEmitter.emit( + { + id: PrebarrageServiceEvents.BASIN_WALL_COPIED, + data: basinCopy + } + ); } public get hasBasins(): boolean { @@ -96,7 +121,7 @@ export class PrebarrageService { return this._model.bassins; } - public addWall(upstreamIndex: number, downstreamIndex: number, emptyFields: boolean): PbCloison { + public addWall(upstreamIndex: number, downstreamIndex: number, emptyFields: boolean) { const wall = new PbCloison( upstreamIndex === 0 ? undefined : this._model.bassins[upstreamIndex - 1], downstreamIndex === 0 ? undefined : this._model.bassins[downstreamIndex - 1], @@ -104,7 +129,12 @@ export class PrebarrageService { emptyFields ); this._model.addChild(wall); - return wall; + this._changeEventEmitter.emit( + { + id: PrebarrageServiceEvents.BASIN_WALL_ADDED, + data: wall + } + ); } /**