From 625530bc46e70dc4f1a91bb370c9b92a9ce97daf Mon Sep 17 00:00:00 2001
From: Mathias Chouet <mathias.chouet@irstea.fr>
Date: Thu, 11 Jun 2020 14:42:27 +0200
Subject: [PATCH] PreBarrage: move "standalone" basins

---
 .../pb-schema/pb-schema.component.html        | 13 ++++-
 .../pb-schema/pb-schema.component.ts          | 55 ++++++++++++++++++-
 src/locale/messages.en.json                   |  2 +
 src/locale/messages.fr.json                   |  2 +
 4 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/src/app/components/pb-schema/pb-schema.component.html b/src/app/components/pb-schema/pb-schema.component.html
index f8bb9bbc0..b9314c653 100644
--- a/src/app/components/pb-schema/pb-schema.component.html
+++ b/src/app/components/pb-schema/pb-schema.component.html
@@ -21,13 +21,20 @@
                 {{ prefixedItemDescription }}
             </span>
             <button type="button" mat-icon-button color="primary" [disabled]="! enableCopyButton" (click)="onCopyClick()"
-            [title]="uitextCopy">
-              <mat-icon>content_copy</mat-icon>
-          </button>
+              [title]="uitextCopy">
+                <mat-icon>content_copy</mat-icon>
+            </button>
+          |
             <button type="button" mat-icon-button color="primary" [disabled]="! enableRemoveButton" (click)="onRemoveClick()"
               [title]="uitextRemove">
                 <mat-icon>delete</mat-icon>
             </button>
+            <button type="button" mat-icon-button [disabled]="! enableUpButton" (click)="onMoveBasinUpClick()" [title]="uitextMoveBasinUp">
+                <mat-icon>arrow_upward</mat-icon>
+            </button>
+            <button type="button" mat-icon-button [disabled]="! enableDownButton" (click)="onMoveBasinDownClick()" [title]="uitextMoveBasinDown">
+                <mat-icon>arrow_downward</mat-icon>
+            </button>
             <!-- 
             |
             <button type="button" mat-icon-button color="primary" (click)="exportAsSpreadsheet()"
diff --git a/src/app/components/pb-schema/pb-schema.component.ts b/src/app/components/pb-schema/pb-schema.component.ts
index 7249b6929..695243eab 100644
--- a/src/app/components/pb-schema/pb-schema.component.ts
+++ b/src/app/components/pb-schema/pb-schema.component.ts
@@ -480,11 +480,64 @@ export class PbSchemaComponent implements AfterViewInit, AfterContentInit, OnIni
         });
     }
 
-
     public get uitextAddWall() {
         return this.i18nService.localizeText("INFO_PB_ADD_WALL");
     }
 
+    public get enableUpButton() {
+        return (
+            this._selectedItem instanceof PbBassin
+            && this.findBasinPosition(this._selectedItem) !== 0
+            && this.isStandaloneBasin(this._selectedItem)
+        );
+    }
+
+    public onMoveBasinUpClick() {
+        if (this._selectedItem instanceof PbBassin) {
+            this.model.moveBasin(this._selectedItem.uid, this.findBasinPosition(this._selectedItem) - 1);
+        }
+        this.refresh();
+    }
+
+    public get uitextMoveBasinUp() {
+        return this.i18nService.localizeText("INFO_PB_MOVE_BASIN_UP");
+    }
+
+    public get enableDownButton() {
+        return (
+            this._selectedItem instanceof PbBassin
+            && this.findBasinPosition(this._selectedItem) !== this.model.bassins.length - 1
+            && this.isStandaloneBasin(this._selectedItem)
+        );
+    }
+
+    public onMoveBasinDownClick() {
+        if (this._selectedItem instanceof PbBassin) {
+            this.model.moveBasin(this._selectedItem.uid, this.findBasinPosition(this._selectedItem) + 1);
+        }
+        this.refresh();
+    }
+
+    public get uitextMoveBasinDown() {
+        return this.i18nService.localizeText("INFO_PB_MOVE_BASIN_DOWN");
+    }
+
+    /**
+     * Returns true if given basin is either connected to nothing, or only to
+     * river upstream or downstream
+     */
+    private isStandaloneBasin(basin: PbBassin) {
+        return (
+            (
+                basin.cloisonsAmont.length === 0
+                || basin.cloisonsAmont.map(c => c.bassinAmont).every(e => e === undefined)
+            ) && (
+                basin.cloisonsAval.length === 0
+                || basin.cloisonsAval.map(c => c.bassinAval).every(e => e === undefined)
+            )
+        );
+    }
+
     /**
      * Computes the global Pab validity : validity of every cell of every row
      */
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index b5004bf82..fe62870ce 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -520,6 +520,8 @@
     "INFO_PB_ADD_WALL": "Add new wall",
     "INFO_PB_BASSIN_N": "Basin #",
     "INFO_PB_CLOISON": "Wall",
+    "INFO_PB_MOVE_BASIN_UP": "Move basin up",
+    "INFO_PB_MOVE_BASIN_DOWN": "Move basin down",
     "INFO_PB_NEW_WALL_SELECT_BASINS": "Select basins to connect",
     "INFO_PB_NEW_WALL_UP_BASIN": "upstream basin",
     "INFO_PB_NEW_WALL_DOWN_BASIN": "Downstream basin",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index 1f2e4117c..2eb5001d8 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -521,6 +521,8 @@
     "INFO_PB_ADD_WALL": "Ajouter une cloison",
     "INFO_PB_BASSIN_N": "Bassin n°",
     "INFO_PB_CLOISON": "Cloison",
+    "INFO_PB_MOVE_BASIN_UP": "Déplacer le bassin vers le haut",
+    "INFO_PB_MOVE_BASIN_DOWN": "Déplacer le bassin vers le bas",
     "INFO_PB_NEW_WALL_SELECT_BASINS": "Choisir les bassins à connecter",
     "INFO_PB_NEW_WALL_UP_BASIN": "Bassin amont",
     "INFO_PB_NEW_WALL_DOWN_BASIN": "Bassin aval",
-- 
GitLab