Browse Source

Resolved PR comments

pull/15908/head
Maksym Tsymbarov 3 months ago
parent
commit
87f69ab9a5
  1. 46
      ui-ngx/src/app/core/services/dashboard-utils.service.ts
  2. 5
      ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts

46
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) {

5
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;

Loading…
Cancel
Save