From 9fae814ab345cc45b0c237c922c90d7b685452fc Mon Sep 17 00:00:00 2001 From: Michele Tessaro Date: Mon, 18 Sep 2023 07:43:19 +0200 Subject: [PATCH] fix: dev: fixed attachments list after editing (#131) The list of attachments is now refreshed after every edit. The refresh is done without refreshing the whole page, some text may not be in the correct language, but reloading the page fixes the issue. --- assets/javascripts/drawioEditor.js | 96 +++++++++++++++++++++++++++++- lib/redmine_drawio/macros.rb | 2 +- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/assets/javascripts/drawioEditor.js b/assets/javascripts/drawioEditor.js index f2a643af..47ae6a40 100644 --- a/assets/javascripts/drawioEditor.js +++ b/assets/javascripts/drawioEditor.js @@ -45,6 +45,13 @@ function editDiagram(image, resource, isDmsf, pageName, originalName) { '
'); + attachTable = $(".attachments table tbody"); + } + + var lastRow = attachTable.find('tr').last(); + var hrefCount = 0; + var linkTextCount = 0; + var lastRowHtml = $(lastRow).html(); + + if(!lastRowHtml) { + lastRowHtml = '' + + ' NAME (SIZE KB)' + + ' NAME ' + + ' ' + + ' ' + + ' USER, TIMESTAMP' + + ' ' + + ' ' + + ' Delete' + + ' '; + } + // replace icon link + lastRowHtml = lastRowHtml.replace(/href="[^"]+"/g, function(match) { + var path = lastAttach.content_url.substring(0, lastAttach.content_url.indexOf('/attachments/download/')+'/attachments'.length); + + //var path = lastAttach.content_url.substring(0, lastAttach.content_url.lastIndexOf('/')); + + //path = path.substring(0, path.lastIndexOf('/')); + + switch(hrefCount++) { + case 0: + case 2: return 'href="'+path+'/'+lastAttach.id+'"'; + case 1: return 'href="'+lastAttach.content_url+'"'; + default: return match; + } + }); + // replace file size + lastRowHtml = lastRowHtml.replace(/\((.*?) KB\)/, '('+(lastAttach.filesize/1024).toFixed(2)+' KB)'); + // replace file name + lastRowHtml = lastRowHtml.replace(/>[^<]+<\/a>/g, function(match) { + if(linkTextCount++ < 2) { + return '>'+lastAttach.filename+''; + } else { + return match; + } + }); + // replace author and date + var attachDate = new Date(lastAttach.created_on).toLocaleString(); + lastRowHtml = lastRowHtml.replace(/author">[^<]+/, 'author">'+lastAttach.author.name+', '+ + attachDate.replaceAll('/', '-').substring(0, attachDate.lastIndexOf(':')) + ); + // add the new attachment to the attachments list + attachTable.append(lastRowHtml); + // increment the number of attachments + var numAttachs = $("fieldset legend"); + numAttachs.text(numAttachs.text().replace(/\d+/, function(match) { + return Number(match)+1; + })); + } + } + }); + } + /** * Fix for `{{fnlist}}` duplication with the `redmine_wiki_extensions` plugin. */ @@ -364,7 +455,7 @@ function editDiagram(image, resource, isDmsf, pageName, originalName) { } /** - * Fix for Wiki Extensions header page. + * Fix for Wiki Extensions footer page. */ function fixWikiExtensionsFooter(value) { return value.replace(/\n\n