@qrvey/utils 1.15.0-9 → 1.16.0-0

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 (147) hide show
  1. package/dist/cjs/columns/constants/COLUMN_ICON.js +32 -32
  2. package/dist/cjs/columns/helpers/index.d.ts +1 -0
  3. package/dist/cjs/columns/helpers/index.js +1 -0
  4. package/dist/cjs/columns/helpers/isNumericalColumn.d.ts +1 -1
  5. package/dist/cjs/columns/helpers/isNumericalColumn.js +1 -1
  6. package/dist/cjs/columns/helpers/isStringColumn.d.ts +7 -0
  7. package/dist/cjs/columns/helpers/isStringColumn.js +19 -0
  8. package/dist/cjs/dates/constants/DATE_DISTINCT_PROPERTY.d.ts +2 -1
  9. package/dist/cjs/dates/constants/DATE_DISTINCT_PROPERTY.js +1 -0
  10. package/dist/cjs/dates/relative/RelativeStatementAdapter.js +2 -1
  11. package/dist/cjs/elements/helpers/fixed.d.ts +96 -0
  12. package/dist/cjs/elements/helpers/fixed.js +248 -0
  13. package/dist/cjs/elements/helpers/gridStrategy.d.ts +29 -0
  14. package/dist/cjs/elements/helpers/gridStrategy.js +83 -0
  15. package/dist/cjs/elements/helpers/index.d.ts +3 -0
  16. package/dist/cjs/elements/helpers/index.js +22 -0
  17. package/dist/cjs/elements/helpers/responsive.d.ts +87 -0
  18. package/dist/cjs/elements/helpers/responsive.js +166 -0
  19. package/dist/cjs/elements/index.d.ts +2 -0
  20. package/dist/cjs/elements/index.js +18 -0
  21. package/dist/cjs/elements/interfaces/ICanvasGrid.d.ts +5 -0
  22. package/dist/cjs/elements/interfaces/ICanvasGrid.js +9 -0
  23. package/dist/cjs/elements/interfaces/IFixedPosition.d.ts +5 -0
  24. package/dist/cjs/elements/interfaces/IFixedPosition.js +2 -0
  25. package/dist/cjs/elements/interfaces/IVemCore.d.ts +29 -0
  26. package/dist/cjs/elements/interfaces/IVemCore.js +3 -0
  27. package/dist/cjs/elements/interfaces/IVemPosition.d.ts +44 -0
  28. package/dist/cjs/elements/interfaces/IVemPosition.js +8 -0
  29. package/dist/cjs/elements/interfaces/index.d.ts +4 -0
  30. package/dist/cjs/elements/interfaces/index.js +20 -0
  31. package/dist/cjs/elements/utils/element.d.ts +4 -0
  32. package/dist/cjs/elements/utils/element.js +28 -0
  33. package/dist/cjs/elements/utils/fixed.position.d.ts +12 -0
  34. package/dist/cjs/elements/utils/fixed.position.js +86 -0
  35. package/dist/cjs/elements/utils/general.d.ts +15 -0
  36. package/dist/cjs/elements/utils/general.js +20 -0
  37. package/dist/cjs/elements/utils/overlap.d.ts +63 -0
  38. package/dist/cjs/elements/utils/overlap.js +99 -0
  39. package/dist/cjs/elements/utils/position.d.ts +4 -0
  40. package/dist/cjs/elements/utils/position.js +5 -0
  41. package/dist/cjs/filters/adapters/FDToLogic.js +2 -1
  42. package/dist/cjs/filters/helpers/backend/buildExpression.js +1 -0
  43. package/dist/cjs/filters/helpers/backend/buildFilters.d.ts +5 -0
  44. package/dist/cjs/filters/helpers/backend/buildFilters.js +23 -1
  45. package/dist/cjs/filters/helpers/backend/getBackendValues.js +4 -1
  46. package/dist/cjs/filters/helpers/getAvailableScopesIDsByConfig.js +1 -0
  47. package/dist/cjs/filters/interfaces/backend/IFBExpression.js +0 -1
  48. package/dist/cjs/general/array/getUniqueSimpleArray.d.ts +6 -0
  49. package/dist/cjs/general/array/getUniqueSimpleArray.js +14 -0
  50. package/dist/cjs/general/array/index.d.ts +1 -0
  51. package/dist/cjs/general/array/index.js +1 -0
  52. package/dist/cjs/general/string/index.d.ts +4 -0
  53. package/dist/cjs/general/string/index.js +4 -0
  54. package/dist/cjs/general/string/secureHTML.d.ts +8 -0
  55. package/dist/cjs/general/string/secureHTML.js +13 -0
  56. package/dist/cjs/general/string/strategies/XSSEstrictedSanitizer.d.ts +8 -0
  57. package/dist/cjs/general/string/strategies/XSSEstrictedSanitizer.js +23 -0
  58. package/dist/cjs/general/string/strategies/XSSSanitizer.d.ts +8 -0
  59. package/dist/cjs/general/string/strategies/XSSSanitizer.js +19 -0
  60. package/dist/cjs/general/string/urlValidator.d.ts +10 -0
  61. package/dist/cjs/general/string/urlValidator.js +38 -0
  62. package/dist/cjs/globalization/helpers/getI18nDateGroupLabel.d.ts +6 -0
  63. package/dist/cjs/globalization/helpers/getI18nDateGroupLabel.js +120 -0
  64. package/dist/cjs/globalization/interfaces/chart_builder/II18nChartBuilderSorting.d.ts +2 -0
  65. package/dist/cjs/globalization/interfaces/export/II18nExportingModal.d.ts +6 -0
  66. package/dist/cjs/globalization/interfaces/export/II18nExportingModalApplyButtonLabel.d.ts +1 -0
  67. package/dist/cjs/globalization/labels/chart_builder/I18N_CHART_BUILDER_GENERAL_OPTIONS.js +2 -0
  68. package/dist/cjs/globalization/labels/export/I18N_EXPORT_MODAL.js +10 -1
  69. package/dist/cjs/index.d.ts +1 -0
  70. package/dist/cjs/index.js +1 -0
  71. package/dist/cjs/interfaces/SatinizerStrategy.d.ts +3 -0
  72. package/dist/cjs/interfaces/SatinizerStrategy.js +2 -0
  73. package/dist/cjs/themes/constants/AUTOMATIC_GLOBAL_CSS_VARIABLES_FOR_LIGHT.js +3 -3
  74. package/dist/columns/constants/COLUMN_ICON.js +32 -32
  75. package/dist/columns/helpers/index.d.ts +1 -0
  76. package/dist/columns/helpers/index.js +1 -0
  77. package/dist/columns/helpers/isNumericalColumn.d.ts +1 -1
  78. package/dist/columns/helpers/isNumericalColumn.js +1 -1
  79. package/dist/columns/helpers/isStringColumn.d.ts +7 -0
  80. package/dist/columns/helpers/isStringColumn.js +15 -0
  81. package/dist/dates/constants/DATE_DISTINCT_PROPERTY.d.ts +2 -1
  82. package/dist/dates/constants/DATE_DISTINCT_PROPERTY.js +1 -0
  83. package/dist/dates/relative/RelativeStatementAdapter.js +2 -1
  84. package/dist/elements/helpers/fixed.d.ts +96 -0
  85. package/dist/elements/helpers/fixed.js +236 -0
  86. package/dist/elements/helpers/gridStrategy.d.ts +29 -0
  87. package/dist/elements/helpers/gridStrategy.js +78 -0
  88. package/dist/elements/helpers/index.d.ts +3 -0
  89. package/dist/elements/helpers/index.js +3 -0
  90. package/dist/elements/helpers/responsive.d.ts +87 -0
  91. package/dist/elements/helpers/responsive.js +155 -0
  92. package/dist/elements/index.d.ts +2 -0
  93. package/dist/elements/index.js +2 -0
  94. package/dist/elements/interfaces/ICanvasGrid.d.ts +5 -0
  95. package/dist/elements/interfaces/ICanvasGrid.js +6 -0
  96. package/dist/elements/interfaces/IFixedPosition.d.ts +5 -0
  97. package/dist/elements/interfaces/IFixedPosition.js +1 -0
  98. package/dist/elements/interfaces/IVemCore.d.ts +29 -0
  99. package/dist/elements/interfaces/IVemCore.js +1 -0
  100. package/dist/elements/interfaces/IVemPosition.d.ts +44 -0
  101. package/dist/elements/interfaces/IVemPosition.js +5 -0
  102. package/dist/elements/interfaces/index.d.ts +4 -0
  103. package/dist/elements/interfaces/index.js +4 -0
  104. package/dist/elements/utils/element.d.ts +4 -0
  105. package/dist/elements/utils/element.js +24 -0
  106. package/dist/elements/utils/fixed.position.d.ts +12 -0
  107. package/dist/elements/utils/fixed.position.js +72 -0
  108. package/dist/elements/utils/general.d.ts +15 -0
  109. package/dist/elements/utils/general.js +16 -0
  110. package/dist/elements/utils/overlap.d.ts +63 -0
  111. package/dist/elements/utils/overlap.js +95 -0
  112. package/dist/elements/utils/position.d.ts +4 -0
  113. package/dist/elements/utils/position.js +1 -0
  114. package/dist/filters/adapters/FDToLogic.js +2 -1
  115. package/dist/filters/helpers/backend/buildExpression.js +1 -0
  116. package/dist/filters/helpers/backend/buildFilters.d.ts +5 -0
  117. package/dist/filters/helpers/backend/buildFilters.js +23 -1
  118. package/dist/filters/helpers/backend/getBackendValues.js +4 -1
  119. package/dist/filters/helpers/getAvailableScopesIDsByConfig.js +1 -0
  120. package/dist/filters/interfaces/backend/IFBExpression.js +0 -1
  121. package/dist/general/array/getUniqueSimpleArray.d.ts +6 -0
  122. package/dist/general/array/getUniqueSimpleArray.js +10 -0
  123. package/dist/general/array/index.d.ts +1 -0
  124. package/dist/general/array/index.js +1 -0
  125. package/dist/general/string/index.d.ts +4 -0
  126. package/dist/general/string/index.js +4 -0
  127. package/dist/general/string/secureHTML.d.ts +8 -0
  128. package/dist/general/string/secureHTML.js +9 -0
  129. package/dist/general/string/strategies/XSSEstrictedSanitizer.d.ts +8 -0
  130. package/dist/general/string/strategies/XSSEstrictedSanitizer.js +19 -0
  131. package/dist/general/string/strategies/XSSSanitizer.d.ts +8 -0
  132. package/dist/general/string/strategies/XSSSanitizer.js +15 -0
  133. package/dist/general/string/urlValidator.d.ts +10 -0
  134. package/dist/general/string/urlValidator.js +34 -0
  135. package/dist/globalization/helpers/getI18nDateGroupLabel.d.ts +6 -0
  136. package/dist/globalization/helpers/getI18nDateGroupLabel.js +120 -0
  137. package/dist/globalization/interfaces/chart_builder/II18nChartBuilderSorting.d.ts +2 -0
  138. package/dist/globalization/interfaces/export/II18nExportingModal.d.ts +6 -0
  139. package/dist/globalization/interfaces/export/II18nExportingModalApplyButtonLabel.d.ts +1 -0
  140. package/dist/globalization/labels/chart_builder/I18N_CHART_BUILDER_GENERAL_OPTIONS.js +2 -0
  141. package/dist/globalization/labels/export/I18N_EXPORT_MODAL.js +10 -1
  142. package/dist/index.d.ts +1 -0
  143. package/dist/index.js +1 -0
  144. package/dist/interfaces/SatinizerStrategy.d.ts +3 -0
  145. package/dist/interfaces/SatinizerStrategy.js +1 -0
  146. package/dist/themes/constants/AUTOMATIC_GLOBAL_CSS_VARIABLES_FOR_LIGHT.js +3 -3
  147. package/package.json +3 -2
