2024-07-25 19:32:46 +02:00
|
|
|
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-25 19:32:46 +02:00
|
|
|
|
|
|
|
|
import CoreViewModel from "../Global/coreViewModel";
|
|
|
|
|
import StorageModel from "../../Model/Global/storageModel";
|
|
|
|
|
import TaskViewModel from "./taskViewModel";
|
|
|
|
|
|
|
|
|
|
export default class CalendarPageViewModel {
|
|
|
|
|
storageModel: StorageModel;
|
|
|
|
|
calendarModel: CalendarModel;
|
2024-07-26 03:14:55 +02:00
|
|
|
boardsAndTasksModel: BoardsAndTasksModel;
|
|
|
|
|
|
|
|
|
|
// data
|
|
|
|
|
get monthString(): string {
|
|
|
|
|
return CalendarModel.getMonthString(
|
|
|
|
|
this.selectedYear.value.toString(),
|
|
|
|
|
this.selectedMonth.value.toString()
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-07-25 19:32:46 +02:00
|
|
|
|
|
|
|
|
// 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);
|
2024-07-25 19:32:46 +02:00
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-25 19:32:46 +02:00
|
|
|
showTask = (taskFileContent: TaskFileContent): void => {
|
|
|
|
|
const taskViewModel: TaskViewModel = new TaskViewModel(
|
|
|
|
|
this.coreViewModel,
|
2024-07-26 03:14:55 +02:00
|
|
|
this.boardsAndTasksModel,
|
2024-07-25 19:32:46 +02:00
|
|
|
null,
|
|
|
|
|
this,
|
|
|
|
|
taskFileContent
|
|
|
|
|
);
|
2024-07-26 03:14:55 +02:00
|
|
|
const mapState: React.MapState<TaskViewModel> | null =
|
|
|
|
|
this.getTaskMapState(taskFileContent);
|
|
|
|
|
mapState?.set(taskFileContent.fileId, taskViewModel);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
removeTaskFromList = (taskFileContent: TaskFileContent): void => {
|
|
|
|
|
const mapState: React.MapState<TaskViewModel> | null =
|
|
|
|
|
this.getTaskMapState(taskFileContent);
|
|
|
|
|
mapState?.remove(taskFileContent.fileId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-07-25 19:32:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// storage
|
|
|
|
|
|
|
|
|
|
// load
|
2024-07-26 03:14:55 +02:00
|
|
|
loadTasks = (): void => {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loadData = (): void => {
|
|
|
|
|
this.loadTasks();
|
|
|
|
|
this.showToday();
|
|
|
|
|
};
|
2024-07-25 19:32:46 +02:00
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
constructor(
|
|
|
|
|
public coreViewModel: CoreViewModel,
|
|
|
|
|
storageModel: StorageModel,
|
|
|
|
|
calendarModel: CalendarModel,
|
2024-07-26 03:38:46 +02:00
|
|
|
boardAndTasksModel: BoardsAndTasksModel,
|
2024-07-25 19:32:46 +02:00
|
|
|
) {
|
|
|
|
|
this.storageModel = storageModel;
|
|
|
|
|
this.calendarModel = calendarModel;
|
2024-07-26 03:14:55 +02:00
|
|
|
this.boardsAndTasksModel = boardAndTasksModel;
|
|
|
|
|
|
|
|
|
|
React.bulkSubscribe([this.selectedYear, this.selectedMonth], () => {
|
|
|
|
|
this.loadTasks();
|
|
|
|
|
});
|
2024-07-25 19:32:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export enum TaskPageViewModelSubPath {
|
|
|
|
|
LastUsedBoard = "last-used-board",
|
|
|
|
|
}
|