budibase/packages/builder/src/userInterface/pagesParsing/searchComponents.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-08-04 23:21:16 +02:00
import {pipe} from "../../common/core";
2019-07-28 09:03:11 +02:00
import {
find,
isUndefined,
filter,
some,
includes
} from "lodash/fp";
const normalString = s => (s||"").trim().toLowerCase();
2019-07-31 09:09:04 +02:00
export const isRootComponent = c => isUndefined(c.inherits);
2019-07-28 09:03:11 +02:00
2019-08-04 23:21:16 +02:00
export const searchAllComponents = (allComponents, phrase) => {
2019-07-28 09:03:11 +02:00
2019-07-28 13:45:00 +02:00
const hasPhrase = (...vals) =>
pipe(vals, [
some(v => includes(normalString(phrase))(normalString(v)))
]);
2019-07-28 09:03:11 +02:00
2019-08-04 23:21:16 +02:00
const componentMatches = c => {
2019-07-28 09:03:11 +02:00
if(hasPhrase(c.name, ...(c.tags || []))) return true;
2019-08-04 23:21:16 +02:00
if(isRootComponent(c)) return false;
2019-07-28 09:03:11 +02:00
const parent = getExactComponent(
2019-08-04 23:21:16 +02:00
allComponents,
2019-07-28 09:03:11 +02:00
c.inherits);
2019-08-04 23:21:16 +02:00
return componentMatches(parent);
2019-07-28 09:03:11 +02:00
}
2019-08-04 23:21:16 +02:00
return filter(componentMatches)(allComponents);
2019-07-28 09:03:11 +02:00
}
2019-08-04 23:21:16 +02:00
export const getExactComponent = (allComponents, name) => {
2019-07-28 09:03:11 +02:00
const stringEquals = (s1, s2) =>
normalString(s1) === normalString(s2);
2019-08-04 23:21:16 +02:00
return pipe(allComponents,[
2019-07-28 09:03:11 +02:00
find(c => stringEquals(c.name, name))
]);
}
2019-08-04 23:21:16 +02:00
export const getAncestorProps = (allComponents, name, found=[]) => {
2019-07-28 09:03:11 +02:00
const thisComponent = getExactComponent(
2019-08-04 23:21:16 +02:00
allComponents, name);
2019-07-28 09:03:11 +02:00
if(isRootComponent(thisComponent))
return [thisComponent.props, ...found];
return getAncestorProps(
2019-08-04 23:21:16 +02:00
allComponents,
2019-07-28 09:03:11 +02:00
thisComponent.inherits,
2019-08-15 09:49:15 +02:00
[{...thisComponent.props},
2019-07-28 09:03:11 +02:00
...found]);
2019-08-14 23:11:59 +02:00
}