2021-08-24 11:38:20 +00:00
|
|
|
import { XYCord } from "utils/hooks/useCanvasDragging";
|
2019-11-13 07:00:25 +00:00
|
|
|
import { WidgetProps, WidgetRowCols } from "widgets/BaseWidget";
|
2020-01-16 11:46:21 +00:00
|
|
|
import { GridDefaults } from "constants/WidgetConstants";
|
2019-11-13 07:00:25 +00:00
|
|
|
|
|
|
|
|
export type UIElementSize = { height: number; width: number };
|
|
|
|
|
|
|
|
|
|
export const RESIZABLE_CONTAINER_BORDER_THEME_INDEX = 1;
|
|
|
|
|
|
2020-02-18 19:56:58 +00:00
|
|
|
export const computeRowCols = (
|
2019-11-13 07:00:25 +00:00
|
|
|
delta: UIElementSize,
|
2021-08-24 11:38:20 +00:00
|
|
|
position: XYCord,
|
2019-11-13 07:00:25 +00:00
|
|
|
props: WidgetProps,
|
2020-02-18 19:56:58 +00:00
|
|
|
) => {
|
|
|
|
|
return {
|
|
|
|
|
leftColumn: Math.round(
|
|
|
|
|
props.leftColumn + position.x / props.parentColumnSpace,
|
2020-01-16 11:46:21 +00:00
|
|
|
),
|
|
|
|
|
topRow: Math.round(props.topRow + position.y / props.parentRowSpace),
|
2020-02-18 19:56:58 +00:00
|
|
|
rightColumn: Math.round(
|
|
|
|
|
props.rightColumn + (delta.width + position.x) / props.parentColumnSpace,
|
2020-01-16 11:46:21 +00:00
|
|
|
),
|
|
|
|
|
bottomRow: Math.round(
|
2019-11-13 07:00:25 +00:00
|
|
|
props.bottomRow + (delta.height + position.y) / props.parentRowSpace,
|
2020-01-16 11:46:21 +00:00
|
|
|
),
|
2019-11-13 07:00:25 +00:00
|
|
|
};
|
2020-02-18 19:56:58 +00:00
|
|
|
};
|
2019-11-13 07:00:25 +00:00
|
|
|
|
2020-02-18 19:56:58 +00:00
|
|
|
export const computeBoundedRowCols = (rowCols: WidgetRowCols) => {
|
|
|
|
|
return {
|
|
|
|
|
leftColumn: Math.max(rowCols.leftColumn, 0),
|
|
|
|
|
rightColumn: Math.min(
|
|
|
|
|
rowCols.rightColumn,
|
|
|
|
|
GridDefaults.DEFAULT_GRID_COLUMNS,
|
|
|
|
|
),
|
|
|
|
|
topRow: rowCols.topRow,
|
|
|
|
|
bottomRow: rowCols.bottomRow,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const hasRowColsChanged = (
|
|
|
|
|
newRowCols: WidgetRowCols,
|
|
|
|
|
props: WidgetProps,
|
|
|
|
|
) => {
|
|
|
|
|
return (
|
2019-11-13 07:00:25 +00:00
|
|
|
props.leftColumn !== newRowCols.leftColumn ||
|
|
|
|
|
props.topRow !== newRowCols.topRow ||
|
|
|
|
|
props.bottomRow !== newRowCols.bottomRow ||
|
|
|
|
|
props.rightColumn !== newRowCols.rightColumn
|
2020-02-18 19:56:58 +00:00
|
|
|
);
|
2019-11-13 07:00:25 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-18 19:56:58 +00:00
|
|
|
export const computeFinalRowCols = (
|
2019-11-13 07:00:25 +00:00
|
|
|
delta: UIElementSize,
|
2021-08-24 11:38:20 +00:00
|
|
|
position: XYCord,
|
2019-11-13 07:00:25 +00:00
|
|
|
props: WidgetProps,
|
2020-02-18 19:56:58 +00:00
|
|
|
): WidgetRowCols | false => {
|
|
|
|
|
const newRowCols = computeBoundedRowCols(
|
|
|
|
|
computeRowCols(delta, position, props),
|
2019-11-13 07:00:25 +00:00
|
|
|
);
|
2020-03-27 09:02:11 +00:00
|
|
|
|
2020-02-18 19:56:58 +00:00
|
|
|
return hasRowColsChanged(newRowCols, props) ? newRowCols : false;
|
2019-11-13 07:00:25 +00:00
|
|
|
};
|