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

144 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-09-09 09:08:54 +00:00
import React from "react";
import BaseWidget, {
WidgetProps,
WidgetState,
WidgetFunctions,
} from "./BaseWidget";
2019-09-09 09:08:54 +00:00
import ContainerComponent from "../editorComponents/ContainerComponent";
import { ContainerOrientation, WidgetType } from "../constants/WidgetConstants";
import WidgetFactory from "../utils/WidgetFactory";
import _ from "lodash";
import { Color } from "../constants/Colors";
import DropTargetComponent from "../editorComponents/DropTargetComponent";
import { GridDefaults } from "../constants/WidgetConstants";
import DraggableComponent from "../editorComponents/DraggableComponent";
import ResizableComponent from "../editorComponents/ResizableComponent";
const { DEFAULT_GRID_COLUMNS, DEFAULT_GRID_ROW_HEIGHT } = GridDefaults;
class ContainerWidget extends BaseWidget<
2019-09-09 09:08:54 +00:00
ContainerWidgetProps<WidgetProps>,
2019-08-29 11:22:09 +00:00
ContainerWidgetState
> {
2019-09-09 09:08:54 +00:00
constructor(props: ContainerWidgetProps<WidgetProps>) {
super(props);
this.renderChildWidget = this.renderChildWidget.bind(this);
this.state = {
componentWidth: 0,
componentHeight: 0,
snapColumnSpace: 0,
snapRowSpace: 0,
2019-09-09 09:08:54 +00:00
};
}
2019-09-09 09:08:54 +00:00
componentDidUpdate(previousProps: ContainerWidgetProps<WidgetProps>) {
super.componentDidUpdate(previousProps);
let snapColumnSpace = this.state.snapColumnSpace;
if (this.state.componentWidth)
2019-09-26 11:11:28 +00:00
snapColumnSpace =
this.state.componentWidth /
2019-09-26 11:11:28 +00:00
(this.props.snapColumns || DEFAULT_GRID_COLUMNS);
if (this.state.snapColumnSpace !== snapColumnSpace) {
this.setState({
snapColumnSpace,
snapRowSpace: DEFAULT_GRID_ROW_HEIGHT,
2019-09-09 09:08:54 +00:00
});
}
}
2019-09-09 09:08:54 +00:00
renderChildWidget(childWidgetData: WidgetProps) {
childWidgetData.parentColumnSpace = this.state.snapColumnSpace;
childWidgetData.parentRowSpace = this.state.snapRowSpace;
childWidgetData.parentId = this.props.widgetId;
const widgetFunctions: WidgetFunctions = this.props as WidgetFunctions;
return WidgetFactory.createWidget(
childWidgetData,
widgetFunctions,
this.props.renderMode,
);
}
getPageView() {
return (
<ContainerComponent
widgetId={this.props.widgetId}
style={{
2019-09-09 09:08:54 +00:00
...this.getPositionStyle(),
}}
orientation={this.props.orientation || "VERTICAL"}
>
{_.map(this.props.children, this.renderChildWidget)}
</ContainerComponent>
2019-09-09 09:08:54 +00:00
);
}
getOccupiedSpaces(): OccupiedSpace[] | null {
return this.props.children
2019-09-25 21:21:04 +00:00
? this.props.children.map(child => ({
id: child.widgetId,
left: child.leftColumn,
top: child.topRow,
bottom: child.bottomRow,
right: child.rightColumn,
}))
: null;
}
getCanvasView() {
const style = this.getPositionStyle();
const occupiedSpaces = this.getOccupiedSpaces();
const renderDraggableComponent = (
<DraggableComponent
style={{ ...style, xPosition: 0, yPosition: 0 }}
{...this.props}
orientation={"VERTICAL"}
>
<ResizableComponent style={{ ...style }} {...this.props}>
{this.getPageView()}
</ResizableComponent>
</DraggableComponent>
);
return (
<DropTargetComponent
{...this.props}
{...this.state}
2019-09-25 21:21:04 +00:00
occupiedSpaces={occupiedSpaces}
style={{
...style,
}}
>
{this.props.parentId ? renderDraggableComponent : this.getPageView()}
</DropTargetComponent>
2019-09-09 09:08:54 +00:00
);
}
getWidgetType(): WidgetType {
2019-09-09 09:08:54 +00:00
return "CONTAINER_WIDGET";
}
}
2019-09-09 09:08:54 +00:00
export interface ContainerWidgetState extends WidgetState {
2019-08-29 11:22:09 +00:00
snapColumnSpace: number;
snapRowSpace: number;
}
2019-09-09 09:08:54 +00:00
export interface ContainerWidgetProps<T extends WidgetProps>
extends WidgetProps {
2019-08-29 11:22:09 +00:00
children?: T[];
snapColumns?: number;
snapRows?: number;
orientation?: ContainerOrientation;
backgroundColor?: Color;
}
2019-09-25 21:21:04 +00:00
export type OccupiedSpace = {
left: number;
right: number;
top: number;
bottom: number;
id: string;
};
2019-09-09 09:08:54 +00:00
export default ContainerWidget;