Browse Source

Improve layout handling

pull/2411/head
Igor Kulikov 7 years ago
parent
commit
8b4ded7d80
  1. 8
      ui-ngx/src/app/modules/home/components/dashboard/dashboard.component.ts
  2. 7
      ui-ngx/src/app/modules/home/components/widget/widget.component.ts
  3. 34
      ui-ngx/src/app/modules/home/models/dashboard-component.models.ts
  4. 9
      ui-ngx/src/app/modules/home/models/widget-component.models.ts

8
ui-ngx/src/app/modules/home/components/dashboard/dashboard.component.ts

@ -19,7 +19,7 @@ import {
Component, Component,
DoCheck, DoCheck,
Input, Input,
IterableDiffers, IterableDiffers, KeyValueDiffers,
NgZone, NgZone,
OnChanges, OnChanges,
OnDestroy, OnDestroy,
@ -42,7 +42,7 @@ import {
IDashboardComponent IDashboardComponent
} from '../../models/dashboard-component.models'; } from '../../models/dashboard-component.models';
import { ReplaySubject, Subject, Subscription } from 'rxjs'; import { ReplaySubject, Subject, Subscription } from 'rxjs';
import { WidgetLayouts } from '@shared/models/dashboard.models'; import { WidgetLayout, WidgetLayouts } from '@shared/models/dashboard.models';
import { DialogService } from '@core/services/dialog.service'; import { DialogService } from '@core/services/dialog.service';
import { animatedScroll, deepClone, isDefined } from '@app/core/utils'; import { animatedScroll, deepClone, isDefined } from '@app/core/utils';
import { BreakpointObserver } from '@angular/cdk/layout'; import { BreakpointObserver } from '@angular/cdk/layout';
@ -154,7 +154,8 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
dashboardWidgets = new DashboardWidgets(this, dashboardWidgets = new DashboardWidgets(this,
this.differs.find([]).create<Widget>((index, item) => { this.differs.find([]).create<Widget>((index, item) => {
return item; return item;
}) }),
this.kvDiffers.find([]).create<string, WidgetLayout>()
); );
breakpointObserverSubscription: Subscription; breakpointObserverSubscription: Subscription;
@ -168,6 +169,7 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
private dialogService: DialogService, private dialogService: DialogService,
private breakpointObserver: BreakpointObserver, private breakpointObserver: BreakpointObserver,
private differs: IterableDiffers, private differs: IterableDiffers,
private kvDiffers: KeyValueDiffers,
private ngZone: NgZone) { private ngZone: NgZone) {
super(store); super(store);
this.authUser = getCurrentAuthUser(store); this.authUser = getCurrentAuthUser(store);

7
ui-ngx/src/app/modules/home/components/widget/widget.component.ts

@ -529,7 +529,9 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
this.cafs.reinit = null; this.cafs.reinit = null;
} }
this.cafs.reinit = this.raf.raf(() => { this.cafs.reinit = this.raf.raf(() => {
this.reInitImpl(); this.ngZone.run(() => {
this.reInitImpl();
});
}); });
} }
@ -541,6 +543,7 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
if (this.destroyed) { if (this.destroyed) {
this.onDestroy(); this.onDestroy();
} else { } else {
this.widgetContext.reset();
this.subscriptionInited = true; this.subscriptionInited = true;
this.configureDynamicWidgetComponent(); this.configureDynamicWidgetComponent();
this.cd.detectChanges(); this.cd.detectChanges();
@ -551,12 +554,14 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
if (this.destroyed) { if (this.destroyed) {
this.onDestroy(); this.onDestroy();
} else { } else {
this.widgetContext.reset();
this.subscriptionInited = true; this.subscriptionInited = true;
this.onInit(); this.onInit();
} }
} }
); );
} else { } else {
this.widgetContext.reset();
this.subscriptionInited = true; this.subscriptionInited = true;
this.configureDynamicWidgetComponent(); this.configureDynamicWidgetComponent();
this.cd.detectChanges(); this.cd.detectChanges();

34
ui-ngx/src/app/modules/home/models/dashboard-component.models.ts

@ -101,15 +101,16 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
} }
constructor(private dashboard: IDashboardComponent, constructor(private dashboard: IDashboardComponent,
private widgetsDiffer: IterableDiffer<Widget>) { private widgetsDiffer: IterableDiffer<Widget>,
private widgetLayoutsDiffer: KeyValueDiffer<string, WidgetLayout>) {
} }
doCheck() { doCheck() {
const widgetChange = this.widgetsDiffer.diff(this.widgets); const widgetChange = this.widgetsDiffer.diff(this.widgets);
if (widgetChange !== null) { const widgetLayoutChange = this.widgetLayoutsDiffer.diff(this.widgetLayouts);
const updateRecords: Array<DashboardWidgetUpdateRecord> = [];
const updateRecords: Array<DashboardWidgetUpdateRecord> = [];
if (widgetChange !== null) {
widgetChange.forEachAddedItem((added) => { widgetChange.forEachAddedItem((added) => {
updateRecords.push({ updateRecords.push({
widget: added.item, widget: added.item,
@ -130,6 +131,25 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
updateRecords.push(operation); updateRecords.push(operation);
} }
}); });
}
if (widgetLayoutChange !== null) {
widgetLayoutChange.forEachChangedItem((changed) => {
let operation = updateRecords.find((record) => record.widgetId === changed.key);
if (!operation) {
let index = this.dashboardWidgets.findIndex((dashboardWidget) => dashboardWidget.widgetId === changed.key);
if (index > -1) {
const widget = this.dashboardWidgets[index];
updateRecords.push({
widget: widget.widget,
widgetId: changed.key,
widgetLayout: changed.currentValue,
operation: 'update'
});
}
}
});
}
if (updateRecords.length) {
updateRecords.forEach((record) => { updateRecords.forEach((record) => {
switch (record.operation) { switch (record.operation) {
case 'add': case 'add':
@ -147,7 +167,7 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
index = this.dashboardWidgets.findIndex((dashboardWidget) => dashboardWidget.widgetId === record.widgetId); index = this.dashboardWidgets.findIndex((dashboardWidget) => dashboardWidget.widgetId === record.widgetId);
if (index > -1) { if (index > -1) {
const prevDashboardWidget = this.dashboardWidgets[index]; const prevDashboardWidget = this.dashboardWidgets[index];
if (!deepEqual(prevDashboardWidget.widget, record.widget)) { if (!deepEqual(prevDashboardWidget.widget, record.widget) || !deepEqual(prevDashboardWidget.widgetLayout, record.widgetLayout)) {
this.dashboardWidgets[index] = new DashboardWidget(this.dashboard, record.widget, record.widgetLayout); this.dashboardWidgets[index] = new DashboardWidget(this.dashboard, record.widget, record.widgetLayout);
this.dashboardWidgets[index].highlighted = prevDashboardWidget.highlighted; this.dashboardWidgets[index].highlighted = prevDashboardWidget.highlighted;
this.dashboardWidgets[index].selected = prevDashboardWidget.selected; this.dashboardWidgets[index].selected = prevDashboardWidget.selected;
@ -159,9 +179,7 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
break; break;
} }
}); });
if (updateRecords.length) { this.updateRowsAndSort();
this.updateRowsAndSort();
}
} }
} }

9
ui-ngx/src/app/modules/home/models/widget-component.models.ts

@ -130,6 +130,15 @@ export class WidgetContext {
} }
} }
reset() {
this.destroyed = false;
this.hideTitlePanel = false;
this.widgetTitleTemplate = undefined;
this.widgetTitle = undefined;
this.customHeaderActions = undefined;
this.widgetActions = undefined;
}
inited = false; inited = false;
destroyed = false; destroyed = false;

Loading…
Cancel
Save