From adb7babc02fa5b7014421bc6771b026048409fa3 Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 16 Apr 2019 19:42:20 +1000 Subject: [PATCH] Fixed a bug where hitting delete on one post would affect selected attachments on other posts. Fixed a bug where attachments couldn't be deleted after being added with the attachment manager without refreshing. Fixed a bug where an attachment can't be reuploaded with the attachment manager after being uploaded and deleted without refreshing. Improved the error message for RemoveAttachFromReplySubmit when there aren't any aids. The delete button is now hidden when there aren't any attachments to delete. --- public/global.js | 64 ++++++++++++++++++++-------------- routes/reply.go | 8 +++-- templates/topic_alt.html | 4 +-- templates/topic_alt_posts.html | 4 +-- themes/nox/public/main.css | 3 ++ 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/public/global.js b/public/global.js index 766ef33d..20cc0b9e 100644 --- a/public/global.js +++ b/public/global.js @@ -846,11 +846,9 @@ function mainInit(){ $(".topic_create_form").hide(); }); - function uploadFileHandler(fileList,maxFiles = 5, step1 = () => {}, step2 = () => {}) { + function uploadFileHandler(fileList, maxFiles = 5, step1 = () => {}, step2 = () => {}) { let files = []; - for(var i = 0; i < fileList.length && i < 5; i++) { - files[i] = fileList[i]; - } + for(var i = 0; i < fileList.length && i < 5; i++) files[i] = fileList[i]; let totalSize = 0; for(let i = 0; i < files.length; i++) { @@ -883,44 +881,44 @@ function mainInit(){ // TODO: Surely, there's a prettier and more elegant way of doing this? function getExt(filename) { - if(!filename.indexOf('.' > -1)) { - throw("This file doesn't have an extension"); - } + if(!filename.indexOf('.' > -1)) throw("This file doesn't have an extension"); return filename.split('.').pop(); } // Attachment Manager function uploadAttachHandler2() { + let post = this.closest(".post_item"); let fileDock = this.closest(".attach_edit_bay"); try { uploadFileHandler(this.files, 5, () => {}, (e, hash, filename) => { console.log("hash",hash); - + let formData = new FormData(); formData.append("session",me.User.Session); - for(let i = 0; i < this.files.length; i++) { - formData.append("upload_files",this.files[i]); - } + for(let i = 0; i < this.files.length; i++) formData.append("upload_files",this.files[i]); + bindAttachManager(); let req = new XMLHttpRequest(); req.addEventListener("load", () => { let data = JSON.parse(req.responseText); + //console.log("hash:",hash); + //console.log("rdata:",data); let fileItem = document.createElement("div"); let ext = getExt(filename); + //console.log("ext:",ext); // TODO: Check if this is actually an image, maybe push ImageFileExts to the client from the server in some sort of gen.js? - fileItem.className = "attach_item attach_image_holder"; + fileItem.className = "attach_item attach_item_item attach_image_holder"; fileItem.innerHTML = Template_topic_c_attach_item({ - ID: data[hash+"."+ext], + ID: data.elems[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"); - $(".attach_item_copy").unbind("click"); - bindAttachItems() + post.classList.add("has_attachs"); + bindAttachItems(); }); req.open("POST","//"+window.location.host+"/"+fileDock.getAttribute("type")+"/attach/add/submit/"+fileDock.getAttribute("id")); req.send(formData); @@ -976,12 +974,18 @@ function mainInit(){ if(uploadFilesOp != null) { uploadFilesOp.addEventListener("change", uploadAttachHandler2, false); } - let uploadFilesPost = document.getElementsByClassName("upload_files_post"); - if(uploadFilesPost != null) { - for(let i = 0; i < uploadFilesPost.length; i++) { - uploadFilesPost[i].addEventListener("change", uploadAttachHandler2, false); + + function bindAttachManager() { + let uploadFiles = document.getElementsByClassName("upload_files_post"); + if(uploadFiles == null) return; + for(let i = 0; i < uploadFiles.length; i++) { + let uploader = uploadFiles[i]; + uploader.value = ""; + uploader.removeEventListener("change", uploadAttachHandler2, false); + uploader.addEventListener("change", uploadAttachHandler2, false); } } + bindAttachManager(); function copyToClipboard(str) { const el = document.createElement('textarea'); @@ -996,13 +1000,12 @@ function mainInit(){ } function bindAttachItems() { + $(".attach_item_select").unbind("click"); + $(".attach_item_copy").unbind("click"); $(".attach_item_select").click(function(){ let hold = $(this).closest(".attach_item"); - if(hold.hasClass("attach_item_selected")) { - hold.removeClass("attach_item_selected"); - } else { - hold.addClass("attach_item_selected"); - } + if(hold.hasClass("attach_item_selected")) hold.removeClass("attach_item_selected"); + else hold.addClass("attach_item_selected"); }); $(".attach_item_copy").click(function(){ let hold = $(this).closest(".attach_item"); @@ -1016,8 +1019,9 @@ function mainInit(){ let formData = new URLSearchParams(); formData.append("session",me.User.Session); + let post = this.closest(".post_item"); let aidList = ""; - let elems = document.getElementsByClassName("attach_item_selected"); + let elems = post.getElementsByClassName("attach_item_selected"); if(elems == null) return; for(let i = 0; i < elems.length; i++) { @@ -1029,11 +1033,19 @@ function mainInit(){ if(aidList.length > 0) aidList = aidList.slice(0, -1); console.log("aidList",aidList) formData.append("aids",aidList); + + let ec = 0; + let e = post.getElementsByClassName("attach_item_item"); + if(e!=null) ec = e.length; + if(ec==0) post.classList.remove("has_attachs"); let req = new XMLHttpRequest(); let fileDock = this.closest(".attach_edit_bay"); req.open("POST","//"+window.location.host+"/"+fileDock.getAttribute("type")+"/attach/remove/submit/"+fileDock.getAttribute("id"),true); req.send(formData); + + bindAttachItems(); + bindAttachManager(); }); $(".moderate_link").click((event) => { diff --git a/routes/reply.go b/routes/reply.go index 9b8cff43..9004e23d 100644 --- a/routes/reply.go +++ b/routes/reply.go @@ -402,7 +402,7 @@ func AddAttachToReplySubmit(w http.ResponseWriter, r *http.Request, user common. elemStr = elemStr[:len(elemStr)-1] } - w.Write([]byte(`{"success":"1","elems":[{` + elemStr + `}]}`)) + w.Write([]byte(`{"success":"1","elems":{` + elemStr + `}}`)) return nil } @@ -436,7 +436,11 @@ func RemoveAttachFromReplySubmit(w http.ResponseWriter, r *http.Request, user co return common.NoPermissionsJS(w, r, user) } - for _, said := range strings.Split(r.PostFormValue("aids"), ",") { + saids := strings.Split(r.PostFormValue("aids"), ",") + if len(saids) == 0 { + return common.LocalErrorJS("No aids provided", w, r) + } + for _, said := range saids { aid, err := strconv.Atoi(said) if err != nil { return common.LocalErrorJS(phrases.GetErrorPhrase("id_must_be_integer"), w, r) diff --git a/templates/topic_alt.html b/templates/topic_alt.html index d4158194..0bc0d9c6 100644 --- a/templates/topic_alt.html +++ b/templates/topic_alt.html @@ -61,7 +61,7 @@ {{end}} -
+
{{template "topic_alt_userinfo.html" .Topic }}
{{.Topic.ContentHTML}}
@@ -70,7 +70,7 @@ {{if .CurrentUser.Perms.EditTopic}}
{{range .Topic.Attachments}} -
+
{{if .Image}}{{end}} {{.Path}} diff --git a/templates/topic_alt_posts.html b/templates/topic_alt_posts.html index b124de8f..9bb1c99f 100644 --- a/templates/topic_alt_posts.html +++ b/templates/topic_alt_posts.html @@ -1,4 +1,4 @@ -{{range .ItemList}}
+{{range .ItemList}}
{{template "topic_alt_userinfo.html" . }}
{{if .ActionType}} @@ -12,7 +12,7 @@ {{if $.CurrentUser.Perms.EditReply}}
{{range .Attachments}} -
+
{{if .Image}}{{end}} {{.Path}} diff --git a/themes/nox/public/main.css b/themes/nox/public/main.css index a4eefa9a..b99ecfaa 100644 --- a/themes/nox/public/main.css +++ b/themes/nox/public/main.css @@ -1016,6 +1016,9 @@ input[type=checkbox]:checked + label .sel { .attach_item button { margin-top: -1px; } +.post_item:not(.has_attachs) .attach_item_delete { + display: none; +} .zone_view_topic .pageset { margin-bottom: 14px;