2021-01-29 06:04:28 +00:00
|
|
|
import {
|
|
|
|
|
DependencyMap,
|
|
|
|
|
isChildPropertyPath,
|
2021-02-16 10:29:08 +00:00
|
|
|
isDynamicValue,
|
2021-01-29 06:04:28 +00:00
|
|
|
} from "../utils/DynamicBindingUtils";
|
2021-01-04 10:16:08 +00:00
|
|
|
import { WidgetType } from "../constants/WidgetConstants";
|
|
|
|
|
import { WidgetProps } from "../widgets/BaseWidget";
|
|
|
|
|
import { WidgetTypeConfigMap } from "../utils/WidgetFactory";
|
|
|
|
|
import { VALIDATORS } from "./validations";
|
|
|
|
|
import { Diff } from "deep-diff";
|
|
|
|
|
import {
|
|
|
|
|
DataTree,
|
2021-01-29 06:04:28 +00:00
|
|
|
DataTreeAction,
|
2021-01-04 10:16:08 +00:00
|
|
|
DataTreeEntity,
|
|
|
|
|
DataTreeWidget,
|
|
|
|
|
ENTITY_TYPE,
|
|
|
|
|
} from "../entities/DataTree/dataTreeFactory";
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
|
2021-01-29 06:04:28 +00:00
|
|
|
// Dropdown1.options[1].value -> Dropdown1.options[1]
|
|
|
|
|
// Dropdown1.options[1] -> Dropdown1.options
|
|
|
|
|
// Dropdown1.options -> Dropdown1
|
|
|
|
|
export const IMMEDIATE_PARENT_REGEX = /^(.*)(\..*|\[.*\])$/;
|
|
|
|
|
|
2021-01-04 10:16:08 +00:00
|
|
|
export enum DataTreeDiffEvent {
|
|
|
|
|
NEW = "NEW",
|
|
|
|
|
DELETE = "DELETE",
|
|
|
|
|
EDIT = "EDIT",
|
|
|
|
|
NOOP = "NOOP",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DataTreeDiff = {
|
|
|
|
|
payload: {
|
|
|
|
|
propertyPath: string;
|
|
|
|
|
value?: string;
|
|
|
|
|
};
|
|
|
|
|
event: DataTreeDiffEvent;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export class CrashingError extends Error {}
|
|
|
|
|
|
|
|
|
|
export const convertPathToString = (arrPath: Array<string | number>) => {
|
|
|
|
|
let string = "";
|
|
|
|
|
arrPath.forEach((segment) => {
|
2021-02-16 10:29:08 +00:00
|
|
|
if (isInt(segment)) {
|
|
|
|
|
string = string + "[" + segment + "]";
|
|
|
|
|
} else {
|
2021-01-04 10:16:08 +00:00
|
|
|
if (string.length !== 0) {
|
|
|
|
|
string = string + ".";
|
|
|
|
|
}
|
|
|
|
|
string = string + segment;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return string;
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-16 10:29:08 +00:00
|
|
|
// Todo: improve the logic here
|
|
|
|
|
// Right now NaN, Infinity, floats, everything works
|
|
|
|
|
function isInt(val: string | number): boolean {
|
|
|
|
|
if (typeof val === "number") return true;
|
|
|
|
|
return !isNaN(parseInt(val));
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 10:16:08 +00:00
|
|
|
export const translateDiffEventToDataTreeDiffEvent = (
|
|
|
|
|
difference: Diff<any, any>,
|
|
|
|
|
): DataTreeDiff => {
|
|
|
|
|
const result: DataTreeDiff = {
|
|
|
|
|
payload: {
|
|
|
|
|
propertyPath: "",
|
|
|
|
|
value: "",
|
|
|
|
|
},
|
|
|
|
|
event: DataTreeDiffEvent.NOOP,
|
|
|
|
|
};
|
|
|
|
|
if (!difference.path) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
const propertyPath = convertPathToString(difference.path);
|
|
|
|
|
switch (difference.kind) {
|
|
|
|
|
case "N": {
|
|
|
|
|
result.event = DataTreeDiffEvent.NEW;
|
|
|
|
|
result.payload = {
|
|
|
|
|
propertyPath,
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "D": {
|
|
|
|
|
result.event = DataTreeDiffEvent.DELETE;
|
|
|
|
|
result.payload = { propertyPath };
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "E": {
|
|
|
|
|
const rhsChange =
|
|
|
|
|
typeof difference.rhs === "string" && isDynamicValue(difference.rhs);
|
|
|
|
|
|
|
|
|
|
const lhsChange =
|
|
|
|
|
typeof difference.lhs === "string" && isDynamicValue(difference.lhs);
|
|
|
|
|
|
|
|
|
|
if (rhsChange || lhsChange) {
|
|
|
|
|
result.event = DataTreeDiffEvent.EDIT;
|
|
|
|
|
result.payload = {
|
|
|
|
|
propertyPath,
|
|
|
|
|
value: difference.rhs,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
// Handle static value changes that change structure that can lead to
|
|
|
|
|
// old bindings being eligible
|
|
|
|
|
if (
|
|
|
|
|
difference.lhs === undefined &&
|
|
|
|
|
typeof difference.rhs === "object"
|
|
|
|
|
) {
|
|
|
|
|
result.event = DataTreeDiffEvent.NEW;
|
|
|
|
|
result.payload = { propertyPath };
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
difference.rhs === undefined &&
|
|
|
|
|
typeof difference.lhs === "object"
|
|
|
|
|
) {
|
|
|
|
|
result.event = DataTreeDiffEvent.DELETE;
|
|
|
|
|
result.payload = { propertyPath };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "A": {
|
2021-02-16 10:29:08 +00:00
|
|
|
return translateDiffEventToDataTreeDiffEvent({
|
|
|
|
|
...difference.item,
|
|
|
|
|
path: [...difference.path, difference.index],
|
|
|
|
|
});
|
2021-01-04 10:16:08 +00:00
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Table1.selectedRow
|
|
|
|
|
Table1.selectedRow.email: ["Input1.defaultText"]
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export const addDependantsOfNestedPropertyPaths = (
|
|
|
|
|
parentPaths: Array<string>,
|
|
|
|
|
inverseMap: DependencyMap,
|
2021-02-16 10:29:08 +00:00
|
|
|
): Set<string> => {
|
2021-01-04 10:16:08 +00:00
|
|
|
const withNestedPaths: Set<string> = new Set();
|
|
|
|
|
const dependantNodes = Object.keys(inverseMap);
|
|
|
|
|
parentPaths.forEach((propertyPath) => {
|
|
|
|
|
withNestedPaths.add(propertyPath);
|
|
|
|
|
dependantNodes
|
|
|
|
|
.filter((dependantNodePath) =>
|
2021-01-29 06:04:28 +00:00
|
|
|
isChildPropertyPath(propertyPath, dependantNodePath),
|
2021-01-04 10:16:08 +00:00
|
|
|
)
|
|
|
|
|
.forEach((dependantNodePath) => {
|
|
|
|
|
inverseMap[dependantNodePath].forEach((path) => {
|
|
|
|
|
withNestedPaths.add(path);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-02-16 10:29:08 +00:00
|
|
|
return withNestedPaths;
|
2021-01-04 10:16:08 +00:00
|
|
|
};
|
|
|
|
|
|
2021-01-29 06:04:28 +00:00
|
|
|
export function isWidget(entity: DataTreeEntity): entity is DataTreeWidget {
|
2021-01-04 10:16:08 +00:00
|
|
|
return (
|
|
|
|
|
typeof entity === "object" &&
|
|
|
|
|
"ENTITY_TYPE" in entity &&
|
|
|
|
|
entity.ENTITY_TYPE === ENTITY_TYPE.WIDGET
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 06:04:28 +00:00
|
|
|
export function isAction(entity: DataTreeEntity): entity is DataTreeAction {
|
2021-01-04 10:16:08 +00:00
|
|
|
return (
|
|
|
|
|
typeof entity === "object" &&
|
|
|
|
|
"ENTITY_TYPE" in entity &&
|
|
|
|
|
entity.ENTITY_TYPE === ENTITY_TYPE.ACTION
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to remove functions from data tree to avoid any unexpected identifier while JSON parsing
|
|
|
|
|
// Check issue https://github.com/appsmithorg/appsmith/issues/719
|
|
|
|
|
export const removeFunctions = (value: any) => {
|
|
|
|
|
if (_.isFunction(value)) {
|
|
|
|
|
return "Function call";
|
2021-01-25 11:19:31 +00:00
|
|
|
} else if (_.isObject(value)) {
|
2021-01-04 10:16:08 +00:00
|
|
|
return JSON.parse(JSON.stringify(value));
|
|
|
|
|
} else {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const removeFunctionsFromDataTree = (dataTree: DataTree) => {
|
|
|
|
|
dataTree.actionPaths?.forEach((functionPath) => {
|
|
|
|
|
_.set(dataTree, functionPath, {});
|
|
|
|
|
});
|
|
|
|
|
delete dataTree.actionPaths;
|
|
|
|
|
return dataTree;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const makeParentsDependOnChildren = (
|
|
|
|
|
depMap: DependencyMap,
|
|
|
|
|
): DependencyMap => {
|
|
|
|
|
//return depMap;
|
|
|
|
|
// Make all parents depend on child
|
|
|
|
|
Object.keys(depMap).forEach((key) => {
|
|
|
|
|
depMap = makeParentsDependOnChild(depMap, key);
|
|
|
|
|
depMap[key].forEach((path) => {
|
|
|
|
|
depMap = makeParentsDependOnChild(depMap, path);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return depMap;
|
|
|
|
|
};
|
2021-01-29 06:04:28 +00:00
|
|
|
|
2021-01-04 10:16:08 +00:00
|
|
|
export const makeParentsDependOnChild = (
|
|
|
|
|
depMap: DependencyMap,
|
|
|
|
|
child: string,
|
|
|
|
|
): DependencyMap => {
|
|
|
|
|
const result: DependencyMap = depMap;
|
|
|
|
|
let curKey = child;
|
2021-01-29 06:04:28 +00:00
|
|
|
|
2021-01-04 10:16:08 +00:00
|
|
|
let matches: Array<string> | null;
|
|
|
|
|
// Note: The `=` is intentional
|
|
|
|
|
// Stops looping when match is null
|
2021-01-29 06:04:28 +00:00
|
|
|
while ((matches = curKey.match(IMMEDIATE_PARENT_REGEX)) !== null) {
|
2021-01-04 10:16:08 +00:00
|
|
|
const parentKey = matches[1];
|
|
|
|
|
// Todo: switch everything to set.
|
|
|
|
|
const existing = new Set(result[parentKey] || []);
|
|
|
|
|
existing.add(curKey);
|
|
|
|
|
result[parentKey] = Array.from(existing);
|
|
|
|
|
curKey = parentKey;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-29 06:04:28 +00:00
|
|
|
// The idea is to find the immediate parents of the property paths
|
|
|
|
|
// e.g. For Table1.selectedRow.email, the parent is Table1.selectedRow
|
|
|
|
|
export const getImmediateParentsOfPropertyPaths = (
|
|
|
|
|
propertyPaths: Array<string>,
|
|
|
|
|
): Array<string> => {
|
|
|
|
|
// Use a set to ensure that we dont have duplicates
|
|
|
|
|
const parents: Set<string> = new Set();
|
|
|
|
|
|
|
|
|
|
propertyPaths.forEach((path) => {
|
|
|
|
|
const matches = path.match(IMMEDIATE_PARENT_REGEX);
|
|
|
|
|
|
|
|
|
|
if (matches !== null) {
|
|
|
|
|
parents.add(matches[1]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Array.from(parents);
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-04 10:16:08 +00:00
|
|
|
export function validateWidgetProperty(
|
|
|
|
|
widgetConfigMap: WidgetTypeConfigMap,
|
|
|
|
|
widgetType: WidgetType,
|
|
|
|
|
property: string,
|
|
|
|
|
value: any,
|
|
|
|
|
props: WidgetProps,
|
|
|
|
|
dataTree?: DataTree,
|
|
|
|
|
) {
|
|
|
|
|
const propertyValidationTypes = widgetConfigMap[widgetType].validations;
|
|
|
|
|
const validationTypeOrValidator = propertyValidationTypes[property];
|
|
|
|
|
let validator;
|
|
|
|
|
|
|
|
|
|
if (typeof validationTypeOrValidator === "function") {
|
|
|
|
|
validator = validationTypeOrValidator;
|
|
|
|
|
} else {
|
|
|
|
|
validator = VALIDATORS[validationTypeOrValidator];
|
|
|
|
|
}
|
|
|
|
|
if (validator) {
|
|
|
|
|
return validator(value, props, dataTree);
|
|
|
|
|
} else {
|
|
|
|
|
return { isValid: true, parsed: value };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getValidatedTree(
|
|
|
|
|
widgetConfigMap: WidgetTypeConfigMap,
|
|
|
|
|
tree: DataTree,
|
|
|
|
|
) {
|
|
|
|
|
return Object.keys(tree).reduce((tree, entityKey: string) => {
|
|
|
|
|
const entity = tree[entityKey] as DataTreeWidget;
|
|
|
|
|
if (!isWidget(entity)) {
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
const parsedEntity = { ...entity };
|
|
|
|
|
Object.keys(entity).forEach((property: string) => {
|
|
|
|
|
const validationProperties = widgetConfigMap[entity.type].validations;
|
|
|
|
|
|
|
|
|
|
if (property in validationProperties) {
|
|
|
|
|
const value = _.get(entity, property);
|
|
|
|
|
// Pass it through parse
|
|
|
|
|
const {
|
|
|
|
|
parsed,
|
|
|
|
|
isValid,
|
|
|
|
|
message,
|
|
|
|
|
transformed,
|
|
|
|
|
} = validateWidgetProperty(
|
|
|
|
|
widgetConfigMap,
|
|
|
|
|
entity.type,
|
|
|
|
|
property,
|
|
|
|
|
value,
|
|
|
|
|
entity,
|
|
|
|
|
tree,
|
|
|
|
|
);
|
|
|
|
|
parsedEntity[property] = parsed;
|
|
|
|
|
const evaluatedValue = isValid
|
|
|
|
|
? parsed
|
|
|
|
|
: _.isUndefined(transformed)
|
|
|
|
|
? value
|
|
|
|
|
: transformed;
|
|
|
|
|
const safeEvaluatedValue = removeFunctions(evaluatedValue);
|
|
|
|
|
_.set(parsedEntity, `evaluatedValues.${property}`, safeEvaluatedValue);
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
_.set(parsedEntity, `invalidProps.${property}`, true);
|
|
|
|
|
_.set(parsedEntity, `validationMessages.${property}`, message);
|
|
|
|
|
} else {
|
|
|
|
|
_.set(parsedEntity, `invalidProps.${property}`, false);
|
|
|
|
|
_.set(parsedEntity, `validationMessages.${property}`, "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return { ...tree, [entityKey]: parsedEntity };
|
|
|
|
|
}, tree);
|
|
|
|
|
}
|
2021-01-29 17:59:23 +00:00
|
|
|
|
|
|
|
|
export const getAllPaths = (
|
|
|
|
|
records: any,
|
|
|
|
|
curKey = "",
|
|
|
|
|
result: Record<string, true> = {},
|
|
|
|
|
): Record<string, true> => {
|
|
|
|
|
// Add the key if it exists
|
|
|
|
|
if (curKey) result[curKey] = true;
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(records)) {
|
|
|
|
|
for (let i = 0; i < records.length; i++) {
|
|
|
|
|
const tempKey = curKey ? `${curKey}[${i}]` : `${i}`;
|
|
|
|
|
getAllPaths(records[i], tempKey, result);
|
|
|
|
|
}
|
|
|
|
|
} else if (typeof records === "object") {
|
|
|
|
|
for (const key in records) {
|
|
|
|
|
const tempKey = curKey ? `${curKey}.${key}` : `${key}`;
|
|
|
|
|
getAllPaths(records[key], tempKey, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
2021-02-16 10:29:08 +00:00
|
|
|
export const trimDependantChangePaths = (
|
|
|
|
|
changePaths: Set<string>,
|
|
|
|
|
dependencyMap: DependencyMap,
|
|
|
|
|
): Array<string> => {
|
|
|
|
|
const trimmedPaths = [];
|
|
|
|
|
for (const path of changePaths) {
|
|
|
|
|
let foundADependant = false;
|
|
|
|
|
if (path in dependencyMap) {
|
|
|
|
|
const dependants = dependencyMap[path];
|
|
|
|
|
for (const dependantPath of dependants) {
|
|
|
|
|
if (changePaths.has(dependantPath)) {
|
|
|
|
|
foundADependant = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!foundADependant) {
|
|
|
|
|
trimmedPaths.push(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return trimmedPaths;
|
|
|
|
|
};
|