Commit 8b4ded7d803d4104501cd930a0f87bc98ed746aa

Authored by Igor Kulikov
1 parent ac8a2cea

Improve layout handling

@@ -19,7 +19,7 @@ import { @@ -19,7 +19,7 @@ import {
19 Component, 19 Component,
20 DoCheck, 20 DoCheck,
21 Input, 21 Input,
22 - IterableDiffers, 22 + IterableDiffers, KeyValueDiffers,
23 NgZone, 23 NgZone,
24 OnChanges, 24 OnChanges,
25 OnDestroy, 25 OnDestroy,
@@ -42,7 +42,7 @@ import { @@ -42,7 +42,7 @@ import {
42 IDashboardComponent 42 IDashboardComponent
43 } from '../../models/dashboard-component.models'; 43 } from '../../models/dashboard-component.models';
44 import { ReplaySubject, Subject, Subscription } from 'rxjs'; 44 import { ReplaySubject, Subject, Subscription } from 'rxjs';
45 -import { WidgetLayouts } from '@shared/models/dashboard.models'; 45 +import { WidgetLayout, WidgetLayouts } from '@shared/models/dashboard.models';
46 import { DialogService } from '@core/services/dialog.service'; 46 import { DialogService } from '@core/services/dialog.service';
47 import { animatedScroll, deepClone, isDefined } from '@app/core/utils'; 47 import { animatedScroll, deepClone, isDefined } from '@app/core/utils';
48 import { BreakpointObserver } from '@angular/cdk/layout'; 48 import { BreakpointObserver } from '@angular/cdk/layout';
@@ -154,7 +154,8 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo @@ -154,7 +154,8 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
154 dashboardWidgets = new DashboardWidgets(this, 154 dashboardWidgets = new DashboardWidgets(this,
155 this.differs.find([]).create<Widget>((index, item) => { 155 this.differs.find([]).create<Widget>((index, item) => {
156 return item; 156 return item;
157 - }) 157 + }),
  158 + this.kvDiffers.find([]).create<string, WidgetLayout>()
158 ); 159 );
159 160
160 breakpointObserverSubscription: Subscription; 161 breakpointObserverSubscription: Subscription;
@@ -168,6 +169,7 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo @@ -168,6 +169,7 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
168 private dialogService: DialogService, 169 private dialogService: DialogService,
169 private breakpointObserver: BreakpointObserver, 170 private breakpointObserver: BreakpointObserver,
170 private differs: IterableDiffers, 171 private differs: IterableDiffers,
  172 + private kvDiffers: KeyValueDiffers,
171 private ngZone: NgZone) { 173 private ngZone: NgZone) {
172 super(store); 174 super(store);
173 this.authUser = getCurrentAuthUser(store); 175 this.authUser = getCurrentAuthUser(store);
@@ -529,7 +529,9 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI @@ -529,7 +529,9 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
529 this.cafs.reinit = null; 529 this.cafs.reinit = null;
530 } 530 }
531 this.cafs.reinit = this.raf.raf(() => { 531 this.cafs.reinit = this.raf.raf(() => {
532 - this.reInitImpl(); 532 + this.ngZone.run(() => {
  533 + this.reInitImpl();
  534 + });
533 }); 535 });
534 } 536 }
535 537
@@ -541,6 +543,7 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI @@ -541,6 +543,7 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
541 if (this.destroyed) { 543 if (this.destroyed) {
542 this.onDestroy(); 544 this.onDestroy();
543 } else { 545 } else {
  546 + this.widgetContext.reset();
544 this.subscriptionInited = true; 547 this.subscriptionInited = true;
545 this.configureDynamicWidgetComponent(); 548 this.configureDynamicWidgetComponent();
546 this.cd.detectChanges(); 549 this.cd.detectChanges();
@@ -551,12 +554,14 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI @@ -551,12 +554,14 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
551 if (this.destroyed) { 554 if (this.destroyed) {
552 this.onDestroy(); 555 this.onDestroy();
553 } else { 556 } else {
  557 + this.widgetContext.reset();
554 this.subscriptionInited = true; 558 this.subscriptionInited = true;
555 this.onInit(); 559 this.onInit();
556 } 560 }
557 } 561 }
558 ); 562 );
559 } else { 563 } else {
  564 + this.widgetContext.reset();
560 this.subscriptionInited = true; 565 this.subscriptionInited = true;
561 this.configureDynamicWidgetComponent(); 566 this.configureDynamicWidgetComponent();
562 this.cd.detectChanges(); 567 this.cd.detectChanges();
@@ -101,15 +101,16 @@ export class DashboardWidgets implements Iterable<DashboardWidget> { @@ -101,15 +101,16 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
101 } 101 }
102 102
103 constructor(private dashboard: IDashboardComponent, 103 constructor(private dashboard: IDashboardComponent,
104 - private widgetsDiffer: IterableDiffer<Widget>) { 104 + private widgetsDiffer: IterableDiffer<Widget>,
  105 + private widgetLayoutsDiffer: KeyValueDiffer<string, WidgetLayout>) {
105 } 106 }
106 107
107 doCheck() { 108 doCheck() {
108 const widgetChange = this.widgetsDiffer.diff(this.widgets); 109 const widgetChange = this.widgetsDiffer.diff(this.widgets);
109 - if (widgetChange !== null) {  
110 -  
111 - const updateRecords: Array<DashboardWidgetUpdateRecord> = []; 110 + const widgetLayoutChange = this.widgetLayoutsDiffer.diff(this.widgetLayouts);
  111 + const updateRecords: Array<DashboardWidgetUpdateRecord> = [];
112 112
  113 + if (widgetChange !== null) {
113 widgetChange.forEachAddedItem((added) => { 114 widgetChange.forEachAddedItem((added) => {
114 updateRecords.push({ 115 updateRecords.push({
115 widget: added.item, 116 widget: added.item,
@@ -130,6 +131,25 @@ export class DashboardWidgets implements Iterable<DashboardWidget> { @@ -130,6 +131,25 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
130 updateRecords.push(operation); 131 updateRecords.push(operation);
131 } 132 }
132 }); 133 });
  134 + }
  135 + if (widgetLayoutChange !== null) {
  136 + widgetLayoutChange.forEachChangedItem((changed) => {
  137 + let operation = updateRecords.find((record) => record.widgetId === changed.key);
  138 + if (!operation) {
  139 + let index = this.dashboardWidgets.findIndex((dashboardWidget) => dashboardWidget.widgetId === changed.key);
  140 + if (index > -1) {
  141 + const widget = this.dashboardWidgets[index];
  142 + updateRecords.push({
  143 + widget: widget.widget,
  144 + widgetId: changed.key,
  145 + widgetLayout: changed.currentValue,
  146 + operation: 'update'
  147 + });
  148 + }
  149 + }
  150 + });
  151 + }
  152 + if (updateRecords.length) {
133 updateRecords.forEach((record) => { 153 updateRecords.forEach((record) => {
134 switch (record.operation) { 154 switch (record.operation) {
135 case 'add': 155 case 'add':
@@ -147,7 +167,7 @@ export class DashboardWidgets implements Iterable<DashboardWidget> { @@ -147,7 +167,7 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
147 index = this.dashboardWidgets.findIndex((dashboardWidget) => dashboardWidget.widgetId === record.widgetId); 167 index = this.dashboardWidgets.findIndex((dashboardWidget) => dashboardWidget.widgetId === record.widgetId);
148 if (index > -1) { 168 if (index > -1) {
149 const prevDashboardWidget = this.dashboardWidgets[index]; 169 const prevDashboardWidget = this.dashboardWidgets[index];
150 - if (!deepEqual(prevDashboardWidget.widget, record.widget)) { 170 + if (!deepEqual(prevDashboardWidget.widget, record.widget) || !deepEqual(prevDashboardWidget.widgetLayout, record.widgetLayout)) {
151 this.dashboardWidgets[index] = new DashboardWidget(this.dashboard, record.widget, record.widgetLayout); 171 this.dashboardWidgets[index] = new DashboardWidget(this.dashboard, record.widget, record.widgetLayout);
152 this.dashboardWidgets[index].highlighted = prevDashboardWidget.highlighted; 172 this.dashboardWidgets[index].highlighted = prevDashboardWidget.highlighted;
153 this.dashboardWidgets[index].selected = prevDashboardWidget.selected; 173 this.dashboardWidgets[index].selected = prevDashboardWidget.selected;
@@ -159,9 +179,7 @@ export class DashboardWidgets implements Iterable<DashboardWidget> { @@ -159,9 +179,7 @@ export class DashboardWidgets implements Iterable<DashboardWidget> {
159 break; 179 break;
160 } 180 }
161 }); 181 });
162 - if (updateRecords.length) {  
163 - this.updateRowsAndSort();  
164 - } 182 + this.updateRowsAndSort();
165 } 183 }
166 } 184 }
167 185
@@ -130,6 +130,15 @@ export class WidgetContext { @@ -130,6 +130,15 @@ export class WidgetContext {
130 } 130 }
131 } 131 }
132 132
  133 + reset() {
  134 + this.destroyed = false;
  135 + this.hideTitlePanel = false;
  136 + this.widgetTitleTemplate = undefined;
  137 + this.widgetTitle = undefined;
  138 + this.customHeaderActions = undefined;
  139 + this.widgetActions = undefined;
  140 + }
  141 +
133 inited = false; 142 inited = false;
134 destroyed = false; 143 destroyed = false;
135 144