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 static functions into helper

Oliver Günther76ff3a53a5df
diff --git a/frontend/src/app/shared/components/datepicker/helpers/date-modal.helpers.ts b/frontend/src/app/shared/components/datepicker/helpers/date-modal.helpers.ts
new file mode 100644
index 00000000000..b9e8eff6ba6
--- /dev/null
+++ b/frontend/src/app/shared/components/datepicker/helpers/date-modal.helpers.ts
@@ -0,0 +1,110 @@
+// -- 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 { DatePicker } from 'core-app/shared/components/op-date-picker/datepicker';
+import { DateOption } from 'flatpickr/dist/types/options';
+import { DayElement } from 'flatpickr/dist/types/instance';
+
+
+// eslint-disable-next-line class-methods-use-this
+export function parseDate(date:Date|string):Date|'' {
+ if (date instanceof Date) {
+ return new Date(date.setHours(0, 0, 0, 0));
+ }
+ if (date === '') {
+ return '';
+ }
+ return new Date(moment(date).toDate().setHours(0, 0, 0, 0));
+}
+
+// eslint-disable-next-line class-methods-use-this
+export function validDate(date:Date|string):boolean {
+ return (date instanceof Date)
+ || (date === '')
+ || !!new Date(date).valueOf();
+}
+
+export function areDatesEqual(firstDate:Date|string, secondDate:Date|string):boolean {
+ const parsedDate1 = parseDate(firstDate);
+ const parsedDate2 = parseDate(secondDate);
+
+ if ((typeof (parsedDate1) === 'string') || (typeof (parsedDate2) === 'string')) {
+ return false;
+ }
+ return parsedDate1.getTime() === parsedDate2.getTime();
+}
+
+export function keepCurrentlyActiveMonth(datePicker:DatePicker, currentMonth:number, currentYear:number) {
+ // Keep currently active month and avoid jump because of two-month layout
+ datePicker.datepickerInstance.currentMonth = currentMonth;
+ datePicker.datepickerInstance.currentYear = currentYear;
+}
+
+export function setDates(dates:DateOption|DateOption[], datePicker:DatePicker, enforceDate?:Date):void {
+ const { currentMonth } = datePicker.datepickerInstance;
+ const { currentYear } = datePicker.datepickerInstance;
+ datePicker.setDates(dates);
+
+ if (enforceDate) {
+ const enforcedMonth = enforceDate.getMonth();
+ const enforcedYear = enforceDate.getFullYear();
+ const monthDiff = enforcedMonth - currentMonth + 12 * (enforcedYear - currentYear);
+
+ // Because of the two-month layout we only have to update the calendar
+ // if the month is further in the past/future than the one additional month that is shown anyway
+ if (Math.abs(monthDiff) > 1) {
+ datePicker.datepickerInstance.currentMonth = enforcedMonth;
+ datePicker.datepickerInstance.currentYear = enforcedYear;
+ } else {
+ keepCurrentlyActiveMonth(datePicker, currentMonth, currentYear);
+ }
+ } else {
+ keepCurrentlyActiveMonth(datePicker, currentMonth, currentYear);
+ }
+
+ datePicker.datepickerInstance.redraw();
+}
+
+export function onDayCreate(
+ dayElem:DayElement,
+ includeNonWorkingDays:boolean,
+ isNonWorkingDay:boolean,
+ minimalDate:Date|null|undefined,
+ isDayDisabled:boolean,
+):void {
+ if (!includeNonWorkingDays && isNonWorkingDay) {
+ dayElem.classList.add('flatpickr-non-working-day');
+ }
+
+ if (isDayDisabled) {
+ dayElem.classList.add('flatpickr-disabled');
+ }
+
+ dayElem.setAttribute('data-iso-date', dayElem.dateObj.toISOString());
+}
+
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 c58713742cb..3cedfaa3d8c 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
@@ -269,8 +269,8 @@ export class MultiDateModalComponent extends OpModalComponent implements AfterVi
// Apply the dates if they could be changed
if (this.isSchedulable) {
- this.changeset.setValue('startDate', this.datepickerService.mappedDate(this.dates.start || ''));
- this.changeset.setValue('dueDate', this.datepickerService.mappedDate(this.dates.end || ''));
+ this.changeset.setValue('startDate', this.dates.start);
+ this.changeset.setValue('dueDate', this.dates.end);
}
this.closeMe();
diff --git a/frontend/src/app/shared/components/datepicker/services/datepicker.modal.service.ts b/frontend/src/app/shared/components/datepicker/services/datepicker.modal.service.ts
index d371a660f37..5739b4aa292 100644
--- a/frontend/src/app/shared/components/datepicker/services/datepicker.modal.service.ts
+++ b/frontend/src/app/shared/components/datepicker/services/datepicker.modal.service.ts
@@ -30,12 +30,9 @@ import {
Inject,
Injectable,
} from '@angular/core';
-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 { WorkPackageChangeset } from 'core-app/features/work-packages/components/wp-edit/work-package-changeset';
-import { DayElement } from 'flatpickr/dist/types/instance';
@Injectable()
export class DatepickerModalService {
@@ -45,90 +42,5 @@ export class DatepickerModalService {
@Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,
) {}
- /**
- * Map the date to the internal format,
- * setting to null if it's empty.
- * @param date
- */
- // eslint-disable-next-line class-methods-use-this
- mappedDate(date:string):string|null {
- return date === '' ? null : date;
- }
- // eslint-disable-next-line class-methods-use-this
- parseDate(date:Date|string):Date|'' {
- if (date instanceof Date) {
- return new Date(date.setHours(0, 0, 0, 0));
- }
- if (date === '') {
- return '';
- }
- return new Date(moment(date).toDate().setHours(0, 0, 0, 0));
- }
-
- // eslint-disable-next-line class-methods-use-this
- validDate(date:Date|string):boolean {
- return (date instanceof Date)
- || (date === '')
- || !!new Date(date).valueOf();
- }
-
- areDatesEqual(firstDate:Date|string, secondDate:Date|string):boolean {
- const parsedDate1 = this.parseDate(firstDate);
- const parsedDate2 = this.parseDate(secondDate);
-
- if ((typeof (parsedDate1) === 'string') || (typeof (parsedDate2) === 'string')) {
- return false;
- }
- return parsedDate1.getTime() === parsedDate2.getTime();
- }
-
- setDates(dates:DateOption|DateOption[], datePicker:DatePicker, enforceDate?:Date):void {
- const { currentMonth } = datePicker.datepickerInstance;
- const { currentYear } = datePicker.datepickerInstance;
- datePicker.setDates(dates);
-
- if (enforceDate) {
- const enforcedMonth = enforceDate.getMonth();
- const enforcedYear = enforceDate.getFullYear();
- const monthDiff = enforcedMonth - currentMonth + 12 * (enforcedYear - currentYear);
-
- // Because of the two-month layout we only have to update the calendar
- // if the month is further in the past/future than the one additional month that is shown anyway
- if (Math.abs(monthDiff) > 1) {
- datePicker.datepickerInstance.currentMonth = enforcedMonth;
- datePicker.datepickerInstance.currentYear = enforcedYear;
- } else {
- this.keepCurrentlyActiveMonth(datePicker, currentMonth, currentYear);
- }
- } else {
- this.keepCurrentlyActiveMonth(datePicker, currentMonth, currentYear);
- }
-
- datePicker.datepickerInstance.redraw();
- }
-
- onDayCreate(
- dayElem:DayElement,
- includeNonWorkingDays:boolean,
- isNonWorkingDay:boolean,
- minimalDate:Date|null|undefined,
- isDayDisabled:boolean,
- ):void {
- if (!includeNonWorkingDays && isNonWorkingDay) {
- dayElem.classList.add('flatpickr-non-working-day');
- }
-
- if (isDayDisabled) {
- dayElem.classList.add('flatpickr-disabled');
- }
-
- dayElem.setAttribute('data-iso-date', dayElem.dateObj.toISOString());
- }
-
- private keepCurrentlyActiveMonth(datePicker:DatePicker, currentMonth:number, currentYear:number) {
- // Keep currently active month and avoid jump because of two-month layout
- datePicker.datepickerInstance.currentMonth = currentMonth;
- datePicker.datepickerInstance.currentYear = currentYear;
- }
}
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 bb2f05f7773..be5f9b13add 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,11 @@ import {
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';
+import { DateModalSchedulingService } from 'core-app/shared/components/datepicker/services/date-modal-scheduling.service';
+import {
+ parseDate,
+ validDate,
+} from 'core-app/shared/components/datepicker/helpers/date-modal.helpers';
@Component({
templateUrl: './single-date.modal.html',
@@ -212,7 +217,7 @@ export class SingleDateModalComponent extends OpModalComponent implements AfterV
// Apply the dates if they could be changed
if (this.isSchedulable) {
- this.changeset.setValue('date', this.datepickerService.mappedDate(this.date || ''));
+ this.changeset.setValue('date', this.date );
}
this.closeMe();