PromucFlow_constructor/app/client/src/widgets/BaseWidget.tsx

227 lines
5.9 KiB
TypeScript
Raw Normal View History

/***
* Widget are responsible for accepting the abstraction layer inputs, interpretting them into rederable props and
* spawing components based on those props
* Widgets are also responsible for dispatching actions and updating the state tree
*/
import {
WidgetType,
RenderMode,
2019-03-19 14:05:48 +00:00
RenderModes,
2019-09-09 09:08:54 +00:00
CSSUnits,
} from "../constants/WidgetConstants";
import React, { Component } from "react";
2019-11-04 10:57:19 +00:00
import { BaseStyle } from "../components/designSystems/appsmith/BaseComponent";
2019-09-09 09:08:54 +00:00
import _ from "lodash";
2019-11-04 10:57:19 +00:00
import DraggableComponent from "../components/editorComponents/DraggableComponent";
import ResizableComponent from "../components/editorComponents/ResizableComponent";
import { ActionPayload } from "../constants/ActionConstants";
2019-10-18 08:16:26 +00:00
import { WidgetFunctionsContext } from "../pages/Editor/WidgetsEditor";
2019-11-04 14:22:50 +00:00
import shallowequal from "shallowequal";
abstract class BaseWidget<
T extends WidgetProps,
2019-09-09 09:08:54 +00:00
K extends WidgetState
> extends Component<T, K> {
constructor(props: T) {
2019-09-09 09:08:54 +00:00
super(props);
const initialState: WidgetState = {
componentHeight: 0,
componentWidth: 0,
2019-09-09 09:08:54 +00:00
};
initialState.componentHeight = 0;
initialState.componentWidth = 0;
2019-09-09 09:08:54 +00:00
this.state = initialState as K;
}
static contextType = WidgetFunctionsContext;
executeAction(actionPayloads?: ActionPayload[]): void {
const { executeAction } = this.context;
executeAction && executeAction(actionPayloads);
}
2019-11-07 11:17:53 +00:00
updateWidgetProperty(
widgetId: string,
propertyName: string,
propertyValue: any,
): void {
const { updateWidgetProperty } = this.context;
updateWidgetProperty &&
updateWidgetProperty(widgetId, propertyName, propertyValue);
}
componentDidMount(): void {
this.calculateWidgetBounds(
this.props.rightColumn,
this.props.leftColumn,
this.props.topRow,
this.props.bottomRow,
this.props.parentColumnSpace,
2019-09-09 09:08:54 +00:00
this.props.parentRowSpace,
);
}
2019-09-09 09:08:54 +00:00
//eslint-disable-next-line @typescript-eslint/no-unused-vars
componentDidUpdate(prevProps: T) {
this.calculateWidgetBounds(
this.props.rightColumn,
this.props.leftColumn,
this.props.topRow,
this.props.bottomRow,
this.props.parentColumnSpace,
2019-09-09 09:08:54 +00:00
this.props.parentRowSpace,
);
}
calculateWidgetBounds(
rightColumn: number,
leftColumn: number,
topRow: number,
bottomRow: number,
parentColumnSpace: number,
2019-09-09 09:08:54 +00:00
parentRowSpace: number,
) {
2019-09-09 09:08:54 +00:00
const widgetState: WidgetState = {
componentWidth: (rightColumn - leftColumn) * parentColumnSpace,
componentHeight: (bottomRow - topRow) * parentRowSpace,
2019-09-09 09:08:54 +00:00
};
if (
_.isNil(this.state) ||
widgetState.componentHeight !== this.state.componentHeight ||
widgetState.componentWidth !== this.state.componentWidth
) {
2019-09-09 09:08:54 +00:00
this.setState(widgetState);
}
}
render() {
2019-09-09 09:08:54 +00:00
return this.getWidgetView();
}
getWidgetView(): JSX.Element {
switch (this.props.renderMode) {
case RenderModes.CANVAS:
2019-09-09 09:08:54 +00:00
return this.getCanvasView();
case RenderModes.PAGE:
2019-11-06 12:12:41 +00:00
if (this.props.isVisible) return this.getPageView();
else return <div />;
default:
2019-09-09 09:08:54 +00:00
return this.getPageView();
}
}
2019-09-09 09:08:54 +00:00
abstract getPageView(): JSX.Element;
getCanvasView(): JSX.Element {
const style = this.getPositionStyle();
if (!this.props.parentId) {
return this.getPageView();
} else {
return (
<DraggableComponent
{...this.props}
style={{ ...style }}
orientation={"VERTICAL"}
>
2019-11-06 12:12:41 +00:00
<ResizableComponent
style={{ ...style, opacity: this.props.isVisible ? 1 : 0.4 }}
{...this.props}
>
{this.getPageView()}
</ResizableComponent>
</DraggableComponent>
);
}
}
2019-11-04 14:22:50 +00:00
shouldComponentUpdate(nextProps: WidgetProps, nextState: WidgetState) {
const isNotEqual =
!shallowequal(nextProps, this.props) ||
!shallowequal(nextState, this.state);
return isNotEqual;
}
2019-09-09 09:08:54 +00:00
abstract getWidgetType(): WidgetType;
2019-03-19 14:05:48 +00:00
getPositionStyle(): BaseStyle {
return {
positionType: "ABSOLUTE",
componentHeight: this.state.componentHeight,
componentWidth: this.state.componentWidth,
2019-03-19 14:05:48 +00:00
yPosition: this.props.topRow * this.props.parentRowSpace,
xPosition: this.props.leftColumn * this.props.parentColumnSpace,
xPositionUnit: CSSUnits.PIXEL,
2019-09-09 09:08:54 +00:00
yPositionUnit: CSSUnits.PIXEL,
backgroundColor: this.props.backgroundColor,
2019-09-09 09:08:54 +00:00
};
2019-03-19 14:05:48 +00:00
}
2019-09-09 09:08:54 +00:00
static defaultProps: Partial<WidgetProps> = {
parentRowSpace: 1,
parentColumnSpace: 1,
topRow: 0,
2019-09-09 09:08:54 +00:00
leftColumn: 0,
};
}
2019-09-09 09:08:54 +00:00
export interface WidgetState {
componentHeight: number;
componentWidth: number;
}
2019-09-09 09:08:54 +00:00
export interface WidgetBuilder<T extends WidgetProps> {
buildWidget(widgetProps: T): JSX.Element;
}
export interface WidgetProps extends WidgetDataProps {
key?: string;
renderMode: RenderMode;
2019-11-06 06:35:15 +00:00
dynamicBindings?: Record<string, string>;
[key: string]: any;
}
export interface WidgetDataProps {
2019-09-12 08:11:25 +00:00
widgetId: string;
type: WidgetType;
2019-09-12 08:11:25 +00:00
widgetName: string;
2019-08-29 11:22:09 +00:00
topRow: number;
leftColumn: number;
bottomRow: number;
rightColumn: number;
parentColumnSpace: number;
parentRowSpace: number;
isVisible?: boolean;
parentId?: string;
backgroundColor?: string;
}
export interface WidgetFunctions {
executeAction?: (actionPayloads?: ActionPayload[]) => void;
updateWidget?: Function;
2019-10-31 05:28:11 +00:00
updateWidgetProperty?: (
widgetId: string,
propertyName: string,
propertyValue: any,
) => void;
}
export interface WidgetCardProps {
type: WidgetType;
2019-08-29 11:22:09 +00:00
key?: string;
2019-09-26 11:37:09 +00:00
widgetCardName: string;
2019-08-29 11:22:09 +00:00
icon: string;
2019-08-21 12:49:16 +00:00
}
export const WidgetOperations = {
// WidgetActivities?
MOVE: "MOVE",
RESIZE: "RESIZE",
ADD_CHILD: "ADD_CHILD",
REMOVE_CHILD: "REMOVE_CHILD",
UPDATE_PROPERTY: "UPDATE_PROPERTY",
DELETE: "DELETE",
};
export type WidgetOperation = (typeof WidgetOperations)[keyof typeof WidgetOperations];
2019-09-09 09:08:54 +00:00
export default BaseWidget;