From 91cd53c147822cd22441df02a48665dced2da9c9 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 12 Apr 2023 20:04:25 +0300 Subject: [PATCH] open and dynamic load app --- go.mod | 1 + go.sum | 2 + pure-html/base.html | 1 + resources/sys/finder/finder.js | 36 +++++---- .../personal-properties.js | 42 ++++++++-- resources/sys/wde/file-view.js | 15 ++-- resources/sys/wde/wde-scrollbar.js | 2 +- resources/wde.js | 76 ++++++++++++------- templates/base/index.tmpl | 1 + templates/wde-widgets/file-tile-view.tmpl | 2 +- webfilesystem/webfilesystem.go | 5 +- 11 files changed, 122 insertions(+), 61 deletions(-) diff --git a/go.mod b/go.mod index 6b4ad4e..12bc530 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/goccy/go-json v0.10.0 // indirect + github.com/google/uuid v1.3.0 github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/leodido/go-urn v1.2.1 // indirect diff --git a/go.sum b/go.sum index 6059cec..3f1d0c0 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= diff --git a/pure-html/base.html b/pure-html/base.html index e67ee1d..6f59604 100644 --- a/pure-html/base.html +++ b/pure-html/base.html @@ -11,6 +11,7 @@
+
diff --git a/resources/sys/finder/finder.js b/resources/sys/finder/finder.js index 05e5d09..acb7f1b 100644 --- a/resources/sys/finder/finder.js +++ b/resources/sys/finder/finder.js @@ -1,27 +1,22 @@ class Finder{ appId = "finder" - /** - * @param {HTMLElement} appElem - */ - constructor(appElem){ - this.appElem = appElem + constructor(){ + // this.appElem = appElem } /** * @param {string} path */ NewWindow(path){ - console.log("Init") + // console.log("Init") fetch(`${window.location.origin}/application/${this.appId}/render`) //TODO Move to wde func. Or Not? .then((response) => response.text()) .then((html) => { - let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, "Personal Properties", 500, 400 ) - - + let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, 500, 350 ) newWindow.innerHTML = html - console.log(newWindow.children[1].children[0]) - let fileView = new FileView("/kek", newWindow.children[1].children[0]) + // console.log(newWindow.children[1].children[0]) + let fileView = new FileView("/kek", newWindow.children[1].children[0], this.click) let scrollBar = new WdeScrollBar(newWindow.children[1].children[1], newWindow.children[1].children[0]) let closeButton = newWindow.children[0].children[0] @@ -32,8 +27,21 @@ class Finder{ .catch((error) => { WebDesktopEnvironment.Alert(error); }) - - - + } + /** + * @param {MouseEvent} event + * @param {string} path + */ + click(event, path){ + let fileName = event.target.getAttribute("fileName") + let fileExtension = fileName.split(".")[fileName.split(".").length - 1] + switch (fileExtension) { + case "lol": + WebDesktopEnvironment.Open("finder", ["pizda"]) + break; + default: + console.log("Unsupported file type") + break; + } } } diff --git a/resources/sys/personal-properties/personal-properties.js b/resources/sys/personal-properties/personal-properties.js index 346b4e7..1ffd6c3 100644 --- a/resources/sys/personal-properties/personal-properties.js +++ b/resources/sys/personal-properties/personal-properties.js @@ -1,6 +1,3 @@ -// const simpleScrollbar = require("../wde/simple-scrollbar"); - - class PersonalProperties{ appId = "personal-properties" /** @@ -10,14 +7,16 @@ class PersonalProperties{ this.appElem = appElem } - Init(){ - console.log("Init") + /** + * @param {string} path + */ + NewWindow(path){ fetch(`${window.location.origin}/application/personal-properties/render`) //TODO Move to wde func. Or Not? .then((response) => response.text()) .then((html) => { // console.log(document.body) // let heigth = Math.max(document.body.clientHeight*0.8, scrollDiv.children[0].scrollHeight) //TODO - let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, "Personal Properties", 360, document.body.clientHeight*0.8 ) + let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, 360, document.body.clientHeight*0.8 ) newWindow.innerHTML = html @@ -33,4 +32,33 @@ class PersonalProperties{ WebDesktopEnvironment.Alert(error); }); } -} \ No newline at end of file + + // Init(){ + // console.log("Init") + // fetch(`${window.location.origin}/application/personal-properties/render`) //TODO Move to wde func. Or Not? + // .then((response) => response.text()) + // .then((html) => { + // // console.log(document.body) + // // let heigth = Math.max(document.body.clientHeight*0.8, scrollDiv.children[0].scrollHeight) //TODO + // let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, "Personal Properties", 360, document.body.clientHeight*0.8 ) + + // newWindow.innerHTML = html + + // let closeButton = newWindow.children[0].children[0] + // let scrollDiv = newWindow.children[1] + // let scrollBar = new WdeScrollBar(scrollDiv.children[1].children[0], scrollDiv.children[0]) + + // closeButton.addEventListener('click', function (params) { + // WebDesktopEnvironment.CloseWindow(newWindow) + // }) + // }) + // .catch((error) => { + // WebDesktopEnvironment.Alert(error); + // }); + // } +} + + +class test{ + +} diff --git a/resources/sys/wde/file-view.js b/resources/sys/wde/file-view.js index 5258a04..c004ba2 100644 --- a/resources/sys/wde/file-view.js +++ b/resources/sys/wde/file-view.js @@ -3,8 +3,10 @@ class FileView{ /** * @param {string} path * @param {HTMLElement} fileViewElem + * @param {Function} clickCallback */ - constructor(path, fileViewElem){ + constructor(path, fileViewElem, clickCallback){ + //TODO check all params this.path = path this.openFolder(path, fileViewElem) @@ -14,23 +16,16 @@ class FileView{ // let scrollBar = new WdeScrollBar(fileViewElem.children[1].children[0], fileViewElem.children[0]) fileViewElem.addEventListener('click', (event) => { - this.click(event) + clickCallback(event) }) } - - /** - * @param {event} event - */ - click(){ - console.log(event.target) - } /** Get html of folder by path * @param {string} path * @param {HTMLElement} parentElem */ openFolder(path, parentElem){ - console.log(`${window.location.origin}/system/wde/widgets/file-tile-view?path=${path}`) + // console.log(`${window.location.origin}/system/wde/widgets/file-tile-view?path=${path}`) fetch(`${window.location.origin}/system/wde/widgets/file-tile-view?path=${path}`) //TODO Move to wde func. Or Not? .then((response) => response.text()) .then((html) => { diff --git a/resources/sys/wde/wde-scrollbar.js b/resources/sys/wde/wde-scrollbar.js index 79ec279..3eb3ddb 100644 --- a/resources/sys/wde/wde-scrollbar.js +++ b/resources/sys/wde/wde-scrollbar.js @@ -5,7 +5,7 @@ class WdeScrollBar{ */ constructor(scrollBarContainer, content){ let nonNativeScroll = false - console.log(scrollBarContainer) + // console.log(scrollBarContainer) let handler = scrollBarContainer.children[0] handler.style.height = (content.clientHeight /content.scrollHeight)* handler.parentElement.clientHeight + 'px' diff --git a/resources/wde.js b/resources/wde.js index c2fdda7..4e4dd0d 100644 --- a/resources/wde.js +++ b/resources/wde.js @@ -1,19 +1,18 @@ document.addEventListener('DOMContentLoaded', function() { wde = new WebDesktopEnvironment + WebDesktopEnvironment.Open("finder") + // WebDesktopEnvironment.Open("personal-properties") }, false); class WebDesktopEnvironment{ + static Applications = {}; constructor(){ - // console.log(window) this.wc = new WindowsCompositor - //Get basic window ready frame fetch(`${window.location.origin}/system/wde/getbasicwindow`) //TODO Move to wde func .then((response) => response.text()) .then((html) => { WebDesktopEnvironment.SetBasicWindow(html) - // let app = this.loadApp("personal-properties") - let finder = this.loadApp("finder") }) .catch((error) => { WebDesktopEnvironment.Alert(error); @@ -22,42 +21,67 @@ class WebDesktopEnvironment{ /** * @param {string} appId + * @param {function} func callback after script loading * @returns {Application | undefined} */ - loadApp(appId){ - let newApp = document.createElement("application") - newApp.setAttribute("id", `application-${appId}`) - - let appElem = document.getElementById("WindowsLayer").appendChild(newApp) + static LoadApp(appId, func){ + console.log(`Load application ${appId}`) + let appDiv = document.createElement("application") + appDiv.setAttribute("id", `application-${appId}`) + document.getElementById("WindowsLayer").appendChild(appDiv) let script = document.createElement("script") + script.setAttribute("id", `application-script-${appId}`) script.setAttribute("src", `${window.location.origin}/system/applications/${appId}/app.js`) script.setAttribute("async", "false") - appElem.appendChild(script) - script.addEventListener("load", function () { - newApp + let appElem = document.getElementById("Applications").appendChild(script) + + script.addEventListener('load', (event) =>{ switch (appId) { case "finder": - let newApp = new Finder(appElem) - newApp.NewWindow() - break; - + let newFinderApp = new Finder() + this.Applications[appId] = newFinderApp + func() + return newFinderApp + case "personal-properties": + let newPersonalPropertiesApp = new PersonalProperties() + this.Applications[appId] = newPersonalPropertiesApp + func() + return newPersonalPropertiesApp default: break; } - - }, false) + + }) + } + + /** + * @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) + // console.log(this.Applications) + }) + } else { + this.Applications[appId].NewWindow(args) + } + + + } /** * @param {string} appId - * @param {string} windowName * @param {number} width * @param {number} height * @returns {HTMLElement} */ - static CreateNewWindow(appId, windowName, width, height) { + static CreateNewWindow(appId, width, height) { let appElem = document.getElementById(`application-${appId}`) let newWindow = document.createElement("div") newWindow.setAttribute("class", "StandartApplicationWindow") @@ -74,19 +98,19 @@ class WebDesktopEnvironment{ let app = window.parentElement window.remove() // console.log(app.childElementCount) - if (app.childElementCount < 2){ - console.log(app) - app.remove() - } + // if (app.childElementCount < 2){ + // console.log(app) + // app.remove() + // } } - static basicWindow /** * @param {string} html */ static SetBasicWindow(html){ this.basicWindow = html } + /** * @returns {string} */ @@ -96,13 +120,11 @@ class WebDesktopEnvironment{ } /** - * * @param {string} alertText */ static Alert(alertText){ console.log(alertText) } - } var getJSON = function(url, callback) { diff --git a/templates/base/index.tmpl b/templates/base/index.tmpl index d56a317..ebce62a 100644 --- a/templates/base/index.tmpl +++ b/templates/base/index.tmpl @@ -16,6 +16,7 @@
+
diff --git a/templates/wde-widgets/file-tile-view.tmpl b/templates/wde-widgets/file-tile-view.tmpl index b8b9058..c27182b 100644 --- a/templates/wde-widgets/file-tile-view.tmpl +++ b/templates/wde-widgets/file-tile-view.tmpl @@ -1,6 +1,6 @@ {{ define "wde-widgets/file-tile-view.tmpl" }} {{ range $fileTile := .Files }} -
+
{{ $fileTile.FileName }}
diff --git a/webfilesystem/webfilesystem.go b/webfilesystem/webfilesystem.go index aeb12f7..efe7197 100644 --- a/webfilesystem/webfilesystem.go +++ b/webfilesystem/webfilesystem.go @@ -1,11 +1,13 @@ package webfilesystem +import "github.com/google/uuid" + type WebFileSystem struct { folders []*Folder } func (fs *WebFileSystem) List() []*File { - return []*File{{"Kek.kek"}, {"lel.lol"}, {"pretty.blog"}, {"ofiget.html"}, {"Kek.kek"}, {"lel.lol"}, {"pretty.blog"}, {"ofiget.html"}, {"Kek.kek"}, {"lel.lol"}, {"pretty.blog"}, {"ofiget.html"}, {"Kek.kek"}, {"lel.lol"}, {"pretty.blog"}, {"ofiget.html"}} + return []*File{{uuid.NewString(), "Kek.kek"}, {uuid.NewString(), "lel.lol"}} } type Folder struct { @@ -13,5 +15,6 @@ type Folder struct { } type File struct { + Id string FileName string }