From 532dc4cce267a4f1450afecb6a269619b9364dc2 Mon Sep 17 00:00:00 2001 From: "mathias.chouet" <mathias.chouet@irstea.fr> Date: Wed, 16 Jan 2019 09:59:34 +0100 Subject: [PATCH] =?UTF-8?q?Structures=20parall=C3=A8les:=20NubFactory=20ne?= =?UTF-8?q?=20se=20charge=20plus=20du=20d=C3=A9placement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/nub_factory.ts | 140 ++-------------------------- src/structure/parallel_structure.ts | 28 ++++++ 2 files changed, 37 insertions(+), 131 deletions(-) diff --git a/src/nub_factory.ts b/src/nub_factory.ts index 25923b66..79af7138 100644 --- a/src/nub_factory.ts +++ b/src/nub_factory.ts @@ -78,7 +78,15 @@ export class NubFactory { if (n.uid === sn.uid) { const newNub = this.newSessionNub(params); this._session[i] = newNub; - this.replaceStructureNub(sn.nub, newNub.nub); + // move structure inside parent + if (sn.nub instanceof Structure) { + const struct = sn.nub as Structure; + if (struct.parent) { + struct.parent.replaceStructureInplace(struct, newNub.nub as Structure); + } else { + throw new Error("Structure nub had no parent"); + } + } return newNub; } i++; @@ -88,67 +96,11 @@ export class NubFactory { return undefined; } - /** - * déplace un SessionNub associé à un nub Structure d'une position vers le début de la liste de structures - * @param sn SessionNub à remplacer - * @param params propriété du nouveau SessionNub - */ - public moveStructureNubUp(sn: SessionNub) { - const psn = this.findParallelStructureWithNub(sn.nub); - - // déplacement - if (psn) { - let i = 0; - for (const n of this._session) { - if (n.uid === sn.uid && i > 0) { - const nS: SessionNub = this._session[i - 1]; - this._session[i - 1] = this._session[i]; - this._session[i] = nS; - 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` - ); - } - - /** - * déplace un SessionNub associé à un nub Structure d'une position vers la fin de la liste de structures - * @param sn SessionNub à remplacer - */ - public moveStructureNubDown(sn: SessionNub) { - const psn = this.findParallelStructureWithNub(sn.nub); - - // déplacement - if (psn) { - let i = 0; - for (const n of this._session) { - if (n.uid === sn.uid && i < this._session.length - 1) { - const nS: SessionNub = this._session[i]; - this._session[i] = this._session[i + 1]; - this._session[i + 1] = nS; - psn.moveStructureDown(sn.nub as Structure); - return; - } - i++; - } - } - - throw new Error( - `NubFactory.moveStructureNubDown() : la structure (uid ${sn.uid}) à déplacer n'a pas été trouvée` - ); - } - public deleteSessionNub(sn: SessionNub) { let i = 0; for (const n of this._session) { if (n.uid === sn.uid) { this._session.splice(i, 1); - this.deleteStructureNub(sn.nub); return; } i++; @@ -162,80 +114,6 @@ export class NubFactory { return new SessionNub(nub, params); } - private replaceStructureNub(oldNub: Nub, newNub: Nub) { - const b1 = oldNub instanceof Structure; - const b2 = newNub instanceof Structure; - if ((b1 && !b2) || (!b1 && b2)) { - throw new Error( - `NubFactory.replaceStructureNub() : arguments incorrects (${typeof (oldNub)}/${typeof (newNub)}` - ); - } - if (b1 && b2) { - for (const sn of this._session) { - if (sn.nub instanceof ParallelStructure) { - const psn = sn.nub as ParallelStructure; - let i = 0; - for (const st of psn.structures) { - if (st.uid === oldNub.uid) { - psn.replaceStructure(i, newNub as Structure); - return; - } - i++; - } - } - } - throw new Error( - `NubFactory.replaceStructureNub() : la structure (uid ${oldNub.uid}) à remplacer n'a pas été trouvée` - ); - } - - // copie (dans la mesure du possible) des modes de valeur des paramètres - for (const p of newNub.parameters) { - try { - const p2 = oldNub.getParameter(p.symbol); - p.valueMode = p2.valueMode; - } catch (e) { - // ??? - } - } - } - - private deleteStructureNub(n: Nub) { - if (n instanceof Structure) { - for (const sn of this._session) { - if (sn.nub instanceof ParallelStructure) { - const psn = sn.nub as ParallelStructure; - let i = 0; - for (const st of psn.structures) { - if (st.uid === n.uid) { - psn.deleteStructure(i); - return; - } - i++; - } - } - } - throw new Error( - `NubFactory.deleteStructureNub() : la structure (uid ${n.uid}) à supprimer n'a pas été trouvée` - ); - } - } - - /** - * recherche du nub ouvrages parallèles possédant l'ouvrage donné - */ - private findParallelStructureWithNub(n: Nub) { - for (const s of this._session) { - if (s.nub instanceof ParallelStructure) { - const res = s.nub as ParallelStructure; - if (res.hasStructure(n)) { - return res; - } - } - } - return undefined; - } - /** * créé un Nub * @param calcType type de Nub diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts index 312b532d..c4d86cf5 100644 --- a/src/structure/parallel_structure.ts +++ b/src/structure/parallel_structure.ts @@ -96,6 +96,34 @@ export class ParallelStructure extends Nub { structure.parent = this; } + /** + * Finds oldStructure in the list, and replaces it (at the same index) with newStructure + * @param oldStructure Structure to get the index for + * @param newStructure Structure to set at this index + */ + public replaceStructureInplace(oldStructure: Structure, newStructure: Structure) { + const index = this.getIndexForStructure(oldStructure); + if (index === -1) { + throw new Error("old Structure not found"); + } + this.replaceStructure(index, newStructure); + } + + /** + * Returns the current index of the given structure if any, + * or else returns -1 + * @param structure Structure to look for + */ + public getIndexForStructure(structure: Structure): number { + let index: number = -1; + for (let i = 0; i < this._structures.length; i++) { + if (this._structures[i].uid === structure.uid) { + index = i; + } + } + return index; + } + /** * @return true si la structure donnée est dans la liste */ -- GitLab