From ba67b8237a5208a940a8b8019ca0259dbe505fa1 Mon Sep 17 00:00:00 2001 From: Denis Travin Date: Tue, 3 Oct 2023 19:20:58 +0500 Subject: [PATCH] feat: added some nice dynamic objects model methods --- .../dynamic-objects/dynamic-objects.model.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/chart/components/dynamic-objects/dynamic-objects.model.ts b/src/chart/components/dynamic-objects/dynamic-objects.model.ts index 9f05bc21..d7addc47 100644 --- a/src/chart/components/dynamic-objects/dynamic-objects.model.ts +++ b/src/chart/components/dynamic-objects/dynamic-objects.model.ts @@ -44,6 +44,22 @@ export class DynamicObjectsModel extends ChartBaseElement { return [obj, paneList]; } + /** + * @returns `DynamicObject` position in associated pane `LinkedList` + */ + getObjectPosition(id: DynamicObjectId): number | undefined { + const objInfo = this.getObjectInfoById(id); + + if (!objInfo) { + return; + } + + const [obj, paneList] = objInfo; + const targetNode = new ListNode(obj); + + return paneList.getNodePosition(targetNode); + } + /** * Adds an object from outside chart-core into model * @param obj @@ -84,6 +100,33 @@ export class DynamicObjectsModel extends ChartBaseElement { this.setDynamicObjects(this.objects); } + /** + * Moves the object inside the associated LinkedList to the specified position + */ + moveToPosition(id: DynamicObjectId, position: number) { + const objInfo = this.getObjectInfoById(id); + + if (!objInfo) { + return; + } + + const [obj, paneList] = objInfo; + const node = new ListNode(obj); + const currentPos = paneList.getNodePosition(node); + + if (currentPos === position) { + return; + } + + if (currentPos < position) { + paneList.insertAt(position, obj); + paneList.removeAt(currentPos); + } else { + paneList.removeAt(currentPos); + paneList.insertAt(position, obj); + } + } + /** * Moves the object inside the drawing order so it's being drawn before the other elements * @param paneId