2024-07-22 19:54:39 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-25 17:59:11 +02:00
|
|
|
import BoardsAndTasksModel, {
|
|
|
|
|
BoardInfoFileContent,
|
|
|
|
|
} from "../../Model/Files/boardsAndTasksModel";
|
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-25 16:02:49 +02:00
|
|
|
import CoreViewModel from "../Global/coreViewModel";
|
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-25 19:32:46 +02:00
|
|
|
boardsAndTasksModel: BoardsAndTasksModel;
|
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-25 19:32:46 +02:00
|
|
|
return [...this.boardsAndTasksModel.getViewPath()];
|
2024-07-23 21:33:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getBoardViewPath = (boardId): string[] => {
|
|
|
|
|
return [...this.getBasePath(), boardId];
|
2024-07-23 18:51:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getLastUsedBoardPath = (): string[] => {
|
2024-07-25 16:44:08 +02:00
|
|
|
return [...this.getBasePath(), TaskPageViewModelSubPath.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-26 03:14:55 +02:00
|
|
|
selectedBoardId: React.State<string | undefined> = new React.State<any>(
|
|
|
|
|
undefined
|
|
|
|
|
);
|
2024-07-23 02:40:11 +02:00
|
|
|
|
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-25 19:32:46 +02:00
|
|
|
this.boardsAndTasksModel.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);
|
2024-07-25 19:32:46 +02:00
|
|
|
this.boardsAndTasksModel.updateBoardAndSend(boardInfoFileContent);
|
2024-07-24 14:34:28 +02:00
|
|
|
this.updateBoardIndices();
|
2024-07-23 00:05:48 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-23 13:19:08 +02:00
|
|
|
updateBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
|
2024-07-25 19:32:46 +02:00
|
|
|
this.boardsAndTasksModel.updateBoardAndSend(boardInfoFileContent);
|
2024-07-24 14:34:28 +02:00
|
|
|
this.updateBoardIndices();
|
2024-07-23 13:19:08 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-23 14:31:50 +02:00
|
|
|
deleteBoard = (boardInfoFileContent: BoardInfoFileContent): void => {
|
2024-07-25 19:32:46 +02:00
|
|
|
this.boardsAndTasksModel.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
|
|
|
};
|
|
|
|
|
|
2024-07-23 00:25:28 +02:00
|
|
|
// view
|
2024-07-23 02:40:11 +02:00
|
|
|
showBoardInList = (boardInfo: BoardInfoFileContent): void => {
|
2024-07-23 21:33:22 +02:00
|
|
|
const boardViewModel: BoardViewModel = new BoardViewModel(
|
2024-07-25 16:02:49 +02:00
|
|
|
this.coreViewModel,
|
2024-07-24 00:38:37 +02:00
|
|
|
this.storageModel,
|
2024-07-25 19:32:46 +02:00
|
|
|
this.boardsAndTasksModel,
|
2024-07-23 21:33:22 +02:00
|
|
|
this,
|
2024-07-24 00:38:37 +02:00
|
|
|
boardInfo
|
2024-07-23 21:33:22 +02:00
|
|
|
);
|
2024-07-23 01:40:30 +02:00
|
|
|
this.boardViewModels.set(boardInfo.fileId, boardViewModel);
|
2024-07-23 00:25:28 +02:00
|
|
|
};
|
|
|
|
|
|
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-25 19:32:46 +02:00
|
|
|
const boardIds: string[] = this.boardsAndTasksModel.listBoardIds();
|
2024-07-23 00:05:48 +02:00
|
|
|
for (const boardId of boardIds) {
|
|
|
|
|
const boardInfo: BoardInfoFileContent | null =
|
2024-07-25 19:32:46 +02:00
|
|
|
this.boardsAndTasksModel.getBoardInfo(boardId);
|
2024-07-23 00:25:28 +02:00
|
|
|
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(
|
2024-07-25 16:02:49 +02:00
|
|
|
public coreViewModel: CoreViewModel,
|
2024-07-23 02:50:22 +02:00
|
|
|
storageModel: StorageModel,
|
2024-07-24 17:28:49 +02:00
|
|
|
boardModel: BoardsAndTasksModel,
|
2024-07-23 02:50:22 +02:00
|
|
|
chatViewModel: ChatViewModel
|
|
|
|
|
) {
|
|
|
|
|
this.storageModel = storageModel;
|
2024-07-25 19:32:46 +02:00
|
|
|
this.boardsAndTasksModel = 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(
|
2024-07-23 01:58:34 +02:00
|
|
|
(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-23 01:58:34 +02:00
|
|
|
}
|
|
|
|
|
);
|
2024-07-22 20:02:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-23 18:23:26 +02:00
|
|
|
|
2024-07-25 16:44:08 +02:00
|
|
|
export enum TaskPageViewModelSubPath {
|
2024-07-23 21:19:56 +02:00
|
|
|
LastUsedBoard = "last-used-board",
|
2024-07-23 18:51:41 +02:00
|
|
|
}
|