From 546aa5f845b22cec17c8352e5217fbab4d845677 Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 15 Apr 2019 11:54:13 +1000 Subject: [PATCH] The attachment managers for replies are now rendered with client templates Avoid loading the topic phrases when the user isn't logged in. Don't load the topic_c_edit_post and topic_c_attach_item client templates when the user isn't logged in. Fixed a bug where multiple attachment managers wouldn't load. Added the topic_c_attach_item template. --- common/attachments.go | 9 ++++++- common/pages.go | 6 +++++ common/routes_common.go | 5 +++- common/template_init.go | 2 ++ public/global.js | 8 +++++-- public/init.js | 38 ++++++++++++++++-------------- templates/header.html | 2 +- templates/topic_c_attach_item.html | 4 ++++ 8 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 templates/topic_c_attach_item.html diff --git a/common/attachments.go b/common/attachments.go index 98bf409f..f3ee2afb 100644 --- a/common/attachments.go +++ b/common/attachments.go @@ -107,14 +107,21 @@ func (store *DefaultAttachmentStore) BulkMiniGetList(originTable string, ids []i } attach.Ext = extarr[len(extarr)-1] attach.Image = ImageFileExts.Contains(attach.Ext) - if attach.ID != currentID { + if currentID == 0 { + currentID = attach.OriginID + } + if attach.OriginID != currentID { if len(buffer) > 0 { amap[currentID] = buffer + currentID = attach.OriginID buffer = nil } } buffer = append(buffer, attach) } + if len(buffer) > 0 { + amap[currentID] = buffer + } return amap, rows.Err() } diff --git a/common/pages.go b/common/pages.go index e9c94f32..7126bea2 100644 --- a/common/pages.go +++ b/common/pages.go @@ -169,6 +169,12 @@ type TopicCEditPost struct { Source string Ref string } +type TopicCAttachItem struct { + ID int + ImgSrc string + Path string + FullPath string +} type TopicPage struct { *Header diff --git a/common/routes_common.go b/common/routes_common.go index 2378e913..8c7ee2c8 100644 --- a/common/routes_common.go +++ b/common/routes_common.go @@ -265,7 +265,10 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head addPreScript("topics_topic") addPreScript("paginator") addPreScript("alert") - addPreScript("topic_c_edit_post") + if user.Loggedin { + addPreScript("topic_c_edit_post") + addPreScript("topic_c_attach_item") + } return header, nil } diff --git a/common/template_init.go b/common/template_init.go index b99e32f7..229752c8 100644 --- a/common/template_init.go +++ b/common/template_init.go @@ -479,6 +479,8 @@ func compileJSTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName stri tmpls.AddStd("topic_c_edit_post", "common.TopicCEditPost", TopicCEditPost{Source: "", Ref: ""}) + tmpls.AddStd("topic_c_attach_item", "common.TopicCAttachItem", TopicCAttachItem{ID: 1, ImgSrc: "", Path: "", FullPath: ""}) + var dirPrefix = "./tmpl_client/" var writeTemplate = func(name string, content string) { log.Print("Writing template '" + name + "'") diff --git a/public/global.js b/public/global.js index 28908da0..766ef33d 100644 --- a/public/global.js +++ b/public/global.js @@ -909,9 +909,13 @@ function mainInit(){ let fileItem = document.createElement("div"); let ext = getExt(filename); // TODO: Check if this is actually an image, maybe push ImageFileExts to the client from the server in some sort of gen.js? - // TODO: Use client templates here fileItem.className = "attach_item attach_image_holder"; - fileItem.innerHTML = ""+hash+"."+ext+""; + fileItem.innerHTML = Template_topic_c_attach_item({ + ID: data[hash+"."+ext], + ImgSrc: e.target.result, + Path: hash+"."+ext, + FullPath: "//" + window.location.host + "/attachs/" + hash + "." + ext, + }); fileDock.insertBefore(fileItem,fileDock.querySelector(".attach_item_buttons")); $(".attach_item_select").unbind("click"); diff --git a/public/init.js b/public/init.js index ac1570d9..9fb24f36 100644 --- a/public/init.js +++ b/public/init.js @@ -173,10 +173,14 @@ function RelativeTime(date) { return date; } -function initPhrases() { +function initPhrases(loggedIn) { console.log("in initPhrases") console.log("tmlInits:",tmplInits) - fetchPhrases("status,topic_list,topic,alerts,paginator,analytics") // TODO: Break this up? + let e = ""; + if(loggedIn) { + e = ",topic" + } + fetchPhrases("status,topic_list,alerts,paginator,analytics"+e) // TODO: Break this up? } function fetchPhrases(plist) { @@ -212,26 +216,24 @@ function fetchPhrases(plist) { (() => { runInitHook("pre_iife"); + let loggedIn = document.head.querySelector("[property='x-loggedin']").content == "true"; + let toLoad = 2; // TODO: Shunt this into loggedIn if there aren't any search and filter widgets? - notifyOnScriptW("template_topics_topic", () => { + let q = (f) => { toLoad--; - if(toLoad===0) initPhrases(); - if(!Template_topics_topic) throw("template function not found"); - }); - notifyOnScriptW("template_paginator", () => { - toLoad--; - if(toLoad===0) initPhrases(); - if(!Template_paginator) throw("template function not found"); - }); - notifyOnScriptW("template_topic_c_edit_post", () => { - toLoad--; - if(toLoad===0) initPhrases(); - if(!Template_topic_c_edit_post) throw("template function not found"); - }); + if(toLoad===0) initPhrases(loggedIn); + if(f) throw("template function not found"); + }; + if(loggedIn) { + toLoad += 2; + notifyOnScriptW("template_topic_c_edit_post", () => q(!Template_topic_c_edit_post)); + notifyOnScriptW("template_topic_c_attach_item", () => q(!Template_topic_c_attach_item)); + } + notifyOnScriptW("template_topics_topic", () => q(!Template_topics_topic)); + notifyOnScriptW("template_paginator", () => q(!Template_paginator)); - let loggedIn = document.head.querySelector("[property='x-loggedin']").content; - if(loggedIn=="true") { + if(loggedIn) { fetch("/api/me/") .then((resp) => resp.json()) .then((data) => { diff --git a/templates/header.html b/templates/header.html index b64069ba..57c8c31f 100644 --- a/templates/header.html +++ b/templates/header.html @@ -7,7 +7,7 @@ {{range .Header.PreScriptsAsync}} {{end}} - + {{range .Header.ScriptsAsync}} {{end}} diff --git a/templates/topic_c_attach_item.html b/templates/topic_c_attach_item.html new file mode 100644 index 00000000..4165056e --- /dev/null +++ b/templates/topic_c_attach_item.html @@ -0,0 +1,4 @@ + +{{.Path}} + + \ No newline at end of file