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

134 lines
3.6 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, "");
};
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;
// get first day of month
2024-07-26 03:14:55 +02:00
date.setFullYear(year);
date.setMonth(month - 1);
date.setDate(1);
// get offset
const offset: number =
date.getDay() - parseInt(this.settingsModel.firstDayOfWeek);
// 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 03:14:55 +02:00
days: {},
};
for (let i = 0; i < daysInMonth; i++) {
grid.days[i + 1] = defaultValueCreator();
}
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);
};
static isoToDateString = (dateISOString: string): string | undefined => {
const [year, month, date, _] = dateISOString.split("-");
return date;
};
static getMonthString = (year: string = "", month: string = ""): string => {
const paddedYear: string = year.padStart(4, "0");
const paddedMonth: string = month.padStart(2, "0");
return `${paddedYear}-${paddedMonth}`;
2024-07-25 19:11:34 +02:00
};
2024-07-26 06:56:34 +02:00
static getISODateString = (year: string, month: string, date: string): string => {
const monthString: string = CalendarModel.getMonthString(year, month);
const paddedDate: string = date.padStart(2, "0");
return `${monthString}-${paddedDate}`;
};
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 03:14:55 +02:00
days: { [date: string]: T };
}