@qrvey/utils 1.15.0-2 → 1.15.0-20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/cjs/elements/helpers/fixed.d.ts +85 -0
  2. package/dist/cjs/elements/helpers/fixed.js +188 -0
  3. package/dist/cjs/elements/helpers/gridStrategy.d.ts +28 -0
  4. package/dist/cjs/elements/helpers/gridStrategy.js +81 -0
  5. package/dist/cjs/elements/helpers/index.d.ts +1 -0
  6. package/dist/cjs/elements/helpers/index.js +17 -0
  7. package/dist/cjs/elements/helpers/responsive.d.ts +87 -0
  8. package/dist/cjs/elements/helpers/responsive.js +166 -0
  9. package/dist/cjs/elements/index.d.ts +2 -0
  10. package/dist/cjs/elements/index.js +18 -0
  11. package/dist/cjs/elements/interfaces/ICanvasGrid.d.ts +5 -0
  12. package/dist/cjs/elements/interfaces/ICanvasGrid.js +9 -0
  13. package/dist/cjs/elements/interfaces/IFixedPosition.d.ts +5 -0
  14. package/dist/cjs/elements/interfaces/IFixedPosition.js +2 -0
  15. package/dist/cjs/elements/interfaces/IVemCore.d.ts +29 -0
  16. package/dist/cjs/elements/interfaces/IVemCore.js +3 -0
  17. package/dist/cjs/elements/interfaces/IVemPosition.d.ts +44 -0
  18. package/dist/cjs/elements/interfaces/IVemPosition.js +8 -0
  19. package/dist/cjs/elements/interfaces/index.d.ts +4 -0
  20. package/dist/cjs/elements/interfaces/index.js +20 -0
  21. package/dist/cjs/elements/utils/element.d.ts +4 -0
  22. package/dist/cjs/elements/utils/element.js +28 -0
  23. package/dist/cjs/elements/utils/general.d.ts +15 -0
  24. package/dist/cjs/elements/utils/general.js +20 -0
  25. package/dist/cjs/elements/utils/overlap.d.ts +63 -0
  26. package/dist/cjs/elements/utils/overlap.js +99 -0
  27. package/dist/cjs/elements/utils/position.d.ts +4 -0
  28. package/dist/cjs/elements/utils/position.js +5 -0
  29. package/dist/cjs/filters/adapters/FDToLogic.js +2 -1
  30. package/dist/cjs/filters/helpers/backend/buildFilters.d.ts +5 -0
  31. package/dist/cjs/filters/helpers/backend/buildFilters.js +23 -1
  32. package/dist/cjs/filters/helpers/common/getDateProperties.d.ts +6 -0
  33. package/dist/cjs/filters/helpers/common/getDateProperties.js +2 -1
  34. package/dist/cjs/filters/helpers/settings/getFilterSettings.js +1 -1
  35. package/dist/cjs/filters/interfaces/common/IFSScopeID.d.ts +4 -0
  36. package/dist/cjs/general/array/howManyByParam.d.ts +8 -0
  37. package/dist/cjs/general/array/howManyByParam.js +21 -0
  38. package/dist/cjs/general/array/index.d.ts +2 -0
  39. package/dist/cjs/general/array/index.js +2 -0
  40. package/dist/cjs/general/string/index.d.ts +4 -0
  41. package/dist/cjs/general/string/index.js +4 -0
  42. package/dist/cjs/general/string/secureHTML.d.ts +8 -0
  43. package/dist/cjs/general/string/secureHTML.js +13 -0
  44. package/dist/cjs/general/string/strategies/XSSEstrictedSanitizer.d.ts +8 -0
  45. package/dist/cjs/general/string/strategies/XSSEstrictedSanitizer.js +23 -0
  46. package/dist/cjs/general/string/strategies/XSSSanitizer.d.ts +8 -0
  47. package/dist/cjs/general/string/strategies/XSSSanitizer.js +19 -0
  48. package/dist/cjs/general/string/urlValidator.d.ts +10 -0
  49. package/dist/cjs/general/string/urlValidator.js +38 -0
  50. package/dist/cjs/globalization/interfaces/export/II18nExportingModal.d.ts +20 -0
  51. package/dist/cjs/globalization/interfaces/filters/II18nFilterDisplay.d.ts +1 -0
  52. package/dist/cjs/globalization/labels/export/I18N_EXPORT_MODAL.js +20 -0
  53. package/dist/cjs/globalization/labels/filters/I18N_FILTER_DISPLAY.js +1 -0
  54. package/dist/cjs/index.d.ts +1 -0
  55. package/dist/cjs/index.js +1 -0
  56. package/dist/cjs/interfaces/SatinizerStrategy.d.ts +3 -0
  57. package/dist/cjs/interfaces/SatinizerStrategy.js +2 -0
  58. package/dist/elements/helpers/fixed.d.ts +85 -0
  59. package/dist/elements/helpers/fixed.js +177 -0
  60. package/dist/elements/helpers/gridStrategy.d.ts +28 -0
  61. package/dist/elements/helpers/gridStrategy.js +76 -0
  62. package/dist/elements/helpers/index.d.ts +1 -0
  63. package/dist/elements/helpers/index.js +1 -0
  64. package/dist/elements/helpers/responsive.d.ts +87 -0
  65. package/dist/elements/helpers/responsive.js +155 -0
  66. package/dist/elements/index.d.ts +2 -0
  67. package/dist/elements/index.js +2 -0
  68. package/dist/elements/interfaces/ICanvasGrid.d.ts +5 -0
  69. package/dist/elements/interfaces/ICanvasGrid.js +6 -0
  70. package/dist/elements/interfaces/IFixedPosition.d.ts +5 -0
  71. package/dist/elements/interfaces/IFixedPosition.js +1 -0
  72. package/dist/elements/interfaces/IVemCore.d.ts +29 -0
  73. package/dist/elements/interfaces/IVemCore.js +1 -0
  74. package/dist/elements/interfaces/IVemPosition.d.ts +44 -0
  75. package/dist/elements/interfaces/IVemPosition.js +5 -0
  76. package/dist/elements/interfaces/index.d.ts +4 -0
  77. package/dist/elements/interfaces/index.js +4 -0
  78. package/dist/elements/utils/element.d.ts +4 -0
  79. package/dist/elements/utils/element.js +24 -0
  80. package/dist/elements/utils/general.d.ts +15 -0
  81. package/dist/elements/utils/general.js +16 -0
  82. package/dist/elements/utils/overlap.d.ts +63 -0
  83. package/dist/elements/utils/overlap.js +95 -0
  84. package/dist/elements/utils/position.d.ts +4 -0
  85. package/dist/elements/utils/position.js +1 -0
  86. package/dist/filters/adapters/FDToLogic.js +2 -1
  87. package/dist/filters/helpers/backend/buildFilters.d.ts +5 -0
  88. package/dist/filters/helpers/backend/buildFilters.js +23 -1
  89. package/dist/filters/helpers/common/getDateProperties.d.ts +6 -0
  90. package/dist/filters/helpers/common/getDateProperties.js +1 -1
  91. package/dist/filters/helpers/settings/getFilterSettings.js +1 -1
  92. package/dist/filters/interfaces/common/IFSScopeID.d.ts +4 -0
  93. package/dist/general/array/howManyByParam.d.ts +8 -0
  94. package/dist/general/array/howManyByParam.js +17 -0
  95. package/dist/general/array/index.d.ts +2 -0
  96. package/dist/general/array/index.js +2 -0
  97. package/dist/general/string/index.d.ts +4 -0
  98. package/dist/general/string/index.js +4 -0
  99. package/dist/general/string/secureHTML.d.ts +8 -0
  100. package/dist/general/string/secureHTML.js +9 -0
  101. package/dist/general/string/strategies/XSSEstrictedSanitizer.d.ts +8 -0
  102. package/dist/general/string/strategies/XSSEstrictedSanitizer.js +19 -0
  103. package/dist/general/string/strategies/XSSSanitizer.d.ts +8 -0
  104. package/dist/general/string/strategies/XSSSanitizer.js +15 -0
  105. package/dist/general/string/urlValidator.d.ts +10 -0
  106. package/dist/general/string/urlValidator.js +34 -0
  107. package/dist/globalization/interfaces/export/II18nExportingModal.d.ts +20 -0
  108. package/dist/globalization/interfaces/filters/II18nFilterDisplay.d.ts +1 -0
  109. package/dist/globalization/labels/export/I18N_EXPORT_MODAL.js +20 -0
  110. package/dist/globalization/labels/filters/I18N_FILTER_DISPLAY.js +1 -0
  111. package/dist/index.d.ts +1 -0
  112. package/dist/index.js +1 -0
  113. package/dist/interfaces/SatinizerStrategy.d.ts +3 -0
  114. package/dist/interfaces/SatinizerStrategy.js +1 -0
  115. package/package.json +3 -2
