2020-12-07 16:27:46 +01:00
|
|
|
/**
|
2021-01-12 21:00:35 +01:00
|
|
|
* Recursively searches for a specific component ID
|
2020-12-07 16:27:46 +01:00
|
|
|
*/
|
2021-01-12 21:00:35 +01:00
|
|
|
export const findComponent = (rootComponent, id) => {
|
|
|
|
return searchComponentTree(rootComponent, comp => comp._id === id)
|
2020-06-01 13:12:25 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* Recursively searches for a specific component type
|
|
|
|
*/
|
|
|
|
export const findComponentType = (rootComponent, type) => {
|
|
|
|
return searchComponentTree(rootComponent, comp => comp._component === type)
|
|
|
|
}
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2021-01-12 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* Recursively searches for the parent component of a specific component ID
|
|
|
|
*/
|
|
|
|
export const findComponentParent = (rootComponent, id, parentComponent) => {
|
|
|
|
if (!rootComponent || !id) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (rootComponent._id === id) {
|
|
|
|
return parentComponent
|
|
|
|
}
|
|
|
|
if (!rootComponent._children) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
for (const child of rootComponent._children) {
|
|
|
|
const childResult = findComponentParent(child, id, rootComponent)
|
|
|
|
if (childResult) {
|
|
|
|
return childResult
|
2020-06-01 13:12:25 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-12 21:00:35 +01:00
|
|
|
return null
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
2020-06-02 12:11:53 +02:00
|
|
|
|
2021-01-12 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* Recursively searches for a specific component ID and records the component
|
|
|
|
* path to this component
|
|
|
|
*/
|
|
|
|
export const findComponentPath = (rootComponent, id, path = []) => {
|
|
|
|
if (!rootComponent || !id) {
|
2021-01-14 16:39:50 +01:00
|
|
|
return []
|
2020-10-17 19:20:06 +02:00
|
|
|
}
|
2021-01-12 21:00:35 +01:00
|
|
|
if (rootComponent._id === id) {
|
2021-01-14 16:39:50 +01:00
|
|
|
return [...path, rootComponent]
|
2020-10-17 19:20:06 +02:00
|
|
|
}
|
2021-01-12 21:00:35 +01:00
|
|
|
if (!rootComponent._children) {
|
2021-01-14 16:39:50 +01:00
|
|
|
return []
|
2020-10-17 19:20:06 +02:00
|
|
|
}
|
2021-01-12 21:00:35 +01:00
|
|
|
for (const child of rootComponent._children) {
|
2021-01-14 16:39:50 +01:00
|
|
|
const newPath = [...path, rootComponent]
|
2021-01-12 21:00:35 +01:00
|
|
|
const childResult = findComponentPath(child, id, newPath)
|
2021-01-14 16:39:50 +01:00
|
|
|
if (childResult?.length) {
|
2021-01-12 21:00:35 +01:00
|
|
|
return childResult
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 16:39:50 +01:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recurses through the component tree and finds all components of a certain
|
|
|
|
* type.
|
|
|
|
*/
|
|
|
|
export const findAllMatchingComponents = (rootComponent, selector) => {
|
|
|
|
if (!rootComponent || !selector) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
let components = []
|
|
|
|
if (rootComponent._children) {
|
|
|
|
rootComponent._children.forEach(child => {
|
|
|
|
components = [
|
|
|
|
...components,
|
|
|
|
...findAllMatchingComponents(child, selector),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (selector(rootComponent)) {
|
|
|
|
components.push(rootComponent)
|
|
|
|
}
|
|
|
|
return components.reverse()
|
2021-01-12 21:00:35 +01:00
|
|
|
}
|
2020-10-17 19:20:06 +02:00
|
|
|
|
2021-01-12 21:00:35 +01:00
|
|
|
/**
|
|
|
|
* Recurses through a component tree evaluating a matching function against
|
|
|
|
* components until a match is found
|
|
|
|
*/
|
|
|
|
const searchComponentTree = (rootComponent, matchComponent) => {
|
|
|
|
if (!rootComponent || !matchComponent) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (matchComponent(rootComponent)) {
|
|
|
|
return rootComponent
|
|
|
|
}
|
|
|
|
if (!rootComponent._children) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
for (const child of rootComponent._children) {
|
|
|
|
const childResult = searchComponentTree(child, matchComponent)
|
2020-10-17 19:20:06 +02:00
|
|
|
if (childResult) {
|
|
|
|
return childResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|