From c57152a15377aeb32c612dbd2f56814d439d22ef Mon Sep 17 00:00:00 2001 From: Mathias Chouet <mathias.chouet@irstea.fr> Date: Fri, 26 Jun 2020 14:45:02 +0200 Subject: [PATCH] PreBarrage when deleting a wall, leave no basin without connection (connect to river upstream/downstream) prevent deletion of walls that would be restored by the aforementioned mechanism "del" hotkey to delete element on schema --- .../pb-schema/pb-schema.component.ts | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts index fa57966e7..abc16fc94 100644 --- a/src/app/components/pb-schema/pb-schema.component.ts +++ b/src/app/components/pb-schema/pb-schema.component.ts @@ -7,10 +7,13 @@ import { import * as mermaid from "mermaid"; +import { HotkeysService, Hotkey } from 'angular2-hotkeys'; + import { I18nService } from "../../services/internationalisation.service"; import { PbSchema } from "../../formulaire/elements/pb-schema"; import { DialogNewPbCloisonComponent } from "../dialog-new-pb-cloison/dialog-new-pb-cloison.component"; import { GenericCalculatorComponent } from "../generic-calculator/calculator.component"; +import { AppComponent } from "../../app.component"; /** * The interactive schema for calculator type "PreBarrage" (component) @@ -63,8 +66,11 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni public constructor( @Inject(forwardRef(() => GenericCalculatorComponent)) private calculatorComponent: GenericCalculatorComponent, private i18nService: I18nService, + private hotkeysService: HotkeysService, private newPbCloisonDialog: MatDialog - ) { } + ) { + this.hotkeysService.add(new Hotkey("del", AppComponent.onHotkey(this.removeOnHotkey, this))); + } public get selectedItem(): any { return this._selectedItem; @@ -413,12 +419,42 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni } public get enableRemoveButton() { - return (this._selectedItem !== undefined); + if (this._selectedItem === undefined) { + return false; + } + // if deleting a PbCloison would replace it by a new one at + // the same place (@see onRemoveClick), make it not deletable + if (this._selectedItem instanceof PbCloison) { + if (( + this._selectedItem.bassinAmont !== undefined + && this._selectedItem.bassinAmont.cloisonsAval.length === 1 + && this._selectedItem.bassinAval === undefined + ) || ( + this._selectedItem.bassinAval !== undefined + && this._selectedItem.bassinAval.cloisonsAmont.length === 1 + && this._selectedItem.bassinAmont === undefined + )) { + return false; + } + } + return true; } /** Removes a basin or wall, and all related items */ public onRemoveClick() { this.model.deleteChild(this._selectedItem.findPositionInParent()); + // never let an unconnected basin ! (not done in model to prevent unwanted + // automatic child addition when clearing children) + if (this._selectedItem instanceof PbCloison) { + // if no downstream connections remain, connect to river downstream + if (this._selectedItem.bassinAmont !== undefined && this._selectedItem.bassinAmont.cloisonsAval.length === 0) { + this.model.addChild(new PbCloison(this._selectedItem.bassinAmont, undefined)); + } + // if no upstream connections remain, connect to river upstream + if (this._selectedItem.bassinAval !== undefined && this._selectedItem.bassinAval.cloisonsAmont.length === 0) { + this.model.addChild(new PbCloison(undefined, this._selectedItem.bassinAval)); + } + } this.unselect(); this.refresh(); } @@ -427,6 +463,13 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni return this.i18nService.localizeText("INFO_FIELDSET_REMOVE"); } + // listener for "del" hotkey + protected removeOnHotkey() { + if (this.enableRemoveButton) { + this.onRemoveClick(); + } + } + public get enableCopyButton() { return (this._selectedItem !== undefined && this._selectedItem instanceof PbCloison); } @@ -559,7 +602,10 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni private updateValidity() { // check that at least 1 basin is present and a route from river // upstream to river downstream exists (2nd check includes 1st) - this._isValid = this.model.hasUpDownConnection(); + this._isValid = ( + this.model.hasUpDownConnection() + && ! this.model.hasBasinNotConnected() + ); console.log("schéma valide", this._isValid); this.validChange.emit(); } -- GitLab