2019-09-09 09:08:54 +00:00
|
|
|
import React from "react";
|
2019-09-17 10:11:50 +00:00
|
|
|
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";
|
2019-09-22 20:25:05 +00:00
|
|
|
import { Color } from "../constants/Colors";
|
2019-09-17 10:09:00 +00:00
|
|
|
import DropTargetComponent from "../editorComponents/DropTargetComponent";
|
2019-09-22 20:25:05 +00:00
|
|
|
import { GridDefaults } from "../constants/WidgetConstants";
|
2019-09-25 18:33:56 +00:00
|
|
|
import DraggableComponent from "../editorComponents/DraggableComponent";
|
|
|
|
|
import ResizableComponent from "../editorComponents/ResizableComponent";
|
2019-02-11 18:22:23 +00:00
|
|
|
|
2019-09-30 03:25:14 +00:00
|
|
|
const { DEFAULT_GRID_COLUMNS, DEFAULT_GRID_ROW_HEIGHT } = GridDefaults;
|
2019-02-10 13:06:05 +00:00
|
|
|
|
|
|
|
|
class ContainerWidget extends BaseWidget<
|
2019-09-09 09:08:54 +00:00
|
|
|
ContainerWidgetProps<WidgetProps>,
|
2019-08-29 11:22:09 +00:00
|
|
|
ContainerWidgetState
|
2019-02-10 13:06:05 +00:00
|
|
|
> {
|
2019-09-09 09:08:54 +00:00
|
|
|
constructor(props: ContainerWidgetProps<WidgetProps>) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.renderChildWidget = this.renderChildWidget.bind(this);
|
2019-02-11 18:22:23 +00:00
|
|
|
this.state = {
|
2019-09-25 17:24:23 +00:00
|
|
|
componentWidth: 0,
|
|
|
|
|
componentHeight: 0,
|
|
|
|
|
snapColumnSpace: 0,
|
|
|
|
|
snapRowSpace: 0,
|
2019-09-09 09:08:54 +00:00
|
|
|
};
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
componentDidUpdate(previousProps: ContainerWidgetProps<WidgetProps>) {
|
|
|
|
|
super.componentDidUpdate(previousProps);
|
|
|
|
|
let snapColumnSpace = this.state.snapColumnSpace;
|
2019-09-25 17:24:23 +00:00
|
|
|
if (this.state.componentWidth)
|
2019-10-02 18:13:04 +00:00
|
|
|
snapColumnSpace = Math.floor(
|
2019-09-25 17:24:23 +00:00
|
|
|
this.state.componentWidth /
|
2019-10-02 18:13:04 +00:00
|
|
|
(this.props.snapColumns || DEFAULT_GRID_COLUMNS),
|
|
|
|
|
);
|
2019-09-30 03:25:14 +00:00
|
|
|
if (this.state.snapColumnSpace !== snapColumnSpace) {
|
2019-04-02 16:12:08 +00:00
|
|
|
this.setState({
|
2019-09-25 17:24:23 +00:00
|
|
|
snapColumnSpace,
|
2019-09-30 03:25:14 +00:00
|
|
|
snapRowSpace: DEFAULT_GRID_ROW_HEIGHT,
|
2019-09-09 09:08:54 +00:00
|
|
|
});
|
2019-04-02 16:12:08 +00:00
|
|
|
}
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
renderChildWidget(childWidgetData: WidgetProps) {
|
|
|
|
|
childWidgetData.parentColumnSpace = this.state.snapColumnSpace;
|
|
|
|
|
childWidgetData.parentRowSpace = this.state.snapRowSpace;
|
2019-09-25 18:33:56 +00:00
|
|
|
childWidgetData.parentId = this.props.widgetId;
|
2019-09-17 10:11:50 +00:00
|
|
|
const widgetFunctions: WidgetFunctions = this.props as WidgetFunctions;
|
|
|
|
|
return WidgetFactory.createWidget(
|
|
|
|
|
childWidgetData,
|
|
|
|
|
widgetFunctions,
|
|
|
|
|
this.props.renderMode,
|
|
|
|
|
);
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-18 15:10:30 +00:00
|
|
|
getPageView() {
|
2019-02-10 13:06:05 +00:00
|
|
|
return (
|
2019-02-11 18:22:23 +00:00
|
|
|
<ContainerComponent
|
|
|
|
|
widgetId={this.props.widgetId}
|
|
|
|
|
style={{
|
2019-09-09 09:08:54 +00:00
|
|
|
...this.getPositionStyle(),
|
2019-02-11 18:22:23 +00:00
|
|
|
}}
|
2019-10-01 20:07:43 +00:00
|
|
|
isRoot={!this.props.parentId}
|
2019-02-11 18:22:23 +00:00
|
|
|
orientation={this.props.orientation || "VERTICAL"}
|
2019-10-02 18:13:04 +00:00
|
|
|
widgetName={this.props.widgetName}
|
2019-02-11 18:22:23 +00:00
|
|
|
>
|
|
|
|
|
{_.map(this.props.children, this.renderChildWidget)}
|
2019-02-10 13:06:05 +00:00
|
|
|
</ContainerComponent>
|
2019-09-09 09:08:54 +00:00
|
|
|
);
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 03:25:14 +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,
|
2019-10-02 21:14:58 +00:00
|
|
|
parentId: this.props.widgetId,
|
2019-09-25 21:21:04 +00:00
|
|
|
left: child.leftColumn,
|
|
|
|
|
top: child.topRow,
|
|
|
|
|
bottom: child.bottomRow,
|
|
|
|
|
right: child.rightColumn,
|
|
|
|
|
}))
|
|
|
|
|
: null;
|
2019-09-30 03:25:14 +00:00
|
|
|
}
|
|
|
|
|
getCanvasView() {
|
|
|
|
|
const style = this.getPositionStyle();
|
|
|
|
|
const occupiedSpaces = this.getOccupiedSpaces();
|
2019-10-03 15:14:50 +00:00
|
|
|
const renderComponent = (
|
2019-09-17 10:09:00 +00:00
|
|
|
<DropTargetComponent
|
2019-04-02 16:12:08 +00:00
|
|
|
{...this.props}
|
2019-09-25 17:24:23 +00:00
|
|
|
{...this.state}
|
2019-09-25 21:21:04 +00:00
|
|
|
occupiedSpaces={occupiedSpaces}
|
2019-04-02 16:12:08 +00:00
|
|
|
style={{
|
2019-09-25 18:33:56 +00:00
|
|
|
...style,
|
2019-04-02 16:12:08 +00:00
|
|
|
}}
|
2019-10-03 15:14:50 +00:00
|
|
|
isRoot={!this.props.parentId}
|
2019-04-02 16:12:08 +00:00
|
|
|
>
|
2019-10-03 15:14:50 +00:00
|
|
|
{this.getPageView()}
|
2019-09-17 10:09:00 +00:00
|
|
|
</DropTargetComponent>
|
2019-09-09 09:08:54 +00:00
|
|
|
);
|
2019-10-03 15:14:50 +00:00
|
|
|
const renderDraggableComponent = (
|
|
|
|
|
<DraggableComponent
|
|
|
|
|
style={{ ...style }}
|
|
|
|
|
{...this.props}
|
|
|
|
|
orientation={"VERTICAL"}
|
|
|
|
|
>
|
|
|
|
|
<ResizableComponent style={{ ...style }} {...this.props}>
|
|
|
|
|
{renderComponent}
|
|
|
|
|
</ResizableComponent>
|
|
|
|
|
</DraggableComponent>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return this.props.parentId ? renderDraggableComponent : renderComponent;
|
2019-04-02 16:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-10 13:06:05 +00:00
|
|
|
getWidgetType(): WidgetType {
|
2019-09-09 09:08:54 +00:00
|
|
|
return "CONTAINER_WIDGET";
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-04-02 16:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
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-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 21:21:04 +00:00
|
|
|
export type OccupiedSpace = {
|
|
|
|
|
left: number;
|
|
|
|
|
right: number;
|
|
|
|
|
top: number;
|
|
|
|
|
bottom: number;
|
|
|
|
|
id: string;
|
2019-10-02 21:14:58 +00:00
|
|
|
parentId?: string;
|
2019-09-25 21:21:04 +00:00
|
|
|
};
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export default ContainerWidget;
|