2020-11-13 19:09:28 +02:00
|
|
|
import { LayoutItem } from './types';
|
|
|
|
|
|
|
|
type ItemItemCallback = (itemIndex: number, item: LayoutItem, parent: LayoutItem)=> boolean;
|
|
|
|
|
2021-01-02 15:32:15 +02:00
|
|
|
// Callback should return `true` if iteration should continue, or `false` if it
|
|
|
|
// should stop
|
2020-11-13 19:09:28 +02:00
|
|
|
export default function iterateItems(layout: LayoutItem, callback: ItemItemCallback) {
|
|
|
|
const result = callback(0, layout, null);
|
|
|
|
if (result === false) return;
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-13 19:09:28 +02:00
|
|
|
function recurseFind(item: LayoutItem, callback: Function): boolean {
|
|
|
|
if (item.children) {
|
|
|
|
for (let childIndex = 0; childIndex < item.children.length; childIndex++) {
|
|
|
|
const child = item.children[childIndex];
|
|
|
|
if (callback(childIndex, child, item) === false) return false;
|
|
|
|
if (recurseFind(child, callback) === false) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recurseFind(layout, callback) === false) return;
|
|
|
|
}
|