Files
CT/src/Model/Files/calendarModel.ts
T

165 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-07-25 19:11:34 +02:00
// it is responsible for managing calendar-realted reference files for tasks.
2024-07-26 03:14:55 +02:00
import FileModel, { FileModelSubPath } from "./fileModel";
2024-07-25 19:11:34 +02:00
2024-07-26 03:14:55 +02:00
import SettingsModel from "../Global/settingsModel";
2024-07-25 19:11:34 +02:00
import StorageModel from "../Global/storageModel";
2024-07-26 03:14:55 +02:00
import { TaskFileContent } from "./boardsAndTasksModel";
2024-07-25 19:11:34 +02:00
export default class CalendarModel {
storageModel: StorageModel;
fileModel: FileModel;
2024-07-26 03:14:55 +02:00
settingsModel: SettingsModel;
2024-07-25 19:11:34 +02:00
// paths
getBasePath = (): string[] => {
return this.fileModel.getModelContainerPath(FileModelSubPath.ModelCalendar);
};
getViewPath = (): string[] => {
return [...this.getBasePath(), FileModelSubPath.ModelView];
};
getMonthContainerPath = (): string[] => {
return [...this.getBasePath(), CalendarModelSubPaths.Months];
};
getMonthPath = (monthString: string): string[] => {
return [...this.getMonthContainerPath(), monthString];
};
// main
storeTaskReference = (taskFileContent: TaskFileContent): void => {
if (taskFileContent.date == undefined) return;
2024-07-26 03:14:55 +02:00
2024-07-25 19:11:34 +02:00
const monthString: string = CalendarModel.isoToMonthString(
taskFileContent.date
);
const monthPath: string[] = this.getMonthPath(monthString);
const referencePath: string[] = [...monthPath, taskFileContent.fileId];
this.storageModel.write(referencePath, "");
};
2024-07-26 16:49:13 +02:00
deleteTaskReference = (monthString: string, taskId: string): void => {
const monthPath: string[] = this.getMonthPath(monthString);
const referencePath: string[] = [...monthPath, taskId];
this.storageModel.write(referencePath, "");
};
2024-07-25 19:11:34 +02:00
listTaskIds = (monthString: string): string[] => {
const monthPath: string[] = this.getMonthPath(monthString);
return this.storageModel.list(monthPath);
};
2024-07-26 03:14:55 +02:00
generateMonthGrid = <T>(
year: number,
month: number,
defaultValueCreator: () => T
): MonthGrid<T> => {
2024-07-26 05:32:07 +02:00
// get today
2024-07-26 03:14:55 +02:00
const date: Date = new Date();
2024-07-26 05:32:07 +02:00
const isCurrentMonth: boolean =
year == date.getFullYear() && month == date.getMonth() + 1;
2024-09-07 11:30:43 -07:00
// get data for offset
2024-07-26 03:14:55 +02:00
date.setDate(1);
2026-03-31 19:52:17 +00:00
date.setMonth(month - 1);
date.setFullYear(year);
2024-07-26 03:14:55 +02:00
2024-09-07 11:30:43 -07:00
const firstWeekdayOfMonth = date.getDay();
const firstDayOfWeekSetting: number = parseInt(
this.settingsModel.firstDayOfWeek
);
2024-07-26 03:14:55 +02:00
const offset: number =
2024-09-07 11:33:51 -07:00
firstWeekdayOfMonth < firstDayOfWeekSetting
2024-09-07 11:30:43 -07:00
? 7 - firstDayOfWeekSetting
: firstWeekdayOfMonth - firstDayOfWeekSetting;
2024-07-26 03:14:55 +02:00
// get total day count
date.setMonth(month);
date.setDate(-1);
const daysInMonth: number = date.getDate() + 1;
const grid: MonthGrid<T> = {
offset,
2024-07-26 03:29:07 +02:00
firstDayOfWeek: parseInt(this.settingsModel.firstDayOfWeek),
2024-07-26 05:32:07 +02:00
isCurrentMonth,
2024-07-26 07:07:07 +02:00
year,
month,
2024-07-26 03:14:55 +02:00
days: {},
};
for (let i = 0; i < daysInMonth; i++) {
2024-07-26 16:49:13 +02:00
const paddedDate: string = CalendarModel.padDateOrMonth(
(i + 1).toString()
);
grid.days[paddedDate] = defaultValueCreator();
2024-07-26 03:14:55 +02:00
}
return grid;
};
2024-07-25 19:11:34 +02:00
// init
constructor(
storageModel: StorageModel,
2024-07-26 03:14:55 +02:00
settingsModel: SettingsModel,
fileModel: FileModel
) {
2024-07-25 19:11:34 +02:00
this.storageModel = storageModel;
2024-07-26 03:14:55 +02:00
this.settingsModel = settingsModel;
2024-07-25 19:11:34 +02:00
this.fileModel = fileModel;
}
// utility
static isoToMonthString = (dateISOString: string): string => {
const [year, month, _] = dateISOString.split("-");
2024-07-26 03:14:55 +02:00
return CalendarModel.getMonthString(year, month);
};
2024-07-26 16:49:13 +02:00
static isoToDateString = (dateISOString: string): string => {
2024-07-26 03:14:55 +02:00
const [year, month, date, _] = dateISOString.split("-");
2024-07-26 16:49:13 +02:00
const paddedDate = CalendarModel.padDateOrMonth(date ?? "");
return paddedDate;
2024-07-26 03:14:55 +02:00
};
static getMonthString = (year: string = "", month: string = ""): string => {
const paddedYear: string = year.padStart(4, "0");
2024-07-26 16:49:13 +02:00
const paddedMonth: string = CalendarModel.padDateOrMonth(month);
2024-07-26 03:14:55 +02:00
return `${paddedYear}-${paddedMonth}`;
2024-07-25 19:11:34 +02:00
};
2024-07-26 06:56:34 +02:00
2024-07-26 07:07:07 +02:00
static getISODateString = (
year: string,
month: string,
date: string
): string => {
2024-07-26 06:56:34 +02:00
const monthString: string = CalendarModel.getMonthString(year, month);
2024-07-26 16:49:13 +02:00
const paddedDate: string = CalendarModel.padDateOrMonth(date);
2024-07-26 06:56:34 +02:00
return `${monthString}-${paddedDate}`;
};
2024-07-26 16:49:13 +02:00
static padDateOrMonth = (input: string): string => {
return input.padStart(2, "0");
};
2024-07-25 19:11:34 +02:00
}
export enum CalendarModelSubPaths {
Months = "months",
}
2024-07-26 03:14:55 +02:00
// types
export interface MonthGrid<T> {
offset: number;
2024-07-26 03:29:07 +02:00
firstDayOfWeek: number;
2024-07-26 05:32:07 +02:00
isCurrentMonth: boolean;
2024-07-26 07:07:07 +02:00
year: number;
month: number;
2024-07-26 03:14:55 +02:00
days: { [date: string]: T };
}