65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { IContainerModel } from '../Interfaces/IContainerModel';
|
|
|
|
/**
|
|
* Returns a Generator iterating of over the children depth-first
|
|
*/
|
|
export function * MakeIterator(root: IContainerModel): Generator<IContainerModel, void, unknown> {
|
|
const queue: IContainerModel[] = [root];
|
|
const visited = new Set<IContainerModel>(queue);
|
|
while (queue.length > 0) {
|
|
const container = queue.pop() as IContainerModel;
|
|
|
|
yield container;
|
|
|
|
for (let i = container.children.length - 1; i >= 0; i--) {
|
|
const child = container.children[i];
|
|
if (visited.has(child)) {
|
|
return;
|
|
}
|
|
visited.add(child);
|
|
queue.push(child);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the depth of the container
|
|
* @returns The depth of the container
|
|
*/
|
|
export function getDepth(parent: IContainerModel): number {
|
|
let depth = 0;
|
|
|
|
let current: IContainerModel | null = parent;
|
|
while (current != null) {
|
|
depth++;
|
|
current = current.parent;
|
|
}
|
|
|
|
return depth;
|
|
}
|
|
|
|
/**
|
|
* Returns the absolute position by iterating to the parent
|
|
* @returns The absolute position of the container
|
|
*/
|
|
export function getAbsolutePosition(container: IContainerModel): [number, number] {
|
|
let x = container.properties.x;
|
|
let y = container.properties.y;
|
|
let current = container.parent;
|
|
while (current != null) {
|
|
x += current.properties.x;
|
|
y += current.properties.y;
|
|
current = current.parent;
|
|
}
|
|
return [x, y];
|
|
}
|
|
|
|
export function findContainerById(root: IContainerModel, id: string): IContainerModel | undefined {
|
|
const it = MakeIterator(root);
|
|
for (const container of it) {
|
|
if (container.properties.id === id) {
|
|
return container;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|