Files
CT/src/ViewModel/Pages/calendarPageViewModel.ts
T

166 lines
4.6 KiB
TypeScript
Raw Normal View History

import * as React from "bloatless-react";
import BoardsAndTasksModel, {
TaskFileContent,
} from "../../Model/Files/boardsAndTasksModel";
2024-07-26 03:14:55 +02:00
import CalendarModel, { MonthGrid } from "../../Model/Files/calendarModel";
2024-07-26 05:43:49 +02:00
import ChatViewModel from "../Chat/chatViewModel";
import CoreViewModel from "../Global/coreViewModel";
import StorageModel from "../../Model/Global/storageModel";
2024-07-26 06:20:16 +02:00
import TaskContainingPageViewModel from "./taskContainingPageViewModel";
import TaskViewModel from "./taskViewModel";
2024-07-26 06:20:16 +02:00
export default class CalendarPageViewModel extends TaskContainingPageViewModel {
storageModel: StorageModel;
calendarModel: CalendarModel;
2024-07-26 03:14:55 +02:00
boardsAndTasksModel: BoardsAndTasksModel;
2024-07-26 05:43:49 +02:00
chatViewModel: ChatViewModel;
2024-07-26 03:14:55 +02:00
// data
get monthString(): string {
return CalendarModel.getMonthString(
this.selectedYear.value.toString(),
this.selectedMonth.value.toString()
);
}
// paths
getBasePath = (): string[] => {
return [...this.calendarModel.getViewPath()];
};
// state
2024-07-26 03:14:55 +02:00
selectedYear: React.State<number> = new React.State(0);
selectedMonth: React.State<number> = new React.State(0);
2024-07-26 03:38:46 +02:00
selectedDate: React.State<number> = new React.State(0);
2024-07-26 03:14:55 +02:00
monthGrid: React.State<MonthGrid<React.MapState<TaskViewModel>> | undefined> =
new React.State<any>(undefined);
// view
2024-07-26 03:14:55 +02:00
getTaskMapState = (
taskFileContent: TaskFileContent
): React.MapState<TaskViewModel> | null => {
if (this.monthGrid.value == null) return null;
const dateString: string | undefined = CalendarModel.isoToDateString(
taskFileContent.date ?? ""
);
if (dateString == undefined) return null;
return this.monthGrid.value.days[dateString];
};
showTask = (taskFileContent: TaskFileContent): void => {
2024-07-26 03:52:05 +02:00
const monthString: string | undefined = CalendarModel.isoToMonthString(
taskFileContent.date ?? ""
);
if (monthString == undefined || monthString != this.monthString) {
// TODO remove
return;
}
const taskViewModel: TaskViewModel = new TaskViewModel(
this.coreViewModel,
2024-07-26 03:14:55 +02:00
this.boardsAndTasksModel,
this,
taskFileContent
);
2024-07-26 03:14:55 +02:00
const mapState: React.MapState<TaskViewModel> | null =
this.getTaskMapState(taskFileContent);
mapState?.set(taskFileContent.fileId, taskViewModel);
2024-07-26 06:20:16 +02:00
this.taskViewModels.set(taskFileContent.fileId, taskViewModel);
2024-07-26 03:14:55 +02:00
};
2024-07-26 03:52:05 +02:00
removeTaskFromView = (taskFileContent: TaskFileContent): void => {
2024-07-26 03:14:55 +02:00
const mapState: React.MapState<TaskViewModel> | null =
this.getTaskMapState(taskFileContent);
mapState?.remove(taskFileContent.fileId);
2024-07-26 06:20:16 +02:00
this.taskViewModels.remove(taskFileContent.fileId);
2024-07-26 03:14:55 +02:00
};
showToday = (): void => {
const today: Date = new Date();
this.selectedYear.value = today.getFullYear();
this.selectedMonth.value = today.getMonth() + 1;
2024-07-26 03:38:46 +02:00
this.selectedDate.value = today.getDate();
2024-07-26 03:14:55 +02:00
};
showPreviousMonth = (): void => {
this.selectedMonth.value -= 1;
if (this.selectedMonth.value <= 0) {
this.selectedYear.value -= 1;
this.selectedMonth.value = 12;
}
};
showNextMonth = (): void => {
this.selectedMonth.value += 1;
if (this.selectedMonth.value >= 13) {
this.selectedYear.value += 1;
this.selectedMonth.value = 1;
}
};
// load
2024-07-26 05:43:49 +02:00
loadMonthTasks = (): void => {
2024-07-26 03:14:55 +02:00
this.monthGrid.value = this.calendarModel.generateMonthGrid(
this.selectedYear.value,
this.selectedMonth.value,
() => new React.MapState()
);
const taskIds: string[] = this.calendarModel.listTaskIds(this.monthString);
for (const taskId of taskIds) {
const taskFileContent: TaskFileContent | null =
this.boardsAndTasksModel.getLatestTaskFileContent(taskId);
if (taskFileContent == null) continue;
this.showTask(taskFileContent);
}
2024-07-26 06:20:16 +02:00
this.updateTaskIndices();
2024-07-26 03:14:55 +02:00
};
loadData = (): void => {
2024-07-26 05:43:49 +02:00
this.loadMonthTasks();
2024-07-26 03:14:55 +02:00
this.showToday();
};
// init
constructor(
public coreViewModel: CoreViewModel,
storageModel: StorageModel,
calendarModel: CalendarModel,
2024-07-26 06:20:16 +02:00
boardsAndTasksModel: BoardsAndTasksModel,
2024-07-26 05:43:49 +02:00
chatViewModel: ChatViewModel
) {
2024-07-26 06:20:16 +02:00
super(coreViewModel, boardsAndTasksModel);
this.storageModel = storageModel;
this.calendarModel = calendarModel;
2024-07-26 06:20:16 +02:00
this.boardsAndTasksModel = boardsAndTasksModel;
2024-07-26 03:14:55 +02:00
2024-07-26 05:43:49 +02:00
this.chatViewModel = chatViewModel;
2024-07-26 03:14:55 +02:00
React.bulkSubscribe([this.selectedYear, this.selectedMonth], () => {
2024-07-26 05:43:49 +02:00
this.loadMonthTasks();
2024-07-26 03:14:55 +02:00
});
2024-07-26 03:52:05 +02:00
// handlers
2024-07-26 06:20:16 +02:00
boardsAndTasksModel.taskHandlerManager.addHandler(
2024-07-26 03:52:05 +02:00
(taskFileContent: TaskFileContent) => {
this.showTask(taskFileContent);
}
);
}
}
export enum TaskPageViewModelSubPath {
LastUsedBoard = "last-used-board",
}