personal-website/res/dev-fs/apps/Finder/finder.js

317 lines
11 KiB
JavaScript
Raw Normal View History

2023-03-20 11:20:37 +00:00
class Finder{
2023-05-15 23:04:16 +00:00
static AppId = "Finder"
2023-03-22 21:53:06 +00:00
/**
2023-04-29 21:01:23 +00:00
* @param {string[]} args
2023-03-22 21:53:06 +00:00
*/
2023-05-18 01:57:44 +00:00
async NewWindow(args, runContext){
2023-05-10 12:39:49 +00:00
let newFinder = new FinderWindow()
2023-05-18 01:57:44 +00:00
await newFinder.Init(args, runContext)
2023-04-29 13:58:39 +00:00
}
2023-05-15 23:04:16 +00:00
2023-05-10 12:39:49 +00:00
/**
2023-05-15 23:04:16 +00:00
* @param {string} path
* @returns {boolean}
2023-05-10 12:39:49 +00:00
*/
static async RenderProperites(path){
if (path == null || path ==""){
return
}
2023-05-10 12:39:49 +00:00
const params = new URLSearchParams({
path: path
})
const response = await fetch(`/app/${Finder.AppId}/renderProps?` + params)
2023-05-10 12:39:49 +00:00
if (response.status != 200){
WebDesktopEnvironment.Alert("Error in properties render") //TODO
return false
}
const html = await response.text()
2023-05-15 23:04:16 +00:00
let newWindow = WebDesktopEnvironment.CreateNewWindow(Finder.AppId, 350, 500 )
2023-05-10 12:39:49 +00:00
newWindow.innerHTML = html
newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) {
WebDesktopEnvironment.CloseWindow(newWindow)
})
}
2023-05-10 12:39:49 +00:00
}
class FinderWindow{
2023-05-15 23:04:16 +00:00
curPath = ""
2023-05-10 12:39:49 +00:00
fileView = undefined
windowElem = undefined
2023-05-15 23:04:16 +00:00
2023-05-28 01:28:48 +00:00
async Init(args, appContext){
if (args[1] == "--desktop"){
let desktopNode = document.body.querySelector(`#${args[2]}`)
if (desktopNode == null){
WebDesktopEnvironment.Alert("Desktop node not found")
return
}
2023-05-10 23:45:38 +00:00
const params = new URLSearchParams({
path: args[0]
})
2023-05-18 01:57:44 +00:00
const response = await fetch(`/app/${Finder.AppId}/renderDesktop?` + params,
{
method: "POST",
2023-05-28 01:28:48 +00:00
body: JSON.stringify(appContext)
2023-05-18 01:57:44 +00:00
})
2023-05-10 23:45:38 +00:00
if (response.status != 200){
2023-05-28 01:28:48 +00:00
console.log(response.status)
2023-05-10 23:45:38 +00:00
WebDesktopEnvironment.Alert("Error in render desktop") //TODO
2023-05-28 01:28:48 +00:00
return
2023-05-10 23:45:38 +00:00
}
const html = await response.text()
2023-05-28 01:28:48 +00:00
desktopNode.innerHTML = html
2023-05-10 23:45:38 +00:00
this.fileView = new FileView(
2023-05-28 01:28:48 +00:00
desktopNode.querySelector(".FileTileView"), (event) =>{this.Click(event)},
(event) => { this.RightClick(event) },
(event, draggedElem) => { this.DropEvent(event, draggedElem)},
() => { this.ReRenderDir() }
2023-05-15 23:04:16 +00:00
)
this.RenderDir(args[0])
return
2023-05-18 01:57:44 +00:00
}
const params = new URLSearchParams({isMobile: WebDesktopEnvironment.isMobile})
const response = await fetch(`/app/${Finder.AppId}/render?` + params,
{
method: "POST",
2023-05-28 01:28:48 +00:00
body: JSON.stringify(appContext)
2023-05-18 01:57:44 +00:00
})
2023-05-10 12:39:49 +00:00
if (response.status != 200){
2023-05-18 01:57:44 +00:00
const error = await response.json()
WebDesktopEnvironment.Alert(error.message)
2023-05-10 12:39:49 +00:00
return
}
const html = await response.text()
2023-05-15 23:04:16 +00:00
let newWindow = WebDesktopEnvironment.CreateNewWindow(Finder.AppId, 500, 350 )
2023-05-10 12:39:49 +00:00
newWindow.innerHTML = html
2023-05-15 23:04:16 +00:00
2023-05-10 22:56:28 +00:00
this.fileView = new FileView(
2023-05-15 23:04:16 +00:00
newWindow.querySelector(".FileTileView"),
(event) => { this.Click(event) },
(event) => { this.RightClick(event) },
(event, draggedElem) => { this.DropEvent(event, draggedElem)},
() => { this.ReRenderDir() }
)
2023-05-10 12:39:49 +00:00
2023-05-15 23:04:16 +00:00
newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) {
WebDesktopEnvironment.CloseWindow(newWindow)
})
2023-05-10 12:39:49 +00:00
2023-05-15 23:04:16 +00:00
newWindow.querySelector("#RootButton").addEventListener('click', () =>{
this.RenderDir('/')
})
newWindow.querySelector("#HomeButton").addEventListener('click', () =>{
this.RenderDir('/home/user')
})
let scrollBar = new WdeScrollBar(newWindow.querySelector(".ScrollbarPlace"), newWindow.querySelector(".FileTileView"))
this.windowElem = newWindow
this.RenderDir(args[0])
}
/**
* @param {string} path
*/
2023-05-10 12:39:49 +00:00
RenderDir(path){
2023-05-15 23:04:16 +00:00
this.curPath = path
2023-05-10 12:39:49 +00:00
this.fileView.OpenFolder(path)
}
2023-05-15 23:04:16 +00:00
2023-05-10 12:39:49 +00:00
ReRenderDir(){
2023-05-15 23:04:16 +00:00
this.RenderDir(this.curPath)
2023-05-10 12:39:49 +00:00
}
2023-05-15 23:04:16 +00:00
2023-05-10 22:56:28 +00:00
/**
2023-05-15 23:04:16 +00:00
* @param {DragEvent} event
* @param {HTMLElement} draggedElem
2023-05-10 22:56:28 +00:00
*/
async DropEvent(event){
// console.log(event.dataTransfer.getData("dropType"))
if (event.dataTransfer.getData("dropType") == "move"){
const sourcePath= event.dataTransfer.getData("filePath")
2023-05-15 23:04:16 +00:00
const targetPath = this.curPath + "/" + event.dataTransfer.getData("fileName")
2023-05-10 22:56:28 +00:00
const res = await WebFS.MoveFile(sourcePath, targetPath)
if (res){
this.ReRenderDir()
} else {
WebDesktopEnvironment.Alert("UWAGA TODO MOVE FILE ERROR") //TODO
}
} else {
2023-05-17 13:57:55 +00:00
console.log(event, this.curPath)
2023-05-10 22:56:28 +00:00
let files = event.dataTransfer.files
for (let i = 0; i < files.length; i++) {
2023-05-17 13:09:22 +00:00
const file = files[i];
2023-05-17 13:57:55 +00:00
console.log("file:" + file.name)
2023-05-17 13:09:22 +00:00
const res = await WebFS.UploadFile(file, this.curPath)
if (res){
this.ReRenderDir()
}
2023-05-10 22:56:28 +00:00
}
2023-05-17 13:09:22 +00:00
return
2023-05-10 22:56:28 +00:00
const params = new URLSearchParams({
2023-05-15 23:04:16 +00:00
parentPath: this.curPath,
2023-05-10 22:56:28 +00:00
})
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
*/
2023-05-04 22:11:11 +00:00
Click(event){
2023-05-16 12:12:55 +00:00
this.OpenFile(this.curPath, event.target.getAttribute("name"), event.target.getAttribute("filetype"))
}
/**
* @param {string} filePath
*/
async OpenFile(parentPath, fileName, fileType){
2023-05-17 13:09:22 +00:00
// console.log(parentPath, fileName, fileType)
2023-05-16 12:12:55 +00:00
// const splittedPath = filePath.split("/")
// const fileName = splittedPath[splittedPath.length - 1]
2023-05-09 03:17:34 +00:00
const fileExtension = fileName.split(".")[fileName.split(".").length - 1] //FIXME
2023-05-16 12:12:55 +00:00
switch (true) {
case fileType == "objectlink":
WebDesktopEnvironment.Alert("Links not supported yet")
2023-04-29 13:58:39 +00:00
break
2023-05-16 12:12:55 +00:00
case fileType == "pathlink":
let res = await WebFS.ReadPathLink(`${parentPath}/${fileName}`)
console.log(res)
this.OpenFile(res.parentPath, res.name, res.filetype)
break
2023-05-15 23:04:16 +00:00
case fileExtension == "app":
2023-05-16 12:12:55 +00:00
WebDesktopEnvironment.Open(`${parentPath}/${fileName}`, [])
2023-05-15 23:04:16 +00:00
break
2023-05-18 01:57:44 +00:00
case fileExtension == "blog":
WebDesktopEnvironment.Open(`/Applications/BlogViewer.app`, [`${parentPath}/${fileName}`])
break
case fileType == "directory":
2023-05-16 12:12:55 +00:00
WebDesktopEnvironment.Open(`/Applications/Finder.app`, [`${parentPath}/${fileName}`])
break
case fileExtension == "blog":
2023-05-16 12:12:55 +00:00
WebDesktopEnvironment.Open("/Applications/BlogViewer.app", [`${parentPath}/${fileName}`])
2023-04-29 13:58:39 +00:00
break
case fileExtension == "jpeg" | fileExtension == "png":
2023-05-16 12:12:55 +00:00
WebDesktopEnvironment.Open("img-viewer", [`${parentPath}/${fileName}`])
2023-04-29 16:47:12 +00:00
break;
2023-04-12 21:05:23 +00:00
default:
2023-05-02 11:32:56 +00:00
WebDesktopEnvironment.Alert("Unsupported file type")
2023-04-12 17:04:25 +00:00
break;
}
2023-03-17 01:16:51 +00:00
}
2023-05-04 00:38:24 +00:00
2023-05-10 12:39:49 +00:00
/**
* @param {MouseEvent} event
*/
RightClick(event){
this.CreateContextMenu(event.target, [event.clientY, event.clientX])
}
/**
* @param {HTMLElement} target
* @param {string[]} pos
*/
async CreateContextMenu(target, pos){
2023-05-04 19:49:22 +00:00
let context = ""
const fileName = target.getAttribute("name") //TODO check for null
2023-05-09 03:17:34 +00:00
const fileType = target.getAttribute("fileType")
if (target.classList.contains("FileTileView"))
{
context = "FileTileView"
2023-05-09 03:17:34 +00:00
} else {
context = fileType
}
let path = ""
if (fileName === null){
path = this.curPath
} else {
path = `${this.curPath}/${fileName}`
}
const params = new URLSearchParams({context: context, path: path})
2023-05-15 23:04:16 +00:00
const response = await fetch(`/app/${Finder.AppId}/contextMenu?` + params)
2023-05-10 12:39:49 +00:00
if (response.status != 200){
WebDesktopEnvironment.Alert("ERROR in Context menu TODO"); //TODO
return
}
const html = await response.text()
let overlay = document.createElement("div") //TODO Move to WDE.CreateOverlay()
overlay.setAttribute('id', 'finder-context-menu-overlay')
overlay.style.position = 'absolute'
overlay.style.width = "100%"
overlay.style.height = "100%"
let menu = document.createElement("div")
menu.setAttribute('class', 'ContextMenu WindowFrameShadow')
menu.style.position = 'absolute';
menu.style.top = pos[0] + "px";
menu.style.left = pos[1] + "px";
menu.innerHTML = html
2023-05-18 01:57:44 +00:00
// menu.children[0].firstElementChild.remove()
2023-05-10 12:39:49 +00:00
menu.children[0].lastElementChild.remove() //FIXME Can't ommit rendering of horLine in end of menu on backend
overlay.appendChild(menu)
document.body.appendChild(overlay)
overlay.addEventListener('click', async (event) => {
if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks??
let res = false
switch (event.target.children[0].getAttribute("action")) {
2023-05-16 12:12:55 +00:00
case "createPathLink":
2023-05-17 13:09:22 +00:00
res = await WebFS.CreatePathLink(`${this.curPath}/${fileName}`, `${this.curPath}/Link to ${fileName}` )
2023-05-16 12:12:55 +00:00
if (res){
this.ReRenderDir()
}
break
2023-05-10 12:39:49 +00:00
case "createDir":
2023-05-15 23:04:16 +00:00
res = await WebFS.CreateDirectory(`${this.curPath}`)
2023-05-10 12:39:49 +00:00
console.log(res)
if (res){
this.ReRenderDir()
}
break
case "deleteFile":
2023-05-15 23:04:16 +00:00
res = await WebFS.DeleteFile(`${this.curPath}/${fileName}`)
2023-05-10 12:39:49 +00:00
console.log(res)
if (res){
this.ReRenderDir()
}
break
case "getInfo":
Finder.RenderProperites(path)
break
case "openAsDir":
WebDesktopEnvironment.Open(`/Applications/${Finder.AppId}.app`,[`${this.curPath}/${fileName}`])
2023-05-10 12:39:49 +00:00
break
default:
break;
}
}
overlay.remove()
})
overlay.addEventListener('contextmenu', (event) => {
event.preventDefault();
overlay.remove()
2023-05-04 00:38:24 +00:00
})
}
2023-05-10 12:39:49 +00:00
}