From 87f69ab9a5b7979c595a77d38e16b00e4e9710d9 Mon Sep 17 00:00:00 2001 From: Maksym Tsymbarov Date: Tue, 15 Sep 2026 21:50:09 +0200 Subject: [PATCH] Resolved PR comments --- .../core/services/dashboard-utils.service.ts | 46 +++++++++++++++++-- .../dashboard-page.component.ts | 5 +- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/ui-ngx/src/app/core/services/dashboard-utils.service.ts b/ui-ngx/src/app/core/services/dashboard-utils.service.ts index 285078ba5b..8ca8532de5 100644 --- a/ui-ngx/src/app/core/services/dashboard-utils.service.ts +++ b/ui-ngx/src/app/core/services/dashboard-utils.service.ts @@ -841,10 +841,14 @@ export class DashboardUtilsService { const widgets: WidgetLayout[] = []; for (const w of Object.keys(layout.widgets)) { const widget = layout.widgets[w]; - widget.row = Math.round(widget.row * ratio); - widget.col = Math.round(widget.col * ratio); - widget.sizeX = Math.max(1, Math.round(widget.sizeX * ratio)); - widget.sizeY = Math.max(1, Math.round(widget.sizeY * ratio)); + const col = widget.col || 0; + const row = widget.row || 0; + const right = Math.round((col + widget.sizeX) * ratio); + const bottom = Math.round((row + widget.sizeY) * ratio); + widget.col = Math.round(col * ratio); + widget.row = Math.round(row * ratio); + widget.sizeX = Math.max(1, right - widget.col); + widget.sizeY = Math.max(1, bottom - widget.row); widgets.push(widget); } widgets.sort((w1, w2) => { @@ -890,6 +894,40 @@ export class DashboardUtilsService { } } } + // The loop above only repairs a one unit overlap and mutates widgets it has already + // visited, so it may leave - or introduce - conflicts. Re-place whatever is still colliding. + this.resolveWidgetCollisions(widgets, gridSettings.minColumns || columns); + } + + private resolveWidgetCollisions(widgets: WidgetLayout[], columns: number) { + const placed: WidgetLayout[] = []; + for (const widget of widgets) { + if (this.hasWidgetCollision(widget.row, widget.col, widget.sizeX, widget.sizeY, placed)) { + this.findFreeWidgetPosition(widget, placed, columns); + } + placed.push(widget); + } + } + + private findFreeWidgetPosition(widgetLayout: WidgetLayout, widgetLayouts: WidgetLayout[], columns: number) { + let maxCol = columns; + let maxRow = 0; + widgetLayouts.forEach(widget => { + maxCol = Math.max(maxCol, widget.col + widget.sizeX); + maxRow = Math.max(maxRow, widget.row + widget.sizeY); + }); + for (let row = 0; row <= maxRow; row++) { + for (let col = 0; col + widgetLayout.sizeX <= maxCol; col++) { + if (!this.hasWidgetCollision(row, col, widgetLayout.sizeX, widgetLayout.sizeY, widgetLayouts)) { + widgetLayout.row = row; + widgetLayout.col = col; + return; + } + } + } + // Wider than the grid: park it on a fresh row. + widgetLayout.row = maxRow; + widgetLayout.col = 0; } public moveWidgets(layout: DashboardLayout, cols: number, rows: number) { diff --git a/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts b/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts index e674a4f9f2..3dfda2c4b5 100644 --- a/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts +++ b/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts @@ -1108,7 +1108,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC if (!this.destroyed) { const layoutsData = this.dashboardUtils.getStateLayoutsData(this.dashboard, state); if (layoutsData) { - if (this.isEdit && Object.keys(layoutsData).some(l => { + if (this.isEdit && state !== this.dashboardCtx.state && Object.keys(layoutsData).some(l => { const layout: DashboardPageLayout = this.layouts[l]; const breakpoint = (layout && layout.layoutCtx.breakpoint) || 'default'; const layoutInfo: BreakpointLayoutInfo = layoutsData[l][breakpoint] || layoutsData[l].default; @@ -1116,7 +1116,8 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC })) { this.store.dispatch(new ActionNotificationShow({ message: this.translate.instant('dashboard.state-widgets-overlap-warning'), - type: 'warn' + type: 'warn', + duration: 5000 })); } this.dashboardCtx.state = state;