@@ -0,0 +1,236 @@
1
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
2
+ import { CanvasDevice } from "../interfaces/ICanvasGrid";
3
+ import { VemPositionType } from "../interfaces/IVemPosition";
4
+ import { dxGetFixedGridItemDevice, hasOverflow, hasOverlap, } from "../utils/fixed.position";
5
+ import { sortByDistanceFunction } from "../utils/general";
6
+ import { OverlapIntervalTree } from "../utils/overlap";
7
+ import { setElementPosition } from "../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
+ export 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
+ /**
21
+ * Finds the first available position within a canvas where a new element can be placed without overlapping existing elements.
22
+ * @param canvasWidth - The width of the canvas in pixels.
23
+ * @param newElement - The new element to be placed, with fixed position properties.
24
+ * @param elements - The list of existing elements on the canvas.
25
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
26
+ * @param [options] - Additional options for position finding.
27
+ * @param [options.canvasHeight] - The maximum height of the canvas in pixels. Defaults to `Infinity`.
28
+ * @param [options.findRightToLeft] - Whether to search for positions from right to left. Defaults to `false`.
29
+ * @param [options.initialY] - The initial Y-coordinate to start the search. Defaults to `0`.
30
+ * @returns - The x and y coordinates of the first available position.
31
+ * @throws {Error} - Throws an error if no valid position is found and `canvasHeight` is not specified.
32
+ */
33
+ export function findAvailablePosition(canvasWidth, newElement, elements, device, options = {}) {
34
+ var _a, _b;
35
+ const { width: newWidth, height: newHeight } = getFixedPositionByDevice(newElement, device);
36
+ const canvasHeight = (_a = options.canvasHeight) !== null && _a !== void 0 ? _a : Infinity;
37
+ const sortedElements = options.findRightToLeft
38
+ ? elements
39
+ .slice()
40
+ .sort((b, a) => b.position.fixed[device].x - a.position.fixed[device].x)
41
+ : elements
42
+ .slice()
43
+ .sort((a, b) => a.position.fixed[device].y - b.position.fixed[device].y);
44
+ const intervalTree = new OverlapIntervalTree();
45
+ for (const element of sortedElements) {
46
+ const elemPosition = element.position.fixed[device];
47
+ intervalTree.insert(elemPosition.x, elemPosition.x + elemPosition.width, elemPosition.y, elemPosition.y + elemPosition.height);
48
+ }
49
+ let startX = options.findRightToLeft ? canvasWidth - newWidth : 0;
50
+ const initialY = (_b = options.initialY) !== null && _b !== void 0 ? _b : 0;
51
+ let increment = options.findRightToLeft ? -SPACE_DELTA : SPACE_DELTA;
52
+ for (let y = initialY; y <= canvasHeight - newHeight; y += SPACE_DELTA) {
53
+ let x = startX;
54
+ while (x >= 0 && x <= canvasWidth - newWidth) {
55
+ if (!intervalTree.overlaps(x, x + newWidth, y, y + newHeight)) {
56
+ return { x, y };
57
+ }
58
+ x += increment;
59
+ }
60
+ if (options.findRightToLeft) {
61
+ startX = 0;
62
+ increment = SPACE_DELTA;
63
+ }
64
+ }
65
+ if (options.canvasHeight)
66
+ return { x: 0, y: 0 };
67
+ throw new Error("No valid position found");
68
+ }
69
+ export const getFixedPositionByDevice = (item, device) => {
70
+ const fixedPosition = item.position.fixed;
71
+ const devicePosition = fixedPosition[device];
72
+ return isValidPosition(devicePosition) ? devicePosition : undefined;
73
+ };
74
+ const isValidPosition = (position) => {
75
+ return ((position === null || position === void 0 ? void 0 : position.y) !== undefined &&
76
+ position.x !== undefined &&
77
+ position.z !== undefined &&
78
+ position.height !== undefined &&
79
+ position.width !== undefined);
80
+ };
81
+ /**
82
+ * Finds available positions for multiple new elements within a canvas, ensuring they do not overlap with existing elements.
83
+ * @param canvasWidth - The width of the canvas in pixels.
84
+ * @param newElements - The list of new elements to be positioned, each with fixed position properties.
85
+ * @param elements - The list of existing elements on the canvas.
86
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
87
+ * @param [startWithPositionY] - Whether to start the position search for each new element based on its initial Y-coordinate. Defaults to `false`.
88
+ * @param [findRightToLeft] - Whether to search for positions from right to left. Defaults to `false`.
89
+ * @param [canvasHeight] - The maximum height of the canvas in pixels. If not provided, the search height is unlimited.
90
+ * @returns List of elements with their recalculated positions.
91
+ */
92
+ export function findFixedAvailablePositions(canvasWidth, newElements, elements, device, startWithPositionY = false, findRightToLeft = false, canvasHeight) {
93
+ const processedElements = elements.slice();
94
+ const resultElems = [];
95
+ newElements.forEach((nElement) => {
96
+ const initialY = startWithPositionY
97
+ ? nElement.position.fixed[device].y
98
+ : 0;
99
+ const newElement = updateFixedElementWidth(nElement, canvasWidth, device);
100
+ const availablePosition = findAvailablePosition(canvasWidth, newElement, processedElements, device, { initialY, findRightToLeft, canvasHeight });
101
+ const newElemWithPosition = updateFixedPosition(newElement, availablePosition, device);
102
+ resultElems.push(newElemWithPosition);
103
+ processedElements.push(newElemWithPosition);
104
+ });
105
+ return resultElems;
106
+ }
107
+ /**
108
+ * Recalculates the positions of elements on the canvas, preserving the position of those
109
+ * that meet certain conditions (no overflow or overlap). Elements that do not meet these
110
+ * conditions are recalculated to find a new valid position.
111
+ * @param elements - List of elements to recalculate.
112
+ * @param device - The device on which the canvas is being rendered.
113
+ * @param resolution - The resolution of the canvas.
114
+ * @param [canvasHeight] - Optional height of the canvas.
115
+ * @returns - List of elements with their recalculated positions.
116
+ */
117
+ export const recalculatePreservingPositions = (elements, device, resolution, canvasHeight) => {
118
+ const keepElementPositions = getElementsWithKeepPosition([...elements], device, resolution, canvasHeight);
119
+ const elementIds = keepElementPositions.map((element) => element.elementId);
120
+ const elementsToCalculate = elements.filter((cElement) => !elementIds.includes(cElement.elementId));
121
+ const defaultElements = keepElementPositions.length
122
+ ? keepElementPositions
123
+ : [];
124
+ const currentElements = canvasHeight ? defaultElements : elements;
125
+ const newElements = findFixedAvailablePositions(resolution, elementsToCalculate, currentElements, device, false, false, canvasHeight);
126
+ return [...keepElementPositions, ...newElements];
127
+ };
128
+ /**
129
+ * Filters elements that should retain their current position on the canvas by checking
130
+ * that they do not have overflow (spilling outside the canvas) or overlap (colliding with other elements).
131
+ * @param elements - List of elements to check.
132
+ * @param device - The device on which the canvas is being rendered.
133
+ * @param resolution - The resolution of the canvas.
134
+ * @param [canvasHeight] - Optional height of the canvas.
135
+ * @returns - List of elements that can retain their current position.
136
+ */
137
+ const getElementsWithKeepPosition = (elements, device, resolution, canvasHeight) => {
138
+ const calculatedElements = elements.map((mElement) => {
139
+ const desktopPosition = dxGetFixedGridItemDevice(mElement, CanvasDevice.DESKTOP);
140
+ return updateFixedPosition(mElement, desktopPosition, device);
141
+ });
142
+ return calculatedElements.filter((cElement) => {
143
+ const currentElements = calculatedElements.filter((fElement) => fElement.elementId !== cElement.elementId);
144
+ const overflow = hasOverflow(cElement, device, resolution, canvasHeight);
145
+ const overlap = hasOverlap(cElement, currentElements, device);
146
+ return !overflow && !overlap;
147
+ });
148
+ };
149
+ /**
150
+ * Adjusts the `width` of a fixed element to fit within the specified canvas width and updates its position.
151
+ * @param element - The fixed element to adjust.
152
+ * @param canvasWidth - The width of the canvas in grid columns.
153
+ * @param canvasDevice - The target device type (e.g., desktop, tablet, mobile).
154
+ * @returns - The updated responsive element with an adjusted `width`.
155
+ */
156
+ export function updateFixedElementWidth(element, canvasWidth, canvasDevice) {
157
+ const currentPosition = element.position.fixed[canvasDevice];
158
+ const fixedWidth = Math.min(currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.width, canvasWidth);
159
+ const position = Object.assign(Object.assign({}, currentPosition), { width: fixedWidth });
160
+ return setElementPosition(element, VemPositionType.FIXED, canvasDevice, position);
161
+ }
162
+ /**
163
+ * Retrieves the Y-coordinate of a fixed grid element for a specific device.
164
+ * If `allowNegative` is false (default), the function ensures the Y-coordinate is non-negative.
165
+ * @param element - The element to retrieve the Y-coordinate from.
166
+ * @param device - The device for which the Y-coordinate is calculated.
167
+ * @param [allowNegative] - Whether to allow negative Y-coordinates.
168
+ * @returns - The Y-coordinate of the element.
169
+ */
170
+ const getFixedGridElementY = (element, device, allowNegative = false) => {
171
+ var _a;
172
+ const position = element.position.fixed[device];
173
+ const itemY = (_a = position === null || position === void 0 ? void 0 : position.y) !== null && _a !== void 0 ? _a : 0;
174
+ if (!allowNegative) {
175
+ return itemY >= 0 ? itemY : 0;
176
+ }
177
+ return itemY;
178
+ };
179
+ /**
180
+ * Retrieves the height of a fixed grid element for a specific device.
181
+ * @param element - The element to retrieve the height from.
182
+ * @param device - The device for which the height is calculated.
183
+ * @returns - The height of the element, or undefined if not defined.
184
+ */
185
+ const getFixedGridElementHeight = (element, device) => {
186
+ const position = element.position.fixed[device];
187
+ return position === null || position === void 0 ? void 0 : position.height;
188
+ };
189
+ /**
190
+ * @param elements elements to calculate
191
+ * @param device current device
192
+ * @returns height
193
+ */
194
+ export const getFixedGridElementsBottomLimit = (elements, device) => {
195
+ if (!elements.length)
196
+ return 0;
197
+ return Math.max(...elements.map((element) => getFixedGridElementY(element, device) +
198
+ getFixedGridElementHeight(element, device)));
199
+ };
200
+ /**
201
+ * Calculates distances of elements from a given reference point on the canvas device.
202
+ * @param elements - Array of `VemFixed` elements to calculate distances for.
203
+ * @param device - Canvas device providing the coordinates of the elements.
204
+ * @param referencePoint - The reference point to calculate distances from.
205
+ * @param referencePoint.x
206
+ * @param referencePoint.y
207
+ * @returns An array of objects containing the original elements and their calculated distances from the reference point.
208
+ */
209
+ export const calculateDistancesToReferencePoint = (elements, device, referencePoint) => {
210
+ return elements.map((element) => {
211
+ const elementPosition = getFixedPositionByDevice(element, device);
212
+ if (!elementPosition) {
213
+ throw new Error(`Position for device ${device} is undefined for element with Id ${element.elementId}.`);
214
+ }
215
+ const distance = Math.pow(referencePoint.x - elementPosition.x, 2) +
216
+ Math.pow(referencePoint.y - elementPosition.y, 2);
217
+ return {
218
+ distance,
219
+ element,
220
+ };
221
+ });
222
+ };
223
+ /**
224
+ * Sorts elements based on their distance from a reference point on the canvas.
225
+ * @param elements - Array of `VemFixed` elements to be sorted.
226
+ * @param device - Canvas device providing the coordinates of the elements.
227
+ * @param referencePoint - The reference point to calculate distances from.
228
+ * @param referencePoint.x
229
+ * @param referencePoint.y
230
+ * @returns An array of `VemFixed` elements sorted by their distance from the reference point (0, 0).
231
+ */
232
+ export const sortElementsFromReferencePoint = (elements, device, referencePoint) => {
233
+ const calculatedElements = calculateDistancesToReferencePoint(elements, device, referencePoint);
234
+ const sortedElements = [...calculatedElements].sort(sortByDistanceFunction);
235
+ return sortedElements.map((element) => element.element);
236
+ };
@@ -0,0 +1,29 @@
1
+ import { CanvasDevice, Vem } from "../interfaces";
2
+ interface RecalculateOptions {
3
+ canvasHeight?: number;
4
+ sort?: CanvasDevice;
5
+ allowCopy?: boolean;
6
+ keepPosition?: boolean;
7
+ }
8
+ /**
9
+ * Recalculates the positions of elements on a canvas based on the canvas type.
10
+ * @param newElements - The list of elements to be recalculated. Each element has properties based on its type.
11
+ * @param currentElements - The list of elements to be recalculated. Each element has properties based on its type.
12
+ * @param canvasWidth - The width of the canvas in pixels.
13
+ * @param canvasType - The type of canvas (e.g., "fixed") that determines how positions are recalculated.
14
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
15
+ * @param canvasHeight - variable for the canvas with header component
16
+ * @param options
17
+ * @returns - A list of elements with updated positions if the canvas type is "fixed" or "responsive".
18
+ */
19
+ export declare function recalculateElements(newElements: Vem[], currentElements: Vem[], canvasWidth: number, canvasType: string, device: CanvasDevice, options?: RecalculateOptions): Vem[];
20
+ /**
21
+ * The calculation is delegated to a specific strategy based on the `canvasType`.
22
+ * @param elements - The list of elements in the canvas.
23
+ * @param canvasType - The type of canvas (e.g., "fixed", "responsive").
24
+ * @param device - The target device type (e.g., desktop, tablet, mobile).
25
+ * @param [rowGap] - The optional gap (in pixels) between rows in the canvas.
26
+ * @returns - The calculated canvas height based on the selected strategy. Returns an empty array if no strategy is found.
27
+ */
28
+ export declare function calculateCanvasHeight(elements: Vem[], canvasType: string, device: CanvasDevice, rowGap?: number): number;
29
+ export {};
@@ -0,0 +1,78 @@
1
+ import { findFixedAvailablePositions, getFixedGridElementsBottomLimit, recalculatePreservingPositions, sortElementsFromReferencePoint, } from "./fixed";
2
+ import { findResponsiveAvailablePositions, getResponsiveGridElementsBottomLimit, sortResponsiveElements, } from "./responsive";
3
+ import { CanvasDevice, VemPositionType, } from "../interfaces";
4
+ import { copyDesktopPositionToDevice } from "../utils/element";
5
+ const recalculateStrategies = {
6
+ fixed: (elements, currentElements, canvasWidth, device, options) => {
7
+ const isSorted = !currentElements.length;
8
+ const changedElements = (options.allowCopy
9
+ ? copyDesktopPositionToDevice(elements, device, VemPositionType.FIXED)
10
+ : elements);
11
+ // sort elements
12
+ const orderedElements = isSorted
13
+ ? sortElementsFromReferencePoint(changedElements, options.sort, {
14
+ x: 0,
15
+ y: 0,
16
+ })
17
+ : changedElements;
18
+ return options.canvasHeight
19
+ ? recalculatePreservingPositions(orderedElements, device, canvasWidth, options.canvasHeight)
20
+ : findFixedAvailablePositions(canvasWidth, orderedElements, currentElements, device, false, false, options === null || options === void 0 ? void 0 : options.canvasHeight);
21
+ },
22
+ responsive: (elements, currentElements, canvasWidth, device, options) => {
23
+ const isSorted = !currentElements.length;
24
+ const changedElements = (options.allowCopy
25
+ ? copyDesktopPositionToDevice(elements, device, VemPositionType.RESPONSIVE)
26
+ : elements);
27
+ const orderedElements = isSorted
28
+ ? sortResponsiveElements(changedElements, options.sort)
29
+ : changedElements;
30
+ return findResponsiveAvailablePositions(canvasWidth, orderedElements, currentElements, device);
31
+ },
32
+ };
33
+ const calculateCanvasHeightStrategies = {
34
+ fixed: (elements, device) => {
35
+ return getFixedGridElementsBottomLimit(elements, device);
36
+ },
37
+ responsive: (elements, device, rowGap) => {
38
+ return getResponsiveGridElementsBottomLimit(elements, device, rowGap);
39
+ },
40
+ };
41
+ /**
42
+ * Recalculates the positions of elements on a canvas based on the canvas type.
43
+ * @param newElements - The list of elements to be recalculated. Each element has properties based on its type.
44
+ * @param currentElements - The list of elements to be recalculated. Each element has properties based on its type.
45
+ * @param canvasWidth - The width of the canvas in pixels.
46
+ * @param canvasType - The type of canvas (e.g., "fixed") that determines how positions are recalculated.
47
+ * @param device - The device type (e.g., desktop, tablet, mobile) for determining element positions.
48
+ * @param canvasHeight - variable for the canvas with header component
49
+ * @param options
50
+ * @returns - A list of elements with updated positions if the canvas type is "fixed" or "responsive".
51
+ */
52
+ export function recalculateElements(newElements, currentElements, canvasWidth, canvasType, device, options) {
53
+ const strategy = recalculateStrategies[canvasType];
54
+ if (!strategy) {
55
+ return [];
56
+ }
57
+ const defaultOptions = {
58
+ sort: CanvasDevice.DESKTOP,
59
+ allowCopy: true,
60
+ };
61
+ const calculatedOptions = Object.assign(Object.assign({}, defaultOptions), options);
62
+ return strategy(newElements, currentElements, canvasWidth, device, calculatedOptions);
63
+ }
64
+ /**
65
+ * The calculation is delegated to a specific strategy based on the `canvasType`.
66
+ * @param elements - The list of elements in the canvas.
67
+ * @param canvasType - The type of canvas (e.g., "fixed", "responsive").
68
+ * @param device - The target device type (e.g., desktop, tablet, mobile).
69
+ * @param [rowGap] - The optional gap (in pixels) between rows in the canvas.
70
+ * @returns - The calculated canvas height based on the selected strategy. Returns an empty array if no strategy is found.
71
+ */
72
+ export function calculateCanvasHeight(elements, canvasType, device, rowGap) {
73
+ const strategy = calculateCanvasHeightStrategies[canvasType];
74
+ if (!strategy) {
75
+ return 0;
76
+ }
77
+ return strategy(elements, device, rowGap);
78
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./gridStrategy";
2
+ export { updateFixedElementWidth } from "./fixed";
3
+ export { updateResponsiveElementColSpan } from "./responsive";
@@ -0,0 +1,3 @@
1
+ export * from "./gridStrategy";
2
+ export { updateFixedElementWidth } from "./fixed";
3
+ export { updateResponsiveElementColSpan } from "./responsive";
@@ -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 updateResponsiveElementColSpan(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,155 @@
1
+ import { CanvasDevice } from "../interfaces/ICanvasGrid";
2
+ import { VemPositionType, } from "../interfaces/IVemPosition";
3
+ import { OverlapIntervalTree } from "../utils/overlap";
4
+ import { setElementPosition } from "../utils/position";
5
+ const SPACE_DELTA = 1;
6
+ const HEIGHT_DELTA = 32;
7
+ /**
8
+ * Finds the first available position for a new responsive element within a canvas.
9
+ * @param canvasWidth - The width of the canvas in terms of columns.
10
+ * @param newElement - The new responsive element to place on the canvas.
11
+ * @param elements - The existing elements on the canvas.
12
+ * @param device - The target device type (e.g., desktop, tablet, mobile) to calculate responsive positions.
13
+ * @returns - The column and row start positions for the new element, or `undefined` if no valid position is found.
14
+ */
15
+ export function findAvailablePosition(canvasWidth, newElement, elements, device) {
16
+ const { colSpan: newWidth, rowSpan: newHeight } = newElement.position.responsive[device];
17
+ const sortedElements = elements
18
+ .slice()
19
+ .sort((a, b) => a.position.responsive[device].rowStart -
20
+ b.position.responsive[device].rowStart);
21
+ const intervalTree = new OverlapIntervalTree();
22
+ for (const element of sortedElements) {
23
+ const elemPosition = element.position.responsive[device];
24
+ const colEnd = elemPosition.colStart + (elemPosition.colSpan - 1);
25
+ const rowEnd = elemPosition.rowStart + elemPosition.rowSpan - 1;
26
+ if (!intervalTree.overlaps(elemPosition.colStart, colEnd, elemPosition.rowStart, rowEnd)) {
27
+ intervalTree.insert(elemPosition.colStart, colEnd, elemPosition.rowStart, rowEnd);
28
+ }
29
+ }
30
+ for (let rowStart = 1; rowStart <= Infinity - newHeight; rowStart += SPACE_DELTA) {
31
+ // height iteration will be until it finds a result
32
+ for (let colStart = 1; colStart <= canvasWidth - newWidth + 1; colStart += SPACE_DELTA) {
33
+ if (!intervalTree.overlaps(colStart, colStart + newWidth - 1, rowStart, rowStart + newHeight - 1)) {
34
+ const col = colStart === 0 ? 1 : colStart;
35
+ const row = rowStart === 0 ? 1 : rowStart;
36
+ return { colStart: col, rowStart: row };
37
+ }
38
+ }
39
+ }
40
+ return undefined;
41
+ }
42
+ /**
43
+ * Updates the responsive position of a given element for a specified device.
44
+ * @param element - The responsive element to update.
45
+ * @param newPosition - The new position values to update for the element.
46
+ * @param device - The target device type (e.g., desktop, tablet, mobile) for which the position is updated.
47
+ * @returns - A new `VemResponsive` object with the updated responsive position for the specified device.
48
+ */
49
+ export function updateResponsivePosition(element, newPosition, device) {
50
+ var _a;
51
+ 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) }) }) });
52
+ }
53
+ /**
54
+ * Finds available positions for a list of responsive elements on a canvas and updates their positions.
55
+ * @param canvasWidth - The width of the canvas in grid columns.
56
+ * @param newElements - The list of new responsive elements to position.
57
+ * @param elements - The list of existing responsive elements already positioned on the canvas.
58
+ * @param device - The target device type (e.g., desktop, tablet, mobile) for which positions are calculated.
59
+ * @returns - A list of responsive elements with updated positions for the specified device.
60
+ */
61
+ export function findResponsiveAvailablePositions(canvasWidth, newElements, elements, device) {
62
+ const processedElements = elements.slice();
63
+ const resultElems = [];
64
+ newElements.forEach((nElement) => {
65
+ const newElement = updateResponsiveElementColSpan(nElement, canvasWidth, device);
66
+ const availablePosition = findAvailablePosition(canvasWidth, newElement, processedElements, device);
67
+ const newElemWithPosition = updateResponsivePosition(newElement, availablePosition, device);
68
+ resultElems.push(newElemWithPosition);
69
+ processedElements.push(newElemWithPosition);
70
+ });
71
+ return resultElems;
72
+ }
73
+ /**
74
+ * Adjusts the `colSpan` of a responsive element to fit within the specified canvas width and updates its position.
75
+ * @param element - The responsive element to adjust.
76
+ * @param canvasWidth - The width of the canvas in grid columns.
77
+ * @param canvasDevice - The target device type (e.g., desktop, tablet, mobile).
78
+ * @returns - The updated responsive element with an adjusted `colSpan`.
79
+ */
80
+ export function updateResponsiveElementColSpan(element, canvasWidth, canvasDevice) {
81
+ const currentPosition = element.position.responsive[canvasDevice];
82
+ const fixedColSpan = Math.min(currentPosition.colSpan, canvasWidth);
83
+ const position = Object.assign(Object.assign({}, currentPosition), { colSpan: fixedColSpan });
84
+ return setElementPosition(element, VemPositionType.RESPONSIVE, canvasDevice, position);
85
+ }
86
+ export const getResponsiveGridElementsBottomLimit = (elements, device, rowGap) => {
87
+ if (!elements.length)
88
+ return 0;
89
+ const mappedElements = elements.map((element) => {
90
+ const elementPosition = element.position.responsive;
91
+ const maxRows = elementPosition[device].rowStart + elementPosition[device].rowSpan;
92
+ return maxRows * HEIGHT_DELTA + (maxRows - 1) * rowGap;
93
+ });
94
+ return Math.max(...mappedElements);
95
+ };
96
+ /**
97
+ * Calculates the distances of elements from a reference point and returns a list of objects
98
+ * containing the calculated distance and the corresponding element.
99
+ * @param elements - An array of `VemResponsive` objects. Each object represents an element with
100
+ * a `position` property that contains responsive positions for various devices.
101
+ * @param device - The specific device type (key of `CanvasDevice`) to determine the responsive position.
102
+ * @param referencePoint - An object representing the reference point with `x` and `y` coordinates.
103
+ * @param referencePoint.x
104
+ * @param referencePoint.y
105
+ * @returns - An array of objects, where each object contains:
106
+ * - `distance`: The squared Euclidean distance from the reference point.
107
+ * - `element`: The corresponding `VemResponsive` object.
108
+ */
109
+ export const calculateDistancesToReferencePoint = (elements, device, referencePoint) => {
110
+ return elements.map((element) => {
111
+ var _a, _b;
112
+ const position = (_b = (_a = element.position) === null || _a === void 0 ? void 0 : _a.responsive) === null || _b === void 0 ? void 0 : _b[device];
113
+ if (!position) {
114
+ throw new Error(`Position for device ${device} is undefined for element with Id ${element.elementId}.`);
115
+ }
116
+ const distance = Math.pow(referencePoint.x - position.colStart, 2) +
117
+ Math.pow(referencePoint.y - position.rowStart, 2);
118
+ return {
119
+ distance,
120
+ element,
121
+ };
122
+ });
123
+ };
124
+ /**
125
+ /**
126
+ Sorts objects containing a `distance` and `element` by distance in ascending order.
127
+ If two objects have the same distance, it resolves the tie by comparing the `colStart` property
128
+ of their responsive positions for the `DESKTOP` device.
129
+ * @param a - An object containing:
130
+ * - `distance`: The calculated distance from a reference point.
131
+ * - `element`: A `VemResponsive` object with responsive position data.
132
+ * @param b - Another object with the same structure as `a`.
133
+ * @param a.element
134
+ * @param a.distance
135
+ * @param _device - The device type (of type `CanvasDevice`). Currently unused in this function.
136
+ * @param b.distance
137
+ * @param b.element
138
+ * @returns - A negative number if `a` should come before `b`,
139
+ * a positive number if `a` should come after `b`,
140
+ * or 0 if they have the same distance and `colStart` values.
141
+ */
142
+ export const sortByDistanceInResponsiveFunction = (a, b, _device) => {
143
+ if (a.distance === b.distance) {
144
+ return (a.element.position.responsive[CanvasDevice.DESKTOP].colStart -
145
+ b.element.position.responsive[CanvasDevice.DESKTOP].colStart);
146
+ }
147
+ return a.distance - b.distance;
148
+ };
149
+ export const sortResponsiveElements = (elements, sortByDevice) => {
150
+ const sortedElements = calculateDistancesToReferencePoint(elements, sortByDevice, {
151
+ x: 1,
152
+ y: 1,
153
+ }).sort((a, b) => sortByDistanceInResponsiveFunction(a, b, sortByDevice));
154
+ return sortedElements.map((element) => element.element);
155
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./helpers/index";
2
+ export * from "./interfaces/index";
@@ -0,0 +1,2 @@
1
+ export * from "./helpers/index";
2
+ export * from "./interfaces/index";
@@ -0,0 +1,5 @@
1
+ export declare enum CanvasDevice {
2
+ DESKTOP = "desktop",
3
+ TABLET = "tablet",
4
+ MOBILE = "mobile"
5
+ }
@@ -0,0 +1,6 @@
1
+ export var CanvasDevice;
2
+ (function (CanvasDevice) {
3
+ CanvasDevice["DESKTOP"] = "desktop";
4
+ CanvasDevice["TABLET"] = "tablet";
5
+ CanvasDevice["MOBILE"] = "mobile";
6
+ })(CanvasDevice || (CanvasDevice = {}));
@@ -0,0 +1,5 @@
1
+ export interface FindAvailablePositionOptions {
2
+ initialY?: number;
3
+ findRightToLeft?: boolean;
4
+ canvasHeight?: number;
5
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import { VemPositionConfigFixed, VemPositionConfigMap, VemPositionConfigResponsive, VemPositionType } from "./IVemPosition";
2
+ interface VemBase {
3
+ elementId: string;
4
+ type: string;
5
+ contentId?: string;
6
+ }
7
+ export interface VemCorePosition {
8
+ fixed?: VemPositionConfigFixed;
9
+ responsive?: VemPositionConfigResponsive;
10
+ }
11
+ interface VemCore<TPosition extends VemPositionType | unknown, TItemData = unknown> extends VemBase {
12
+ itemData: TItemData;
13
+ /** Indicates if its a personalized Vem */
14
+ isEup?: boolean;
15
+ /** Data to be saved in the client chart when it's saved by EUP (Only applies for VemAnalytics) */
16
+ chartBody?: unknown;
17
+ /** Dashboard Actions in the element */
18
+ actions?: unknown[];
19
+ position: TPosition extends VemPositionType ? VemCorePosition & Record<TPosition, VemPositionConfigMap[TPosition]> : VemCorePosition;
20
+ }
21
+ export type Vem<T = unknown, TPosition extends VemPositionType = VemPositionType> = VemCore<TPosition, T>;
22
+ export type VemPos<TPosition extends VemPositionType> = VemCore<TPosition>;
23
+ export type VemResponsive<T = unknown> = VemCore<VemPositionType.RESPONSIVE, T>;
24
+ export type VemFixed<T = unknown> = VemCore<VemPositionType.FIXED, T>;
25
+ export interface VemElementMap<T = unknown> {
26
+ [VemPositionType.FIXED]: VemFixed<T>;
27
+ [VemPositionType.RESPONSIVE]: VemResponsive<T>;
28
+ }
29
+ export {};