2019-09-18 10:48:56 +00:00
|
|
|
import { FetchPageResponse } from "../api/PageApi";
|
|
|
|
|
import { ContainerWidgetProps } from "../widgets/ContainerWidget";
|
|
|
|
|
import { WidgetProps } from "../widgets/BaseWidget";
|
2019-09-19 22:25:37 +00:00
|
|
|
import { WidgetType, RenderModes } from "../constants/WidgetConstants";
|
|
|
|
|
import { generateReactKey } from "../utils/generators";
|
2019-09-22 20:25:05 +00:00
|
|
|
import { Colors } from "../constants/Colors";
|
|
|
|
|
import { GridDefaults, WidgetTypes } from "../constants/WidgetConstants";
|
|
|
|
|
|
|
|
|
|
const { DEFAULT_GRID_COLUMNS, DEFAULT_GRID_ROWS } = GridDefaults;
|
2019-09-18 10:48:56 +00:00
|
|
|
|
|
|
|
|
export const extractCurrentDSL = (
|
|
|
|
|
fetchPageResponse: FetchPageResponse,
|
|
|
|
|
): ContainerWidgetProps<WidgetProps> => {
|
|
|
|
|
return fetchPageResponse.data.layouts[0].dsl;
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-22 20:25:05 +00:00
|
|
|
export const updateWidgetPosition = (
|
|
|
|
|
widget: WidgetProps,
|
|
|
|
|
left: number,
|
|
|
|
|
top: number,
|
|
|
|
|
parent?: WidgetProps,
|
|
|
|
|
) => {
|
|
|
|
|
const newPositions = {
|
|
|
|
|
leftColumn: Math.floor(left / widget.parentColumnSpace),
|
|
|
|
|
topRow: Math.floor(top / widget.parentRowSpace),
|
|
|
|
|
rightColumn:
|
|
|
|
|
Math.floor(left / widget.parentColumnSpace) +
|
|
|
|
|
(widget.rightColumn - widget.leftColumn),
|
|
|
|
|
bottomRow:
|
|
|
|
|
Math.floor(top / widget.parentRowSpace) +
|
|
|
|
|
(widget.bottomRow - widget.topRow),
|
|
|
|
|
};
|
|
|
|
|
if (parent) {
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...widget,
|
|
|
|
|
...newPositions,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const updateWidgetSize = (
|
|
|
|
|
widget: WidgetProps,
|
|
|
|
|
deltaHeight: number,
|
|
|
|
|
deltaWidth: number,
|
|
|
|
|
): WidgetProps => {
|
|
|
|
|
const origHeight = (widget.bottomRow - widget.topRow) * widget.parentRowSpace;
|
|
|
|
|
const origWidth =
|
|
|
|
|
(widget.rightColumn - widget.leftColumn) * widget.parentColumnSpace;
|
|
|
|
|
return {
|
|
|
|
|
...widget,
|
|
|
|
|
rightColumn:
|
|
|
|
|
widget.leftColumn +
|
|
|
|
|
Math.floor((origWidth + deltaWidth) / widget.parentColumnSpace),
|
|
|
|
|
bottomRow:
|
|
|
|
|
widget.topRow +
|
|
|
|
|
Math.floor((origHeight + deltaHeight) / widget.parentRowSpace),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-19 22:25:37 +00:00
|
|
|
export const generateWidgetProps = (
|
|
|
|
|
parent: ContainerWidgetProps<WidgetProps>,
|
|
|
|
|
type: WidgetType,
|
|
|
|
|
left: number,
|
|
|
|
|
top: number,
|
|
|
|
|
width: number,
|
|
|
|
|
height: number,
|
2019-09-22 20:25:05 +00:00
|
|
|
): ContainerWidgetProps<WidgetProps> => {
|
2019-09-19 22:25:37 +00:00
|
|
|
if (parent && parent.snapColumns && parent.snapRows) {
|
|
|
|
|
const parentColumnWidth = Math.floor(
|
|
|
|
|
((parent.rightColumn - parent.leftColumn) * parent.parentColumnSpace) /
|
|
|
|
|
parent.snapColumns,
|
|
|
|
|
);
|
|
|
|
|
const parentRowHeight = Math.floor(
|
|
|
|
|
((parent.bottomRow - parent.topRow) * parent.parentRowSpace) /
|
|
|
|
|
parent.snapRows,
|
|
|
|
|
);
|
2019-09-22 20:25:05 +00:00
|
|
|
const sizes = {
|
2019-09-19 22:25:37 +00:00
|
|
|
leftColumn: Math.floor(left / parentColumnWidth),
|
|
|
|
|
rightColumn: Math.floor((left + width) / parentColumnWidth),
|
|
|
|
|
topRow: Math.floor(top / parentRowHeight),
|
|
|
|
|
bottomRow: Math.floor((top + height) / parentRowHeight),
|
2019-09-22 20:25:05 +00:00
|
|
|
};
|
|
|
|
|
let others = {};
|
|
|
|
|
if (type === WidgetTypes.CONTAINER_WIDGET) {
|
|
|
|
|
others = {
|
|
|
|
|
snapColumns: DEFAULT_GRID_COLUMNS,
|
|
|
|
|
snapRows: DEFAULT_GRID_ROWS,
|
|
|
|
|
orientation: "VERTICAL",
|
|
|
|
|
children: [],
|
|
|
|
|
background: Colors.WHITE,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
type,
|
|
|
|
|
executeAction: () => {},
|
2019-09-19 22:25:37 +00:00
|
|
|
widgetId: generateReactKey(),
|
|
|
|
|
widgetName: generateReactKey(), //TODO: figure out what this is to populate appropriately
|
|
|
|
|
isVisible: true,
|
|
|
|
|
parentColumnSpace: parentColumnWidth,
|
|
|
|
|
parentRowSpace: parentRowHeight,
|
|
|
|
|
renderMode: RenderModes.CANVAS, //Is this required?
|
2019-09-22 20:25:05 +00:00
|
|
|
...sizes,
|
|
|
|
|
...others,
|
2019-09-19 22:25:37 +00:00
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
if (parent)
|
|
|
|
|
throw Error("Failed to create widget: Parent's size cannot be calculate");
|
|
|
|
|
else throw Error("Failed to create widget: Parent was not provided ");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-18 10:48:56 +00:00
|
|
|
export default {
|
|
|
|
|
extractCurrentDSL,
|
2019-09-19 22:25:37 +00:00
|
|
|
generateWidgetProps,
|
2019-09-18 10:48:56 +00:00
|
|
|
};
|