personal-website/resources/sys/finder/finder.js

148 lines
4.7 KiB
JavaScript

class Finder{
appId = "finder"
fileView = undefined
path = "/"
homePath = "/home/user"
// previousPath = "/"
pathHistory = [] //FIXME Fixed length
constructor(){
// this.appElem = appElem
}
/**
* @param {string[]} args
*/
NewWindow(args){
this.path = args[0]
if (args[1] == "desktop"){
this.NewDesktop(args)
return
}
fetch(`${window.location.origin}/application/${this.appId}/render?` + new URLSearchParams({
isMobile: WebDesktopEnvironment.isMobile,
// path: this.path,
// bar: true,
})) //TODO Move to wde func. Or Not?
.then((response) => response.text())
.then((html) => {
let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, 500, 350 )
newWindow.innerHTML = html
this.fileView = new FileView(newWindow.querySelector(".FileTileView"), (event) =>{
this.Click(event, false)
})
this.OpenDir(this.path)
newWindow.querySelector("#BackButton").addEventListener('click', () =>{
this.OpenPreviousDir()
})
newWindow.querySelector("#HomeButton").addEventListener('click', () =>{
this.OpenDir(this.homePath)
})
if (!WebDesktopEnvironment.isMobile){
// let scrollBar = new WdeScrollBar(newWindow.children[1].children[1], newWindow.children[1].children[0])// TODO to querry selector
// console.log(newWindow.querySelector("#closeWindowButton"))
newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) {
WebDesktopEnvironment.CloseWindow(newWindow)
})
}
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
}
/**
* @param {string[]} args
*/
NewDesktop(args){
fetch(`${window.location.origin}/application/${this.appId}/renderDesktop?` + new URLSearchParams({
isMobile: WebDesktopEnvironment.isMobile,
path: args[0]
}))
.then((response) => response.text())
.then((html) => {
// console.log(args)
args[2].innerHTML = html
this.fileView = new FileView(args[2].querySelector(".FileTileView"), (event) =>{
this.Click(event, true)
})
this.OpenDir(this.path)
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
}
OpenPreviousDir(){
if (this.pathHistory.length > 0){
// console.log(this.pathHistory)
let path = this.pathHistory[this.pathHistory.length - 1]
// console.log(typeof( this.pathHistory))
this.pathHistory.pop()
this.OpenDir(this.path)
}
}
/**
* @param {string} path
*/
OpenDir(path){
this.pathHistory += this.path
this.path = path
this.fileView.OpenFolder(this.path)
}
/**
* @param {MouseEvent} event
* @param {boolean} inNewWindow
*/
Click(event, inNewWindow){
let fileType = event.target.getAttribute("fileType")
let fileName = event.target.getAttribute("name")
switch (fileType) {
case "directory":
if (inNewWindow){
WebDesktopEnvironment.Open("finder", ["/home/user/" + fileName]) //FIXME this.path is shared for all windows
break
}
this.OpenDir(this.path +"/" + fileName)
break
case "blog-page":
WebDesktopEnvironment.Open("blog-viewer", [this.path + "/" + fileName])
break
// case "app":
// //TODO get real id
// WebDesktopEnvironment.Open("personal-properties", [])
// break;
case "base64img":
WebDesktopEnvironment.Open("img-viewer", [this.path + "/" + fileName])
break;
default:
console.log("Unsupported file type")
break;
}
}
// /**
// * @param {path} string
// */
// renderFileView(path){
// fetch(`${window.location.origin}/fs/list?` + new URLSearchParams({
// path: path,
// }))
// .then((response) => response.text())
// .then((html) => {
// this.fileView.innerHTML = html
// })
// .catch((error) => {
// WebDesktopEnvironment.Alert(error);
// })
// }
}