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

308 lines
8.8 KiB
TypeScript
Raw Normal View History

2024-07-23 00:57:40 +02:00
import * as React from "bloatless-react";
import BoardsAndTasksModel, {
2024-07-24 00:38:37 +02:00
BoardInfoFileContent,
2024-07-24 01:19:44 +02:00
TaskFileContent,
} from "../../Model/Files/boardsAndTasksModel";
import ChatViewModel from "../Chat/chatViewModel";
2024-07-23 00:57:40 +02:00
import { Color } from "../../colors";
2024-07-25 16:02:49 +02:00
import CoreViewModel from "../Global/coreViewModel";
2024-07-27 03:41:47 +02:00
import SearchViewModel from "../Utility/searchViewModel";
import StorageModel from "../../Model/Global/storageModel";
2024-07-26 06:20:16 +02:00
import TaskContainingPageViewModel from "./taskContainingPageViewModel";
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
2024-07-26 06:20:16 +02:00
export default class BoardViewModel extends TaskContainingPageViewModel {
storageModel: StorageModel;
boardsAndTasksModel: BoardsAndTasksModel;
2024-07-28 17:15:43 +02:00
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 00:38:37 +02:00
// state
2024-07-23 00:57:40 +02:00
name: React.State<string> = new React.State("");
2024-07-26 03:14:55 +02:00
color: React.State<Color> = new React.State<any>(Color.Standard);
2024-07-23 00:57:40 +02:00
2024-07-24 13:31:28 +02:00
index: React.State<number> = new React.State(0);
2024-07-26 03:14:55 +02:00
selectedPage: React.State<BoardPageType> = new React.State<any>(
2024-07-23 15:41:03 +02:00
BoardPageType.List
);
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
2024-07-27 03:41:47 +02:00
searchViewModel: SearchViewModel<TaskViewModel>;
2024-07-25 15:31:27 +02:00
filteredTaskViewModels: React.ListState<TaskViewModel> =
new React.ListState();
// paths
getBasePath = (): string[] => {
return [...this.taskPageViewModel.getBoardViewPath(this.boardInfo.fileId)];
};
getLastUsedBoardPath = (): string[] => {
2024-07-25 16:44:08 +02:00
return [...this.getBasePath(), BoardViewModelSubPath.LastUsedView];
};
2024-07-27 03:41:47 +02:00
getPreviousSearchesPath = (): string[] => {
return [...this.getBasePath(), BoardViewModelSubPath.PreviousSearches];
};
2024-07-27 03:50:56 +02:00
getLastSearchPath = (): string[] => {
return [...this.getBasePath(), BoardViewModelSubPath.LastSearch];
};
2024-07-24 01:19:44 +02:00
// settings
saveSettings = (): void => {
const newBoardInfoFileContent: BoardInfoFileContent =
BoardsAndTasksModel.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);
2024-07-28 17:15:43 +02:00
this.chatViewModel.taskBoardSuggestions.remove(this.boardInfo.fileId);
2024-07-23 14:31:50 +02:00
this.close();
};
2024-07-24 14:34:28 +02:00
// methods
2024-07-26 06:56:34 +02:00
createTask = (): void => {
this.createTaskFromBoardId(this.boardInfo.fileId);
2024-07-27 03:41:47 +02:00
};
2024-07-26 06:56:34 +02:00
handleDropWithinBoard = (category?: string, status?: string): void => {
2024-07-25 16:20:43 +02:00
const draggedObject: any = this.coreViewModel.draggedObject.value;
if (draggedObject instanceof TaskViewModel == false) return;
draggedObject.setCategoryAndStatus(category, status);
};
handleDropBetweenBoards = (): void => {
const draggedObject: any = this.coreViewModel.draggedObject.value;
if (draggedObject instanceof TaskViewModel == false) return;
draggedObject.setBoardId(this.boardInfo.fileId);
};
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-27 03:50:56 +02:00
handleNewSearch = (searchTerm: string): void => {
const suggestionPath: string[] = [
...this.getPreviousSearchesPath(),
searchTerm,
];
this.storageModel.write(suggestionPath, "");
if (!this.coreViewModel.boardSearchSuggestions.value.has(searchTerm)) {
this.coreViewModel.boardSearchSuggestions.add(searchTerm);
2024-07-27 03:41:47 +02:00
}
2024-07-27 03:50:56 +02:00
const lastSearchPath: string[] = this.getLastSearchPath();
this.storageModel.write(lastSearchPath, searchTerm);
2024-07-27 03:41:47 +02:00
};
2024-07-24 13:31:28 +02:00
// view
2024-07-26 06:20:16 +02:00
showTask = (taskFileContent: TaskFileContent): void => {
if (taskFileContent.boardId != this.boardInfo.fileId) {
// remove task that was moved to different board
this.boardsAndTasksModel.deleteTaskReference(
this.boardInfo.fileId,
taskFileContent.fileId
);
2024-07-26 06:20:16 +02:00
this.removeTaskFromView(taskFileContent);
return;
}
2024-07-24 13:40:17 +02:00
const taskViewModel: TaskViewModel = new TaskViewModel(
2024-07-25 16:02:49 +02:00
this.coreViewModel,
this.chatViewModel,
this.boardsAndTasksModel,
2024-07-24 13:40:17 +02:00
this,
taskFileContent
);
this.taskViewModels.set(taskFileContent.fileId, taskViewModel);
};
2024-07-26 06:20:16 +02:00
removeTaskFromView = (taskFileContent: TaskFileContent): void => {
this.taskViewModels.remove(taskFileContent.fileId);
this.updateIndex();
};
2024-07-24 13:31:28 +02:00
select = (): void => {
this.taskPageViewModel.selectBoard(this);
};
close = (): void => {
this.taskPageViewModel.closeBoard();
2024-07-25 03:00:10 +02:00
this.taskViewModels.clear();
2024-07-24 13:31:28 +02:00
};
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;
};
updateIndex = (): void => {
const index: number =
this.taskPageViewModel.boardIndexManager.getIndex(this);
this.index.value = index;
};
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 => {
const taskIds: string[] = this.boardsAndTasksModel.listTaskIds(
2024-07-24 01:19:44 +02:00
this.boardInfo.fileId
);
for (const taskId of taskIds) {
2024-07-25 03:00:10 +02:00
if (this.taskViewModels.value.has(taskId)) return;
2024-07-25 15:31:27 +02:00
2024-07-24 01:19:44 +02:00
const taskFileContent: TaskFileContent | null =
this.boardsAndTasksModel.getLatestTaskFileContent(taskId);
2024-07-24 01:19:44 +02:00
if (taskFileContent == null) continue;
const taskViewModel: TaskViewModel = new TaskViewModel(
2024-07-25 16:02:49 +02:00
this.coreViewModel,
this.chatViewModel,
this.boardsAndTasksModel,
2024-07-24 01:19:44 +02:00
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
};
2024-07-27 03:41:47 +02:00
loadSearchSuggestions = (): void => {
const dirPath: string[] = this.getPreviousSearchesPath();
const searches: string[] = this.storageModel.list(dirPath);
this.coreViewModel.boardSearchSuggestions.add(...searches);
};
2024-07-27 03:50:56 +02:00
restoreSearch = (): void => {
2024-07-27 03:50:56 +02:00
const lastSearchPath: string[] = this.getLastSearchPath();
const lastSearch: string | null = this.storageModel.read(lastSearchPath);
if (lastSearch != null) {
2024-07-27 03:50:56 +02:00
this.searchViewModel.search(lastSearch);
}
2024-07-27 03:41:47 +02:00
};
loadData = (): void => {
this.restoreLastUsedView();
2024-07-24 01:19:44 +02:00
this.loadTasks();
2024-07-27 03:41:47 +02:00
this.loadSearchSuggestions();
};
2024-07-23 00:57:40 +02:00
// init
2024-07-23 02:40:11 +02:00
constructor(
2024-07-25 16:02:49 +02:00
public coreViewModel: CoreViewModel,
public chatViewModel: ChatViewModel,
2024-07-24 00:38:37 +02:00
storageModel: StorageModel,
2024-07-26 06:20:16 +02:00
boardsAndTasksModel: BoardsAndTasksModel,
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
) {
super(coreViewModel, chatViewModel, boardsAndTasksModel);
2024-07-26 06:20:16 +02:00
2024-07-24 13:40:17 +02:00
// set
this.storageModel = storageModel;
2024-07-26 06:20:16 +02:00
this.boardsAndTasksModel = boardsAndTasksModel;
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
2024-07-26 06:20:16 +02:00
boardsAndTasksModel.taskHandlerManager.addHandler(
2024-07-24 13:40:17 +02:00
(taskFileContent: TaskFileContent) => {
if (taskFileContent.boardId != this.boardInfo.fileId) return;
2024-07-26 06:20:16 +02:00
this.showTask(taskFileContent);
2024-07-24 14:34:28 +02:00
this.updateTaskIndices();
2024-07-24 13:40:17 +02:00
}
);
2024-07-27 03:41:47 +02:00
// search
this.searchViewModel = new SearchViewModel(
this.taskViewModels,
this.filteredTaskViewModels,
TaskViewModel.getStringsForFilter,
this.coreViewModel.boardSearchSuggestions
2024-07-27 03:41:47 +02:00
);
2024-07-27 03:50:56 +02:00
this.searchViewModel.appliedQuery.subscribeSilent((newQuery) => {
this.handleNewSearch(newQuery);
2024-07-27 03:41:47 +02:00
});
this.restoreSearch();
2024-07-23 00:57:40 +02:00
}
}
2024-07-23 15:41:03 +02:00
2024-07-25 16:44:08 +02:00
export enum BoardViewModelSubPath {
LastUsedView = "last-used-view",
2024-07-27 03:41:47 +02:00
PreviousSearches = "previous-searches",
2024-07-27 03:50:56 +02:00
LastSearch = "last-search",
}
2024-07-23 15:41:03 +02:00
// types
export enum BoardPageType {
List = "list",
Kanban = "kanban",
StatusGrid = "status-grid",
}