2020-03-19 03:25:52 +00:00
|
|
|
import {
|
|
|
|
|
ActionData,
|
|
|
|
|
ActionDataState,
|
|
|
|
|
} from "reducers/entityReducers/actionsReducer";
|
2020-02-18 10:41:52 +00:00
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
|
|
|
|
import { ActionResponse } from "api/ActionAPI";
|
2020-03-19 03:25:52 +00:00
|
|
|
import { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
|
|
|
|
|
import { MetaState } from "reducers/entityReducers/metaReducer";
|
2020-04-20 05:42:46 +00:00
|
|
|
import { PageListPayload } from "constants/ReduxActionConstants";
|
2020-04-17 16:15:09 +00:00
|
|
|
import WidgetFactory from "utils/WidgetFactory";
|
2020-07-03 08:58:58 +00:00
|
|
|
import { ActionConfig, Property } from "entities/Action";
|
2020-08-07 14:24:26 +00:00
|
|
|
import { AuthUserState } from "@appsmith/reducers/entityReducers/authUserReducer";
|
|
|
|
|
import { UrlDataState } from "@appsmith/reducers/entityReducers/urlReducer";
|
2020-02-18 10:41:52 +00:00
|
|
|
|
|
|
|
|
export type ActionDescription<T> = {
|
|
|
|
|
type: string;
|
|
|
|
|
payload: T;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ActionDispatcher<T, A extends string[]> = (
|
|
|
|
|
...args: A
|
|
|
|
|
) => ActionDescription<T>;
|
|
|
|
|
|
|
|
|
|
export enum ENTITY_TYPE {
|
|
|
|
|
ACTION = "ACTION",
|
|
|
|
|
WIDGET = "WIDGET",
|
2020-08-07 14:24:26 +00:00
|
|
|
APPSMITH = "APPSMITH",
|
2020-02-18 10:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type RunActionPayload = {
|
|
|
|
|
actionId: string;
|
|
|
|
|
onSuccess: string;
|
|
|
|
|
onError: string;
|
2020-07-17 12:55:34 +00:00
|
|
|
params: Record<string, any>;
|
2020-02-18 10:41:52 +00:00
|
|
|
};
|
|
|
|
|
|
2020-06-29 07:41:53 +00:00
|
|
|
export interface DataTreeAction extends Omit<ActionData, "data" | "config"> {
|
2020-02-18 10:41:52 +00:00
|
|
|
data: ActionResponse["body"];
|
2020-06-29 07:41:53 +00:00
|
|
|
actionId: string;
|
|
|
|
|
config: Partial<ActionConfig>;
|
2020-07-17 12:55:34 +00:00
|
|
|
run: ActionDispatcher<RunActionPayload, [string, string, string]> | {};
|
2020-06-04 13:49:22 +00:00
|
|
|
dynamicBindingPathList: Property[];
|
2020-02-18 10:41:52 +00:00
|
|
|
ENTITY_TYPE: ENTITY_TYPE.ACTION;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 08:09:57 +00:00
|
|
|
export interface DataTreeUrl {
|
|
|
|
|
queryParams: Record<string, string>;
|
|
|
|
|
protocol: string;
|
|
|
|
|
host: string;
|
|
|
|
|
hostname: string;
|
|
|
|
|
port: string;
|
|
|
|
|
pathname: string;
|
|
|
|
|
hash: string;
|
|
|
|
|
href: string;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
export interface DataTreeWidget extends WidgetProps {
|
|
|
|
|
ENTITY_TYPE: ENTITY_TYPE.WIDGET;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 14:24:26 +00:00
|
|
|
export interface DataTreeAppsmith {
|
|
|
|
|
user: AuthUserState;
|
|
|
|
|
URL: UrlDataState;
|
|
|
|
|
ENTITY_TYPE: ENTITY_TYPE.APPSMITH;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 12:44:56 +00:00
|
|
|
export type DataTreeEntity =
|
|
|
|
|
| DataTreeAction
|
|
|
|
|
| DataTreeWidget
|
2020-04-01 08:09:57 +00:00
|
|
|
| DataTreeUrl
|
2020-04-20 05:42:46 +00:00
|
|
|
| PageListPayload
|
2020-08-07 14:24:26 +00:00
|
|
|
| DataTreeAppsmith
|
2020-02-26 12:44:56 +00:00
|
|
|
| ActionDispatcher<any, any>;
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
export type DataTree = {
|
2020-02-26 12:44:56 +00:00
|
|
|
[entityName: string]: DataTreeEntity;
|
2020-02-18 10:41:52 +00:00
|
|
|
} & { actionPaths?: string[] };
|
|
|
|
|
|
2020-03-19 03:25:52 +00:00
|
|
|
type DataTreeSeed = {
|
|
|
|
|
actions: ActionDataState;
|
|
|
|
|
widgets: CanvasWidgetsReduxState;
|
|
|
|
|
widgetsMeta: MetaState;
|
2020-04-20 05:42:46 +00:00
|
|
|
pageList: PageListPayload;
|
2020-08-07 14:24:26 +00:00
|
|
|
url: DataTreeUrl;
|
|
|
|
|
authUser: AuthUserState;
|
2020-03-19 03:25:52 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
export class DataTreeFactory {
|
2020-06-19 13:06:45 +00:00
|
|
|
static create(
|
2020-08-07 14:24:26 +00:00
|
|
|
{ actions, widgets, widgetsMeta, pageList, authUser, url }: DataTreeSeed,
|
2020-06-19 13:06:45 +00:00
|
|
|
// TODO(hetu)
|
|
|
|
|
// temporary fix for not getting functions while normal evals which crashes the app
|
|
|
|
|
// need to remove this after we get a proper solve
|
|
|
|
|
withFunctions?: boolean,
|
|
|
|
|
): DataTree {
|
2020-02-18 10:41:52 +00:00
|
|
|
const dataTree: DataTree = {};
|
2020-06-19 13:06:45 +00:00
|
|
|
const actionPaths = [];
|
2020-03-19 03:25:52 +00:00
|
|
|
actions.forEach(a => {
|
2020-07-03 08:58:58 +00:00
|
|
|
const config = a.config;
|
2020-06-04 13:49:22 +00:00
|
|
|
let dynamicBindingPathList: Property[] = [];
|
|
|
|
|
// update paths
|
|
|
|
|
if (
|
|
|
|
|
config.dynamicBindingPathList &&
|
|
|
|
|
config.dynamicBindingPathList.length
|
|
|
|
|
) {
|
|
|
|
|
dynamicBindingPathList = config.dynamicBindingPathList.map(d => ({
|
|
|
|
|
...d,
|
|
|
|
|
key: `config.${d.key}`,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
dataTree[config.name] = {
|
2020-02-18 10:41:52 +00:00
|
|
|
...a,
|
2020-06-29 07:41:53 +00:00
|
|
|
actionId: config.id,
|
|
|
|
|
config: config.actionConfiguration,
|
2020-06-04 13:49:22 +00:00
|
|
|
dynamicBindingPathList,
|
2020-02-18 10:41:52 +00:00
|
|
|
data: a.data ? a.data.body : {},
|
2020-06-19 13:06:45 +00:00
|
|
|
run: withFunctions
|
2020-07-17 12:55:34 +00:00
|
|
|
? function(
|
|
|
|
|
this: DataTreeAction,
|
|
|
|
|
onSuccess: string,
|
|
|
|
|
onError: string,
|
|
|
|
|
params = "",
|
|
|
|
|
) {
|
2020-06-19 13:06:45 +00:00
|
|
|
return {
|
|
|
|
|
type: "RUN_ACTION",
|
|
|
|
|
payload: {
|
2020-06-29 07:41:53 +00:00
|
|
|
actionId: this.actionId,
|
2020-06-19 13:06:45 +00:00
|
|
|
onSuccess: onSuccess ? `{{${onSuccess.toString()}}}` : "",
|
|
|
|
|
onError: onError ? `{{${onError.toString()}}}` : "",
|
2020-07-17 12:55:34 +00:00
|
|
|
params,
|
2020-06-19 13:06:45 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
: {},
|
2020-02-18 10:41:52 +00:00
|
|
|
ENTITY_TYPE: ENTITY_TYPE.ACTION,
|
|
|
|
|
};
|
2020-06-19 13:06:45 +00:00
|
|
|
if (withFunctions) {
|
|
|
|
|
actionPaths.push(`${config.name}.run`);
|
|
|
|
|
}
|
|
|
|
|
dataTree.actionPaths && dataTree.actionPaths.push();
|
2020-02-18 10:41:52 +00:00
|
|
|
});
|
2020-03-19 03:25:52 +00:00
|
|
|
Object.keys(widgets).forEach(w => {
|
|
|
|
|
const widget = widgets[w];
|
|
|
|
|
const widgetMetaProps = widgetsMeta[w];
|
2020-04-17 16:15:09 +00:00
|
|
|
const defaultMetaProps = WidgetFactory.getWidgetMetaPropertiesMap(
|
|
|
|
|
widget.type,
|
|
|
|
|
);
|
|
|
|
|
const derivedPropertyMap = WidgetFactory.getWidgetDerivedPropertiesMap(
|
|
|
|
|
widget.type,
|
|
|
|
|
);
|
|
|
|
|
const derivedProps: any = {};
|
|
|
|
|
const dynamicBindings = widget.dynamicBindings || {};
|
|
|
|
|
Object.keys(derivedPropertyMap).forEach(propertyName => {
|
|
|
|
|
derivedProps[propertyName] = derivedPropertyMap[propertyName].replace(
|
|
|
|
|
/this./g,
|
|
|
|
|
`${widget.widgetName}.`,
|
|
|
|
|
);
|
|
|
|
|
dynamicBindings[propertyName] = true;
|
|
|
|
|
});
|
2020-02-18 10:41:52 +00:00
|
|
|
dataTree[widget.widgetName] = {
|
|
|
|
|
...widget,
|
2020-04-17 16:15:09 +00:00
|
|
|
...defaultMetaProps,
|
2020-02-18 10:41:52 +00:00
|
|
|
...widgetMetaProps,
|
2020-04-17 16:15:09 +00:00
|
|
|
...derivedProps,
|
|
|
|
|
dynamicBindings,
|
2020-02-18 10:41:52 +00:00
|
|
|
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
|
|
|
|
|
};
|
|
|
|
|
});
|
2020-06-19 13:06:45 +00:00
|
|
|
|
|
|
|
|
if (withFunctions) {
|
|
|
|
|
dataTree.navigateTo = function(pageNameOrUrl: string, params: object) {
|
|
|
|
|
return {
|
|
|
|
|
type: "NAVIGATE_TO",
|
|
|
|
|
payload: { pageNameOrUrl, params },
|
|
|
|
|
};
|
2020-02-18 10:41:52 +00:00
|
|
|
};
|
2020-06-19 13:06:45 +00:00
|
|
|
actionPaths.push("navigateTo");
|
2020-02-18 10:41:52 +00:00
|
|
|
|
2020-06-19 13:06:45 +00:00
|
|
|
dataTree.showAlert = function(message: string, style: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "SHOW_ALERT",
|
|
|
|
|
payload: { message, style },
|
|
|
|
|
};
|
2020-02-18 10:41:52 +00:00
|
|
|
};
|
2020-06-19 13:06:45 +00:00
|
|
|
actionPaths.push("showAlert");
|
2020-02-18 10:41:52 +00:00
|
|
|
|
2020-06-19 13:06:45 +00:00
|
|
|
// dataTree.url = url;
|
|
|
|
|
dataTree.showModal = function(modalName: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "SHOW_MODAL_BY_NAME",
|
|
|
|
|
payload: { modalName },
|
|
|
|
|
};
|
2020-03-27 09:02:11 +00:00
|
|
|
};
|
2020-06-19 13:06:45 +00:00
|
|
|
actionPaths.push("showModal");
|
|
|
|
|
|
|
|
|
|
dataTree.closeModal = function(modalName: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "CLOSE_MODAL",
|
|
|
|
|
payload: { modalName },
|
|
|
|
|
};
|
2020-03-27 09:02:11 +00:00
|
|
|
};
|
2020-06-19 13:06:45 +00:00
|
|
|
actionPaths.push("closeModal");
|
|
|
|
|
}
|
2020-03-27 09:02:11 +00:00
|
|
|
|
2020-04-20 05:42:46 +00:00
|
|
|
dataTree.pageList = pageList;
|
2020-06-19 13:06:45 +00:00
|
|
|
dataTree.actionPaths = actionPaths;
|
2020-08-07 14:24:26 +00:00
|
|
|
dataTree.appsmith = {
|
|
|
|
|
ENTITY_TYPE: ENTITY_TYPE.APPSMITH,
|
|
|
|
|
user: authUser,
|
|
|
|
|
URL: url,
|
|
|
|
|
};
|
2020-02-18 10:41:52 +00:00
|
|
|
return dataTree;
|
|
|
|
|
}
|
|
|
|
|
}
|