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

74 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-07-21 17:55:53 +02:00
import * as React from "bloatless-react";
2024-07-25 16:02:49 +02:00
import StorageModel, {
2026-04-01 08:29:39 +00:00
PATH_COMPONENT_SEPARATOR,
2024-07-25 16:02:49 +02:00
} from "../../Model/Global/storageModel";
2024-07-21 17:55:53 +02:00
2024-07-25 16:02:49 +02:00
import CoreViewModel from "./coreViewModel";
2024-07-21 17:55:53 +02:00
export default class StorageViewModel {
2026-04-01 08:29:39 +00:00
// state
isShowingStorageModal: React.State<boolean> = new React.State(false);
selectedPath: React.State<string> = new React.State(
PATH_COMPONENT_SEPARATOR,
);
didMakeChanges: React.State<boolean> = new React.State(false);
2024-07-21 17:55:53 +02:00
2026-04-01 08:29:39 +00:00
selectedFileName: React.State<string>;
selectedFileContent: React.State<string>;
2026-04-01 08:29:39 +00:00
lastDeletedItemPath: React.State<string> = new React.State("");
2026-04-01 08:29:39 +00:00
// methods
getSelectedItemContent = (): string => {
const path = StorageModel.stringToPathComponents(
this.selectedPath.value,
);
const content = this.coreViewModel.storageModel.read(path);
2026-04-01 08:29:39 +00:00
return (
(content ?? this.coreViewModel.translations.storage.notAFile) ||
this.coreViewModel.translations.storage.contentEmpty
2026-04-01 08:29:39 +00:00
);
};
2026-04-01 08:29:39 +00:00
deleteSelectedItem = (): void => {
const path = StorageModel.stringToPathComponents(
this.selectedPath.value,
);
this.lastDeletedItemPath.value = this.selectedPath.value;
2024-07-25 16:02:49 +02:00
this.coreViewModel.storageModel.removeRecursively(path);
2026-04-01 08:29:39 +00:00
this.didMakeChanges.value = true;
};
2026-04-01 08:29:39 +00:00
removeJunk = (): void => {
this.coreViewModel.storageModel.removeJunk();
2026-04-01 08:29:39 +00:00
this.selectedPath.value = PATH_COMPONENT_SEPARATOR;
};
2024-07-28 16:08:29 +02:00
2026-04-01 08:29:39 +00:00
// view
showStorageModal = (): void => {
this.isShowingStorageModal.value = true;
};
2024-07-21 17:55:53 +02:00
2026-04-01 08:29:39 +00:00
hideStorageModal = (): void => {
if (this.didMakeChanges.value == true) {
window.location.reload();
return;
}
this.isShowingStorageModal.value = false;
};
2024-07-21 17:55:53 +02:00
2026-04-01 08:29:39 +00:00
// init
constructor(public coreViewModel: CoreViewModel) {
2026-04-01 08:29:39 +00:00
this.selectedFileName = React.createProxyState(
[this.selectedPath],
() => StorageModel.getFileNameFromString(this.selectedPath.value),
);
this.selectedFileContent = React.createProxyState(
[this.selectedPath],
() => this.getSelectedItemContent(),
);
}
2024-07-21 17:55:53 +02:00
}