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

165 lines
4.6 KiB
TypeScript
Raw Normal View History

2024-07-22 19:54:39 +02:00
import * as React from "bloatless-react";
import BoardModel, { BoardInfoFileContent } from "../../Model/Files/boardModel";
2024-07-22 20:02:13 +02:00
2024-07-23 00:57:40 +02:00
import BoardViewModel from "./boardViewModel";
2024-07-23 02:50:22 +02:00
import ChatViewModel from "../Chat/chatViewModel";
2024-07-23 14:27:38 +02:00
import { IndexManager } from "../../Model/Utility/utility";
2024-07-22 20:02:13 +02:00
import StorageModel from "../../Model/Global/storageModel";
2024-07-22 21:07:44 +02:00
export default class TaskPageViewModel {
2024-07-23 02:50:22 +02:00
storageModel: StorageModel;
2024-07-24 00:38:37 +02:00
boardModel: BoardModel;
2024-07-22 20:02:13 +02:00
2024-07-23 02:50:22 +02:00
chatViewModel: ChatViewModel;
2024-07-23 14:27:38 +02:00
// data
boardIndexManager: IndexManager<BoardViewModel> = new IndexManager(
(boardViewModel: BoardViewModel) => boardViewModel.name.value
);
2024-07-23 18:51:41 +02:00
// paths
getBasePath = (): string[] => {
2024-07-24 00:38:37 +02:00
return [...this.boardModel.getViewPath()];
};
getBoardViewPath = (boardId): string[] => {
return [...this.getBasePath(), boardId];
2024-07-23 18:51:41 +02:00
};
getLastUsedBoardPath = (): string[] => {
2024-07-23 21:19:56 +02:00
return [...this.getBasePath(), TaskPageViewModelSubPaths.LastUsedBoard];
2024-07-23 18:51:41 +02:00
};
2024-07-22 20:02:13 +02:00
// state
newBoardNameInput: React.State<string> = new React.State("");
2024-07-23 01:40:30 +02:00
boardViewModels: React.MapState<BoardViewModel> = new React.MapState();
2024-07-22 20:02:13 +02:00
2024-07-23 13:51:27 +02:00
selectedBoardId: React.State<string | undefined> = new React.State<
string | undefined
2024-07-23 02:40:11 +02:00
>(undefined);
2024-07-22 20:02:13 +02:00
// guards
cannotCreateBoard: React.State<boolean> = React.createProxyState(
[this.newBoardNameInput],
() => this.newBoardNameInput.value == ""
);
// methods
createBoard = (): void => {
if (this.cannotCreateBoard.value == true) return;
2024-07-23 00:32:29 +02:00
const boardInfoFileContent: BoardInfoFileContent =
2024-07-24 00:38:37 +02:00
this.boardModel.createBoard(this.newBoardNameInput.value);
2024-07-22 20:02:13 +02:00
this.newBoardNameInput.value = "";
2024-07-23 00:05:48 +02:00
2024-07-23 02:40:11 +02:00
this.showBoardInList(boardInfoFileContent);
this.boardModel.updateBoardAndSend(boardInfoFileContent);
2024-07-24 14:34:28 +02:00
this.updateBoardIndices();
2024-07-23 00:05:48 +02:00
};
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
this.boardModel.updateBoardAndSend(boardInfoFileContent);
2024-07-24 14:34:28 +02:00
this.updateBoardIndices();
};
2024-07-23 14:31:50 +02:00
deleteBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
2024-07-24 00:38:37 +02:00
this.boardModel.deleteBoard(boardInfoFileContent.fileId);
2024-07-23 14:31:50 +02:00
this.boardViewModels.remove(boardInfoFileContent.fileId);
2024-07-24 14:34:28 +02:00
this.updateBoardIndices();
2024-07-23 14:31:50 +02:00
};
// view
2024-07-23 02:40:11 +02:00
showBoardInList = (boardInfo: BoardInfoFileContent): void => {
const boardViewModel: BoardViewModel = new BoardViewModel(
2024-07-24 00:38:37 +02:00
this.storageModel,
this.boardModel,
this,
2024-07-24 00:38:37 +02:00
boardInfo
);
2024-07-23 01:40:30 +02:00
this.boardViewModels.set(boardInfo.fileId, boardViewModel);
};
2024-07-23 02:40:11 +02:00
selectBoard = (boardViewModel: BoardViewModel): void => {
2024-07-23 13:51:27 +02:00
this.selectedBoardId.value = boardViewModel.boardInfo.fileId;
2024-07-23 02:50:22 +02:00
this.chatViewModel.displayedColor.value = boardViewModel.color.value;
2024-07-23 18:51:41 +02:00
this.storeLastUsedBoard();
2024-07-23 02:40:11 +02:00
};
closeBoard = (): void => {
2024-07-23 13:51:27 +02:00
this.selectedBoardId.value = undefined;
2024-07-23 02:50:22 +02:00
this.chatViewModel.resetColor();
2024-07-23 18:51:41 +02:00
this.storeLastUsedBoard();
2024-07-23 02:40:11 +02:00
};
2024-07-24 14:34:28 +02:00
updateBoardIndices = (): void => {
2024-07-23 14:27:38 +02:00
this.boardIndexManager.update([...this.boardViewModels.value.values()]);
for (const boardViewModel of this.boardViewModels.value.values()) {
boardViewModel.updateIndex();
}
};
2024-07-23 18:51:41 +02:00
// storage
storeLastUsedBoard = (): void => {
const path: string[] = this.getLastUsedBoardPath();
const lastUsedBoardId: string = this.selectedBoardId.value ?? "";
this.storageModel.write(path, lastUsedBoardId);
};
openLastUsedBoard = (): void => {
const path: string[] = this.getLastUsedBoardPath();
const lastUsedBoardId: string | null = this.storageModel.read(path);
if (lastUsedBoardId == null) return;
const boardViewModel: BoardViewModel | undefined =
this.boardViewModels.value.get(lastUsedBoardId);
if (boardViewModel == undefined) return;
this.selectBoard(boardViewModel);
};
2024-07-23 00:05:48 +02:00
// load
loadData = (): void => {
2024-07-23 00:57:40 +02:00
this.boardViewModels.clear();
2024-07-23 00:05:48 +02:00
2024-07-24 00:38:37 +02:00
const boardIds: string[] = this.boardModel.listBoardIds();
2024-07-23 00:05:48 +02:00
for (const boardId of boardIds) {
const boardInfo: BoardInfoFileContent | null =
2024-07-24 00:38:37 +02:00
this.boardModel.getBoardInfo(boardId);
if (boardInfo == null) continue;
2024-07-23 02:40:11 +02:00
this.showBoardInList(boardInfo);
2024-07-23 00:05:48 +02:00
}
2024-07-23 14:27:38 +02:00
2024-07-24 14:34:28 +02:00
this.updateBoardIndices();
2024-07-23 18:51:41 +02:00
this.openLastUsedBoard();
2024-07-22 20:02:13 +02:00
};
// init
2024-07-23 02:50:22 +02:00
constructor(
storageModel: StorageModel,
2024-07-24 01:19:44 +02:00
boardModel: BoardModel,
2024-07-23 02:50:22 +02:00
chatViewModel: ChatViewModel
) {
this.storageModel = storageModel;
2024-07-24 01:19:44 +02:00
this.boardModel = boardModel;
2024-07-23 02:50:22 +02:00
this.chatViewModel = chatViewModel;
2024-07-23 01:40:30 +02:00
// handlers
2024-07-24 00:38:37 +02:00
boardModel.boardHandlerManager.addHandler(
(boardInfoFileContent: BoardInfoFileContent) => {
2024-07-23 02:40:11 +02:00
this.showBoardInList(boardInfoFileContent);
2024-07-24 14:34:28 +02:00
this.updateBoardIndices();
}
);
2024-07-22 20:02:13 +02:00
}
}
2024-07-23 18:23:26 +02:00
export enum TaskPageViewModelSubPaths {
2024-07-23 21:19:56 +02:00
LastUsedBoard = "last-used-board",
2024-07-23 18:51:41 +02:00
}