import { IContainerModel } from '../Interfaces/IContainerModel'; /** * Returns a Generator iterating of over the children depth-first */ export function * MakeIterator(root: IContainerModel): Generator { const queue: IContainerModel[] = [root]; const visited = new Set(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 = Number(container.properties.x); let y = Number(container.properties.y); let current = container.parent; while (current != null) { x += Number(current.properties.x); y += Number(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; }