2024-07-21 17:55:53 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-25 16:02:49 +02:00
|
|
|
import StorageModel, {
|
|
|
|
|
PATH_COMPONENT_SEPARATOR,
|
|
|
|
|
} 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-22 19:52:08 +02:00
|
|
|
import { translations } from "../../View/translations";
|
2024-07-21 20:21:32 +02:00
|
|
|
|
2024-07-21 17:55:53 +02:00
|
|
|
export default class StorageViewModel {
|
|
|
|
|
storageModel: StorageModel;
|
|
|
|
|
|
|
|
|
|
// state
|
|
|
|
|
isShowingStorageModal: React.State<boolean> = new React.State(false);
|
2024-07-21 19:44:10 +02:00
|
|
|
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
|
|
|
|
2024-07-21 20:21:32 +02:00
|
|
|
selectedFileName: React.State<string>;
|
|
|
|
|
selectedFileContent: React.State<string>;
|
|
|
|
|
|
|
|
|
|
lastDeletedItemPath: React.State<string> = new React.State("");
|
|
|
|
|
|
|
|
|
|
// methods
|
|
|
|
|
getSelectedItemContent = (): string => {
|
|
|
|
|
const path = StorageModel.stringToPathComponents(this.selectedPath.value);
|
2024-07-21 21:41:19 +02:00
|
|
|
const content = this.storageModel.read(path);
|
2024-07-21 20:21:32 +02:00
|
|
|
return (
|
|
|
|
|
(content ?? translations.storage.notAFile) ||
|
|
|
|
|
translations.storage.contentEmpty
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
deleteSelectedItem = (): void => {
|
|
|
|
|
const path = StorageModel.stringToPathComponents(this.selectedPath.value);
|
|
|
|
|
this.lastDeletedItemPath.value = this.selectedPath.value;
|
2024-07-25 16:02:49 +02:00
|
|
|
|
2024-07-22 01:38:08 +02:00
|
|
|
this.storageModel.removeRecursively(path);
|
2024-07-21 20:21:32 +02:00
|
|
|
this.didMakeChanges.value = true;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-22 23:33:57 +02:00
|
|
|
// view
|
2024-07-21 17:55:53 +02:00
|
|
|
showStorageModal = (): void => {
|
|
|
|
|
this.isShowingStorageModal.value = true;
|
2024-07-21 19:44:10 +02:00
|
|
|
};
|
2024-07-21 17:55:53 +02:00
|
|
|
|
|
|
|
|
hideStorageModal = (): void => {
|
2024-07-21 19:44:10 +02:00
|
|
|
if (this.didMakeChanges.value == true) {
|
|
|
|
|
window.location.reload();
|
2024-07-21 20:24:59 +02:00
|
|
|
return;
|
2024-07-21 19:44:10 +02:00
|
|
|
}
|
2024-07-21 17:55:53 +02:00
|
|
|
this.isShowingStorageModal.value = false;
|
2024-07-21 19:44:10 +02:00
|
|
|
};
|
2024-07-21 17:55:53 +02:00
|
|
|
|
|
|
|
|
// init
|
2024-07-25 16:02:49 +02:00
|
|
|
constructor(public coreViewModel: CoreViewModel, storageModel: StorageModel) {
|
2024-07-21 17:55:53 +02:00
|
|
|
this.storageModel = storageModel;
|
2024-07-21 20:21:32 +02: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
|
|
|
}
|
|
|
|
|
}
|