import { Inject } from "@web-atoms/core/dist/di/Inject"; import { NavigationService } from "@web-atoms/core/dist/services/NavigationService"; import Action from "@web-atoms/core/dist/view-model/Action"; import { AtomWindowViewModel } from "@web-atoms/core/dist/view-model/AtomWindowViewModel"; import FileUploader from "../../controls/file-uploader/FileUploader"; import FileModel from "../../model/FileModel"; import FileUploadService from "../../services/FileUploadService"; import SourceService from "../../services/SourceService"; import NewItem from "../new-item/NewItem"; export default class FileCommandViewModel extends AtomWindowViewModel { public file: FileModel; @Inject public sourceService: SourceService; @Inject private navigationService: NavigationService; @Inject private fileUploadService: FileUploadService; @Action() public async deleteFile() { if (this.file.url === "src") { throw new Error("Cannot delete root folder"); } if (this.file.isReadOnly) { throw new Error("Cannot delete locked file/folder"); } if (!await this.navigationService.confirm("Are you sure you want to delete this?", "Confirm")) { this.close(); return; } await this.sourceService.deleteFile(this.file); this.close(); } @Action() public async renameFile() { try { const name = prompt("Enter Name of the new file", this.file.url); if (!name) { return; } if (name === this.file.name) { return; } await this.sourceService.renameFile(this.file, name); } finally { this.close(); } } public createNewFile(): void { this.app.runAsync(() => this.navigationService.openPage(NewItem, { "ref:file": this.file })); this.close(); } public createNewFolder(): void { const name = prompt("Name of folder", "folder1"); if (name) { const a = this.file.get(name); if (!a) { this.file.get(name, true, true); this.file.isOpen = true; } else { alert("File/Folder already exists"); } } // this.app.runAsync(() => this.navigationService.openPage(NewFolder, { // "ref:file": this.file // })); this.close(); } @Action() public async uploadFile() { const files = await this.fileUploadService.selectFile({ accept: "image/*", multiple: false}); this.app.runAsync(() => this.navigationService.openPage(FileUploader, { "ref:file": this.file, "ref:files": files })); this.close(); } }