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-18 10:48:56 +00:00
|
|
|
|
|
|
|
|
export const extractCurrentDSL = (
|
|
|
|
|
fetchPageResponse: FetchPageResponse,
|
|
|
|
|
): ContainerWidgetProps<WidgetProps> => {
|
|
|
|
|
return fetchPageResponse.data.layouts[0].dsl;
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-19 22:25:37 +00:00
|
|
|
export const generateWidgetProps = (
|
|
|
|
|
parent: ContainerWidgetProps<WidgetProps>,
|
|
|
|
|
type: WidgetType,
|
|
|
|
|
left: number,
|
|
|
|
|
top: number,
|
|
|
|
|
width: number,
|
|
|
|
|
height: number,
|
|
|
|
|
): WidgetProps => {
|
|
|
|
|
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,
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
type,
|
|
|
|
|
leftColumn: Math.floor(left / parentColumnWidth),
|
|
|
|
|
rightColumn: Math.floor((left + width) / parentColumnWidth),
|
|
|
|
|
topRow: Math.floor(top / parentRowHeight),
|
|
|
|
|
bottomRow: Math.floor((top + height) / parentRowHeight),
|
|
|
|
|
widgetId: generateReactKey(),
|
|
|
|
|
widgetName: generateReactKey(), //TODO: figure out what this is to populate appropriately
|
|
|
|
|
isVisible: true,
|
|
|
|
|
parentColumnSpace: parentColumnWidth,
|
|
|
|
|
parentRowSpace: parentRowHeight,
|
|
|
|
|
executeAction: () => {},
|
|
|
|
|
renderMode: RenderModes.CANVAS, //Is this required?
|
|
|
|
|
};
|
|
|
|
|
} 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
|
|
|
};
|