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

194 lines
4.5 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 { Component } from "react";
import { BaseStyle } from "../editorComponents/BaseComponent";
import _ from "lodash";
import React from "react";
import DraggableComponent from "../editorComponents/DraggableComponent";
import { ActionPayload } from "../constants/ActionConstants";
abstract class BaseWidget<
T extends WidgetProps & WidgetFunctions,
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 = {
height: 0,
2019-09-09 09:08:54 +00:00
width: 0,
};
initialState.height = 0;
initialState.width = 0;
this.state = initialState as K;
}
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 = {
width: (rightColumn - leftColumn) * parentColumnSpace,
2019-09-09 09:08:54 +00:00
height: (bottomRow - topRow) * parentRowSpace,
};
if (
_.isNil(this.state) ||
widgetState.height !== this.state.height ||
widgetState.width !== this.state.width
) {
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.COMPONENT_PANE:
2019-09-09 09:08:54 +00:00
return this.getComponentPaneView();
case RenderModes.PAGE:
2019-09-09 09:08:54 +00:00
return this.getPageView();
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 {
2019-09-09 09:08:54 +00:00
return this.getPageView();
}
getComponentPaneView(): JSX.Element {
2019-04-01 07:08:00 +00:00
return (
<DraggableComponent
2019-04-01 07:08:00 +00:00
{...this.props}
style={{
2019-09-09 09:08:54 +00:00
...this.getPositionStyle(),
2019-04-01 07:08:00 +00:00
}}
orientation={"VERTICAL"}
>
{this.getPageView()}
</DraggableComponent>
2019-09-09 09:08:54 +00:00
);
}
2019-09-09 09:08:54 +00:00
abstract getWidgetType(): WidgetType;
2019-03-19 14:05:48 +00:00
getPositionStyle(): BaseStyle {
return {
2019-09-21 01:52:38 +00:00
positionType: "ABSOLUTE",
2019-03-19 15:21:27 +00:00
height: this.state.height,
width: this.state.width,
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,
};
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 {
2019-08-29 11:22:09 +00:00
height: number;
width: number;
}
2019-08-26 12:41:21 +00:00
export interface DraggableWidget {
2019-08-29 11:22:09 +00:00
type: string;
2019-09-09 09:08:54 +00:00
widget: WidgetProps;
2019-08-29 11:22:09 +00:00
key: string;
2019-08-26 12:41:21 +00:00
}
2019-09-09 09:08:54 +00:00
export interface WidgetBuilder<T extends WidgetProps> {
buildWidget(widgetProps: T): JSX.Element;
}
export interface WidgetProps extends WidgetFunctions, WidgetDataProps {
key?: string;
renderMode: RenderMode;
}
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;
}
export interface WidgetFunctions {
executeAction: (actionPayloads?: ActionPayload[]) => void;
updateWidget?: Function;
}
export interface WidgetCardProps {
type: WidgetType;
2019-08-29 11:22:09 +00:00
key?: string;
label: string;
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;