Files
CT/src/ViewModel/Global/coreViewModel.ts
T

146 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-07-25 16:02:49 +02:00
import * as React from "bloatless-react";
import SettingsModel from "../../Model/Global/settingsModel";
import { allTranslations, Translations } from "../../View/translations";
import StorageModel from "../../Model/Global/storageModel";
import ConnectionModel from "../../Model/Global/connectionModel";
import ChatListModel from "../../Model/Chat/chatListModel";
import FileTransferModel from "../../Model/Global/fileTransferModel";
2026-04-12 19:20:37 +00:00
import { v4 } from "uuid";
2024-07-25 16:02:49 +02:00
export default class CoreViewModel {
readonly BUILD = "Build 26.04.14.B";
translations: Translations;
2026-04-08 09:56:37 +00:00
2026-04-12 19:20:37 +00:00
// CONTEXT
private contextStack = new Map<string, Context>();
get contexts(): Context[] {
return [...this.contextStack.values()];
}
get context(): Context | undefined {
return this.contexts.pop();
}
set context(context: Context) {
if (this.contextStack.has(context.contextId)) return;
this.contextStack.set(context.contextId, context);
}
closeContext = (contextId: string): void => {
2026-04-13 11:23:17 +00:00
if (
!this.contexts
.map((context) => context.contextId)
.includes(contextId)
)
return;
2026-04-12 19:20:37 +00:00
while (this.contexts.length > 0) {
2026-04-13 11:23:17 +00:00
const currentContext: Context = this.context;
2026-04-12 19:20:37 +00:00
this.contextStack.delete(currentContext.contextId);
if (currentContext.contextId == contextId) break;
}
};
handleKeyDown = (e: KeyboardEvent): void => {
2026-04-13 11:23:17 +00:00
if (CoreViewModel.checkIsKeystroke(e) == false) return;
const contexts: Context[] = this.contexts;
2026-04-12 19:20:37 +00:00
while (contexts.length > 0) {
2026-04-13 11:23:17 +00:00
const currentContext: Context | undefined = contexts.pop();
2026-04-12 19:20:37 +00:00
if (!currentContext) contexts;
const isHandled: boolean = currentContext.handleKeystroke(e);
if (isHandled == true) break;
}
};
2026-04-01 08:29:39 +00:00
// DRAG & DROP
draggedObject: React.State<any> = new React.State<any>(undefined);
2026-04-01 08:29:39 +00:00
// SUGGESTIONS
// boards & tasks
2026-04-06 15:50:13 +00:00
boardFilterStringSuggestions: React.ListState<string> =
new React.ListState();
2026-04-01 08:29:39 +00:00
taskCategorySuggestions: React.ListState<string> = new React.ListState();
taskStatusSuggestions: React.ListState<string> = new React.ListState();
// init
constructor(
2026-04-13 10:58:21 +00:00
public readonly storageModel: StorageModel,
public readonly settingsModel: SettingsModel,
public readonly connectionModel: ConnectionModel,
public readonly chatListModel: ChatListModel,
public readonly fileTransferModel: FileTransferModel,
) {
this.translations =
allTranslations[settingsModel.language] || allTranslations.en;
2026-04-12 19:20:37 +00:00
document.body.addEventListener("keydown", this.handleKeyDown);
}
// util
static checkIsKeystroke(e: KeyboardEvent): boolean {
return (e.metaKey || e.altKey) && e.ctrlKey;
2026-04-12 19:20:37 +00:00
}
}
export class Context {
contextId = v4();
keystrokes = new Map<string, () => void>();
handleKeystroke = (e: KeyboardEvent): boolean => {
2026-04-13 11:23:17 +00:00
const fn: (() => void) | undefined = this.keystrokes.get(
e.key.toLowerCase(),
);
2026-04-12 19:20:37 +00:00
if (!fn) return false;
e.preventDefault();
fn();
return true;
};
close: () => void;
registerKeyStroke = (key: string, fn: () => void): void => {
this.keystrokes.set(key, fn);
};
2026-04-13 19:45:52 +00:00
constructor(public contextDebugDescription: string) {}
2026-04-12 19:20:37 +00:00
}
export class ContextHost<T> extends Context {
coreViewModel: CoreViewModel;
contexts = new Map<T, Context>();
currentContext = new React.State<Context | undefined>(undefined);
get isOpen(): boolean {
return false;
}
get contextSelection(): T | undefined {
return undefined;
}
2026-04-12 19:20:37 +00:00
registerContext = (key: T, context: Context): void => {
this.contexts.set(key, context);
};
closeContext = (): void => {
if (this.currentContext.value) {
2026-04-13 11:23:17 +00:00
this.coreViewModel.closeContext(
this.currentContext.value.contextId,
);
2026-04-12 19:20:37 +00:00
}
this.currentContext.value = undefined;
};
updateContexts = (): void => {
if (this.isOpen == false) return;
2026-04-13 11:23:17 +00:00
const selectedContext: Context | undefined = this.contexts.get(
this.contextSelection,
);
2026-04-12 19:20:37 +00:00
if (!selectedContext) return;
if (selectedContext != this.currentContext.value) {
this.closeContext();
2026-04-13 11:23:17 +00:00
}
2026-04-12 19:20:37 +00:00
this.coreViewModel.context = selectedContext;
this.currentContext.value = selectedContext;
};
2024-07-25 16:02:49 +02:00
}