Working blog files opening

This commit is contained in:
cyber-dream 2023-04-29 16:58:39 +03:00
parent b6b3cf9ca1
commit 471350ead1
14 changed files with 243 additions and 70 deletions

21
main.go
View File

@ -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)
}
})

View File

@ -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) => {

View File

@ -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);
// })
// }
}

View File

@ -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);
})

View File

@ -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

View File

@ -326,5 +326,5 @@
}
.FileTileTitle{
white-space: nowrap;
}

View File

@ -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)
})
}
}

View File

@ -3,13 +3,21 @@
<button id="closeWindowButton" class="WindowFrameTopBarButton" title="Close Window"></button>
<div id="Drag" class="WindowDragArea"></div>
<div class="WindowFrameTitle">
Files
{{.header}}
</div>
<div id="Drag" class="WindowDragArea"></div>
</div>
<div class="ContentBorder">
<div class="FileTileView">
<div class="blog-viewer">
{{ range $block := .blocks }}
<div class="{{$block.Type}}">
{{ range $data := $block.Data }}
<div>
{{$data}}
</div>
{{ end }}
</div>
{{ end }}
</div>
{{template "wde-widgets/scrollbar.tmpl" .}}
</div>

View File

@ -7,6 +7,10 @@
</div>
<div id="Drag" class="WindowDragArea"></div>
</div>
<div class="ToolBar">
<button id="BackButton">Back</button>
<button id="HomeButton">Home</button>
</div>
<div class="ContentBorder">
<div class="FileTileView">

View File

@ -1,8 +1,8 @@
{{ define "wde-widgets/file-tile-view.tmpl" }}
{{ range $fileTile := .Files }}
<div id="{{ $fileTile.Id }}" fileType="{{ $fileTile.Type }}" class="FileTile" fileName="{{$fileTile.FileName}}">
<div fileType="{{ $fileTile.Type }}" class="FileTile" name="{{$fileTile.Name}}">
<div class="FileTileIcon NoClick"></div>
<div class="FileTileTitle NoClick">{{ $fileTile.FileName }}</div>
<div class="FileTileTitle NoClick">{{ $fileTile.Name }}</div>
</div>
{{ end }}
{{ end }}

32
wde/wde.go Normal file
View File

@ -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
}

View File

@ -0,0 +1,6 @@
package blogviewer
type BlogFileData struct {
Header string `bson:"header"`
Blocks []Block `bson:"blocks"`
}

View File

@ -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"`
}

View File

@ -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{}
}