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

88 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-03-13 15:05:24 +00:00
import BaseWidget, { IWidgetProps, IWidgetState } from "./BaseWidget";
import ContainerComponent, {
IContainerProps
2019-03-13 15:05:24 +00:00
} from "../editorComponents/ContainerComponent";
import {
ContainerOrientation,
WidgetType,
CSSUnits
} from "../constants/WidgetConstants";
import WidgetFactory from "../utils/WidgetFactory";
import React from "react";
import _ from "lodash";
2019-03-13 15:05:24 +00:00
const DEFAULT_NUM_COLS = 13;
const DEFAULT_NUM_ROWS = 13;
class ContainerWidget extends BaseWidget<
IContainerWidgetProps<IWidgetProps>,
IWidgetState
> {
2019-03-13 15:05:24 +00:00
snapColumnSpace: number = 100;
snapRowSpace: number = 100;
constructor(props: IContainerWidgetProps<IWidgetProps>) {
2019-03-13 15:05:24 +00:00
super(props);
this.renderChildWidget = this.renderChildWidget.bind(this);
this.state = {
height: 0,
width: 0
2019-03-13 15:05:24 +00:00
};
}
componentWillReceiveProps(
previousProps: IContainerWidgetProps<IWidgetProps>,
nextProps: IContainerWidgetProps<IWidgetProps>
) {
2019-03-13 15:05:24 +00:00
super.componentWillReceiveProps(previousProps, nextProps);
this.snapColumnSpace =
2019-03-13 15:05:24 +00:00
this.state.width / (nextProps.snapColumns || DEFAULT_NUM_COLS);
this.snapRowSpace =
2019-03-13 15:05:24 +00:00
this.state.height / (nextProps.snapRows || DEFAULT_NUM_ROWS);
}
renderChildWidget(childWidgetData: IWidgetProps) {
2019-03-13 15:05:24 +00:00
childWidgetData.parentColumnSpace = this.snapColumnSpace;
childWidgetData.parentRowSpace = this.snapRowSpace;
return WidgetFactory.createWidget(childWidgetData);
}
getWidgetView() {
return (
<ContainerComponent
widgetId={this.props.widgetId}
style={{
height: this.state.height,
width: this.state.width,
positionType: "ABSOLUTE",
yPosition: this.props.topRow * this.props.parentRowSpace,
xPosition: this.props.leftColumn * this.props.parentColumnSpace,
xPositionUnit: CSSUnits.PIXEL,
yPositionUnit: CSSUnits.PIXEL
}}
snapColumnSpace={this.snapColumnSpace}
snapRowSpace={this.snapRowSpace}
snapColumns={this.props.snapColumns || DEFAULT_NUM_COLS}
snapRows={this.props.snapRows || DEFAULT_NUM_ROWS}
orientation={this.props.orientation || "VERTICAL"}
>
{_.map(this.props.children, this.renderChildWidget)}
</ContainerComponent>
2019-03-13 15:05:24 +00:00
);
}
getWidgetType(): WidgetType {
2019-03-13 15:05:24 +00:00
return "CONTAINER_WIDGET";
}
}
export interface IContainerWidgetProps<T extends IWidgetProps>
extends IWidgetProps {
2019-03-13 15:05:24 +00:00
children?: T[];
snapColumns?: number;
snapRows?: number;
orientation?: ContainerOrientation;
}
2019-03-13 15:05:24 +00:00
export default ContainerWidget;