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

170 lines
5.1 KiB
TypeScript
Raw Normal View History

2026-04-18 11:55:01 +00:00
// cleanup: Phase A
2024-07-25 19:11:34 +02:00
2024-07-26 03:14:55 +02:00
import FileModel, { FileModelSubPath } from "./fileModel";
import { TaskFileContent } from "./boardsAndTasksModel";
2026-04-18 11:55:01 +00:00
import StorageModel from "../Global/storageModel";
import SettingsModel from "../Global/settingsModel";
2026-04-16 11:56:42 +00:00
import CoreViewModel from "../../ViewModel/Global/coreViewModel";
2024-07-25 19:11:34 +02:00
export default class CalendarModel {
2026-04-13 10:58:21 +00:00
readonly storageModel: StorageModel;
readonly fileModel: FileModel;
readonly settingsModel: SettingsModel;
2026-04-01 08:29:39 +00:00
// paths
2026-04-18 11:55:01 +00:00
get basePath (): string[] {
2026-04-01 08:29:39 +00:00
return this.fileModel.getModelContainerPath(
FileModelSubPath.ModelCalendar,
);
2024-07-26 03:14:55 +02:00
};
2026-04-18 11:55:01 +00:00
readonly getViewPath = (): string[] => {
return [...this.basePath, FileModelSubPath.ModelView];
2026-04-01 08:29:39 +00:00
};
2026-04-18 11:55:01 +00:00
readonly getMonthContainerPath = (): string[] => {
return [...this.basePath, CalendarModelSubPaths.Months];
2026-04-01 08:29:39 +00:00
};
2026-04-18 11:55:01 +00:00
readonly getMonthPath = (monthString: string): string[] => {
2026-04-01 08:29:39 +00:00
return [...this.getMonthContainerPath(), monthString];
};
2026-04-18 11:55:01 +00:00
// task references
readonly storeTaskReference = (taskFileContent: TaskFileContent): void => {
2026-04-01 08:29:39 +00:00
if (taskFileContent.date == undefined) return;
const monthString: string = CalendarModel.isoToMonthString(
taskFileContent.date,
);
const monthPath: string[] = this.getMonthPath(monthString);
const referencePath: string[] = [...monthPath, taskFileContent.fileId];
this.storageModel.write(referencePath, "");
};
2026-04-18 11:55:01 +00:00
readonly deleteTaskReference = (monthString: string, taskId: string): void => {
2026-04-01 08:29:39 +00:00
const monthPath: string[] = this.getMonthPath(monthString);
const referencePath: string[] = [...monthPath, taskId];
this.storageModel.write(referencePath, "");
};
2026-04-18 11:55:01 +00:00
// data
readonly listTaskIds = (monthString: string): string[] => {
2026-04-01 08:29:39 +00:00
const monthPath: string[] = this.getMonthPath(monthString);
return this.storageModel.list(monthPath);
};
2026-04-18 11:55:01 +00:00
// util
readonly generateMonthGrid = <T>(
2026-04-16 11:56:42 +00:00
coreViewModel: CoreViewModel,
2026-04-01 08:29:39 +00:00
year: number,
month: number,
defaultValueCreator: () => T,
): MonthGrid<T> => {
// get today
2026-04-16 11:56:42 +00:00
const date = coreViewModel.unwrappedTodayDate;
2026-04-01 08:29:39 +00:00
const isCurrentMonth: boolean =
year == date.getFullYear() && month == date.getMonth() + 1;
// get data for offset
date.setDate(1);
date.setMonth(month - 1);
date.setFullYear(year);
2026-04-13 11:23:17 +00:00
const firstWeekdayOfMonth: number = date.getDay();
2026-04-01 08:29:39 +00:00
const firstDayOfWeekSetting: number = parseInt(
this.settingsModel.firstDayOfWeek,
);
const offset: number =
firstWeekdayOfMonth < firstDayOfWeekSetting
? 7 - firstDayOfWeekSetting
: firstWeekdayOfMonth - firstDayOfWeekSetting;
// get total day count
date.setMonth(month);
date.setDate(-1);
const daysInMonth: number = date.getDate() + 1;
const grid: MonthGrid<T> = {
offset,
firstDayOfWeek: parseInt(this.settingsModel.firstDayOfWeek),
isCurrentMonth,
year,
month,
days: {},
};
for (let i = 0; i < daysInMonth; i++) {
const paddedDate: string = CalendarModel.padDateOrMonth(
(i + 1).toString(),
);
grid.days[paddedDate] = defaultValueCreator();
}
return grid;
};
// init
constructor(
storageModel: StorageModel,
settingsModel: SettingsModel,
fileModel: FileModel,
) {
this.storageModel = storageModel;
this.settingsModel = settingsModel;
this.fileModel = fileModel;
2024-07-26 03:14:55 +02:00
}
2026-04-01 08:29:39 +00:00
// utility
static isoToMonthString = (dateISOString: string): string => {
const [year, month, _] = dateISOString.split("-");
return CalendarModel.getMonthString(year, month);
};
static isoToDateString = (dateISOString: string): string => {
const [year, month, date, _] = dateISOString.split("-");
const paddedDate = CalendarModel.padDateOrMonth(date ?? "");
return paddedDate;
};
static getMonthString = (year: string = "", month: string = ""): string => {
const paddedYear: string = year.padStart(4, "0");
const paddedMonth: string = CalendarModel.padDateOrMonth(month);
return `${paddedYear}-${paddedMonth}`;
};
static getISODateString = (
year: string,
month: string,
date: string,
): string => {
const monthString: string = CalendarModel.getMonthString(year, month);
const paddedDate: string = CalendarModel.padDateOrMonth(date);
return `${monthString}-${paddedDate}`;
};
static padDateOrMonth = (input: string): string => {
return input.padStart(2, "0");
};
2024-07-25 19:11:34 +02:00
}
export enum CalendarModelSubPaths {
2026-04-01 08:29:39 +00:00
Months = "months",
2024-07-25 19:11:34 +02:00
}
2024-07-26 03:14:55 +02:00
// types
export interface MonthGrid<T> {
2026-04-13 10:58:21 +00:00
readonly offset: number;
readonly firstDayOfWeek: number;
readonly isCurrentMonth: boolean;
2024-07-26 07:07:07 +02:00
2026-04-13 10:58:21 +00:00
readonly year: number;
readonly month: number;
2024-07-26 07:07:07 +02:00
2026-04-13 10:58:21 +00:00
readonly days: { [date: string]: T };
2024-07-26 03:14:55 +02:00
}