Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added some nice dynamic objects model methods #47

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/chart/components/dynamic-objects/dynamic-objects.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down