New applications load method

This commit is contained in:
cyber-dream 2023-05-09 06:17:34 +03:00
parent ade7b9b021
commit 46ad190b2a
9 changed files with 165 additions and 104 deletions

View File

@ -32,26 +32,27 @@ func (f *FinderApplication) Render(isMobile bool) gin.H {
return gin.H{}
}
func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H {
func (f *FinderApplication) RenderContextMenu(context string, filePath string, data string) gin.H {
islands := [][]wde.ContexMenuRow{}
islands = append(islands, []wde.ContexMenuRow{})
islands = append(islands, []wde.ContexMenuRow{
{Label: "Get Info", Action: "getInfo"},
{Label: "New Directory", Action: "createDir"},
})
if context == "FileTileView" {
islands = append(islands, []wde.ContexMenuRow{
{Label: "Get Info", Action: "getInfo"},
{Label: "New Directory", Action: "createDir"},
})
return gin.H{
"Islands": islands,
}
}
islands = append(islands, []wde.ContexMenuRow{
{Label: "Delete File", Action: "deleteFile"},
// {Label: "Get Info", Action: "getInfo"},
})
switch context {
case "directory":
if f.fs.GetExtension(filePath) == "app" {
islands = append(islands, []wde.ContexMenuRow{
{Label: "Open as Directory", Action: "openAsDir"},
})
}
default:
islands = append(islands, []wde.ContexMenuRow{
{Label: "temp Menu 1", Action: ""},
@ -59,6 +60,10 @@ func (f *FinderApplication) RenderContextMenu(context string, data string) gin.H
})
}
islands = append(islands, []wde.ContexMenuRow{
{Label: "Delete File", Action: "deleteFile"},
})
return gin.H{
"Islands": islands,
}

View File

@ -36,8 +36,17 @@ func (f *FinderApplication) Routes(routes *gin.RouterGroup) {
routes.GET("contextMenu", func(ctx *gin.Context) {
context := ctx.Query("context")
// if context == "" {
// ctx.Status(http.StatusBadRequest)
// return
// }
filePath := ctx.Query("path")
if filePath == "" {
ctx.Status(http.StatusBadRequest)
return
}
data := ctx.Query("data")
ginH := f.RenderContextMenu(context, data)
ginH := f.RenderContextMenu(context, filePath, data)
ctx.HTML(http.StatusOK, "wde-widgets/context-menu.tmpl", ginH)
})

View File

@ -18,7 +18,7 @@ type WebDEApplication interface {
}
type ApplicationManifest struct {
AppId string `bson:"appid" json:"appid"`
AppId string `bson:"appid" json:"appId"`
Js []string `bson:"js" json:"js"`
Css []string `bson:"css" json:"css"`
}
@ -50,6 +50,7 @@ func (as *ApplicationsStorage) createApp(appName string, appId string, appPath s
Data: primitive.NewObjectID(),
}
//TODO: Create folder for app, etc
_, _, err := as.fs.Write(appPath+"/"+newAppFile.Name, &newAppFile, newAppData)
if err != nil {
return err

View File

@ -128,13 +128,13 @@ func main() {
{
webfs.Route(fs)
}
app := router.Group("application")
app := router.Group("app")
{
persPropApp := app.Group("personal-properties")
persPropApp := app.Group("AboutMe")
{
persPropsApp.Route(persPropApp)
}
finderAppRoute := app.Group("finder")
finderAppRoute := app.Group("Finder")
{
finderApp.Routes(finderAppRoute)
}

View File

@ -1,5 +1,5 @@
class Finder{
appId = "finder"
appId = "Finder"
fileView = undefined
path = "/"
homePath = "/home/user"
@ -8,22 +8,20 @@ class Finder{
pathHistory = [] //FIXME Fixed length
constructor(){
// this.appElem = appElem
// WebDesktopEnvironment.RegisterApp(this)
}
/**
* @param {string[]} args
*/
NewWindow(args){
async 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?
const params = new URLSearchParams({isMobile: WebDesktopEnvironment.isMobile})
fetch(`/app/${this.appId}/render?` + params)
.then((response) => response.text())
.then((html) => {
let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, 500, 350 )
@ -149,9 +147,14 @@ class Finder{
Click(event){
let fileType = event.target.getAttribute("fileType")
let fileName = event.target.getAttribute("name")
const fileExtension = fileName.split(".")[fileName.split(".").length - 1] //FIXME
switch (fileType) {
case "directory":
WebDesktopEnvironment.Open("finder", [`${this.path}/${fileName}`]) //FIXME this.path is shared for all windows
if (fileExtension == "app"){
WebDesktopEnvironment.Open(`${this.path}/${fileName}`, [])
} else{
WebDesktopEnvironment.Open(`/Applications/Finder.app`, [`${this.path}/${fileName}`])
}
break
case "blog":
WebDesktopEnvironment.Open("blog-viewer", [`${this.path}/${fileName}`])
@ -176,15 +179,16 @@ class Finder{
CreateContextMenu(target, pos){
let context = ""
const fileName = target.getAttribute("name")
const fileType = target.getAttribute("fileType")
if (target.classList.contains("FileTileView"))
{
context = "FileTileView"
} else {
context = fileType
}
fetch(`${window.location.origin}/application/${this.appId}/contextMenu?` + new URLSearchParams({
context: context
}))
const params = new URLSearchParams({context: context, path: `${this.path}/${fileName}`})
fetch(`/app/${this.appId}/contextMenu?` + params)
.then((response) => response.text())
.then((html) => {
let overlay = document.createElement("div") //TODO Move to WDE.CreateOverlay()
@ -207,8 +211,6 @@ class Finder{
overlay.addEventListener('click', (event) => {
if (event.target.classList.contains("Row")){ //TODO add uuid id to rows to more accurate checks??
let fileType = target.getAttribute("fileType")
let fileName = target.getAttribute("name")
switch (event.target.children[0].getAttribute("action")) {
case "createDir":
fetch(`/fs/createDir?` + new URLSearchParams({
@ -263,6 +265,10 @@ class Finder{
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
break
case "openAsDir":
WebDesktopEnvironment.Open(`/Applications/${this.appId}.app`,[`${this.path}/${fileName}`])
break
default:
break;
}

View File

@ -1,5 +1,5 @@
class PersonalProperties{
appId = "personal-properties"
class AboutMe{
appId = "AboutMe"
/**
* @param {HTMLElement} appElem
*/
@ -9,12 +9,14 @@ class PersonalProperties{
/**
* @param {string} path
* @param {[]string} args
*/
NewWindow(path){
fetch(`${window.location.origin}/application/personal-properties/render?`+ new URLSearchParams({
isMobile: WebDesktopEnvironment.isMobile,
path: path
}))
NewWindow(path, args){
if (path == ""){
path = "/home/user/aboutme.props" //FIXME
}
const params = new URLSearchParams({isMobile: WebDesktopEnvironment.isMobile, path: path})
fetch(`/app/${this.appId}/render?`+ params)
.then((response) => response.text())
.then((html) => {
// console.log(document.body)

View File

@ -2,14 +2,14 @@
document.addEventListener('DOMContentLoaded', function() {
// console.log(window.screen.width)
wde = new WebDesktopEnvironment
wde = new WebDesktopEnvironment()
if (!WebDesktopEnvironment.isMobile){
WebDesktopEnvironment.Open("finder", ["/home/user"])
WebDesktopEnvironment.fetchApp("/home/user/AboutMe.app")
// WebDesktopEnvironment.Open("finder", ["/home/user"])
// WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog1.blog"])
// WebDesktopEnvironment.Open("personal-properties", ["/home/user/aboutme.props"])
} else {
WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"])
// WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"])
}
@ -20,11 +20,18 @@ class WebDesktopEnvironment{
static Applications = {};
static isMobile = false
constructor(){
WebDesktopEnvironment.isMobile = WebDesktopEnvironment.CheckMobile()
// WebDesktopEnvironment.Open("finder", ["/home/user", "desktop", document.querySelector('#desktop-layer')])
//TODO create key -desktop-mode
this.wc = new WindowsCompositor()
this.loadWDE()
return
// WebDesktopEnvironment.isMobile = WebDesktopEnvironment.CheckMobile()
let applications = document.createElement("div")
applications.setAttribute('id', 'applications')
document.body.appendChild(applications)
// let applications = document.createElement("div")
// applications.setAttribute('id', 'applications')
// document.body.appendChild(applications)
if( WebDesktopEnvironment.isMobile){
document.body.style.setProperty('--zoom', 3)
@ -61,17 +68,83 @@ class WebDesktopEnvironment{
} else{
document.body.style.setProperty('--zoom', 1)
let desktopLayer = document.createElement("div")
desktopLayer.setAttribute('id', 'desktop-layer')
document.body.appendChild(desktopLayer)
WebDesktopEnvironment.Open("finder", ["/home/user", "desktop", desktopLayer])
// let desktopLayer = document.createElement("div")
// desktopLayer.setAttribute('id', 'desktop-layer')
// document.body.appendChild(desktopLayer)
WebDesktopEnvironment.Open("finder", ["/home/user", "desktop", desktopLayer])
let windowsLayer = document.createElement("div")
windowsLayer.setAttribute('id', 'windows-layer')
document.body.appendChild(windowsLayer)
// let windowsLayer = document.createElement("div")
// windowsLayer.setAttribute('id', 'windows-layer')
// document.body.appendChild(windowsLayer)
this.Autoload()
}
this.wc = new WindowsCompositor
}
async loadWDE(){
// await WebDesktopEnvironment.load2('/Applications/Finder.app', [ "desktop", document.querySelector('#desktop-layer')])
// WebDesktopEnvironment.Open('/Applications/Finder.app', ["/home/user", "desktop", document.querySelector('#desktop-layer')])
WebDesktopEnvironment.Open('/Applications/Finder.app', ["/home/user",])
}
/**
* @param {string} appPath
* @param {string[]} args
*/
static async Open(appPath, args){
WebDesktopEnvironment.fetchApp(appPath).then(async appManifest =>{
if (appManifest === undefined) return //TODO return err
if (WebDesktopEnvironment.Applications[appManifest.appId] === undefined){
WebDesktopEnvironment.load2(appManifest, () =>{
WebDesktopEnvironment.Applications[appManifest.appId].NewWindow(args)
})
} else {
WebDesktopEnvironment.Applications[appManifest.appId].NewWindow(args)
}
})
}
/**
* @param {string} appManifest
* @param {function} onload callback after script loading
*/
static async load2(appManifest, onload){
let appElem = document.createElement("div")
appElem.setAttribute("appId", appManifest.appId)
//TODO Render scripts nodes on server
//FIXME Not support more than one js now :)
let script = document.createElement("script")
script.setAttribute("src", appManifest.js[0]) //FIXME path by fs read
script.setAttribute("async", "false") //TODO Possible may creates a problems??
appElem.appendChild(script)
document.getElementById("applications").appendChild(appElem)
script.addEventListener('load', (event) => {
let newApp = eval(`new ${appManifest.appId}()`);
//TODO Check if newApp undefined
WebDesktopEnvironment.Applications[appManifest.appId] = newApp;
onload()
})
return //TODO return result
}
/**
* @param {string} path
* @returns {Object | undefined} //FIXME
*/
static async fetchApp(path){
const params = new URLSearchParams({path: path})
const response = await fetch(`/system/libs/apps/loadApp?` + params)
if (response.status != 200){
return undefined
}
const appManifest = response.json()
return appManifest
}
Autoload(){
// WebDesktopEnvironment.Open(["/home/user/AboutMe.app"])
// WebDesktopEnvironment.load2(["/home/user/AboutMe.app"])
}
/**
* @param {string} appId
@ -116,44 +189,7 @@ class WebDesktopEnvironment{
})
}
/**
* @param {string} path
* @param {function} callbackFunc callback after script loading
* @returns {Application | undefined}
*/
static loadApp2(path, callbackFunc){
//var myObject = window[classNameString]; to load random classes
}
/**
* @param {string} path
* @returns {Application | undefined} //FIXME
*/
static fetchApp(path){
fetch(`/system/libs/apps/loadApp?` + new URLSearchParams({
path: path,
})) .then(async (response) => {
console.log(await response.json())
})
.catch((error) => {
WebDesktopEnvironment.Alert(error);
})
}
/**
* @param {string} appId
* @param {string[]} args
*/
static Open(appId, args){
if (this.Applications[appId] == undefined){
console.log(`Application ${appId} is not loaded yet`)
WebDesktopEnvironment.LoadApp(appId, () =>{
this.Applications[appId].NewWindow(args)
})
} else {
this.Applications[appId].NewWindow(args)
}
}
/**
* @param {string} appId

View File

@ -24,7 +24,9 @@
</head>
<body>
<!-- <div id="WindowsLayer"></div> -->
<!-- <div id="Applications"></div> -->
<div id="applications"></div>
<div id="desktop-layer"></div>
<div id="windows-layer"></div>
<!-- <div id="TestWindow" style="width: 100px; height: 100px; background-color: darkkhaki; position: absolute;" >
<div id="TestWindowHeader" class="VisualDragArea" style="width: 100%;height: 15px; background-color: cornflowerblue;">
</div> -->

View File

@ -106,21 +106,21 @@ func (fs *WebFileSystem) Route(route *gin.RouterGroup) {
ctx.JSON(http.StatusOK, &files)
})
// route.GET("read", func(ctx *gin.Context) {
// path := ctx.Query("path")
// if path == "" {
// ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
// return
// }
route.GET("read", func(ctx *gin.Context) {
path := ctx.Query("path")
if path == "" {
ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct
return
}
// file, err := fs.NewReadDeprecated(path)
// if err != nil {
// ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
// return
// }
file, err := fs.Read(path, nil)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO json error struct
return
}
// ctx.JSON(http.StatusOK, &file)
// })
ctx.JSON(http.StatusOK, &file)
})
route.GET("validate", func(ctx *gin.Context) {
err := fs.Validate()