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 loading of weekdays to a service so that it can be used for the Gantt chart as well

Henriette Dargec7a1e7dd1b39
diff --git a/frontend/src/app/core/days/weekday.service.ts b/frontend/src/app/core/days/weekday.service.ts
new file mode 100644
index 00000000000..b4e75d66570
--- /dev/null
+++ b/frontend/src/app/core/days/weekday.service.ts
@@ -0,0 +1,69 @@
+// -- 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 {
+ Injectable,
+ Injector,
+} from '@angular/core';
+import * as moment from 'moment';
+import {
+ take,
+ tap,
+} from 'rxjs/operators';
+import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
+import { WeekdayResourceService } from 'core-app/core/state/days/weekday.service';
+import { IWeekday } from 'core-app/core/state/days/weekday.model';
+import { Observable } from 'rxjs';
+
+@Injectable({ providedIn: 'root' })
+export class WeekdayService {
+ @InjectField() weekdaysService:WeekdayResourceService;
+
+ private weekdays:IWeekday[] = [];
+
+ constructor(
+ readonly injector:Injector,
+ ) {}
+
+ public isDateDisabled(date:Date):boolean {
+ const dayOfWeek = moment(date).isoWeekday();
+ return !!this.weekdays.find((wd) => wd.day === dayOfWeek && !wd.working);
+ }
+
+ loadWeekdays():Observable<IWeekday[]> {
+ return this
+ .weekdaysService
+ .require()
+ .pipe(
+ take(1),
+ tap((weekdays) => {
+ this.weekdays = weekdays;
+ }),
+ );
+ }
+}
diff --git a/frontend/src/app/features/work-packages/components/wp-table/timeline/container/wp-timeline-container.directive.ts b/frontend/src/app/features/work-packages/components/wp-table/timeline/container/wp-timeline-container.directive.ts
index a8558695e86..e56cd475578 100644
--- a/frontend/src/app/features/work-packages/components/wp-table/timeline/container/wp-timeline-container.directive.ts
+++ b/frontend/src/app/features/work-packages/components/wp-table/timeline/container/wp-timeline-container.directive.ts
@@ -66,6 +66,7 @@ import {
TimelineViewParameters,
zoomLevelOrder,
} from '../wp-timeline';
+import { WeekdayService } from 'core-app/core/days/weekday.service';
@Component({
selector: 'wp-timeline-container',
@@ -120,7 +121,8 @@ export class WorkPackageTimelineTableController extends UntilDestroyedMixin impl
return workPackagesWithGroupHeaderCell;
}
- constructor(public readonly injector:Injector,
+ constructor(
+ public readonly injector:Injector,
private elementRef:ElementRef,
private states:States,
public wpTableComponent:WorkPackagesTableComponent,
@@ -132,7 +134,9 @@ export class WorkPackageTimelineTableController extends UntilDestroyedMixin impl
private halEvents:HalEventsService,
private querySpace:IsolatedQuerySpace,
readonly I18n:I18nService,
- private workPackageViewCollapsedGroupsService:WorkPackageViewCollapsedGroupsService) {
+ private workPackageViewCollapsedGroupsService:WorkPackageViewCollapsedGroupsService,
+ private weekdaysService:WeekdayService,
+ ) {
super();
}
@@ -157,10 +161,11 @@ export class WorkPackageTimelineTableController extends UntilDestroyedMixin impl
this.querySpace.tableRendered.values$(),
this.refreshRequest.changes$(),
this.wpTableTimeline.live$(),
+ this.weekdaysService.loadWeekdays(),
]).pipe(
this.commonPipes,
)
- .subscribe(([orderedRows, changes, timelineState]) => {
+ .subscribe(([orderedRows]) => {
// Remember all visible rows in their order of appearance.
this.workPackageIdOrder = orderedRows.filter((row:RenderedWorkPackage) => !row.hidden);
this.orderedRows = orderedRows;
diff --git a/frontend/src/app/features/work-packages/components/wp-table/timeline/grid/wp-timeline-grid.directive.ts b/frontend/src/app/features/work-packages/components/wp-table/timeline/grid/wp-timeline-grid.directive.ts
index 62c8871902f..8769e94c3e4 100644
--- a/frontend/src/app/features/work-packages/components/wp-table/timeline/grid/wp-timeline-grid.directive.ts
+++ b/frontend/src/app/features/work-packages/components/wp-table/timeline/grid/wp-timeline-grid.directive.ts
@@ -25,7 +25,11 @@
//
// See COPYRIGHT and LICENSE files for more details.
//++
-import { AfterViewInit, Component, ElementRef } from '@angular/core';
+import {
+ AfterViewInit,
+ Component,
+ ElementRef,
+} from '@angular/core';
import * as moment from 'moment';
import { TimelineZoomLevel } from 'core-app/features/hal/resources/query-resource';
import { WorkPackageTimelineTableController } from '../container/wp-timeline-container.directive';
@@ -36,18 +40,9 @@ import {
timelineGridElementCssClass,
TimelineViewParameters,
} from '../wp-timeline';
+import { WeekdayService } from 'core-app/core/days/weekday.service';
import Moment = moment.Moment;
-function checkForNonWorkingDayHighlight(date:Moment, cell:HTMLElement) {
- const day = date.day();
-
- // Sunday = 0
- // Monday = 6
- if (day === 0 || day === 6) {
- cell.classList.add('wp-timeline--non-working-day');
- }
-}
-
@Component({
selector: 'wp-timeline-grid',
template: '<div class="wp-table-timeline--grid"></div>',
@@ -57,9 +52,11 @@ export class WorkPackageTableTimelineGrid implements AfterViewInit {
private gridContainer:JQuery;
- constructor(private elementRef:ElementRef,
- public wpTimeline:WorkPackageTimelineTableController) {
- }
+ constructor(
+ private elementRef:ElementRef,
+ public wpTimeline:WorkPackageTimelineTableController,
+ private weekdaysService:WeekdayService,
+ ) {}
ngAfterViewInit():void {
const $element = jQuery(this.elementRef.nativeElement);
@@ -104,7 +101,7 @@ export class WorkPackageTableTimelineGrid implements AfterViewInit {
private renderLabelsDays(vp:TimelineViewParameters):void {
this.renderTimeSlices(vp, 'day', vp.dateDisplayStart, vp.dateDisplayEnd, (start, cell) => {
cell.style.paddingTop = '1px';
- checkForNonWorkingDayHighlight(start, cell);
+ this.checkForNonWorkingDayHighlight(start, cell);
});
this.renderTimeSlices(vp, 'year', vp.dateDisplayStart, vp.dateDisplayEnd, (start, cell) => {
@@ -115,7 +112,7 @@ export class WorkPackageTableTimelineGrid implements AfterViewInit {
private renderLabelsWeeks(vp:TimelineViewParameters):void {
this.renderTimeSlices(vp, 'day', vp.dateDisplayStart, vp.dateDisplayEnd, (start, cell) => {
- checkForNonWorkingDayHighlight(start, cell);
+ this.checkForNonWorkingDayHighlight(start, cell);
});
this.renderTimeSlices(vp, 'week', vp.dateDisplayStart, vp.dateDisplayEnd, (start, cell) => {
@@ -191,4 +188,12 @@ export class WorkPackageTableTimelineGrid implements AfterViewInit {
}
}, 0);
}
+
+ private checkForNonWorkingDayHighlight(date:Moment, cell:HTMLElement) {
+ const day = date.toDate();
+
+ if (this.weekdaysService.isDateDisabled(day)) {
+ cell.classList.add('wp-timeline--non-working-day');
+ }
+ }
}
diff --git a/frontend/src/app/shared/components/datepicker/datepicker.modal.ts b/frontend/src/app/shared/components/datepicker/datepicker.modal.ts
index 4221908343f..dfe2492dccc 100644
--- a/frontend/src/app/shared/components/datepicker/datepicker.modal.ts
+++ b/frontend/src/app/shared/components/datepicker/datepicker.modal.ts
@@ -244,7 +244,7 @@ export class DatePickerModalComponent extends OpModalComponent implements AfterV
this.onDataChange();
},
onDayCreate: (dObj:Date[], dStr:string, fp:flatpickr.Instance, dayElem:DayElement) => {
- if (this.datePickerInstance?.isDateDisabled(dayElem.dateObj)) {
+ if (this.datePickerInstance?.weekdaysService.isDateDisabled(dayElem.dateObj)) {
dayElem.classList.add('flatpickr-non-working-day');
}
diff --git a/frontend/src/app/shared/components/op-date-picker/datepicker.ts b/frontend/src/app/shared/components/op-date-picker/datepicker.ts
index 9eb7416db84..f8c7ee03c5a 100644
--- a/frontend/src/app/shared/components/op-date-picker/datepicker.ts
+++ b/frontend/src/app/shared/components/op-date-picker/datepicker.ts
@@ -34,11 +34,9 @@ import {
import { ConfigurationService } from 'core-app/core/config/configuration.service';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { rangeSeparator } from 'core-app/shared/components/op-date-picker/op-range-date-picker/op-range-date-picker.component';
-import { WeekdayResourceService } from 'core-app/core/state/days/weekday.service';
-import { IWeekday } from 'core-app/core/state/days/weekday.model';
import { Injector } from '@angular/core';
import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
-import { take } from 'rxjs/operators';
+import { WeekdayService } from 'core-app/core/days/weekday.service';
import DateOption = flatpickr.Options.DateOption;
export class DatePicker {
@@ -50,11 +48,9 @@ export class DatePicker {
private reshowTimeout:ReturnType<typeof setTimeout>;
- private weekdays:IWeekday[] = [];
-
@InjectField() configurationService:ConfigurationService;
- @InjectField() weekdaysService:WeekdayResourceService;
+ @InjectField() weekdaysService:WeekdayService;
@InjectField() I18n:I18nService;
@@ -69,7 +65,14 @@ export class DatePicker {
}
private initialize(options:flatpickr.Options.Options) {
- this.loadWeekdays();
+ this
+ .weekdaysService
+ .loadWeekdays()
+ .subscribe(() => {
+ if (this.datepickerInstance) {
+ this.datepickerInstance.redraw();
+ }
+ });
const mergedOptions = _.extend({}, this.defaultOptions, options);
@@ -159,27 +162,6 @@ export class DatePicker {
);
}
- public isDateDisabled(date:Date):boolean {
- const dayOfWeek = moment(date).isoWeekday();
- return !!this.weekdays.find((wd) => wd.day === dayOfWeek && !wd.working);
- }
-
- private loadWeekdays() {
- this
- .weekdaysService
- .require()
- .pipe(
- take(1),
- )
- .subscribe((weekdays) => {
- this.weekdays = weekdays;
-
- if (this.datepickerInstance) {
- this.datepickerInstance.redraw();
- }
- });
- }
-
private get defaultOptions() {
const firstDayOfWeek = this.configurationService.startOfWeek();
@@ -189,7 +171,7 @@ export class DatePicker {
return moment(dateObj).format('W');
},
onDayCreate: (dObj:Date[], dStr:string, fp:flatpickr.Instance, dayElem:DayElement) => {
- if (this.isDateDisabled(dayElem.dateObj)) {
+ if (this.weekdaysService.isDateDisabled(dayElem.dateObj)) {
dayElem.classList.add('flatpickr-grey-out');
}
},