@@ -0,0 +1,85 @@
1
+ import { CanvasDevice } from "../interfaces/ICanvasGrid";
2
+ import { FindAvailablePositionOptions } from "../interfaces/IFixedPosition";
3
+ import { VemFixed } from "../interfaces/IVemCore";
4
+ import { VemPositionFixed } from "../interfaces/IVemPosition";
5
+ /**
6
+ * update the position for a specific device
7
+ * @param element element to be updated
8
+ * @param newDevicePosition new position attributes for the element
9
+ * @param device device for the update
10
+ * @returns new element with its position updated
11
+ */
12
+ export declare function updateFixedPosition(element: VemFixed, newDevicePosition: Partial<VemPositionFixed>, device: CanvasDevice): VemFixed;
13
+ /**
14
+ * Finds the first available position within a canvas where a new element can be placed without overlapping existing elements.
15
+ * @param canvasWidth - The width of the canvas in pixels.
16
+ * @param newElement - The new element to be placed, with fixed position properties.
17
+ * @param elements - The list of existing elements on the canvas.
18
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
19
+ * @param [options] - Additional options for position finding.
20
+ * @param [options.canvasHeight] - The maximum height of the canvas in pixels. Defaults to `Infinity`.
21
+ * @param [options.findRightToLeft] - Whether to search for positions from right to left. Defaults to `false`.
22
+ * @param [options.initialY] - The initial Y-coordinate to start the search. Defaults to `0`.
23
+ * @returns - The x and y coordinates of the first available position.
24
+ * @throws {Error} - Throws an error if no valid position is found and `canvasHeight` is not specified.
25
+ */
26
+ export declare function findAvailablePosition(canvasWidth: number, newElement: VemFixed, elements: VemFixed[], device: CanvasDevice, options?: FindAvailablePositionOptions): {
27
+ x: number;
28
+ y: number;
29
+ };
30
+ export declare const getFixedPositionByDevice: (item: VemFixed, device: CanvasDevice) => VemPositionFixed | undefined;
31
+ /**
32
+ * Finds available positions for multiple new elements within a canvas, ensuring they do not overlap with existing elements.
33
+ * @param canvasWidth - The width of the canvas in pixels.
34
+ * @param newElements - The list of new elements to be positioned, each with fixed position properties.
35
+ * @param elements - The list of existing elements on the canvas.
36
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
37
+ * @param [startWithPositionY] - Whether to start the position search for each new element based on its initial Y-coordinate. Defaults to `false`.
38
+ * @param [findRightToLeft] - Whether to search for positions from right to left. Defaults to `false`.
39
+ * @param [canvasHeight] - The maximum height of the canvas in pixels. If not provided, the search height is unlimited.
40
+ * @returns
41
+ */
42
+ export declare function findFixedAvailablePositions(canvasWidth: number, newElements: VemFixed[], elements: VemFixed[], device: CanvasDevice, startWithPositionY?: boolean, findRightToLeft?: boolean, canvasHeight?: number): VemFixed[];
43
+ /**
44
+ * Adjusts the `width` of a responsive element to fit within the specified canvas width and updates its position.
45
+ * @param element - The fixed element to adjust.
46
+ * @param canvasWidth - The width of the canvas in grid columns.
47
+ * @param canvasDevice - The target device type (e.g., desktop, tablet, mobile).
48
+ * @returns - The updated responsive element with an adjusted `width`.
49
+ */
50
+ export declare function calculateWidth(element: VemFixed, canvasWidth: number, canvasDevice: CanvasDevice): VemFixed;
51
+ /**
52
+ * @param elements elements to calculate
53
+ * @param device current device
54
+ * @returns height
55
+ */
56
+ export declare const getFixedGridElementsBottomLimit: (elements: VemFixed[], device: CanvasDevice) => number;
57
+ /**
58
+ * Calculates distances of elements from a given reference point on the canvas device.
59
+ * @param elements - Array of `VemFixed` elements to calculate distances for.
60
+ * @param device - Canvas device providing the coordinates of the elements.
61
+ * @param referencePoint - The reference point to calculate distances from.
62
+ * @param referencePoint.x
63
+ * @param referencePoint.y
64
+ * @returns An array of objects containing the original elements and their calculated distances from the reference point.
65
+ */
66
+ export declare const calculateDistancesToReferencePoint: (elements: VemFixed[], device: CanvasDevice, referencePoint: {
67
+ x: number;
68
+ y: number;
69
+ }) => {
70
+ distance: number;
71
+ element: VemFixed;
72
+ }[];
73
+ /**
74
+ * Sorts elements based on their distance from a reference point on the canvas.
75
+ * @param elements - Array of `VemFixed` elements to be sorted.
76
+ * @param device - Canvas device providing the coordinates of the elements.
77
+ * @param referencePoint - The reference point to calculate distances from.
78
+ * @param referencePoint.x
79
+ * @param referencePoint.y
80
+ * @returns An array of `VemFixed` elements sorted by their distance from the reference point (0, 0).
81
+ */
82
+ export declare const sortElementsFromReferencePoint: (elements: VemFixed<unknown>[], device: CanvasDevice, referencePoint: {
83
+ x: number;
84
+ y: number;
85
+ }) => VemFixed<unknown>[];
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortElementsFromReferencePoint = exports.calculateDistancesToReferencePoint = exports.getFixedGridElementsBottomLimit = exports.calculateWidth = exports.findFixedAvailablePositions = exports.getFixedPositionByDevice = exports.findAvailablePosition = exports.updateFixedPosition = void 0;
4
+ const IVemPosition_1 = require("../interfaces/IVemPosition");
5
+ const general_1 = require("../utils/general");
6
+ const overlap_1 = require("../utils/overlap");
7
+ const position_1 = require("../utils/position");
8
+ const SPACE_DELTA = 10;
9
+ /**
10
+ * update the position for a specific device
11
+ * @param element element to be updated
12
+ * @param newDevicePosition new position attributes for the element
13
+ * @param device device for the update
14
+ * @returns new element with its position updated
15
+ */
16
+ function updateFixedPosition(element, newDevicePosition, device) {
17
+ var _a;
18
+ return Object.assign(Object.assign({}, element), { position: Object.assign(Object.assign({}, element.position), { fixed: Object.assign(Object.assign({}, element.position.fixed), { [device]: Object.assign(Object.assign({}, (_a = element.position.fixed) === null || _a === void 0 ? void 0 : _a[device]), newDevicePosition) }) }) });
19
+ }
20
+ exports.updateFixedPosition = updateFixedPosition;
21
+ /**
22
+ * Finds the first available position within a canvas where a new element can be placed without overlapping existing elements.
23
+ * @param canvasWidth - The width of the canvas in pixels.
24
+ * @param newElement - The new element to be placed, with fixed position properties.
25
+ * @param elements - The list of existing elements on the canvas.
26
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
27
+ * @param [options] - Additional options for position finding.
28
+ * @param [options.canvasHeight] - The maximum height of the canvas in pixels. Defaults to `Infinity`.
29
+ * @param [options.findRightToLeft] - Whether to search for positions from right to left. Defaults to `false`.
30
+ * @param [options.initialY] - The initial Y-coordinate to start the search. Defaults to `0`.
31
+ * @returns - The x and y coordinates of the first available position.
32
+ * @throws {Error} - Throws an error if no valid position is found and `canvasHeight` is not specified.
33
+ */
34
+ function findAvailablePosition(canvasWidth, newElement, elements, device, options = {}) {
35
+ var _a, _b;
36
+ const { width: newWidth, height: newHeight } = (0, exports.getFixedPositionByDevice)(newElement, device);
37
+ const canvasHeight = (_a = options.canvasHeight) !== null && _a !== void 0 ? _a : Infinity;
38
+ const sortedElements = options.findRightToLeft
39
+ ? elements
40
+ .slice()
41
+ .sort((b, a) => b.position.fixed[device].x - a.position.fixed[device].x)
42
+ : elements
43
+ .slice()
44
+ .sort((a, b) => a.position.fixed[device].y - b.position.fixed[device].y);
45
+ const intervalTree = new overlap_1.OverlapIntervalTree();
46
+ for (const element of sortedElements) {
47
+ const elemPosition = element.position.fixed[device];
48
+ intervalTree.insert(elemPosition.x, elemPosition.x + elemPosition.width, elemPosition.y, elemPosition.y + elemPosition.height);
49
+ }
50
+ let startX = options.findRightToLeft ? canvasWidth - newWidth : 0;
51
+ const initialY = (_b = options.initialY) !== null && _b !== void 0 ? _b : 0;
52
+ let increment = options.findRightToLeft ? -SPACE_DELTA : SPACE_DELTA;
53
+ for (let y = initialY; y <= canvasHeight - newHeight; y += SPACE_DELTA) {
54
+ let x = startX;
55
+ while (x >= 0 && x <= canvasWidth - newWidth) {
56
+ if (!intervalTree.overlaps(x, x + newWidth, y, y + newHeight)) {
57
+ return { x, y };
58
+ }
59
+ x += increment;
60
+ }
61
+ if (options.findRightToLeft) {
62
+ startX = 0;
63
+ increment = SPACE_DELTA;
64
+ }
65
+ }
66
+ if (options.canvasHeight)
67
+ return { x: 0, y: 0 };
68
+ throw new Error("No valid position found");
69
+ }
70
+ exports.findAvailablePosition = findAvailablePosition;
71
+ const getFixedPositionByDevice = (item, device) => {
72
+ const fixedPosition = item.position.fixed;
73
+ const devicePosition = fixedPosition[device];
74
+ return isValidPosition(devicePosition) ? devicePosition : undefined;
75
+ };
76
+ exports.getFixedPositionByDevice = getFixedPositionByDevice;
77
+ const isValidPosition = (position) => {
78
+ return ((position === null || position === void 0 ? void 0 : position.y) !== undefined &&
79
+ position.x !== undefined &&
80
+ position.z !== undefined &&
81
+ position.height !== undefined &&
82
+ position.width !== undefined);
83
+ };
84
+ /**
85
+ * Finds available positions for multiple new elements within a canvas, ensuring they do not overlap with existing elements.
86
+ * @param canvasWidth - The width of the canvas in pixels.
87
+ * @param newElements - The list of new elements to be positioned, each with fixed position properties.
88
+ * @param elements - The list of existing elements on the canvas.
89
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
90
+ * @param [startWithPositionY] - Whether to start the position search for each new element based on its initial Y-coordinate. Defaults to `false`.
91
+ * @param [findRightToLeft] - Whether to search for positions from right to left. Defaults to `false`.
92
+ * @param [canvasHeight] - The maximum height of the canvas in pixels. If not provided, the search height is unlimited.
93
+ * @returns
94
+ */
95
+ function findFixedAvailablePositions(canvasWidth, newElements, elements, device, startWithPositionY = false, findRightToLeft = false, canvasHeight) {
96
+ const processedElements = elements.slice();
97
+ const resultElems = [];
98
+ newElements.forEach((nElement) => {
99
+ const initialY = startWithPositionY
100
+ ? nElement.position.fixed[device].y
101
+ : 0;
102
+ const newElement = calculateWidth(nElement, canvasWidth, device);
103
+ const availablePosition = findAvailablePosition(canvasWidth, newElement, processedElements, device, { initialY, findRightToLeft, canvasHeight });
104
+ const newElemWithPosition = updateFixedPosition(newElement, availablePosition, device);
105
+ resultElems.push(newElemWithPosition);
106
+ processedElements.push(newElemWithPosition);
107
+ });
108
+ return resultElems;
109
+ }
110
+ exports.findFixedAvailablePositions = findFixedAvailablePositions;
111
+ /**
112
+ * Adjusts the `width` of a responsive element to fit within the specified canvas width and updates its position.
113
+ * @param element - The fixed element to adjust.
114
+ * @param canvasWidth - The width of the canvas in grid columns.
115
+ * @param canvasDevice - The target device type (e.g., desktop, tablet, mobile).
116
+ * @returns - The updated responsive element with an adjusted `width`.
117
+ */
118
+ function calculateWidth(element, canvasWidth, canvasDevice) {
119
+ const currentPosition = element.position.fixed[canvasDevice];
120
+ const fixedWidth = Math.min(currentPosition.width, canvasWidth);
121
+ const position = Object.assign(Object.assign({}, currentPosition), { width: fixedWidth });
122
+ return (0, position_1.setElementPosition)(element, IVemPosition_1.VemPositionType.FIXED, canvasDevice, position);
123
+ }
124
+ exports.calculateWidth = calculateWidth;
125
+ const getFixedGridElementY = (element, device, allowNegative = false) => {
126
+ var _a;
127
+ const position = element.position.fixed[device];
128
+ const itemY = (_a = position === null || position === void 0 ? void 0 : position.y) !== null && _a !== void 0 ? _a : 0;
129
+ if (!allowNegative) {
130
+ return itemY >= 0 ? itemY : 0;
131
+ }
132
+ return itemY;
133
+ };
134
+ const getFixedGridElementHeight = (element, device) => {
135
+ const position = element.position.fixed[device];
136
+ return position === null || position === void 0 ? void 0 : position.height;
137
+ };
138
+ /**
139
+ * @param elements elements to calculate
140
+ * @param device current device
141
+ * @returns height
142
+ */
143
+ const getFixedGridElementsBottomLimit = (elements, device) => {
144
+ if (!elements.length)
145
+ return 0;
146
+ return Math.max(...elements.map((element) => getFixedGridElementY(element, device) +
147
+ getFixedGridElementHeight(element, device)));
148
+ };
149
+ exports.getFixedGridElementsBottomLimit = getFixedGridElementsBottomLimit;
150
+ /**
151
+ * Calculates distances of elements from a given reference point on the canvas device.
152
+ * @param elements - Array of `VemFixed` elements to calculate distances for.
153
+ * @param device - Canvas device providing the coordinates of the elements.
154
+ * @param referencePoint - The reference point to calculate distances from.
155
+ * @param referencePoint.x
156
+ * @param referencePoint.y
157
+ * @returns An array of objects containing the original elements and their calculated distances from the reference point.
158
+ */
159
+ const calculateDistancesToReferencePoint = (elements, device, referencePoint) => {
160
+ return elements.map((element) => {
161
+ const elementPosition = (0, exports.getFixedPositionByDevice)(element, device);
162
+ if (!elementPosition) {
163
+ throw new Error(`Position for device ${device} is undefined for element with Id ${element.elementId}.`);
164
+ }
165
+ const distance = Math.pow(referencePoint.x - elementPosition.x, 2) +
166
+ Math.pow(referencePoint.y - elementPosition.y, 2);
167
+ return {
168
+ distance,
169
+ element,
170
+ };
171
+ });
172
+ };
173
+ exports.calculateDistancesToReferencePoint = calculateDistancesToReferencePoint;
174
+ /**
175
+ * Sorts elements based on their distance from a reference point on the canvas.
176
+ * @param elements - Array of `VemFixed` elements to be sorted.
177
+ * @param device - Canvas device providing the coordinates of the elements.
178
+ * @param referencePoint - The reference point to calculate distances from.
179
+ * @param referencePoint.x
180
+ * @param referencePoint.y
181
+ * @returns An array of `VemFixed` elements sorted by their distance from the reference point (0, 0).
182
+ */
183
+ const sortElementsFromReferencePoint = (elements, device, referencePoint) => {
184
+ const calculatedElements = (0, exports.calculateDistancesToReferencePoint)(elements, device, referencePoint);
185
+ const sortedElements = [...calculatedElements].sort(general_1.sortByDistanceFunction);
186
+ return sortedElements.map((element) => element.element);
187
+ };
188
+ exports.sortElementsFromReferencePoint = sortElementsFromReferencePoint;
@@ -0,0 +1,28 @@
1
+ import { CanvasDevice, Vem } from "../interfaces";
2
+ interface RecalculateOptions {
3
+ canvasHeight?: number;
4
+ sort?: CanvasDevice;
5
+ allowCopy?: boolean;
6
+ }
7
+ /**
8
+ * Recalculates the positions of elements on a canvas based on the canvas type.
9
+ * @param newElements - The list of elements to be recalculated. Each element has properties based on its type.
10
+ * @param currentElements - The list of elements to be recalculated. Each element has properties based on its type.
11
+ * @param canvasWidth - The width of the canvas in pixels.
12
+ * @param canvasType - The type of canvas (e.g., "fixed") that determines how positions are recalculated.
13
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
14
+ * @param canvasHeight - variable for the canvas with header component
15
+ * @param options
16
+ * @returns - A list of elements with updated positions if the canvas type is "fixed" or "responsive".
17
+ */
18
+ export declare function recalculateElements(newElements: Vem[], currentElements: Vem[], canvasWidth: number, canvasType: string, device: CanvasDevice, options?: RecalculateOptions): Vem[];
19
+ /**
20
+ * The calculation is delegated to a specific strategy based on the `canvasType`.
21
+ * @param elements - The list of elements in the canvas.
22
+ * @param canvasType - The type of canvas (e.g., "fixed", "responsive").
23
+ * @param device - The target device type (e.g., desktop, tablet, mobile).
24
+ * @param [rowGap] - The optional gap (in pixels) between rows in the canvas.
25
+ * @returns - The calculated canvas height based on the selected strategy. Returns an empty array if no strategy is found.
26
+ */
27
+ export declare function calculateCanvasHeight(elements: Vem[], canvasType: string, device: CanvasDevice, rowGap?: number): number;
28
+ export {};
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateCanvasHeight = exports.recalculateElements = void 0;
4
+ const fixed_1 = require("./fixed");
5
+ const responsive_1 = require("./responsive");
6
+ const interfaces_1 = require("../interfaces");
7
+ const element_1 = require("../utils/element");
8
+ const recalculateStrategies = {
9
+ fixed: (elements, currentElements, canvasWidth, device, options) => {
10
+ const isSorted = !currentElements.length;
11
+ const changedElements = (options.allowCopy
12
+ ? (0, element_1.copyDesktopPositionToDevice)(elements, device, interfaces_1.VemPositionType.FIXED)
13
+ : elements);
14
+ // sort elements
15
+ const orderedElements = isSorted
16
+ ? (0, fixed_1.sortElementsFromReferencePoint)(changedElements, options.sort, {
17
+ x: 0,
18
+ y: 0,
19
+ })
20
+ : changedElements;
21
+ return (0, fixed_1.findFixedAvailablePositions)(canvasWidth, orderedElements, currentElements, device, false, false, options === null || options === void 0 ? void 0 : options.canvasHeight);
22
+ },
23
+ responsive: (elements, currentElements, canvasWidth, device, options) => {
24
+ const isSorted = !currentElements.length;
25
+ const changedElements = (options.allowCopy
26
+ ? (0, element_1.copyDesktopPositionToDevice)(elements, device, interfaces_1.VemPositionType.RESPONSIVE)
27
+ : elements);
28
+ const orderedElements = isSorted
29
+ ? (0, responsive_1.sortResponsiveElements)(changedElements, options.sort)
30
+ : changedElements;
31
+ return (0, responsive_1.findResponsiveAvailablePositions)(canvasWidth, orderedElements, currentElements, device);
32
+ },
33
+ };
34
+ const calculateCanvasHeightStrategies = {
35
+ fixed: (elements, device) => {
36
+ return (0, fixed_1.getFixedGridElementsBottomLimit)(elements, device);
37
+ },
38
+ responsive: (elements, device, rowGap) => {
39
+ return (0, responsive_1.getResponsiveGridElementsBottomLimit)(elements, device, rowGap);
40
+ },
41
+ };
42
+ /**
43
+ * Recalculates the positions of elements on a canvas based on the canvas type.
44
+ * @param newElements - The list of elements to be recalculated. Each element has properties based on its type.
45
+ * @param currentElements - The list of elements to be recalculated. Each element has properties based on its type.
46
+ * @param canvasWidth - The width of the canvas in pixels.
47
+ * @param canvasType - The type of canvas (e.g., "fixed") that determines how positions are recalculated.
48
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
49
+ * @param canvasHeight - variable for the canvas with header component
50
+ * @param options
51
+ * @returns - A list of elements with updated positions if the canvas type is "fixed" or "responsive".
52
+ */
53
+ function recalculateElements(newElements, currentElements, canvasWidth, canvasType, device, options) {
54
+ const strategy = recalculateStrategies[canvasType];
55
+ if (!strategy) {
56
+ return [];
57
+ }
58
+ const defaultOptions = {
59
+ sort: interfaces_1.CanvasDevice.DESKTOP,
60
+ allowCopy: true,
61
+ };
62
+ const calculatedOptions = Object.assign(Object.assign({}, defaultOptions), options);
63
+ return strategy(newElements, currentElements, canvasWidth, device, calculatedOptions);
64
+ }
65
+ exports.recalculateElements = recalculateElements;
66
+ /**
67
+ * The calculation is delegated to a specific strategy based on the `canvasType`.
68
+ * @param elements - The list of elements in the canvas.
69
+ * @param canvasType - The type of canvas (e.g., "fixed", "responsive").
70
+ * @param device - The target device type (e.g., desktop, tablet, mobile).
71
+ * @param [rowGap] - The optional gap (in pixels) between rows in the canvas.
72
+ * @returns - The calculated canvas height based on the selected strategy. Returns an empty array if no strategy is found.
73
+ */
74
+ function calculateCanvasHeight(elements, canvasType, device, rowGap) {
75
+ const strategy = calculateCanvasHeightStrategies[canvasType];
76
+ if (!strategy) {
77
+ return 0;
78
+ }
79
+ return strategy(elements, device, rowGap);
80
+ }
81
+ exports.calculateCanvasHeight = calculateCanvasHeight;
@@ -0,0 +1 @@
1
+ export * from "./gridStrategy";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./gridStrategy"), exports);
@@ -0,0 +1,87 @@
1
+ import { CanvasDevice } from "../interfaces/ICanvasGrid";
2
+ import { VemResponsive } from "../interfaces/IVemCore";
3
+ import { VemPositionResponsive } from "../interfaces/IVemPosition";
4
+ /**
5
+ * Finds the first available position for a new responsive element within a canvas.
6
+ * @param canvasWidth - The width of the canvas in terms of columns.
7
+ * @param newElement - The new responsive element to place on the canvas.
8
+ * @param elements - The existing elements on the canvas.
9
+ * @param device - The target device type (e.g., desktop, tablet, mobile) to calculate responsive positions.
10
+ * @returns - The column and row start positions for the new element, or `undefined` if no valid position is found.
11
+ */
12
+ export declare function findAvailablePosition(canvasWidth: number, newElement: VemResponsive, elements: VemResponsive[], device: CanvasDevice): {
13
+ colStart: number;
14
+ rowStart: number;
15
+ } | undefined;
16
+ /**
17
+ * Updates the responsive position of a given element for a specified device.
18
+ * @param element - The responsive element to update.
19
+ * @param newPosition - The new position values to update for the element.
20
+ * @param device - The target device type (e.g., desktop, tablet, mobile) for which the position is updated.
21
+ * @returns - A new `VemResponsive` object with the updated responsive position for the specified device.
22
+ */
23
+ export declare function updateResponsivePosition(element: VemResponsive, newPosition: Partial<VemPositionResponsive>, device: CanvasDevice): VemResponsive;
24
+ /**
25
+ * Finds available positions for a list of responsive elements on a canvas and updates their positions.
26
+ * @param canvasWidth - The width of the canvas in grid columns.
27
+ * @param newElements - The list of new responsive elements to position.
28
+ * @param elements - The list of existing responsive elements already positioned on the canvas.
29
+ * @param device - The target device type (e.g., desktop, tablet, mobile) for which positions are calculated.
30
+ * @returns - A list of responsive elements with updated positions for the specified device.
31
+ */
32
+ export declare function findResponsiveAvailablePositions(canvasWidth: number, newElements: VemResponsive[], elements: VemResponsive[], device: CanvasDevice): VemResponsive[];
33
+ /**
34
+ * Adjusts the `colSpan` of a responsive element to fit within the specified canvas width and updates its position.
35
+ * @param element - The responsive element to adjust.
36
+ * @param canvasWidth - The width of the canvas in grid columns.
37
+ * @param canvasDevice - The target device type (e.g., desktop, tablet, mobile).
38
+ * @returns - The updated responsive element with an adjusted `colSpan`.
39
+ */
40
+ export declare function calculateColSpan(element: VemResponsive, canvasWidth: number, canvasDevice: CanvasDevice): VemResponsive;
41
+ export declare const getResponsiveGridElementsBottomLimit: (elements: VemResponsive[], device: CanvasDevice, rowGap: number) => number;
42
+ /**
43
+ * Calculates the distances of elements from a reference point and returns a list of objects
44
+ * containing the calculated distance and the corresponding element.
45
+ * @param elements - An array of `VemResponsive` objects. Each object represents an element with
46
+ * a `position` property that contains responsive positions for various devices.
47
+ * @param device - The specific device type (key of `CanvasDevice`) to determine the responsive position.
48
+ * @param referencePoint - An object representing the reference point with `x` and `y` coordinates.
49
+ * @param referencePoint.x
50
+ * @param referencePoint.y
51
+ * @returns - An array of objects, where each object contains:
52
+ * - `distance`: The squared Euclidean distance from the reference point.
53
+ * - `element`: The corresponding `VemResponsive` object.
54
+ */
55
+ export declare const calculateDistancesToReferencePoint: (elements: VemResponsive[], device: CanvasDevice, referencePoint: {
56
+ x: number;
57
+ y: number;
58
+ }) => {
59
+ distance: number;
60
+ element: VemResponsive;
61
+ }[];
62
+ /**
63
+ /**
64
+ Sorts objects containing a `distance` and `element` by distance in ascending order.
65
+ If two objects have the same distance, it resolves the tie by comparing the `colStart` property
66
+ of their responsive positions for the `DESKTOP` device.
67
+ * @param a - An object containing:
68
+ * - `distance`: The calculated distance from a reference point.
69
+ * - `element`: A `VemResponsive` object with responsive position data.
70
+ * @param b - Another object with the same structure as `a`.
71
+ * @param a.element
72
+ * @param a.distance
73
+ * @param _device - The device type (of type `CanvasDevice`). Currently unused in this function.
74
+ * @param b.distance
75
+ * @param b.element
76
+ * @returns - A negative number if `a` should come before `b`,
77
+ * a positive number if `a` should come after `b`,
78
+ * or 0 if they have the same distance and `colStart` values.
79
+ */
80
+ export declare const sortByDistanceInResponsiveFunction: (a: {
81
+ distance: number;
82
+ element: VemResponsive;
83
+ }, b: {
84
+ distance: number;
85
+ element: VemResponsive;
86
+ }, _device: CanvasDevice) => number;
87
+ export declare const sortResponsiveElements: (elements: VemResponsive<unknown>[], sortByDevice?: CanvasDevice) => VemResponsive<unknown>[];
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortResponsiveElements = exports.sortByDistanceInResponsiveFunction = exports.calculateDistancesToReferencePoint = exports.getResponsiveGridElementsBottomLimit = exports.calculateColSpan = exports.findResponsiveAvailablePositions = exports.updateResponsivePosition = exports.findAvailablePosition = void 0;
4
+ const ICanvasGrid_1 = require("../interfaces/ICanvasGrid");
5
+ const IVemPosition_1 = require("../interfaces/IVemPosition");
6
+ const overlap_1 = require("../utils/overlap");
7
+ const position_1 = require("../utils/position");
8
+ const SPACE_DELTA = 1;
9
+ const HEIGHT_DELTA = 32;
10
+ /**
11
+ * Finds the first available position for a new responsive element within a canvas.
12
+ * @param canvasWidth - The width of the canvas in terms of columns.
13
+ * @param newElement - The new responsive element to place on the canvas.
14
+ * @param elements - The existing elements on the canvas.
15
+ * @param device - The target device type (e.g., desktop, tablet, mobile) to calculate responsive positions.
16
+ * @returns - The column and row start positions for the new element, or `undefined` if no valid position is found.
17
+ */
18
+ function findAvailablePosition(canvasWidth, newElement, elements, device) {
19
+ const { colSpan: newWidth, rowSpan: newHeight } = newElement.position.responsive[device];
20
+ const sortedElements = elements
21
+ .slice()
22
+ .sort((a, b) => a.position.responsive[device].rowStart -
23
+ b.position.responsive[device].rowStart);
24
+ const intervalTree = new overlap_1.OverlapIntervalTree();
25
+ for (const element of sortedElements) {
26
+ const elemPosition = element.position.responsive[device];
27
+ const colEnd = elemPosition.colStart + (elemPosition.colSpan - 1);
28
+ const rowEnd = elemPosition.rowStart + elemPosition.rowSpan - 1;
29
+ if (!intervalTree.overlaps(elemPosition.colStart, colEnd, elemPosition.rowStart, rowEnd)) {
30
+ intervalTree.insert(elemPosition.colStart, colEnd, elemPosition.rowStart, rowEnd);
31
+ }
32
+ }
33
+ for (let rowStart = 1; rowStart <= Infinity - newHeight; rowStart += SPACE_DELTA) {
34
+ // height iteration will be until it finds a result
35
+ for (let colStart = 1; colStart <= canvasWidth - newWidth + 1; colStart += SPACE_DELTA) {
36
+ if (!intervalTree.overlaps(colStart, colStart + newWidth - 1, rowStart, rowStart + newHeight - 1)) {
37
+ const col = colStart === 0 ? 1 : colStart;
38
+ const row = rowStart === 0 ? 1 : rowStart;
39
+ return { colStart: col, rowStart: row };
40
+ }
41
+ }
42
+ }
43
+ return undefined;
44
+ }
45
+ exports.findAvailablePosition = findAvailablePosition;
46
+ /**
47
+ * Updates the responsive position of a given element for a specified device.
48
+ * @param element - The responsive element to update.
49
+ * @param newPosition - The new position values to update for the element.
50
+ * @param device - The target device type (e.g., desktop, tablet, mobile) for which the position is updated.
51
+ * @returns - A new `VemResponsive` object with the updated responsive position for the specified device.
52
+ */
53
+ function updateResponsivePosition(element, newPosition, device) {
54
+ var _a;
55
+ return Object.assign(Object.assign({}, element), { position: Object.assign(Object.assign({}, element.position), { responsive: Object.assign(Object.assign({}, element.position.responsive), { [device]: Object.assign(Object.assign({}, (_a = element.position.responsive) === null || _a === void 0 ? void 0 : _a[device]), newPosition) }) }) });
56
+ }
57
+ exports.updateResponsivePosition = updateResponsivePosition;
58
+ /**
59
+ * Finds available positions for a list of responsive elements on a canvas and updates their positions.
60
+ * @param canvasWidth - The width of the canvas in grid columns.
61
+ * @param newElements - The list of new responsive elements to position.
62
+ * @param elements - The list of existing responsive elements already positioned on the canvas.
63
+ * @param device - The target device type (e.g., desktop, tablet, mobile) for which positions are calculated.
64
+ * @returns - A list of responsive elements with updated positions for the specified device.
65
+ */
66
+ function findResponsiveAvailablePositions(canvasWidth, newElements, elements, device) {
67
+ const processedElements = elements.slice();
68
+ const resultElems = [];
69
+ newElements.forEach((nElement) => {
70
+ const newElement = calculateColSpan(nElement, canvasWidth, device);
71
+ const availablePosition = findAvailablePosition(canvasWidth, newElement, processedElements, device);
72
+ const newElemWithPosition = updateResponsivePosition(newElement, availablePosition, device);
73
+ resultElems.push(newElemWithPosition);
74
+ processedElements.push(newElemWithPosition);
75
+ });
76
+ return resultElems;
77
+ }
78
+ exports.findResponsiveAvailablePositions = findResponsiveAvailablePositions;
79
+ /**
80
+ * Adjusts the `colSpan` of a responsive element to fit within the specified canvas width and updates its position.
81
+ * @param element - The responsive element to adjust.
82
+ * @param canvasWidth - The width of the canvas in grid columns.
83
+ * @param canvasDevice - The target device type (e.g., desktop, tablet, mobile).
84
+ * @returns - The updated responsive element with an adjusted `colSpan`.
85
+ */
86
+ function calculateColSpan(element, canvasWidth, canvasDevice) {
87
+ const currentPosition = element.position.responsive[canvasDevice];
88
+ const fixedColSpan = Math.min(currentPosition.colSpan, canvasWidth);
89
+ const position = Object.assign(Object.assign({}, currentPosition), { colSpan: fixedColSpan });
90
+ return (0, position_1.setElementPosition)(element, IVemPosition_1.VemPositionType.RESPONSIVE, canvasDevice, position);
91
+ }
92
+ exports.calculateColSpan = calculateColSpan;
93
+ const getResponsiveGridElementsBottomLimit = (elements, device, rowGap) => {
94
+ if (!elements.length)
95
+ return 0;
96
+ const mappedElements = elements.map((element) => {
97
+ const elementPosition = element.position.responsive;
98
+ const maxRows = elementPosition[device].rowStart + elementPosition[device].rowSpan;
99
+ return maxRows * HEIGHT_DELTA + (maxRows - 1) * rowGap;
100
+ });
101
+ return Math.max(...mappedElements);
102
+ };
103
+ exports.getResponsiveGridElementsBottomLimit = getResponsiveGridElementsBottomLimit;
104
+ /**
105
+ * Calculates the distances of elements from a reference point and returns a list of objects
106
+ * containing the calculated distance and the corresponding element.
107
+ * @param elements - An array of `VemResponsive` objects. Each object represents an element with
108
+ * a `position` property that contains responsive positions for various devices.
109
+ * @param device - The specific device type (key of `CanvasDevice`) to determine the responsive position.
110
+ * @param referencePoint - An object representing the reference point with `x` and `y` coordinates.
111
+ * @param referencePoint.x
112
+ * @param referencePoint.y
113
+ * @returns - An array of objects, where each object contains:
114
+ * - `distance`: The squared Euclidean distance from the reference point.
115
+ * - `element`: The corresponding `VemResponsive` object.
116
+ */
117
+ const calculateDistancesToReferencePoint = (elements, device, referencePoint) => {
118
+ return elements.map((element) => {
119
+ var _a, _b;
120
+ const position = (_b = (_a = element.position) === null || _a === void 0 ? void 0 : _a.responsive) === null || _b === void 0 ? void 0 : _b[device];
121
+ if (!position) {
122
+ throw new Error(`Position for device ${device} is undefined for element with Id ${element.elementId}.`);
123
+ }
124
+ const distance = Math.pow(referencePoint.x - position.colStart, 2) +
125
+ Math.pow(referencePoint.y - position.rowStart, 2);
126
+ return {
127
+ distance,
128
+ element,
129
+ };
130
+ });
131
+ };
132
+ exports.calculateDistancesToReferencePoint = calculateDistancesToReferencePoint;
133
+ /**
134
+ /**
135
+ Sorts objects containing a `distance` and `element` by distance in ascending order.
136
+ If two objects have the same distance, it resolves the tie by comparing the `colStart` property
137
+ of their responsive positions for the `DESKTOP` device.
138
+ * @param a - An object containing:
139
+ * - `distance`: The calculated distance from a reference point.
140
+ * - `element`: A `VemResponsive` object with responsive position data.
141
+ * @param b - Another object with the same structure as `a`.
142
+ * @param a.element
143
+ * @param a.distance
144
+ * @param _device - The device type (of type `CanvasDevice`). Currently unused in this function.
145
+ * @param b.distance
146
+ * @param b.element
147
+ * @returns - A negative number if `a` should come before `b`,
148
+ * a positive number if `a` should come after `b`,
149
+ * or 0 if they have the same distance and `colStart` values.
150
+ */
151
+ const sortByDistanceInResponsiveFunction = (a, b, _device) => {
152
+ if (a.distance === b.distance) {
153
+ return (a.element.position.responsive[ICanvasGrid_1.CanvasDevice.DESKTOP].colStart -
154
+ b.element.position.responsive[ICanvasGrid_1.CanvasDevice.DESKTOP].colStart);
155
+ }
156
+ return a.distance - b.distance;
157
+ };
158
+ exports.sortByDistanceInResponsiveFunction = sortByDistanceInResponsiveFunction;
159
+ const sortResponsiveElements = (elements, sortByDevice) => {
160
+ const sortedElements = (0, exports.calculateDistancesToReferencePoint)(elements, sortByDevice, {
161
+ x: 1,
162
+ y: 1,
163
+ }).sort((a, b) => (0, exports.sortByDistanceInResponsiveFunction)(a, b, sortByDevice));
164
+ return sortedElements.map((element) => element.element);
165
+ };
166
+ exports.sortResponsiveElements = sortResponsiveElements;