From 4426d30342f93965a3d035cf1016c32caee23b6f Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Thu, 11 May 2023 01:56:28 +0300 Subject: [PATCH] Finder refactoring --- libs/cat.go | 1 + resources/sys/finder/finder.js | 156 +++++++++++---------------------- resources/sys/libs/fs.js | 0 resources/sys/wde/file-view.js | 11 ++- resources/wde.js | 60 +++++++++++++ templates/base/index.tmpl | 1 + 6 files changed, 122 insertions(+), 107 deletions(-) create mode 100644 resources/sys/libs/fs.js diff --git a/libs/cat.go b/libs/cat.go index 5c0a64c..703ef60 100644 --- a/libs/cat.go +++ b/libs/cat.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" ) +//TODO Use this to get json from back type Cat struct { fs *webfilesystem.WebFileSystem } diff --git a/resources/sys/finder/finder.js b/resources/sys/finder/finder.js index 82906f3..f386ab0 100644 --- a/resources/sys/finder/finder.js +++ b/resources/sys/finder/finder.js @@ -63,42 +63,7 @@ class Finder{ }) } - /** - * @param {DragEvent} event - * @param {HTMLElement} draggedElem - */ - async DropEvent(event){ - // console.log(event.dataTransfer.getData("dropType")) - if (event.dataTransfer.getData("dropType") == "move"){ - let params = new URLSearchParams({ - sourcePath: event.dataTransfer.getData("filePath"), - targetPath: this.path + "/" + event.dataTransfer.getData("fileName"), - }) - let response = await fetch('/fs/move/?' + params) - console.log(response.status) - } else { - let formData = new FormData() - console.log(this.path) - for (let i = 0; i < event.length; i++) { - const element = event[i]; - formData.append("file", element.getAsFile()) - // console.log(formData) - } - // console.log(formData) - // formData.append("photo", photo); - fetch('/fs/upload/?' + new URLSearchParams({ - parentPath: this.path, - }), - { - method: "POST", - body: formData - }) - .catch((error) => { - WebDesktopEnvironment.Alert(error); - }) - } - } - + /** * @param {string[]} args */ @@ -166,46 +131,6 @@ class Finder{ WebDesktopEnvironment.CloseWindow(newWindow) }) } - - /** - * @param {string} path - * @returns {boolean} - */ - static async DeleteFile(path){ - const params = new URLSearchParams({ - path: path - }) - const response = await fetch(`/fs/delete?` + params) - if (response.status != 200){ - WebDesktopEnvironment.Alert("DELETE ERROR") //TODO - return false - } - return true - } - - /** - * @param {string} path - * @returns {boolean} - */ - static async CreateDirectory(path){ - if (path == undefined){ - WebDesktopEnvironment.Alert("Path is undefined") - return false - } - const params = new URLSearchParams({ - path: `${path}/New Directory` - }) - const response = await fetch(`/fs/createDir?` + params) - if (response.status != 200){ - WebDesktopEnvironment.Alert("DIRCTORY CREATION ERROR") //TODO - return false - } - return true - } - - - - } @@ -226,11 +151,14 @@ class FinderWindow{ let newWindow = WebDesktopEnvironment.CreateNewWindow(Finder.appId, 500, 350 ) newWindow.innerHTML = html - this.fileView = new FileView(newWindow.querySelector(".FileTileView"), - (event) =>{ this.Click(event) }, - (event) =>{ this.RightClick(event) }, - (event, draggedElem) =>{ this.DropEvent(event, draggedElem) - }) + // console.log(this.ReRenderDir) + this.fileView = new FileView( + newWindow.querySelector(".FileTileView"), + (event) => { this.Click(event) }, + (event) => { this.RightClick(event) }, + (event, draggedElem) => { this.DropEvent(event, draggedElem)}, + () => { this.ReRenderDir() } + ) newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) { @@ -262,6 +190,48 @@ class FinderWindow{ this.RenderDir(this.CurPath) } + /** + * @param {DragEvent} event + * @param {HTMLElement} draggedElem + */ + async DropEvent(event){ + // console.log(event.dataTransfer.getData("dropType")) + if (event.dataTransfer.getData("dropType") == "move"){ + const sourcePath= event.dataTransfer.getData("filePath") + const targetPath = this.CurPath + "/" + event.dataTransfer.getData("fileName") + const res = await WebFS.MoveFile(sourcePath, targetPath) + if (res){ + this.ReRenderDir() + } else { + WebDesktopEnvironment.Alert("UWAGA TODO MOVE FILE ERROR") //TODO + } + } else { + WebDesktopEnvironment.Alert("Not fixed yet") + return + let formData = new FormData() + let files = event.dataTransfer.files + for (let i = 0; i < files.length; i++) { + const element = files[i]; + console.log(element) + formData.append("file", element) //FIXME Conn reset + } + const params = new URLSearchParams({ + parentPath: this.CurPath, + }) + const response = await fetch('/fs/upload/?' + params, + { + method: "POST", //TODO Change to PUT? + body: formData + }) + if (response.status != 200){ + WebDesktopEnvironment.Alert("ERROR IN UPLOADING FILE")//TODO + } else { + this.ReRenderDir() + } + + } + } + /** * @param {MouseEvent} event @@ -351,14 +321,14 @@ class FinderWindow{ let res = false switch (event.target.children[0].getAttribute("action")) { case "createDir": - res = await Finder.CreateDirectory(`${this.CurPath}`) + res = await WebFS.CreateDirectory(`${this.CurPath}`) console.log(res) if (res){ this.ReRenderDir() } break case "deleteFile": - res = await Finder.DeleteFile(`${this.CurPath}/${fileName}`) + res = await WebFS.DeleteFile(`${this.CurPath}/${fileName}`) console.log(res) if (res){ this.ReRenderDir() @@ -366,26 +336,6 @@ class FinderWindow{ break case "getInfo": res = await Finder.RenderProperites(`${this.CurPath}/${fileName}`) - // fetch(`/application/${Finder.appId}/renderProps?` + new URLSearchParams({ - // path: `${this.CurPath}/${fileName}` - // })) - // .then((response) => { - // console.log(response) - // if (response.status == 200){ - // console.log("Success") - // return response.text(); - // } - // }) - // .then((html) =>{ - // let newWindow = WebDesktopEnvironment.CreateNewWindow(Finder.appId, 350, 500 ) - // newWindow.innerHTML = html - // newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) { - // WebDesktopEnvironment.CloseWindow(newWindow) - // }) - // }) - // .catch((error) => { - // WebDesktopEnvironment.Alert(error); - // }) break case "openAsDir": WebDesktopEnvironment.Open(`/Applications/${Finder.appId}.app`,[`${this.CurPath}/${fileName}`]) diff --git a/resources/sys/libs/fs.js b/resources/sys/libs/fs.js new file mode 100644 index 0000000..e69de29 diff --git a/resources/sys/wde/file-view.js b/resources/sys/wde/file-view.js index 57bc881..4016e19 100644 --- a/resources/sys/wde/file-view.js +++ b/resources/sys/wde/file-view.js @@ -6,8 +6,9 @@ class FileView{ * @param {HTMLElement} fileViewElem * @param {Function} doubleClickCallback * @param {Function} rightClickCallback + * @param {Function} updateFileViewCallback */ - constructor(fileViewElem, doubleClickCallback, rightClickCallback, fileUploadCallback){ + constructor(fileViewElem, doubleClickCallback, rightClickCallback, fileUploadCallback, updateFileViewCallback){ //TODO check all params this.parentElem = fileViewElem @@ -46,6 +47,8 @@ class FileView{ event.dataTransfer.setData("fileName", event.target.getAttribute("name")) event.dataTransfer.setData("filePath", this.path + "/" + event.target.getAttribute("name")) event.dataTransfer.setData("dropType", "move") + // console.log(updateFileViewCallback) + // event.dataTransfer.setData("updateCallback", updateFileViewCallback) // event.dataTransfer.setData("fileName", ) // console.log(draggedElem) }) @@ -54,8 +57,6 @@ class FileView{ event.preventDefault(); counter++ fileViewElem.classList.add("DragDropBorder") - // draggedElem = event.relatedTarget - }) fileViewElem.addEventListener('dragend', function(event) { @@ -65,6 +66,7 @@ class FileView{ if (counter === 0){ fileViewElem.classList.remove("DragDropBorder") } + // updateFileViewCallback() // draggedElem = undefined }) @@ -84,9 +86,10 @@ class FileView{ fileViewElem.addEventListener("drop", (event) => { event.preventDefault(); - // console.log(event.dataTransfer.getData("dropType")) fileUploadCallback(event) fileViewElem.classList.remove("DragDropBorder") + // updateFileViewCallback() + // this.OpenFolder(this.path) // draggedElem = undefined }) } diff --git a/resources/wde.js b/resources/wde.js index c0afb22..a2cc780 100644 --- a/resources/wde.js +++ b/resources/wde.js @@ -24,6 +24,7 @@ class WebDesktopEnvironment{ document.body.style.setProperty('--zoom', 1) this.wc = new WindowsCompositor() this.loadWDE() + let kek = new WebFS() return // let applications = document.createElement("div") @@ -343,3 +344,62 @@ class WindowsCompositor{ } + + +class WebFS{ + constructor(){ + console.log("fs loaded") + } + /** + * @param {string} path + * @returns {boolean} + */ + static async CreateDirectory(path){ + if (path == undefined){ + WebDesktopEnvironment.Alert("Path is undefined") + return false + } + const params = new URLSearchParams({ + path: `${path}/New Directory` + }) + const response = await fetch(`/fs/createDir?` + params) + if (response.status != 200){ + WebDesktopEnvironment.Alert("DIRCTORY CREATION ERROR") //TODO + return false + } + return true + } + + /** + * @param {string} path + * @returns {boolean} + */ + static async DeleteFile(path){ + const params = new URLSearchParams({ + path: path + }) + const response = await fetch(`/fs/delete?` + params) + if (response.status != 200){ + WebDesktopEnvironment.Alert("DELETE ERROR") //TODO + return false + } + return true + } + + /** + * @param {string} path + * @returns {boolean} + */ + static async MoveFile(sourcePath, targetPath){ + const params = new URLSearchParams({ + sourcePath: sourcePath, + targetPath: targetPath + }) + const response = await fetch(`/fs/move?` + params) + if (response.status != 200){ + // WebDesktopEnvironment.Alert("Move ERROR") //TODO + return false + } + return true + } +} \ No newline at end of file diff --git a/templates/base/index.tmpl b/templates/base/index.tmpl index d6b87ef..31370b6 100644 --- a/templates/base/index.tmpl +++ b/templates/base/index.tmpl @@ -8,6 +8,7 @@ +