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

Introduce calendar drag service for shared data handling

Henriette Dargef4ff022280f7
diff --git a/frontend/src/app/features/calendar/op-calendar.service.ts b/frontend/src/app/features/calendar/op-calendar.service.ts
index 61cdc4cbd42..e5043ececaf 100644
--- a/frontend/src/app/features/calendar/op-calendar.service.ts
+++ b/frontend/src/app/features/calendar/op-calendar.service.ts
@@ -37,7 +37,10 @@ import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { UIRouterGlobals } from '@uirouter/core';
import { TimezoneService } from 'core-app/core/datetime/timezone.service';
import { WorkPackagesListChecksumService } from 'core-app/features/work-packages/components/wp-list/wp-list-checksum.service';
-import { EventResizeDoneArg } from '@fullcalendar/interaction';
+import {
+ EventReceiveArg,
+ EventResizeDoneArg,
+} from '@fullcalendar/interaction';
import { HalResourceEditingService } from 'core-app/shared/components/fields/edit/services/hal-resource-editing.service';
import { ResourceChangeset } from 'core-app/shared/components/fields/changeset/resource-changeset';
import * as moment from 'moment';
@@ -335,7 +338,7 @@ export class OpCalendarService extends UntilDestroyedMixin {
);
}
- updateDates(resizeInfo:EventResizeDoneArg|EventDropArg):ResourceChangeset<WorkPackageResource> {
+ updateDates(resizeInfo:EventResizeDoneArg|EventDropArg|EventReceiveArg):ResourceChangeset<WorkPackageResource> {
const workPackage = resizeInfo.event.extendedProps.workPackage as WorkPackageResource;
const changeset = this.halEditing.edit(workPackage);
diff --git a/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.html b/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.html
index d59cd297b5e..56790377913 100644
--- a/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.html
+++ b/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.html
@@ -31,7 +31,7 @@
<div
#container
class="op-quick-add-pane--results">
- <ng-container *ngFor="let wp of resultingWorkPackages">
+ <ng-container *ngFor="let wp of currentWorkPackages$ | async">
<wp-single-card
[workPackage]="wp"
[attr.data-drag-helper-id]="wp.id"
diff --git a/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.ts b/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.ts
index c2210098c70..86c72881ce6 100644
--- a/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.ts
+++ b/frontend/src/app/features/team-planner/team-planner/add-work-packages/quick-add-pane.component.ts
@@ -2,7 +2,6 @@ import {
ChangeDetectionStrategy,
Component,
ElementRef,
- OnInit,
ViewChild,
} from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
@@ -24,9 +23,7 @@ import { CurrentProjectService } from 'core-app/core/current-project/current-pro
import { UrlParamsHelperService } from 'core-app/features/work-packages/components/wp-query/url-params-helper';
import { IsolatedQuerySpace } from 'core-app/features/work-packages/directives/query-space/isolated-query-space';
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
-import { ThirdPartyDraggable } from '@fullcalendar/interaction';
-import { DragMetaInput } from '@fullcalendar/common';
-import { Drake } from 'dragula';
+import { CalendarDragService } from 'core-app/features/team-planner/team-planner/calendar-drag.service';
@Component({
selector: 'op-quick-add-pane',
@@ -34,7 +31,7 @@ import { Drake } from 'dragula';
styleUrls: ['./quick-add-pane.component.sass'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
-export class QuickAddPaneComponent extends UntilDestroyedMixin implements OnInit {
+export class QuickAddPaneComponent extends UntilDestroyedMixin {
@ViewChild('container') container:ElementRef;
@ViewChild('container')
@@ -42,10 +39,8 @@ export class QuickAddPaneComponent extends UntilDestroyedMixin implements OnInit
// ViewChild reference may be undefined initially
// due to ngIf
if (v !== undefined) {
- if (this.drake) {
- this.drake.destroy();
- }
- this.registerDrag();
+ this.calendarDrag.destroyDrake();
+ this.calendarDrag.registerDrag(v);
}
}
@@ -55,6 +50,8 @@ export class QuickAddPaneComponent extends UntilDestroyedMixin implements OnInit
isLoading$ = new BehaviorSubject<boolean>(false);
+ currentWorkPackages$ = this.calendarDrag.draggableWorkPackages$;
+
text = {
empty_state: this.I18n.t('js.team_planner.quick_add.empty_state'),
placeholder: this.I18n.t('js.team_planner.quick_add.search_placeholder'),
@@ -64,10 +61,6 @@ export class QuickAddPaneComponent extends UntilDestroyedMixin implements OnInit
empty_state: imagePath('team-planner/quick-add-empty-state.svg'),
};
- resultingWorkPackages:WorkPackageResource[] = [];
-
- drake:Drake;
-
constructor(
private readonly querySpace:IsolatedQuerySpace,
private I18n:I18nService,
@@ -75,19 +68,14 @@ export class QuickAddPaneComponent extends UntilDestroyedMixin implements OnInit
private readonly notificationService:WorkPackageNotificationService,
private readonly currentProject:CurrentProjectService,
private readonly urlParamsHelper:UrlParamsHelperService,
- private ref:ElementRef,
+ private readonly calendarDrag:CalendarDragService,
) {
super();
}
- ngOnInit():void {
- }
-
- ngOnDestroy() {
+ ngOnDestroy():void {
super.ngOnDestroy();
- if (this.drake) {
- this.drake.destroy();
- }
+ this.calendarDrag.destroyDrake();
}
searchWorkPackages(searchString:string):void {
@@ -141,7 +129,7 @@ export class QuickAddPaneComponent extends UntilDestroyedMixin implements OnInit
this.untilDestroyed(),
)
.subscribe((results) => {
- this.resultingWorkPackages = results;
+ this.calendarDrag.draggableWorkPackages$.next(results);
this.isEmpty$.next(results.length === 0);
this.isLoading$.next(false);
@@ -155,51 +143,4 @@ export class QuickAddPaneComponent extends UntilDestroyedMixin implements OnInit
get isSearching():boolean {
return this.searchString !== '';
}
-
- private registerDrag():void {
- this.drake = dragula({
- containers: [this.container?.nativeElement],
- revertOnSpill: true,
- });
-
- this.drake.on('drag', (el:HTMLElement) => {
- el.classList.add('gu-transit');
- });
-
- // eslint-disable-next-line no-new
- new ThirdPartyDraggable(this.container?.nativeElement, {
- itemSelector: '.op-quick-add-pane--wp',
- mirrorSelector: '.gu-mirror', // the dragging element that dragula renders
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
- eventData: this.eventData.bind(this),
- });
- }
-
- private eventData(eventEl:HTMLElement):undefined|DragMetaInput {
- const wpID = eventEl.dataset.dragHelperId;
- if (wpID) {
- const workPackage = this.resultingWorkPackages.find((wp) => wp.id === wpID);
-
- if (workPackage) {
- const startDate = moment(workPackage.startDate);
- const dueDate = moment(workPackage.dueDate);
- const diff = dueDate.diff(startDate, 'days') + 1;
-
- return {
- title: workPackage.subject,
- duration: {
- days: diff || 1,
- },
- className: `__hl_background_type_${workPackage.type.id as string}`,
- extendedProps: {
- workPackage,
- },
- };
- }
-
- return undefined;
- }
-
- return undefined;
- }
}
diff --git a/frontend/src/app/features/team-planner/team-planner/calendar-drag.service.ts b/frontend/src/app/features/team-planner/team-planner/calendar-drag.service.ts
new file mode 100644
index 00000000000..bf60ab1fdec
--- /dev/null
+++ b/frontend/src/app/features/team-planner/team-planner/calendar-drag.service.ts
@@ -0,0 +1,99 @@
+import {
+ ElementRef,
+ Injectable,
+} from '@angular/core';
+import { ThirdPartyDraggable } from '@fullcalendar/interaction';
+import { DragMetaInput } from '@fullcalendar/common';
+import { Drake } from 'dragula';
+import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
+import { BehaviorSubject } from 'rxjs';
+
+@Injectable({
+ providedIn: 'root',
+})
+export class CalendarDragService {
+ drake:Drake;
+
+ draggableWorkPackages$ = new BehaviorSubject<WorkPackageResource[]>([]);
+
+ private workPackages:WorkPackageResource[];
+
+ set draggableWorkPackages(wps:WorkPackageResource[]) {
+ this.workPackages = wps;
+ }
+
+ get draggableWorkPackages():WorkPackageResource[] {
+ return this.workPackages;
+ }
+
+ destroyDrake():void {
+ if (this.drake) {
+ this.drake.destroy();
+ }
+ }
+
+ registerDrag(container:ElementRef):void {
+ this.drake = dragula({
+ containers: [container.nativeElement],
+ revertOnSpill: true,
+ });
+
+ this.drake.on('drag', (el:HTMLElement) => {
+ el.classList.add('gu-transit');
+ });
+
+ // eslint-disable-next-line no-new
+ new ThirdPartyDraggable(container.nativeElement, {
+ itemSelector: '.op-quick-add-pane--wp',
+ mirrorSelector: '.gu-mirror', // the dragging element that dragula renders
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
+ eventData: this.eventData.bind(this),
+ });
+ }
+
+ handleDrop(workPackage:WorkPackageResource):void {
+ this.draggableWorkPackages$
+ .next(this
+ .draggableWorkPackages$
+ .value
+ .filter((wp) => wp.id !== workPackage.id));
+ }
+
+ handleDropError(workPackage:WorkPackageResource):void {
+ const oldDraggables = this.draggableWorkPackages$.value;
+ const isElementStillVisible = oldDraggables.filter((wp) => wp.id === workPackage.id).length === 1;
+
+ if (!isElementStillVisible) {
+ this.draggableWorkPackages$
+ .next(oldDraggables.concat(workPackage));
+ }
+ }
+
+ private eventData(eventEl:HTMLElement):undefined|DragMetaInput {
+ const wpID = eventEl.dataset.dragHelperId;
+ if (wpID) {
+ const workPackage = this.draggableWorkPackages$.value.find((wp) => wp.id === wpID);
+
+ if (workPackage) {
+ const startDate = moment(workPackage.startDate);
+ const dueDate = moment(workPackage.dueDate);
+ const diff = dueDate.diff(startDate, 'days') + 1;
+
+ return {
+ title: workPackage.subject,
+ duration: {
+ days: diff || 1,
+ },
+ className: `__hl_background_type_${workPackage.type.id as string}`,
+ extendedProps: {
+ workPackage,
+ },
+ };
+ }
+
+ return undefined;
+ }
+
+ return undefined;
+ }
+}
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 ae6b5181ae7..127e3b4d258 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
@@ -30,9 +30,8 @@ import {
import { StateService } from '@uirouter/angular';
import resourceTimelinePlugin from '@fullcalendar/resource-timeline';
import interactionPlugin, {
- DropArg,
- EventResizeDoneArg,
EventReceiveArg,
+ EventResizeDoneArg,
} from '@fullcalendar/interaction';
import { FullCalendarComponent } from '@fullcalendar/angular';
import { I18nService } from 'core-app/core/i18n/i18n.service';
@@ -55,6 +54,7 @@ import { HalResourceEditingService } from 'core-app/shared/components/fields/edi
import { HalResourceNotificationService } from 'core-app/features/hal/services/hal-resource-notification.service';
import { SchemaCacheService } from 'core-app/core/schemas/schema-cache.service';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
+import { CalendarDragService } from 'core-app/features/team-planner/team-planner/calendar-drag.service';
@Component({
selector: 'op-team-planner',
@@ -138,6 +138,7 @@ export class TeamPlannerComponent extends UntilDestroyedMixin implements OnInit,
readonly halNotification:HalResourceNotificationService,
readonly schemaCache:SchemaCacheService,
readonly apiV3Service:ApiV3Service,
+ readonly calendarDrag:CalendarDragService,
) {
super();
}
@@ -287,7 +288,7 @@ export class TeamPlannerComponent extends UntilDestroyedMixin implements OnInit,
droppable: true,
eventResize: (resizeInfo:EventResizeDoneArg) => this.updateEvent(resizeInfo),
eventDrop: (dropInfo:EventDropArg) => this.updateEvent(dropInfo),
- eventReceive: (dropInfo:EventReceiveArg) => this.updateWorkPackage(dropInfo),
+ eventReceive: (dropInfo:EventReceiveArg) => this.updateEvent(dropInfo),
} as CalendarOptions),
);
});
@@ -421,60 +422,26 @@ export class TeamPlannerComponent extends UntilDestroyedMixin implements OnInit,
);
}
- private async updateEvent(info:EventResizeDoneArg|EventDropArg):Promise<void> {
+ private async updateEvent(info:EventResizeDoneArg|EventDropArg|EventReceiveArg):Promise<void> {
const changeset = this.calendar.updateDates(info);
- const resource = (info as EventDropArg).newResource;
+ const resource = info.event.getResources()[0];
if (resource) {
changeset.setValue('assignee', { href: resource.id });
}
+ this.calendarDrag.handleDrop(changeset.projectedResource as WorkPackageResource);
+
try {
const result = await this.halEditing.save(changeset);
this.halNotification.showSave(result.resource, result.wasNew);
} catch (e) {
this.halNotification.showError(e.resource, changeset.projectedResource);
+ this.calendarDrag.handleDropError(changeset.projectedResource as WorkPackageResource);
info.revert();
}
}
- private async updateWorkPackage(dropInfo:EventReceiveArg):Promise<void> {
- const id = dropInfo.draggedEl.dataset.dragHelperId;
-
- if (id) {
- this
- .apiV3Service
- .work_packages
- .id(id)
- .requireAndStream()
- .pipe(
- take(1),
- this.untilDestroyed(),
- )
- .subscribe(async (wp) => {
- const changeset = this.halEditing.edit(wp);
- changeset.setValue('startDate', dropInfo.event.startStr);
- const due = moment(dropInfo.event.endStr)
- .subtract(1, 'day')
- .format('YYYY-MM-DD');
- changeset.setValue('dueDate', due);
-
- const resource = dropInfo.event.getResources()[0];
- if (resource) {
- changeset.setValue('assignee', { href: resource.id });
- }
-
- try {
- const result = await this.halEditing.save(changeset);
- this.halNotification.showSave(result.resource, result.wasNew);
- } catch (e) {
- this.halNotification.showError(e.resource, changeset.projectedResource);
- dropInfo.revert();
- }
- });
- }
- }
-
private eventResourceEditable(wp:WorkPackageResource):boolean {
const schema = this.schemaCache.of(wp);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access