2019-08-29 11:22:09 +00:00
|
|
|
import React from "react"
|
2019-03-19 15:21:27 +00:00
|
|
|
import BaseWidget, { IWidgetProps, IWidgetState } from "./BaseWidget"
|
2019-08-29 11:22:09 +00:00
|
|
|
import ContainerComponent from "../editorComponents/ContainerComponent"
|
2019-03-13 15:05:24 +00:00
|
|
|
import {
|
|
|
|
|
ContainerOrientation,
|
|
|
|
|
WidgetType,
|
2019-03-19 15:21:27 +00:00
|
|
|
} from "../constants/WidgetConstants"
|
|
|
|
|
import WidgetFactory from "../utils/WidgetFactory"
|
|
|
|
|
import _ from "lodash"
|
|
|
|
|
import { Color } from "../constants/StyleConstants"
|
2019-04-02 16:12:08 +00:00
|
|
|
import DroppableComponent from "../editorComponents/DroppableComponent"
|
2019-02-11 18:22:23 +00:00
|
|
|
|
2019-08-29 11:22:09 +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-08-29 11:22:09 +00:00
|
|
|
ContainerWidgetProps<IWidgetProps>,
|
|
|
|
|
ContainerWidgetState
|
2019-02-10 13:06:05 +00:00
|
|
|
> {
|
2019-08-29 11:22:09 +00:00
|
|
|
constructor(props: ContainerWidgetProps<IWidgetProps>) {
|
2019-03-19 15:21:27 +00:00
|
|
|
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,
|
|
|
|
|
snapRowSpace: DEFAULT_NUM_ROWS
|
2019-03-19 14:47:18 +00:00
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
componentDidUpdate(previousProps: ContainerWidgetProps<IWidgetProps>) {
|
2019-03-19 15:21:27 +00:00
|
|
|
super.componentDidUpdate(previousProps)
|
2019-04-02 16:12:08 +00:00
|
|
|
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-03-19 15:21:27 +00:00
|
|
|
this.state.width / (this.props.snapColumns || DEFAULT_NUM_COLS)
|
|
|
|
|
if (this.state.height)
|
2019-04-02 16:12:08 +00:00
|
|
|
snapRowSpace =
|
2019-03-19 15:21:27 +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,
|
|
|
|
|
snapRowSpace: snapRowSpace
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderChildWidget(childWidgetData: IWidgetProps) {
|
2019-04-02 16:12:08 +00:00
|
|
|
childWidgetData.parentColumnSpace = this.state.snapColumnSpace
|
|
|
|
|
childWidgetData.parentRowSpace = this.state.snapRowSpace
|
2019-03-19 15:21:27 +00:00
|
|
|
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-03-19 14:05:48 +00:00
|
|
|
...this.getPositionStyle(),
|
2019-03-19 15:21:27 +00:00
|
|
|
backgroundColor: this.props.backgroundColor
|
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-03-19 15:21:27 +00:00
|
|
|
)
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-02 16:12:08 +00:00
|
|
|
getCanvasView() {
|
|
|
|
|
return (
|
|
|
|
|
<DroppableComponent
|
|
|
|
|
{...this.props}
|
|
|
|
|
style={{
|
|
|
|
|
...this.getPositionStyle()
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{super.getCanvasView()}
|
|
|
|
|
</DroppableComponent>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-10 13:06:05 +00:00
|
|
|
getWidgetType(): WidgetType {
|
2019-03-19 15:21:27 +00:00
|
|
|
return "CONTAINER_WIDGET"
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
export interface ContainerWidgetState extends IWidgetState {
|
|
|
|
|
snapColumnSpace: number;
|
|
|
|
|
snapRowSpace: number;
|
2019-04-02 16:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
export interface ContainerWidgetProps<T extends IWidgetProps>
|
2019-02-11 18:22:23 +00:00
|
|
|
extends IWidgetProps {
|
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-03-19 15:21:27 +00:00
|
|
|
export default ContainerWidget
|