2019-11-13 07:00:25 +00:00
|
|
|
import React from "react";
|
2019-09-09 09:08:54 +00:00
|
|
|
import _ from "lodash";
|
2019-11-13 07:00:25 +00:00
|
|
|
|
2019-11-14 09:28:51 +00:00
|
|
|
import ContainerComponent from "../components/designSystems/appsmith/ContainerComponent";
|
2019-11-13 07:00:25 +00:00
|
|
|
import { ContainerOrientation, WidgetType } from "constants/WidgetConstants";
|
|
|
|
|
import WidgetFactory from "utils/WidgetFactory";
|
|
|
|
|
import { Color } from "constants/Colors";
|
|
|
|
|
import DropTargetComponent from "components/editorComponents/DropTargetComponent";
|
|
|
|
|
import { GridDefaults } from "constants/WidgetConstants";
|
|
|
|
|
|
|
|
|
|
import ResizeBoundsContainerComponent from "components/editorComponents/ResizeBoundsContainerComponent";
|
|
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
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-11-13 07:00:25 +00:00
|
|
|
renderChildWidget(childWidgetData: WidgetProps): JSX.Element {
|
2019-09-09 09:08:54 +00:00
|
|
|
childWidgetData.parentColumnSpace = this.state.snapColumnSpace;
|
|
|
|
|
childWidgetData.parentRowSpace = this.state.snapRowSpace;
|
2019-09-25 18:33:56 +00:00
|
|
|
childWidgetData.parentId = this.props.widgetId;
|
2019-10-03 16:24:29 +00:00
|
|
|
return WidgetFactory.createWidget(childWidgetData, this.props.renderMode);
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-13 07:00:25 +00:00
|
|
|
renderChildren = () => {
|
|
|
|
|
return _.map(this.props.children, this.renderChildWidget);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderAsDropTarget() {
|
|
|
|
|
return (
|
|
|
|
|
<DropTargetComponent {...this.props} {...this.state}>
|
|
|
|
|
<ResizeBoundsContainerComponent {...this.props}>
|
|
|
|
|
{this.renderChildren()}
|
|
|
|
|
</ResizeBoundsContainerComponent>
|
|
|
|
|
</DropTargetComponent>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getContainerComponentProps = () => {
|
|
|
|
|
const containerProps: ContainerWidgetProps<WidgetProps> = { ...this.props };
|
|
|
|
|
containerProps.backgroundColor = this.props.backgroundColor || "white";
|
|
|
|
|
if (!this.props.parentId) {
|
|
|
|
|
containerProps.containerStyle = "none";
|
|
|
|
|
}
|
|
|
|
|
return containerProps;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderAsContainerComponent() {
|
2019-02-10 13:06:05 +00:00
|
|
|
return (
|
2019-11-13 07:00:25 +00:00
|
|
|
<ContainerComponent {...this.getContainerComponentProps()}>
|
|
|
|
|
{this.renderChildren()}
|
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-11-13 07:00:25 +00:00
|
|
|
getPageView() {
|
|
|
|
|
return this.renderAsContainerComponent();
|
2019-09-30 03:25:14 +00:00
|
|
|
}
|
2019-10-08 06:19:10 +00:00
|
|
|
|
2019-09-30 03:25:14 +00:00
|
|
|
getCanvasView() {
|
2019-11-13 07:00:25 +00:00
|
|
|
return this.renderAsDropTarget();
|
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;
|