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

174 lines
4.0 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";
abstract class BaseWidget<
2019-09-09 09:08:54 +00:00
T extends WidgetProps,
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 {
positionType:
this.props.renderMode !== RenderModes.PAGE
? "CONTAINER_DIRECTION"
: "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> = {
2019-08-29 11:22:09 +00:00
parentRowSpace: 64,
parentColumnSpace: 64,
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> {
2019-08-29 11:22:09 +00:00
buildWidget(data: T): JSX.Element;
}
2019-09-09 09:08:54 +00:00
export interface WidgetProps {
2019-08-29 11:22:09 +00:00
widgetType: WidgetType;
key?: string;
widgetId: string;
topRow: number;
leftColumn: number;
bottomRow: number;
rightColumn: number;
parentColumnSpace: number;
parentRowSpace: number;
renderMode: RenderMode;
}
export interface WidgetCardProps {
2019-08-29 11:22:09 +00:00
widgetType: WidgetType;
key?: string;
label: string;
icon: string;
2019-08-21 12:49:16 +00:00
}
2019-09-09 09:08:54 +00:00
export default BaseWidget;