2019-09-09 09:08:54 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
|
|
|
|
import ContainerComponent from "../editorComponents/ContainerComponent";
|
|
|
|
|
import { ContainerOrientation, WidgetType } from "../constants/WidgetConstants";
|
|
|
|
|
import WidgetFactory from "../utils/WidgetFactory";
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { Color } from "../constants/DefaultTheme";
|
|
|
|
|
import DroppableComponent from "../editorComponents/DroppableComponent";
|
2019-02-11 18:22:23 +00:00
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
const DEFAULT_NUM_COLS = 16;
|
|
|
|
|
const DEFAULT_NUM_ROWS = 16;
|
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-03-19 14:47:18 +00:00
|
|
|
width: 0,
|
2019-04-02 16:12:08 +00:00
|
|
|
height: 0,
|
2019-08-29 11:22:09 +00:00
|
|
|
snapColumnSpace: DEFAULT_NUM_COLS,
|
2019-09-09 09:08:54 +00:00
|
|
|
snapRowSpace: DEFAULT_NUM_ROWS,
|
|
|
|
|
};
|
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;
|
|
|
|
|
let snapRowSpace = this.state.snapRowSpace;
|
2019-03-19 15:21:27 +00:00
|
|
|
if (this.state.width)
|
2019-04-02 16:12:08 +00:00
|
|
|
snapColumnSpace =
|
2019-09-09 09:08:54 +00:00
|
|
|
this.state.width / (this.props.snapColumns || DEFAULT_NUM_COLS);
|
2019-03-19 15:21:27 +00:00
|
|
|
if (this.state.height)
|
2019-04-02 16:12:08 +00:00
|
|
|
snapRowSpace =
|
2019-09-09 09:08:54 +00:00
|
|
|
this.state.height / (this.props.snapRows || DEFAULT_NUM_ROWS);
|
2019-04-02 16:12:08 +00:00
|
|
|
if (
|
|
|
|
|
this.state.snapColumnSpace !== snapColumnSpace ||
|
|
|
|
|
this.state.snapRowSpace !== snapRowSpace
|
|
|
|
|
) {
|
|
|
|
|
this.setState({
|
|
|
|
|
snapColumnSpace: snapColumnSpace,
|
2019-09-09 09:08:54 +00:00
|
|
|
snapRowSpace: snapRowSpace,
|
|
|
|
|
});
|
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;
|
|
|
|
|
return WidgetFactory.createWidget(childWidgetData);
|
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
|
|
|
}}
|
|
|
|
|
orientation={this.props.orientation || "VERTICAL"}
|
|
|
|
|
>
|
|
|
|
|
{_.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-04-02 16:12:08 +00:00
|
|
|
getCanvasView() {
|
|
|
|
|
return (
|
|
|
|
|
<DroppableComponent
|
|
|
|
|
{...this.props}
|
|
|
|
|
style={{
|
2019-09-09 09:08:54 +00:00
|
|
|
...this.getPositionStyle(),
|
2019-04-02 16:12:08 +00:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{super.getCanvasView()}
|
|
|
|
|
</DroppableComponent>
|
2019-09-09 09:08:54 +00:00
|
|
|
);
|
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-09 09:08:54 +00:00
|
|
|
export default ContainerWidget;
|