From 471350ead128f2c995f0854ad2c56b17bfe47665 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Sat, 29 Apr 2023 16:58:39 +0300 Subject: [PATCH] Working blog files opening --- main.go | 21 ++++-- resources/sys/blog-viewer/blog-viewer.js | 6 +- resources/sys/finder/finder.js | 83 ++++++++++++++++++++--- resources/sys/wde/file-view.js | 20 +++--- resources/wde.js | 6 +- resources/wdeUI.css | 2 +- routewde/wde.go | 21 +++--- templates/blog-viewer/app.tmpl | 14 +++- templates/finder/app.tmpl | 4 ++ templates/wde-widgets/file-tile-view.tmpl | 4 +- wde/wde.go | 32 +++++++++ websiteapp/blogviewer/blogfile.go | 6 ++ websiteapp/blogviewer/blogviewer.go | 81 +++++++++++++++------- websiteapp/finder/finder.go | 13 +++- 14 files changed, 243 insertions(+), 70 deletions(-) create mode 100644 wde/wde.go create mode 100644 websiteapp/blogviewer/blogfile.go diff --git a/main.go b/main.go index 5d4a09b..ade89bc 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "personalwebsite/routewde" + "personalwebsite/wde" "personalwebsite/webfilesystem" "personalwebsite/websiteapp" "personalwebsite/websiteapp/blogviewer" @@ -61,23 +62,25 @@ func main() { }) webfs := webfilesystem.NewWebFileSystem(client, dBName, webFsCollection) + wde := wde.NewWDE(webfs) persPropsApp := personalprops.NewPersPropsApp() - finderApp := finder.FinderApplication{} + // finderApp := finder.FinderApplication{} + finderApp := finder.NewFinderApplication(webfs) imgViewerApp := imgviewer.NewImgViewerApp() blogViewerApp := blogviewer.NewBlogViewerApp(webfs) appsStorage := websiteapp.ApplicationsStorage{ Apps: map[string]websiteapp.WebDEApplication{}, } appsStorage.Apps["personal-properties"] = &persPropsApp - appsStorage.Apps["finder"] = &finderApp + appsStorage.Apps["finder"] = finderApp appsStorage.Apps["img-viewer"] = &imgViewerApp appsStorage.Apps["blog-viewer"] = blogViewerApp system := router.Group("system") { - wde := system.Group("wde") + wdeGroup := system.Group("wde") { - routewde.Route(wde) + routewde.Route(wdeGroup, wde) } apps := system.Group("applications") { @@ -240,10 +243,16 @@ func main() { } isMobile := isMobileParam == "true" + ginH, err := blogViewerApp.Render(path, isMobile) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") + return + } + if isMobile { - ctx.HTML(http.StatusOK, "blog-viewer/mobile-app.tmpl", blogViewerApp.Render(path, isMobile)) + ctx.HTML(http.StatusOK, "blog-viewer/mobile-app.tmpl", ginH) } else { - ctx.HTML(http.StatusOK, "blog-viewer/app.tmpl", blogViewerApp.Render(path, isMobile)) + ctx.HTML(http.StatusOK, "blog-viewer/app.tmpl", ginH) } }) diff --git a/resources/sys/blog-viewer/blog-viewer.js b/resources/sys/blog-viewer/blog-viewer.js index b905557..9469aef 100644 --- a/resources/sys/blog-viewer/blog-viewer.js +++ b/resources/sys/blog-viewer/blog-viewer.js @@ -4,12 +4,12 @@ class BlogViewer{ } /** - * @param {string} path + * @param {string[]} args */ - NewWindow(path){ + NewWindow(args){ fetch(`${window.location.origin}/application/${this.appId}/render?` + new URLSearchParams({ isMobile: WebDesktopEnvironment.isMobile, - path: path, + path: args[0], })) .then((response) => response.text()) .then((html) => { diff --git a/resources/sys/finder/finder.js b/resources/sys/finder/finder.js index 5b904c9..ec993f0 100644 --- a/resources/sys/finder/finder.js +++ b/resources/sys/finder/finder.js @@ -1,5 +1,10 @@ class Finder{ appId = "finder" + fileView = undefined + path = "/" + homePath = "/home/user" + // previousPath = "/" + pathHistory = [] //FIXME Fixed length constructor(){ // this.appElem = appElem } @@ -7,9 +12,11 @@ class Finder{ /** * @param {string} path */ - NewWindow(path){ + NewWindow(args){ + this.path = args[0] 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()) @@ -17,7 +24,19 @@ class Finder{ let newWindow = WebDesktopEnvironment.CreateNewWindow(this.appId, 500, 350 ) newWindow.innerHTML = html - let fileView = new FileView("/kek", newWindow.querySelector(".FileTileView"), Finder.Click) + this.fileView = new FileView(newWindow.querySelector(".FileTileView"), (event) =>{ + this.Click(event, this.path) + }) + 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 newWindow.querySelector("#closeWindowButton").addEventListener('click', function (params) { @@ -30,23 +49,65 @@ class Finder{ }) } + 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 {MouseEvent} event * @param {string} path */ - static Click(event, path){ + OpenDir(path){ + console.log() + this.pathHistory += this.path + this.path = path + this.fileView.OpenFolder(this.path) + } + + /** + * @param {MouseEvent} event + */ + Click(event){ let fileType = event.target.getAttribute("fileType") + let fileName = event.target.getAttribute("name") switch (fileType) { - case "app": - //TODO get real id - WebDesktopEnvironment.Open("personal-properties", []) - break; - case "img": - WebDesktopEnvironment.Open("img-viewer", ["pizda"]) - break; + case "directory": + 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 "img": + // WebDesktopEnvironment.Open("img-viewer", ["pizda"]) + // 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); + // }) + // } } diff --git a/resources/sys/wde/file-view.js b/resources/sys/wde/file-view.js index c004ba2..150f41f 100644 --- a/resources/sys/wde/file-view.js +++ b/resources/sys/wde/file-view.js @@ -1,15 +1,14 @@ class FileView{ - path = "" + parentElem = undefined /** - * @param {string} path * @param {HTMLElement} fileViewElem * @param {Function} clickCallback */ - constructor(path, fileViewElem, clickCallback){ + constructor(fileViewElem, clickCallback){ //TODO check all params - this.path = path + this.parentElem = fileViewElem - this.openFolder(path, fileViewElem) + // this.OpenFolder(path) // let scrollDiv = fileViewElem.children[1] // console.log(scrollDiv) @@ -22,14 +21,17 @@ class FileView{ /** 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}`) + OpenFolder(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) => { - parentElem.innerHTML = html + //TODO + // console.log(responseStatus) + // if (responseStatus != 200) { + // WebDesktopEnvironment.Alert("Error") + // } + this.parentElem.innerHTML = html }).catch((error) => { WebDesktopEnvironment.Alert(error); }) diff --git a/resources/wde.js b/resources/wde.js index 817408d..aa7da97 100644 --- a/resources/wde.js +++ b/resources/wde.js @@ -2,7 +2,8 @@ document.addEventListener('DOMContentLoaded', function() { // console.log(window.screen.width) wde = new WebDesktopEnvironment if (!WebDesktopEnvironment.isMobile){ - WebDesktopEnvironment.Open("finder", ["/home/user/"]) + WebDesktopEnvironment.Open("finder", ["/home/user"]) + // WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"]) // WebDesktopEnvironment.Open("personal-properties", ["kek"]) } else { WebDesktopEnvironment.Open("blog-viewer", ["/home/user/blog/test-1.blog"]) @@ -210,7 +211,8 @@ class WindowsCompositor{ //TODO refactor this to dynamic add/remove listeners constructor(){ this.windowLayer = document.body.querySelector('#windows-layer') - if (!WebDesktopEnvironment.isMobile) { + // if (!WebDesktopEnvironment.isMobile) { + if (false) { //FIXME addEventListener("mousedown", (event) => { this.xPosInit = event.offsetX this.yPosInit = event.offsetY diff --git a/resources/wdeUI.css b/resources/wdeUI.css index db0dc11..3970f0b 100644 --- a/resources/wdeUI.css +++ b/resources/wdeUI.css @@ -326,5 +326,5 @@ } .FileTileTitle{ - + white-space: nowrap; } \ No newline at end of file diff --git a/routewde/wde.go b/routewde/wde.go index 118fcac..0e7a20b 100644 --- a/routewde/wde.go +++ b/routewde/wde.go @@ -2,12 +2,12 @@ package routewde import ( "net/http" - "personalwebsite/webfilesystem" + "personalwebsite/wde" "github.com/gin-gonic/gin" ) -func Route(route *gin.RouterGroup) { +func Route(route *gin.RouterGroup, wde *wde.WDE) { route.GET("/getbasicwindow", func(ctx *gin.Context) { ctx.HTML(http.StatusOK, "basic-window.html", nil) }) @@ -19,12 +19,17 @@ func Route(route *gin.RouterGroup) { widgets := route.Group("widgets") { widgets.GET("/file-tile-view", func(ctx *gin.Context) { - fs := webfilesystem.WebFileSystem{} - list, _ := fs.List("/") //TODO check errors - ctx.HTML(http.StatusOK, "wde-widgets/file-tile-view.tmpl", gin.H{ - "Name": "Greg Brzezinski", - "Files": list, - }) + path := ctx.Query("path") + if path == "" { + ctx.JSON(http.StatusBadRequest, "TODO") //TODO json error struct + return + } + ginH, err := wde.Render(path) + if err != nil { + ctx.JSON(http.StatusInternalServerError, "TODO") //TODO + return + } + ctx.HTML(http.StatusOK, "wde-widgets/file-tile-view.tmpl", ginH) }) } } diff --git a/templates/blog-viewer/app.tmpl b/templates/blog-viewer/app.tmpl index 7dbd113..77cd13f 100644 --- a/templates/blog-viewer/app.tmpl +++ b/templates/blog-viewer/app.tmpl @@ -3,13 +3,21 @@
- Files + {{.header}}
-
- +
+ {{ range $block := .blocks }} +
+ {{ range $data := $block.Data }} +
+ {{$data}} +
+ {{ end }} +
+ {{ end }}
{{template "wde-widgets/scrollbar.tmpl" .}}
diff --git a/templates/finder/app.tmpl b/templates/finder/app.tmpl index 202bf57..e7d05f2 100644 --- a/templates/finder/app.tmpl +++ b/templates/finder/app.tmpl @@ -7,6 +7,10 @@
+
+ + +
diff --git a/templates/wde-widgets/file-tile-view.tmpl b/templates/wde-widgets/file-tile-view.tmpl index 5da3216..3e3ce22 100644 --- a/templates/wde-widgets/file-tile-view.tmpl +++ b/templates/wde-widgets/file-tile-view.tmpl @@ -1,8 +1,8 @@ {{ define "wde-widgets/file-tile-view.tmpl" }} {{ range $fileTile := .Files }} -
+
-
{{ $fileTile.FileName }}
+
{{ $fileTile.Name }}
{{ end }} {{ end }} \ No newline at end of file diff --git a/wde/wde.go b/wde/wde.go new file mode 100644 index 0000000..317133d --- /dev/null +++ b/wde/wde.go @@ -0,0 +1,32 @@ +package wde + +import ( + "personalwebsite/webfilesystem" + + "github.com/gin-gonic/gin" +) + +type WDE struct { + fs *webfilesystem.WebFileSystem + FilesWidget FilesWidget +} + +func NewWDE(webFs *webfilesystem.WebFileSystem) *WDE { + return &WDE{ + fs: webFs, + } +} + +type FilesWidget struct { +} + +func (w *WDE) Render(path string) (gin.H, error) { + list, err := w.fs.List(path) + if err != nil { + return nil, err + } + return gin.H{ + "Name": "Greg Brzezinski", + "Files": list, + }, nil +} diff --git a/websiteapp/blogviewer/blogfile.go b/websiteapp/blogviewer/blogfile.go new file mode 100644 index 0000000..a96c955 --- /dev/null +++ b/websiteapp/blogviewer/blogfile.go @@ -0,0 +1,6 @@ +package blogviewer + +type BlogFileData struct { + Header string `bson:"header"` + Blocks []Block `bson:"blocks"` +} diff --git a/websiteapp/blogviewer/blogviewer.go b/websiteapp/blogviewer/blogviewer.go index 295bbc0..8ba103f 100644 --- a/websiteapp/blogviewer/blogviewer.go +++ b/websiteapp/blogviewer/blogviewer.go @@ -1,17 +1,21 @@ package blogviewer import ( + "personalwebsite/webfilesystem" "personalwebsite/websiteapp" "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson/primitive" ) type BlogViewerApplication struct { + fs *webfilesystem.WebFileSystem manifest websiteapp.ApplicationManifest } -func NewBlogViewerApp() *BlogViewerApplication { +func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication { return &BlogViewerApplication{ + fs: webFs, manifest: websiteapp.ApplicationManifest{ AppId: "blog-viewer", WindowName: "", @@ -22,36 +26,65 @@ func NewBlogViewerApp() *BlogViewerApplication { func (b *BlogViewerApplication) GetManifest() websiteapp.ApplicationManifest { return b.manifest } + func (b *BlogViewerApplication) GetId() string { return b.manifest.AppId } -func (b *BlogViewerApplication) Render(isMobile bool) gin.H { - mockBlocks := []Block{ - { - BlockType: "plain-text", - Data: []string{"qwerty qwerty werty", "oiposi sdfsp sdfip"}, - }, - { - BlockType: "plain-text", - Data: []string{"perpoer", "kekekeke"}, - }, - { - BlockType: "plain-text", - Data: []string{"perpoer", "kekekeke"}, - }, - { - BlockType: "plain-text", - Data: []string{"perpoer", "kekekeke"}, +func (b *BlogViewerApplication) WriteMock(path string) { + blogFile := webfilesystem.WebFSFile{ + MongoId: primitive.NewObjectID(), + Name: "test-1.blog", + Type: "blog-page", + Data: BlogFileData{ + Header: "OMG THIS IS BLOG", + Blocks: []Block{ + { + Type: "plain-text", + Data: []string{ + "Apoqiwepoqiwepo", + ".,mas;dakls;d", + "q[poqwieqpipoi]", + }, + }, + }, }, } - return gin.H{ - "header": "AAAAAA Header", - "blocks": mockBlocks, + err := b.fs.CreateFile(&blogFile, path) + if err != nil { + println(err.Error()) } } -type Block struct { - BlockType string - Data []string +func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) { + file, err := b.fs.Read(path) + if err != nil { + println(err.Error()) + return nil, err + } + blogDataRaw := file.Data.(primitive.D).Map() + header := blogDataRaw["header"] + blocks := []Block{} + + for _, rawBlock := range blogDataRaw["blocks"].(primitive.A) { + lel := rawBlock.(primitive.D).Map() + block := Block{ + Type: lel["type"].(string), + Data: []string{}, + } + for _, v := range lel["data"].(primitive.A) { + block.Data = append(block.Data, v.(string)) + } + blocks = append(blocks, block) + } + + return gin.H{ + "header": header, + "blocks": blocks, + }, nil +} + +type Block struct { + Type string `bson:"type"` + Data []string `bson:"data"` } diff --git a/websiteapp/finder/finder.go b/websiteapp/finder/finder.go index 8782160..d7a65a4 100644 --- a/websiteapp/finder/finder.go +++ b/websiteapp/finder/finder.go @@ -1,16 +1,26 @@ package finder import ( + "personalwebsite/webfilesystem" "personalwebsite/websiteapp" "github.com/gin-gonic/gin" ) type FinderApplication struct { + fs *webfilesystem.WebFileSystem manifest websiteapp.ApplicationManifest } -func NewFinderApplication() *FinderApplication {} +func NewFinderApplication(webFs *webfilesystem.WebFileSystem) *FinderApplication { + return &FinderApplication{ + fs: webFs, + manifest: websiteapp.ApplicationManifest{ + AppId: "finder", + WindowName: "TODO DELETE", //TODO DELETE + }, + } +} func (f *FinderApplication) GetManifest() websiteapp.ApplicationManifest { return f.manifest } @@ -19,5 +29,6 @@ func (f *FinderApplication) GetId() string { } func (f *FinderApplication) Render(isMobile bool) gin.H { + return gin.H{} }