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

250 lines
6.4 KiB
TypeScript
Raw Normal View History

2024-07-23 00:57:40 +02:00
import * as React from "bloatless-react";
2024-07-24 00:38:37 +02:00
import BoardModel, {
BoardInfoFileContent,
2024-07-24 01:19:44 +02:00
TaskFileContent,
2024-07-24 00:38:37 +02:00
} from "../../Model/Files/boardModel";
2024-07-23 00:57:40 +02:00
import { Color } from "../../colors";
2024-07-24 14:34:28 +02:00
import { IndexManager } from "../../Model/Utility/utility";
import StorageModel from "../../Model/Global/storageModel";
2024-07-23 00:57:40 +02:00
import TaskPageViewModel from "./taskPageViewModel";
2024-07-24 01:19:44 +02:00
import TaskViewModel from "./taskViewModel";
2024-07-23 00:57:40 +02:00
export default class BoardViewModel {
storageModel: StorageModel;
2024-07-24 00:38:37 +02:00
boardModel: BoardModel;
2024-07-23 00:57:40 +02:00
taskPageViewModel: TaskPageViewModel;
2024-07-24 00:38:37 +02:00
// data
2024-07-23 00:57:40 +02:00
boardInfo: BoardInfoFileContent;
2024-07-24 14:34:28 +02:00
taskIndexManager: IndexManager<TaskViewModel> = new IndexManager(
(taskViewModel: TaskViewModel) => taskViewModel.sortingString
);
2024-07-24 00:38:37 +02:00
// state
2024-07-23 00:57:40 +02:00
name: React.State<string> = new React.State("");
color: React.State<Color> = new React.State<Color>(Color.Standard);
2024-07-24 13:31:28 +02:00
index: React.State<number> = new React.State(0);
taskViewModels: React.MapState<TaskViewModel> = new React.MapState();
2024-07-23 02:40:11 +02:00
2024-07-23 15:41:03 +02:00
selectedPage: React.State<BoardPageType> = new React.State<BoardPageType>(
BoardPageType.List
);
2024-07-24 13:31:28 +02:00
selectedTaskViewModel: React.State<TaskViewModel | undefined> =
new React.State<TaskViewModel | undefined>(undefined);
2024-07-23 15:41:03 +02:00
2024-07-24 13:31:28 +02:00
isSelected: React.State<boolean>;
isPresentingSettingsModal: React.State<boolean> = new React.State(false);
isPresentingFilterModal: React.State<boolean> = new React.State(false);
2024-07-24 01:19:44 +02:00
// paths
getBasePath = (): string[] => {
return [...this.taskPageViewModel.getBoardViewPath(this.boardInfo.fileId)];
};
getLastUsedBoardPath = (): string[] => {
return [...this.getBasePath(), BoardViewModelSubPaths.LastUsedView];
};
2024-07-24 01:19:44 +02:00
// settings
saveSettings = (): void => {
const newBoardInfoFileContent: BoardInfoFileContent =
2024-07-24 00:12:45 +02:00
BoardModel.createBoardInfoFileContent(
this.boardInfo.fileId,
this.name.value,
this.color.value
);
this.taskPageViewModel.updateBoard(newBoardInfoFileContent);
};
2024-07-23 13:53:50 +02:00
applyColor = (): void => {
this.taskPageViewModel.chatViewModel.setDisplayedColor(this.color.value);
2024-07-23 13:48:17 +02:00
};
2024-07-23 14:31:50 +02:00
deleteBoard = (): void => {
this.taskPageViewModel.deleteBoard(this.boardInfo);
this.close();
};
2024-07-24 14:34:28 +02:00
// methods
2024-07-24 13:31:28 +02:00
createTask = (): void => {
const taskFileContent: TaskFileContent = this.boardModel.createTask(
this.boardInfo.fileId
);
const taskViewModel: TaskViewModel = new TaskViewModel(
this.boardModel,
this,
taskFileContent
);
this.selectTask(taskViewModel);
2024-07-24 14:34:28 +02:00
this.updateTaskIndices();
};
removeTaskFromList = (taskId: string): void => {
this.taskViewModels.remove(taskId);
this.updateIndex();
2024-07-24 13:31:28 +02:00
};
2024-07-24 01:19:44 +02:00
// storage
storeLastUsedView = (): void => {
const path: string[] = this.getLastUsedBoardPath();
const lastUsedView: string = this.selectedPage.value;
this.storageModel.write(path, lastUsedView);
};
restoreLastUsedView = (): void => {
const path: string[] = this.getLastUsedBoardPath();
const lastUsedView: string | null = this.storageModel.read(path);
if (lastUsedView == null) return;
this.selectedPage.value = lastUsedView as BoardPageType;
};
2024-07-24 13:31:28 +02:00
// view
2024-07-24 13:40:17 +02:00
showTaskInList = (taskFileContent: TaskFileContent): void => {
const taskViewModel: TaskViewModel = new TaskViewModel(
this.boardModel,
this,
taskFileContent
);
this.taskViewModels.set(taskFileContent.fileId, taskViewModel);
};
2024-07-24 13:31:28 +02:00
select = (): void => {
this.taskPageViewModel.selectBoard(this);
};
close = (): void => {
this.taskPageViewModel.closeBoard();
};
showSettings = (): void => {
this.isPresentingSettingsModal.value = true;
};
hideSettings = (): void => {
this.saveSettings();
this.isPresentingSettingsModal.value = false;
};
showFilterModal = (): void => {
this.isPresentingFilterModal.value = true;
};
hideFilterModal = (): void => {
this.isPresentingFilterModal.value = false;
};
selectTask = (selectedTask: TaskViewModel): void => {
this.selectedTaskViewModel.value = selectedTask;
};
closeTask = () => {
this.selectedTaskViewModel.value = undefined;
};
updateIndex = (): void => {
const index: number =
this.taskPageViewModel.boardIndexManager.getIndex(this);
this.index.value = index;
};
2024-07-24 14:34:28 +02:00
updateTaskIndices = (): void => {
this.taskIndexManager.update([...this.taskViewModels.value.values()]);
for (const boardViewModel of this.taskViewModels.value.values()) {
boardViewModel.updateIndex();
}
};
2024-07-23 00:57:40 +02:00
// load
loadListRelevantData = (): void => {
this.name.value = this.boardInfo.name;
this.color.value = this.boardInfo.color;
2024-07-23 02:40:11 +02:00
};
2024-07-23 00:57:40 +02:00
2024-07-24 01:19:44 +02:00
loadTasks = (): void => {
this.taskViewModels.clear();
const taskIds: string[] = this.boardModel.listTaskIds(
this.boardInfo.fileId
);
for (const taskId of taskIds) {
const taskFileContent: TaskFileContent | null =
2024-07-24 15:57:02 +02:00
this.boardModel.getLatestTaskFileContent(taskId);
2024-07-24 01:19:44 +02:00
if (taskFileContent == null) continue;
const taskViewModel: TaskViewModel = new TaskViewModel(
this.boardModel,
this,
taskFileContent
);
this.taskViewModels.set(taskFileContent.fileId, taskViewModel);
}
2024-07-24 14:34:28 +02:00
this.updateTaskIndices();
2024-07-24 01:19:44 +02:00
};
loadData = (): void => {
this.restoreLastUsedView();
2024-07-24 01:19:44 +02:00
this.loadTasks();
};
2024-07-23 00:57:40 +02:00
// init
2024-07-23 02:40:11 +02:00
constructor(
2024-07-24 00:38:37 +02:00
storageModel: StorageModel,
boardModel: BoardModel,
2024-07-23 02:40:11 +02:00
taskPageViewModel: TaskPageViewModel,
2024-07-24 00:38:37 +02:00
boardInfo: BoardInfoFileContent
2024-07-23 02:40:11 +02:00
) {
2024-07-24 13:40:17 +02:00
// set
this.storageModel = storageModel;
2024-07-24 00:38:37 +02:00
this.boardModel = boardModel;
2024-07-23 00:57:40 +02:00
this.taskPageViewModel = taskPageViewModel;
this.boardInfo = boardInfo;
2024-07-24 13:40:17 +02:00
// load
2024-07-23 00:57:40 +02:00
this.loadListRelevantData();
2024-07-23 02:40:11 +02:00
2024-07-24 13:40:17 +02:00
// subscriptions
2024-07-23 02:40:11 +02:00
this.isSelected = React.createProxyState(
2024-07-23 13:51:27 +02:00
[this.taskPageViewModel.selectedBoardId],
() =>
this.taskPageViewModel.selectedBoardId.value == this.boardInfo.fileId
2024-07-23 02:40:11 +02:00
);
2024-07-23 13:48:17 +02:00
2024-07-23 13:53:50 +02:00
this.color.subscribe(() => {
if (this.isSelected.value == false) return;
this.applyColor();
2024-07-23 13:48:17 +02:00
});
2024-07-24 13:40:17 +02:00
this.selectedPage.subscribeSilent(() => {
this.storeLastUsedView();
});
2024-07-24 13:40:17 +02:00
// handlers
boardModel.taskHandlerManager.addHandler(
(taskFileContent: TaskFileContent) => {
if (taskFileContent.boardId != this.boardInfo.fileId) return;
2024-07-24 13:40:17 +02:00
this.showTaskInList(taskFileContent);
2024-07-24 14:34:28 +02:00
this.updateTaskIndices();
2024-07-24 13:40:17 +02:00
}
);
2024-07-23 00:57:40 +02:00
}
}
2024-07-23 15:41:03 +02:00
export enum BoardViewModelSubPaths {
LastUsedView = "last-used-view",
}
2024-07-23 15:41:03 +02:00
// types
export enum BoardPageType {
List = "list",
Kanban = "kanban",
StatusGrid = "status-grid",
}