Skip to content

Commit ecb9e5c

Browse files
estelafsunai-reliefappAntoineRelief
authored
refactor: use cdk portal to prevent rebuilding widget when expanding it (#2013)
--------- Co-authored-by: Unai Zalba <[email protected]> Co-authored-by: Antoine Hurard <[email protected]>
1 parent 9a71b5e commit ecb9e5c

File tree

23 files changed

+150
-109
lines changed

23 files changed

+150
-109
lines changed

apps/back-office/src/app/app-preview/pages/workflow/workflow.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
[activeStep]="activeStep"
88
>
99
</shared-workflow-stepper>
10-
<router-outlet (activate)="onActivate($event)"></router-outlet>
10+
<router-outlet
11+
(activate)="onActivate($event)"
12+
(deactivate)="clearSubscriptions()"
13+
></router-outlet>

apps/back-office/src/app/app-preview/pages/workflow/workflow.component.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { TranslateService } from '@ngx-translate/core';
1313
import { takeUntil } from 'rxjs/operators';
1414
import { SnackbarService } from '@oort-front/ui';
1515
import { PreviewService } from '../../../services/preview.service';
16+
import { Subscription } from 'rxjs';
1617

1718
/**
1819
* Workflow page component for application preview.
@@ -35,6 +36,8 @@ export class WorkflowComponent extends UnsubscribeComponent implements OnInit {
3536
public activeStep = 0;
3637
/** Role used for preview */
3738
public role = '';
39+
/** Subscription to change step events */
40+
private changeStepSubscription!: Subscription;
3841

3942
/**
4043
* Workflow page component for application preview
@@ -116,16 +119,25 @@ export class WorkflowComponent extends UnsubscribeComponent implements OnInit {
116119
*/
117120
onActivate(elementRef: any): void {
118121
if (elementRef.changeStep) {
119-
elementRef.changeStep.subscribe((event: any) => {
120-
if (event > 0) {
121-
this.goToNextStep();
122-
} else {
123-
this.goToPreviousStep();
122+
this.changeStepSubscription = elementRef.changeStep.subscribe(
123+
(event: any) => {
124+
if (event > 0) {
125+
this.goToNextStep();
126+
} else {
127+
this.goToPreviousStep();
128+
}
124129
}
125-
});
130+
);
126131
}
127132
}
128133

134+
/**
135+
* Clear subscriptions set from the router-outlet
136+
*/
137+
clearSubscriptions() {
138+
this.changeStepSubscription?.unsubscribe();
139+
}
140+
129141
/**
130142
* Navigates to the next step if possible and change selected step / index consequently
131143
*/

apps/back-office/src/app/application/pages/workflow/workflow.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ <h1 class="!m-0 overflow-hidden text-ellipsis">
4949
[activeStep]="activeStep"
5050
>
5151
</shared-workflow-stepper>
52-
<router-outlet (activate)="onActivate($event)"></router-outlet>
52+
<router-outlet
53+
(activate)="onActivate($event)"
54+
(deactivate)="clearSubscriptions()"
55+
></router-outlet>

apps/back-office/src/app/application/pages/workflow/workflow.component.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { DELETE_STEP, EDIT_WORKFLOW } from './graphql/mutations';
1919
import { TranslateService } from '@ngx-translate/core';
2020
import { takeUntil } from 'rxjs/operators';
2121
import { SnackbarService } from '@oort-front/ui';
22+
import { Subscription } from 'rxjs';
2223

2324
/**
2425
* Application workflow page component.
@@ -45,6 +46,8 @@ export class WorkflowComponent extends UnsubscribeComponent implements OnInit {
4546

4647
// === ACTIVE STEP ===
4748
public activeStep = 0;
49+
/** Subscription to change step events */
50+
private changeStepSubscription!: Subscription;
4851

4952
// === DUP APP SELECTION ===
5053
public showAppMenu = false;
@@ -276,16 +279,25 @@ export class WorkflowComponent extends UnsubscribeComponent implements OnInit {
276279
*/
277280
onActivate(elementRef: any): void {
278281
if (elementRef.changeStep) {
279-
elementRef.changeStep.subscribe((event: number) => {
280-
if (event > 0) {
281-
this.goToNextStep();
282-
} else {
283-
this.goToPreviousStep();
282+
this.changeStepSubscription = elementRef.changeStep.subscribe(
283+
(event: number) => {
284+
if (event > 0) {
285+
this.goToNextStep();
286+
} else {
287+
this.goToPreviousStep();
288+
}
284289
}
285-
});
290+
);
286291
}
287292
}
288293

294+
/**
295+
* Clear subscriptions set from the router-outlet
296+
*/
297+
clearSubscriptions() {
298+
this.changeStepSubscription?.unsubscribe();
299+
}
300+
289301
/**
290302
* Saves the new ordering
291303
*

apps/front-office/src/app/application/pages/workflow/workflow.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ <h1 class="!m-0 overflow-hidden text-ellipsis">
2828
class="block"
2929
>
3030
</shared-workflow-stepper>
31-
<router-outlet (activate)="onActivate($event)"></router-outlet>
31+
<router-outlet
32+
(activate)="onActivate($event)"
33+
(deactivate)="clearSubscriptions()"
34+
></router-outlet>

apps/front-office/src/app/application/pages/workflow/workflow.component.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { GET_WORKFLOW_BY_ID } from './graphql/queries';
1212
import { TranslateService } from '@ngx-translate/core';
1313
import { takeUntil } from 'rxjs/operators';
1414
import { SnackbarService } from '@oort-front/ui';
15+
import { Subscription } from 'rxjs';
1516

1617
/**
1718
* Workflow page.
@@ -32,6 +33,8 @@ export class WorkflowComponent extends UnsubscribeComponent implements OnInit {
3233
public steps: Step[] = [];
3334
/** Current step */
3435
public activeStep = 0;
36+
/** Subscription to change step events */
37+
private changeStepSubscription!: Subscription;
3538

3639
/**
3740
* Workflow page.
@@ -127,16 +130,25 @@ export class WorkflowComponent extends UnsubscribeComponent implements OnInit {
127130
*/
128131
onActivate(elementRef: any): void {
129132
if (elementRef.changeStep) {
130-
elementRef.changeStep.subscribe((event: any) => {
131-
if (event > 0) {
132-
this.goToNextStep();
133-
} else {
134-
this.goToPreviousStep();
133+
this.changeStepSubscription = elementRef.changeStep.subscribe(
134+
(event: any) => {
135+
if (event > 0) {
136+
this.goToNextStep();
137+
} else {
138+
this.goToPreviousStep();
139+
}
135140
}
136-
});
141+
);
137142
}
138143
}
139144

145+
/**
146+
* Clear subscriptions set from the router-outlet
147+
*/
148+
clearSubscriptions() {
149+
this.changeStepSubscription?.unsubscribe();
150+
}
151+
140152
/**
141153
* Navigates to the clicked step.
142154
*

libs/shared/src/lib/components/widget-grid/expanded-widget/expanded-widget.component.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<ui-dialog size="fullscreen">
22
<ng-container ngProjectAs="content">
3-
<shared-widget
4-
[widget]="data.widget"
5-
[header]="false"
6-
(changeStep)="changeStep.emit($event)"
7-
></shared-widget>
3+
<ng-template [cdkPortalOutlet]="portal"></ng-template>
84
</ng-container>
95
<ng-container ngProjectAs="actions">
106
<ui-button uiDialogClose>

libs/shared/src/lib/components/widget-grid/expanded-widget/expanded-widget.component.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
import {
22
AfterViewInit,
33
Component,
4-
EventEmitter,
4+
ElementRef,
55
Inject,
66
OnDestroy,
7-
Output,
87
} from '@angular/core';
98
import { DIALOG_DATA } from '@angular/cdk/dialog';
109
import { DOCUMENT } from '@angular/common';
10+
import { DomPortal, PortalModule } from '@angular/cdk/portal';
11+
import { ButtonModule, DialogModule } from '@oort-front/ui';
1112

12-
/** Dialog data */
13-
interface DialogData {
14-
widget: any;
13+
/** Widget data and template for class dialog */
14+
interface WidgetData {
15+
element?: ElementRef<any>;
1516
}
1617

17-
/** Component for expanded widgets */
18+
/** Expand widgets in a modal */
1819
@Component({
20+
standalone: true,
1921
selector: 'shared-expanded-widget',
2022
templateUrl: './expanded-widget.component.html',
2123
styleUrls: ['./expanded-widget.component.scss'],
24+
imports: [DialogModule, ButtonModule, PortalModule],
2225
})
2326
export class ExpandedWidgetComponent implements AfterViewInit, OnDestroy {
24-
// === EMIT STEP CHANGE FOR WORKFLOW ===
25-
@Output() changeStep: EventEmitter<number> = new EventEmitter();
27+
/** CDK portal */
28+
public portal?: DomPortal;
2629

2730
/**
28-
* Constructor for the component
31+
* Expand widgets in a modal
2932
*
30-
* @param data The input data for the dialog
33+
* @param data Dialog data
3134
* @param document Document
3235
*/
3336
constructor(
34-
@Inject(DIALOG_DATA) public data: DialogData,
37+
@Inject(DIALOG_DATA) public data: WidgetData,
3538
@Inject(DOCUMENT) private document: Document
3639
) {}
3740

3841
ngAfterViewInit(): void {
3942
this.document.dispatchEvent(
4043
new CustomEvent('expandchange', { detail: { expanded: true } })
4144
);
45+
this.portal = new DomPortal(this.data.element);
4246
}
4347

4448
ngOnDestroy(): void {

libs/shared/src/lib/components/widget-grid/widget-actions/widget-actions.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class WidgetActionsComponent extends UnsubscribeComponent {
7777
});
7878
}
7979
if (action === 'expand') {
80-
this.expand.emit({ widget: this.widget });
80+
this.expand.emit({ id: this.id });
8181
}
8282
if (action === 'style') {
8383
this.style.emit({ id: this.id, widget: this.widget });

libs/shared/src/lib/components/widget-grid/widget-grid.component.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ <h2 *ngIf="widgets.length === 0">{{ 'models.widget.add' | translate }}</h2>
4242
}"
4343
>
4444
<shared-widget
45-
#widgetElement
4645
[widget]="widget"
47-
(changeStep)="changeStep.emit($event)"
46+
(changeStep)="triggerChangeStepAction($event)"
4847
(edit)="onEditWidget($event)"
4948
[canUpdate]="canUpdate"
5049
[headerLeftTemplate]="headerLeftTemplate"
@@ -71,7 +70,9 @@ <h2 *ngIf="widgets.length === 0">{{ 'models.widget.add' | translate }}</h2>
7170
class="-mr-3"
7271
[id]="id"
7372
[canUpdate]="canUpdate"
74-
[collapsed]="widgetElement.elementRef.nativeElement.clientWidth < 500"
73+
[collapsed]="
74+
widgetComponents.get(i)?.elementRef?.nativeElement.clientWidth < 500
75+
"
7576
[widget]="widget"
7677
(edit)="onEditWidget($event)"
7778
(delete)="onDeleteWidget($event)"

0 commit comments

Comments
 (0)