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

Extract relation related stuff from modal into common service

Oliver Güntherf7eea16c0ecb
diff --git a/frontend/src/app/shared/components/datepicker/banner/datepicker-banner.component.ts b/frontend/src/app/shared/components/datepicker/banner/datepicker-banner.component.ts
index 5ef34debf1e..de0303e7943 100644
--- a/frontend/src/app/shared/components/datepicker/banner/datepicker-banner.component.ts
+++ b/frontend/src/app/shared/components/datepicker/banner/datepicker-banner.component.ts
@@ -33,12 +33,12 @@ import {
Input,
} from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
-import { DatepickerModalService } from 'core-app/shared/components/datepicker/datepicker.modal.service';
import {
map,
take,
} from 'rxjs/operators';
import { StateService } from '@uirouter/core';
+import { DateModalRelationsService } from 'core-app/shared/components/datepicker/services/date-modal-relations.service';
@Component({
selector: 'op-datepicker-banner',
@@ -48,25 +48,25 @@ import { StateService } from '@uirouter/core';
export class DatepickerBannerComponent {
@Input() scheduleManually = false;
- hasRelations$ = this.datepickerService.hasRelations$;
+ hasRelations$ = this.dateModalRelations.hasRelations$;
hasPrecedingRelations$ = this
- .datepickerService
+ .dateModalRelations
.precedingWorkPackages$
.pipe(
map((relations) => relations?.length > 0),
);
hasFollowingRelations$ = this
- .datepickerService
+ .dateModalRelations
.followingWorkPackages$
.pipe(
map((relations) => relations?.length > 0),
);
- isParent = this.datepickerService.isParent;
+ isParent = this.dateModalRelations.isParent;
- isChild = this.datepickerService.isChild;
+ isChild = this.dateModalRelations.isChild;
text = {
automatically_scheduled_parent: this.I18n.t('js.work_packages.datepicker_modal.automatically_scheduled_parent'),
@@ -81,7 +81,7 @@ export class DatepickerBannerComponent {
};
constructor(
- readonly datepickerService:DatepickerModalService,
+ readonly dateModalRelations:DateModalRelationsService,
readonly injector:Injector,
readonly I18n:I18nService,
readonly state:StateService,
@@ -91,7 +91,7 @@ export class DatepickerBannerComponent {
evt.preventDefault();
this
- .datepickerService
+ .dateModalRelations
.getInvolvedWorkPackageIds()
.pipe(
take(1),
diff --git a/frontend/src/app/shared/components/datepicker/datepicker.modal.service.ts b/frontend/src/app/shared/components/datepicker/datepicker.modal.service.ts
index c6c724a71ab..be1483ad28a 100644
--- a/frontend/src/app/shared/components/datepicker/datepicker.modal.service.ts
+++ b/frontend/src/app/shared/components/datepicker/datepicker.modal.service.ts
@@ -34,120 +34,16 @@ import { DatePicker } from 'core-app/shared/components/op-date-picker/datepicker
import { DateOption } from 'flatpickr/dist/types/options';
import { OpModalLocalsToken } from 'core-app/shared/components/modal/modal.service';
import { OpModalLocalsMap } from 'core-app/shared/components/modal/modal.types';
-import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
-import { ApiV3Filter } from 'core-app/shared/helpers/api-v3/api-v3-filter-builder';
import { WorkPackageChangeset } from 'core-app/features/work-packages/components/wp-edit/work-package-changeset';
-import {
- defaultIfEmpty,
- filter,
- map,
- shareReplay,
- switchMap,
-} from 'rxjs/operators';
-import {
- combineLatest,
- Observable,
- of,
-} from 'rxjs';
-import { HalResource } from 'core-app/features/hal/resources/hal-resource';
-import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
-import isNewResource from 'core-app/features/hal/helpers/is-new-resource';
@Injectable()
export class DatepickerModalService {
private changeset:WorkPackageChangeset = this.locals.changeset as WorkPackageChangeset;
- precedingWorkPackages$:Observable<{ id:string, dueDate?:string, date?:string }[]> = of(this.changeset)
- .pipe(
- filter((changeset) => !isNewResource(changeset.pristineResource)),
- switchMap((changeset) => this
- .apiV3Service
- .work_packages
- .signalled(
- ApiV3Filter('precedes', '=', [changeset.id]),
- [
- 'elements/id',
- 'elements/dueDate',
- 'elements/date',
- ],
- )),
- map((collection:IHALCollection<{ id:string }>) => collection._embedded.elements || []),
- defaultIfEmpty([]),
- shareReplay(1),
- );
-
- followingWorkPackages$:Observable<{ id:string }[]> = of(this.changeset)
- .pipe(
- filter((changeset) => !isNewResource(changeset.pristineResource)),
- switchMap((changeset) => this
- .apiV3Service
- .work_packages
- .signalled(
- ApiV3Filter('follows', '=', [changeset.id]),
- ['elements/id'],
- )),
- map((collection:IHALCollection<{ id:string }>) => collection._embedded.elements || []),
- defaultIfEmpty([]),
- shareReplay(1),
- );
-
- hasRelations$ = combineLatest([
- this.precedingWorkPackages$,
- this.followingWorkPackages$,
- ])
- .pipe(
- map(([precedes, follows]) => precedes.length > 0 || follows.length > 0 || this.isParent || this.isChild),
- );
-
constructor(
@Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,
- private apiV3Service:ApiV3Service,
) {}
- /**
- * Determines whether the work package is a child. It does so
- * by checking the ancestors links.
- */
- get isChild():boolean {
- return this.ancestors.length > 0;
- }
-
- get ancestors():HalResource[] {
- const wp = this.changeset.projectedResource;
- return wp.ancestors || [];
- }
-
- /**
- * Determines whether the work package is a parent. It does so
- * by checking the children links.
- */
- get isParent():boolean {
- return this.children.length > 0;
- }
-
- get children():HalResource[] {
- const wp = this.changeset.projectedResource;
- return wp.children || [];
- }
-
- getInvolvedWorkPackageIds():Observable<string[]> {
- return combineLatest([
- this.precedingWorkPackages$,
- this.followingWorkPackages$,
- ])
- .pipe(
- map(
- ([preceding, following]) => [
- this.changeset.pristineResource,
- ...preceding,
- ...following,
- ...this.children,
- ...this.ancestors,
- ].map((el) => el.id as string),
- ),
- );
- }
-
/**
* Map the date to the internal format,
* setting to null if it's empty.
diff --git a/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts b/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts
index a6cee9c2402..5df70eed49b 100644
--- a/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts
+++ b/frontend/src/app/shared/components/datepicker/multi-date-modal/multi-date.modal.ts
@@ -64,6 +64,7 @@ import {
} from 'rxjs';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { FormResource } from 'core-app/features/hal/resources/form-resource';
+import { DateModalRelationsService } from 'core-app/shared/components/datepicker/services/date-modal-relations.service';
export type DateKeys = 'start'|'end';
export type DateFields = DateKeys|'duration';
@@ -85,6 +86,7 @@ export type FieldUpdates =
encapsulation: ViewEncapsulation.None,
providers: [
DatepickerModalService,
+ DateModalRelationsService,
],
})
export class MultiDateModalComponent extends OpModalComponent implements AfterViewInit {
@@ -96,6 +98,8 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
@InjectField() datepickerService:DatepickerModalService;
+ @InjectField() dateModalRelations:DateModalRelationsService;
+
@InjectField() browserDetector:BrowserDetector;
@ViewChild('modalContainer') modalContainer:ElementRef<HTMLElement>;
@@ -183,13 +187,10 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
ngAfterViewInit():void {
this
- .datepickerService
- .precedingWorkPackages$
- .pipe(
- take(1),
- )
- .subscribe((relation) => {
- this.initializeDatepicker(this.minimalDateFromPrecedingRelationship(relation));
+ .dateModalRelations
+ .getMinimalDateFromPreceeding()
+ .subscribe((date) => {
+ this.initializeDatepicker(date);
this.onDataChange();
});
@@ -330,7 +331,7 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
* Returns whether the user can alter the dates of the work package.
*/
get isSchedulable():boolean {
- return this.scheduleManually || !this.datepickerService.isParent;
+ return this.scheduleManually || !this.dateModalRelations.isParent;
}
showFieldAsActive(field:DateFields):boolean {
@@ -525,30 +526,6 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
}
}
- private minimalDateFromPrecedingRelationship(relations:{ id:string, dueDate?:string, date?:string }[]):Date|null {
- if (relations.length === 0) {
- return null;
- }
-
- let minimalDate:Date|null = null;
-
- relations.forEach((relation) => {
- if (!relation.dueDate && !relation.date) {
- return;
- }
-
- const relationDate = relation.dueDate || relation.date;
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const parsedRelationDate = this.datepickerService.parseDate(relationDate!);
-
- if (!minimalDate || minimalDate < parsedRelationDate) {
- minimalDate = parsedRelationDate === '' ? null : parsedRelationDate;
- }
- });
-
- return minimalDate;
- }
-
private isDayDisabled(dayElement:DayElement, minimalDate?:Date|null):boolean {
return !this.isSchedulable || (!this.scheduleManually && !!minimalDate && dayElement.dateObj <= minimalDate);
}
diff --git a/frontend/src/app/shared/components/datepicker/services/date-modal-relations.service.ts b/frontend/src/app/shared/components/datepicker/services/date-modal-relations.service.ts
new file mode 100644
index 00000000000..a08646b54f3
--- /dev/null
+++ b/frontend/src/app/shared/components/datepicker/services/date-modal-relations.service.ts
@@ -0,0 +1,184 @@
+// -- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 2012-2022 the OpenProject GmbH
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+// Copyright (C) 2006-2013 Jean-Philippe Lang
+// Copyright (C) 2010-2013 the ChiliProject Team
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See COPYRIGHT and LICENSE files for more details.
+//++
+
+import {
+ Inject,
+ Injectable,
+} from '@angular/core';
+import { OpModalLocalsToken } from 'core-app/shared/components/modal/modal.service';
+import { OpModalLocalsMap } from 'core-app/shared/components/modal/modal.types';
+import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
+import { ApiV3Filter } from 'core-app/shared/helpers/api-v3/api-v3-filter-builder';
+import { WorkPackageChangeset } from 'core-app/features/work-packages/components/wp-edit/work-package-changeset';
+import {
+ defaultIfEmpty,
+ filter,
+ map,
+ shareReplay,
+ switchMap,
+ take,
+} from 'rxjs/operators';
+import {
+ combineLatest,
+ Observable,
+ of,
+} from 'rxjs';
+import { HalResource } from 'core-app/features/hal/resources/hal-resource';
+import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
+import isNewResource from 'core-app/features/hal/helpers/is-new-resource';
+import { DatepickerModalService } from 'core-app/shared/components/datepicker/datepicker.modal.service';
+
+@Injectable()
+export class DateModalRelationsService {
+ private changeset:WorkPackageChangeset = this.locals.changeset as WorkPackageChangeset;
+
+ precedingWorkPackages$:Observable<{ id:string, dueDate?:string, date?:string }[]> = of(this.changeset)
+ .pipe(
+ filter((changeset) => !isNewResource(changeset.pristineResource)),
+ switchMap((changeset) => this
+ .apiV3Service
+ .work_packages
+ .signalled(
+ ApiV3Filter('precedes', '=', [changeset.id]),
+ [
+ 'elements/id',
+ 'elements/dueDate',
+ 'elements/date',
+ ],
+ )),
+ map((collection:IHALCollection<{ id:string }>) => collection._embedded.elements || []),
+ defaultIfEmpty([]),
+ shareReplay(1),
+ );
+
+ followingWorkPackages$:Observable<{ id:string }[]> = of(this.changeset)
+ .pipe(
+ filter((changeset) => !isNewResource(changeset.pristineResource)),
+ switchMap((changeset) => this
+ .apiV3Service
+ .work_packages
+ .signalled(
+ ApiV3Filter('follows', '=', [changeset.id]),
+ ['elements/id'],
+ )),
+ map((collection:IHALCollection<{ id:string }>) => collection._embedded.elements || []),
+ defaultIfEmpty([]),
+ shareReplay(1),
+ );
+
+ hasRelations$ = combineLatest([
+ this.precedingWorkPackages$,
+ this.followingWorkPackages$,
+ ])
+ .pipe(
+ map(([precedes, follows]) => precedes.length > 0 || follows.length > 0 || this.isParent || this.isChild),
+ );
+
+ constructor(
+ @Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,
+ private apiV3Service:ApiV3Service,
+ private datepickerService:DatepickerModalService,
+ ) {}
+
+ getMinimalDateFromPreceeding():Observable<Date|null> {
+ return this
+ .precedingWorkPackages$
+ .pipe(
+ take(1),
+ map((relation) => this.minimalDateFromPrecedingRelationship(relation)),
+ );
+ }
+
+ private minimalDateFromPrecedingRelationship(relations:{ id:string, dueDate?:string, date?:string }[]):Date|null {
+ if (relations.length === 0) {
+ return null;
+ }
+
+ let minimalDate:Date|null = null;
+
+ relations.forEach((relation) => {
+ if (!relation.dueDate && !relation.date) {
+ return;
+ }
+
+ const relationDate = relation.dueDate || relation.date;
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ const parsedRelationDate = this.datepickerService.parseDate(relationDate!);
+
+ if (!minimalDate || minimalDate < parsedRelationDate) {
+ minimalDate = parsedRelationDate === '' ? null : parsedRelationDate;
+ }
+ });
+
+ return minimalDate;
+ }
+
+ /**
+ * Determines whether the work package is a child. It does so
+ * by checking the ancestors links.
+ */
+ get isChild():boolean {
+ return this.ancestors.length > 0;
+ }
+
+ get ancestors():HalResource[] {
+ const wp = this.changeset.projectedResource;
+ return wp.ancestors || [];
+ }
+
+ /**
+ * Determines whether the work package is a parent. It does so
+ * by checking the children links.
+ */
+ get isParent():boolean {
+ return this.children.length > 0;
+ }
+
+ get children():HalResource[] {
+ const wp = this.changeset.projectedResource;
+ return wp.children || [];
+ }
+
+ getInvolvedWorkPackageIds():Observable<string[]> {
+ return combineLatest([
+ this.precedingWorkPackages$,
+ this.followingWorkPackages$,
+ ])
+ .pipe(
+ map(
+ ([preceding, following]) => [
+ this.changeset.pristineResource,
+ ...preceding,
+ ...following,
+ ...this.children,
+ ...this.ancestors,
+ ].map((el) => el.id as string),
+ ),
+ );
+ }
+}
diff --git a/frontend/src/app/shared/components/datepicker/single-date-modal/single-date.modal.ts b/frontend/src/app/shared/components/datepicker/single-date-modal/single-date.modal.ts
index 06cdba9df92..e6835e9efca 100644
--- a/frontend/src/app/shared/components/datepicker/single-date-modal/single-date.modal.ts
+++ b/frontend/src/app/shared/components/datepicker/single-date-modal/single-date.modal.ts
@@ -64,6 +64,7 @@ import {
} from 'rxjs';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { FormResource } from 'core-app/features/hal/resources/form-resource';
+import { DateModalRelationsService } from 'core-app/shared/components/datepicker/services/date-modal-relations.service';
@Component({
templateUrl: './single-date.modal.html',
@@ -72,6 +73,7 @@ import { FormResource } from 'core-app/features/hal/resources/form-resource';
encapsulation: ViewEncapsulation.None,
providers: [
DatepickerModalService,
+ DateModalRelationsService,
],
})
export class SingleDateModalComponent extends OpModalComponent implements AfterViewInit {
@@ -83,6 +85,8 @@ export class SingleDateModalComponent extends OpModalComponent implements AfterV
@InjectField() datepickerService:DatepickerModalService;
+ @InjectField() dateModalRelations:DateModalRelationsService;
+
@InjectField() browserDetector:BrowserDetector;
@ViewChild('modalContainer') modalContainer:ElementRef<HTMLElement>;
@@ -154,13 +158,10 @@ export class SingleDateModalComponent extends OpModalComponent implements AfterV
ngAfterViewInit():void {
this
- .datepickerService
- .precedingWorkPackages$
- .pipe(
- take(1),
- )
- .subscribe((relation) => {
- this.initializeDatepicker(this.minimalDateFromPrecedingRelationship(relation));
+ .dateModalRelations
+ .getMinimalDateFromPreceeding()
+ .subscribe((date) => {
+ this.initializeDatepicker(date);
this.onDataChange();
});
@@ -252,7 +253,7 @@ export class SingleDateModalComponent extends OpModalComponent implements AfterV
* Returns whether the user can alter the dates of the work package.
*/
get isSchedulable():boolean {
- return this.scheduleManually || !this.datepickerService.isParent;
+ return this.scheduleManually || !this.dateModalRelations.isParent;
}
private initializeDatepicker(minimalDate?:Date|null) {
@@ -302,30 +303,6 @@ export class SingleDateModalComponent extends OpModalComponent implements AfterV
this.onDataUpdated.emit(this.date || '');
}
- private minimalDateFromPrecedingRelationship(relations:{ id:string, dueDate?:string, date?:string }[]):Date|null {
- if (relations.length === 0) {
- return null;
- }
-
- let minimalDate:Date|null = null;
-
- relations.forEach((relation) => {
- if (!relation.dueDate && !relation.date) {
- return;
- }
-
- const relationDate = relation.dueDate || relation.date;
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- const parsedRelationDate = this.datepickerService.parseDate(relationDate!);
-
- if (!minimalDate || minimalDate < parsedRelationDate) {
- minimalDate = parsedRelationDate === '' ? null : parsedRelationDate;
- }
- });
-
- return minimalDate;
- }
-
private isDayDisabled(dayElement:DayElement, minimalDate?:Date|null):boolean {
return !this.isSchedulable || (!this.scheduleManually && !!minimalDate && dayElement.dateObj <= minimalDate);
}