2023-09-11 15:55:11 +00:00
|
|
|
import type { RenderModes } from "constants/WidgetConstants";
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { useSelector } from "react-redux";
|
2023-10-04 08:54:16 +00:00
|
|
|
import { getRenderMode } from "selectors/editorSelectors";
|
|
|
|
|
import { getLayoutSystemType } from "selectors/layoutSystemSelectors";
|
2023-09-11 15:55:11 +00:00
|
|
|
import type { WidgetProps } from "widgets/BaseWidget";
|
|
|
|
|
import { getAutoLayoutSystem } from "./autolayout";
|
|
|
|
|
import { getFixedLayoutSystem } from "./fixedlayout";
|
2023-10-04 11:53:29 +00:00
|
|
|
import type { LayoutSystem } from "./types";
|
2023-10-04 08:54:16 +00:00
|
|
|
import { LayoutSystemTypes } from "./types";
|
2023-10-19 20:27:40 +00:00
|
|
|
import { getAnvilLayoutSystem } from "./anvil";
|
2023-09-11 15:55:11 +00:00
|
|
|
|
2023-10-04 11:53:29 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param renderMode - render mode specifies whether the application is in edit/deploy mode.
|
2023-10-19 15:20:17 +00:00
|
|
|
* @param layoutSystemType - layout system of the application.
|
2023-10-04 11:53:29 +00:00
|
|
|
* @returns
|
|
|
|
|
* @property — widgetSystem - widget specific wrappers and enhancers of a layout system
|
|
|
|
|
* @property — canvasSystem - canvas specific implementation and enhancers of a layout system
|
|
|
|
|
*/
|
2023-09-11 15:55:11 +00:00
|
|
|
|
2023-09-19 05:22:11 +00:00
|
|
|
export const getLayoutSystem = (
|
2023-09-11 15:55:11 +00:00
|
|
|
renderMode: RenderModes,
|
2023-10-04 08:54:16 +00:00
|
|
|
layoutSystemType: LayoutSystemTypes,
|
2023-09-11 15:55:11 +00:00
|
|
|
): LayoutSystem => {
|
2023-10-04 08:54:16 +00:00
|
|
|
switch (layoutSystemType) {
|
2023-10-19 20:27:40 +00:00
|
|
|
case LayoutSystemTypes.ANVIL:
|
|
|
|
|
return getAnvilLayoutSystem(renderMode);
|
2023-10-04 08:54:16 +00:00
|
|
|
case LayoutSystemTypes.AUTO:
|
2023-10-02 19:41:05 +00:00
|
|
|
return getAutoLayoutSystem(renderMode);
|
|
|
|
|
default:
|
|
|
|
|
return getFixedLayoutSystem(renderMode);
|
2023-09-11 15:55:11 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const LayoutSystemWrapper = ({
|
|
|
|
|
Widget,
|
|
|
|
|
widgetProps,
|
|
|
|
|
}: {
|
|
|
|
|
widgetProps: WidgetProps;
|
2023-10-04 11:53:29 +00:00
|
|
|
Widget: (props: WidgetProps) => JSX.Element;
|
2023-09-11 15:55:11 +00:00
|
|
|
}) => {
|
|
|
|
|
const renderMode = useSelector(getRenderMode);
|
2023-10-04 08:54:16 +00:00
|
|
|
const layoutSystemType = useSelector(getLayoutSystemType);
|
|
|
|
|
// based on layoutSystemType and renderMode
|
2023-09-11 15:55:11 +00:00
|
|
|
// get the layout system wrapper(adds layout system specific functionality) and
|
|
|
|
|
// properties enhancer(adds/modifies properties of a widget based on layout system)
|
2023-10-04 11:53:29 +00:00
|
|
|
const { widgetSystem } = getLayoutSystem(renderMode, layoutSystemType);
|
|
|
|
|
const { propertyEnhancer, WidgetWrapper } = widgetSystem;
|
2023-09-11 15:55:11 +00:00
|
|
|
const enhancedProperties = propertyEnhancer(widgetProps);
|
|
|
|
|
return (
|
2023-10-04 11:53:29 +00:00
|
|
|
<WidgetWrapper {...enhancedProperties}>
|
2023-09-11 15:55:11 +00:00
|
|
|
<Widget {...enhancedProperties} />
|
2023-10-04 11:53:29 +00:00
|
|
|
</WidgetWrapper>
|
2023-09-11 15:55:11 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-04 11:53:29 +00:00
|
|
|
export const withLayoutSystemWidgetHOC = (Widget: any) => {
|
2023-09-11 15:55:11 +00:00
|
|
|
return function LayoutWrappedWidget(props: WidgetProps) {
|
|
|
|
|
return <LayoutSystemWrapper Widget={Widget} widgetProps={props} />;
|
|
|
|
|
};
|
|
|
|
|
};
|