From e19f29389f7d02de8dd796a1efa379dd52f974e7 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Fri, 27 Apr 2018 10:11:28 +0200
Subject: [PATCH] =?UTF-8?q?=20#47=20ouvrages=20parall=C3=A8les=20:=20d?=
 =?UTF-8?q?=C3=A9placement=20d'un=20ouvrage=20d'un=20cran=20vers=20le=20d?=
 =?UTF-8?q?=C3=A9but=20de=20la=20liste?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/nub_factory.ts                  | 35 +++++++++++++++++++++++++++++
 src/structure/parallel_structure.ts | 26 +++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/src/nub_factory.ts b/src/nub_factory.ts
index 52907eb4..4905003c 100644
--- a/src/nub_factory.ts
+++ b/src/nub_factory.ts
@@ -125,6 +125,41 @@ export class NubFactory {
         return undefined;
     }
 
+    /**
+     * déplace un SessionNub associé à un nub Structure vers le début de la liste de structures
+     * @param sn SessionNub à remplacer
+     * @param params propriété du nouveau SessionNub
+     */
+    public moveStructureNubUp(sn: SessionNub) {
+        // recherche du nub ouvrages parallèles possédant l'ouvrage
+        let psn;
+        for (const s of this._session)
+            if (s.nub instanceof ParallelStructure) {
+                psn = s.nub as ParallelStructure;
+                if (psn.hasStructure(sn.nub))
+                    break;
+                else
+                    psn = undefined;
+            }
+
+        // déplacement
+        if (psn) {
+            let i = 0;
+            for (const n of this._session) {
+                if (n.uid == sn.uid && i > 0) {
+                    const n = this._session[i - 1];
+                    this._session[i - 1] = this._session[i];
+                    this._session[i] = n;
+                    psn.moveStructureUp(sn.nub as Structure);
+                    return;
+                }
+                i++;
+            }
+        }
+
+        throw new Error(`NubFactory.moveStructureNubUp() : la structure (uid ${sn.uid}) à déplacer n'a pas été trouvée`);
+    }
+
     /**
      * créé un Nub
      * @param calcType type de Nub
diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts
index 6184f7f4..aecfda53 100644
--- a/src/structure/parallel_structure.ts
+++ b/src/structure/parallel_structure.ts
@@ -80,6 +80,32 @@ export class ParallelStructure extends Nub {
         }
     }
 
+    /**
+     * @return true si la structure donnée est dans la liste
+     */
+    public hasStructure(structure: Nub): boolean {
+        for (const s of this.structures)
+            if (s.uid == structure.uid)
+                return true;
+        return false;
+    }
+
+    /**
+     * déplace une structure d'une position vers le début de la liste
+     */
+    public moveStructureUp(structure: Structure) {
+        let i = 0;
+        for (const s of this.structures) {
+            if (s.uid == structure.uid && i > 0) {
+                const t = this.structures[i - 1];
+                this.structures[i - 1] = this.structures[i];
+                this.structures[i] = t;
+                return;
+            }
+            i++;
+        }
+    }
+
     /**
      * Supprime une structure hydraulique
      * @param index numéro de la structure dans le tableau
-- 
GitLab