Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Detach views instead of removing them immediately

Oliver Güntherc7b634e5edc5
diff --git a/frontend/src/app/features/team-planner/team-planner/planner/event-view-lookup.service.ts b/frontend/src/app/features/team-planner/team-planner/planner/event-view-lookup.service.ts
index 351d12eceb3..94612f91490 100644
--- a/frontend/src/app/features/team-planner/team-planner/planner/event-view-lookup.service.ts
+++ b/frontend/src/app/features/team-planner/team-planner/planner/event-view-lookup.service.ts
@@ -5,6 +5,7 @@ import {
TemplateRef,
ViewContainerRef,
} from '@angular/core';
+import { debugLog } from 'core-app/shared/helpers/debug_output';
/**
* View lookup service for injecting angular templates
@@ -16,7 +17,11 @@ import {
*/
@Injectable()
export class EventViewLookupService implements OnDestroy {
- private readonly views = new Map<string, EmbeddedViewRef<unknown>>();
+ /** Active templates currently rendered */
+ private readonly activeViews = new Map<string, EmbeddedViewRef<unknown>>();
+
+ /** Remember detached views to be destroyed on destroyAll */
+ private readonly detachedViews:EmbeddedViewRef<unknown>[] = [];
constructor(private viewContainerRef:ViewContainerRef) {
}
@@ -32,109 +37,87 @@ export class EventViewLookupService implements OnDestroy {
* @param context The available variables for the <ng-template>. For
* example, if it looks like this: <ng-template let-localVar="value"> then
* your context should be an object with a `value` key.
- * @param comparator If you're re-rendering the same view and the context
- * hasn't changed, then performance is a lot better if we just return the
- * original view rather than destroying and re-creating the view.
- * Optionally pass this function to return true when the views should be
- * re-used.
*/
getView(
template:TemplateRef<unknown>, id:string, context:unknown,
- comparator?:(v1:unknown, v2:unknown) => boolean,
):EmbeddedViewRef<unknown> {
- let view = this.views.get(id);
+ let view = this.activeViews.get(id);
if (view) {
- if (comparator && comparator(view.context, context)) {
- // Nothing changed -- no need to re-render the component.
- view.markForCheck();
- return view;
- }
-
- // The performance would be better if we didn't need to destroy
- // the view here... but just updating the context and checking
- // changes doesn't work.
- this.destroyView(id);
+ debugLog('Returning active view %O', id);
+ view.detectChanges();
+ return view;
}
+ // Create a new view and move to active
+ debugLog('CREATING new view %O', id);
view = this.viewContainerRef.createEmbeddedView(template, context);
- this.views.set(id, view);
+ this.activeViews.set(id, view);
view.detectChanges();
return view;
}
+ ngOnDestroy():void {
+ this.destroyAll();
+ }
+
/**
- * Generates a view for the given template and returns the root DOM node(s)
- * for the view, which can be returned from an eventContent call.
- * @param template The template ref (get this from a @ViewChild of an
- * <ng-template>)
- * @param id The unique ID for this instance of the view. Use this so that
- * you don't keep around views for the same event.
- * @param context The available variables for the <ng-template>. For
- * example, if it looks like this: <ng-template let-localVar="value"> then
- * your context should be an object with a `value` key.
- * @param comparator If you're re-rendering the same view and the context
- * hasn't changed, then performance is a lot better if we just return the
- * original view rather than destroying and re-creating the view.
- * Optionally pass this function to return true when the views should be
- * re-used.
+ * Call this method if all views need to be cleaned up. This will happen
+ * when your parent component is destroyed (e.g., in ngOnDestroy),
+ * but it may also be needed if you are clearing just the area where the
+ * views have been placed.
*/
- getTemplateRootNodes(
- template:TemplateRef<unknown>,
- id:string,
- context:unknown,
- comparator?:(v1:unknown, v2:unknown) => boolean,
- ):unknown[] {
- return this.getView(template, id, context, comparator).rootNodes;
- }
+ public destroyAll():void {
+ debugLog('Destroying all views');
+
+ Array
+ .from(this.activeViews.values())
+ .forEach(this.destroyView.bind(this));
- hasView(id:string):boolean {
- return this.views.has(id);
+ debugLog('Destroying %O active views', this.activeViews.size);
+ this.activeViews.clear();
+
+ this.destroyDetached();
}
/**
- * Marks the given view (or all views) as needing change detection.
- * Call `detectChanges` on your component if you need to run change
- * detection synchronously; normally Angular handles that.
+ * Call this method if you want to clean detached views.
+ * This is safe to call outside of drag & drop operations.
+ *
*/
- markForCheck(id?:string):void {
- if (id) {
- this.views.get(id)?.markForCheck();
- } else {
- // eslint-disable-next-line no-restricted-syntax
- for (const view of this.views.values()) {
- view.markForCheck();
- }
- }
- }
+ public destroyDetached():void {
+ debugLog('Destroying %O detached views', this.detachedViews.length);
- ngOnDestroy():void {
- this.destroyAll();
+ while (this.detachedViews.length) {
+ this.destroyView(this.detachedViews.pop() as EmbeddedViewRef<unknown>);
+ }
}
/**
- * Call this method if all views need to be cleaned up. This will happen
- * when your parent component is destroyed (e.g., in ngOnDestroy),
- * but it may also be needed if you are clearing just the area where the
- * views have been placed.
+ * Mark a view to be destroyed.
+ * It will only be destroyed once +destroyAll+ is called.
+ *
+ * Ensure that destroyAll is called when, e.g., refreshing the calendar.
+ *
+ * @param id View ID
*/
- public destroyAll():void {
- // eslint-disable-next-line no-restricted-syntax
- for (const view of this.views.values()) {
- view.destroy();
+ public markForDestruction(id:string):void {
+ const view = this.activeViews.get(id);
+ if (!view) {
+ return;
}
- this.views.clear();
+
+ debugLog('Marking view %O to be destroyed', id);
+ this.activeViews.delete(id);
+ this.detachedViews.push(view);
}
- public destroyView(id:string):void {
- const view = this.views.get(id);
- if (view) {
- const index = this.viewContainerRef.indexOf(view);
- if (index !== -1) {
- this.viewContainerRef.remove(index);
- }
- view.destroy();
- this.views.delete(id);
+ private destroyView(view:EmbeddedViewRef<unknown>) {
+ const index = this.viewContainerRef.indexOf(view);
+ if (index !== -1) {
+ this.viewContainerRef.remove(index);
}
+
+ view.destroy();
}
}
diff --git a/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.html b/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.html
index 678e0cbdc8e..b14c04353de 100644
--- a/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.html
+++ b/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.html
@@ -75,19 +75,18 @@
<ng-template #eventContent let-event="event">
<wp-single-card
- *ngIf="event.extendedProps.workPackage"
[workPackage]="event.extendedProps.workPackage"
- [orientation]="'horizontal'"
- [highlightingMode]="'type'"
- [showInfoButton]="true"
- [disabledInfo]="showDisabledText(event.extendedProps.workPackage)"
- [isClosed]="isStatusClosed(event.extendedProps.workPackage)"
- [showAsInlineCard]="true"
- [showStartDate]="!this.isWpDateInCurrentView(event.extendedProps.workPackage, 'start')"
- [showEndDate]="!this.isWpDateInCurrentView(event.extendedProps.workPackage, 'end')"
- >
- </wp-single-card>
- </ng-template>
+ [orientation]="'horizontal'"
+ [highlightingMode]="'type'"
+ [showInfoButton]="true"
+ [disabledInfo]="showDisabledText(event.extendedProps.workPackage)"
+ [isClosed]="isStatusClosed(event.extendedProps.workPackage)"
+ [showAsInlineCard]="true"
+ [showStartDate]="!this.isWpDateInCurrentView(event.extendedProps.workPackage, 'start')"
+ [showEndDate]="!this.isWpDateInCurrentView(event.extendedProps.workPackage, 'end')"
+ >
+ </wp-single-card>
+ </ng-template>
<div class="op-team-planner--footer" data-qa-selector="op-team-planner-footer">
<div class="op-team-planner--add-assignee">
diff --git a/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.ts b/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.ts
index 89ca1815ebb..399af192821 100644
--- a/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.ts
+++ b/frontend/src/app/features/team-planner/team-planner/planner/team-planner.component.ts
@@ -89,7 +89,7 @@ export class TeamPlannerComponent extends UntilDestroyedMixin implements OnInit,
calendarOptions$ = new Subject<CalendarOptions>();
- draggingItem$ = new Subject<EventDragStartArg|undefined>();
+ draggingItem$ = new BehaviorSubject<EventDragStartArg|undefined>(undefined);
dropzoneHovered$ = new BehaviorSubject<boolean>(false);
@@ -346,9 +346,9 @@ export class TeamPlannerComponent extends UntilDestroyedMixin implements OnInit,
eventDrop: (dropInfo:EventDropArg) => this.updateEvent(dropInfo),
eventReceive: (dropInfo:EventReceiveArg) => this.updateEvent(dropInfo),
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
- eventContent: (data:EventContentArg) => this.renderTemplate(this.eventContent, data.event.extendedProps.workPackage.href, data),
+ eventContent: (data:EventContentArg) => this.renderTemplate(this.eventContent, this.eventId(data), data),
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
- eventWillUnmount: (data:EventContentArg) => this.unrenderTemplate(data.event.extendedProps.workPackage.href),
+ eventWillUnmount: (data:EventContentArg) => this.unrenderTemplate(this.eventId(data)),
} as CalendarOptions),
);
});
@@ -365,6 +365,9 @@ export class TeamPlannerComponent extends UntilDestroyedMixin implements OnInit,
.toPromise()
.then((workPackages:WorkPackageCollectionResource) => {
const events = this.mapToCalendarEvents(workPackages.elements);
+
+ this.viewLookup.destroyDetached();
+
successCallback(events);
})
.catch(failureCallback);
@@ -373,12 +376,25 @@ export class TeamPlannerComponent extends UntilDestroyedMixin implements OnInit,
}
renderTemplate(template:TemplateRef<unknown>, id:string, data:ResourceLabelContentArg|EventContentArg):{ domNodes:unknown[] } {
+ if (this.isDragggedEvent(id)) {
+ this.viewLookup.markForDestruction(id);
+ }
+
const ref = this.viewLookup.getView(template, id, data);
return { domNodes: ref.rootNodes };
}
unrenderTemplate(id:string):void {
- this.viewLookup.destroyView(id);
+ this.viewLookup.markForDestruction(id);
+ }
+
+ isDragggedEvent(id:string):boolean {
+ const dragging = this.draggingItem$.getValue();
+ return !!dragging && (dragging.event.extendedProps.workPackage as WorkPackageResource).href === id;
+ }
+
+ eventId(data:EventContentArg):string {
+ return `${data.event.id},dragging=${data.isDragging.toString()}`;
}
public showAssigneeAddRow():void {
diff --git a/frontend/src/app/shared/helpers/debug_output.ts b/frontend/src/app/shared/helpers/debug_output.ts
index 9d5c20ec939..78fd2927799 100644
--- a/frontend/src/app/shared/helpers/debug_output.ts
+++ b/frontend/src/app/shared/helpers/debug_output.ts
@@ -14,7 +14,7 @@ export function whenDebugging(cb:() => void) {
* Log with console.log when DEBUG is defined
* through webpack.
*/
-export function debugLog(message:string, ...args:any[]) {
+export function debugLog(message:string, ...args:unknown[]):void {
// eslint-disable-next-line no-console
whenDebugging(() => console.log(`[DEBUG] ${message}`, ...args));
}