Skip to content

Commit

Permalink
#1548 Issue attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
picman committed Sep 18, 2024
1 parent 62bfc18 commit e0532a8
Show file tree
Hide file tree
Showing 26 changed files with 220 additions and 115 deletions.
24 changes: 23 additions & 1 deletion app/models/dmsf_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,29 @@ def self.create_from_uploaded_attachment(project, folder, uploaded_file)
end
end

def initialize(project, folder, uploaded)
def initialize(project, folder = nil, uploaded = nil)
unless uploaded
@name = ''
@disk_filename = ''
@mime_type = ''
@size = 0
@tempfile_path = ''
@token = ''
@digest = ''
if Setting.plugin_redmine_dmsf['empty_minor_version_by_default']
@major_version = 1
@minor_version = nil
else
@major_version = 0
@minor_version = 0
end
@patch_version = nil
@workflow = nil
revision = DmsfFileRevision.new
@custom_values = revision.custom_field_values
return
end

@name = uploaded[:original_filename]

file = DmsfFile.find_file_by_name(project, folder, @name)
Expand Down
6 changes: 4 additions & 2 deletions app/views/dmsf_upload/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@
upload_path: dmsf_uploads_path(format: 'js'),
description_placeholder: l(:label_optional_description),
project: @project ? "#{@project.identifier}" : '',
description: description,
awf: awf
awf: awf,
dmsf_file_details_form: container ? controller.send( :render_to_string,
{ partial: 'dmsf_upload/upload_file',
locals: { upload: DmsfUpload.new(container.project, nil, nil), i: 0 } }) : nil
} %>
</span>
<% if defined?(container) && container %>
Expand Down
17 changes: 7 additions & 10 deletions app/views/dmsf_upload/_upload_file.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@
<div class="box tabular dmfs-box-tabular">
<%= hidden_field_tag "committed_files[#{i}][disk_filename]", upload.disk_filename %>
<%= hidden_field_tag "committed_files[#{i}][token]", upload.token %>
<%= hidden_field_tag "committed_files[#{i}][size]", upload.size %>
<%= hidden_field_tag "committed_files[#{i}][mime_type]", upload.mime_type %>
<%= hidden_field_tag "committed_files[#{i}][digest]", upload.digest %>
<div class="splitcontent">
<div class="splitcontentleft">
<p>
<%= label_tag "committed_files[#{i}][title]", l(:label_title) %>
<%= text_field_tag "committed_files[#{i}][title]", upload.title, required:true %>
<%= text_field_tag "committed_files[#{i}][title]", upload.title, required: true %>
</p>
</div>
<div class="splitcontentright">
<p>
<%= label_tag '', l(:label_filename) %>
<%= text_field_tag :name, h(upload.name), readonly: true %>
<%= hidden_field_tag "committed_files[#{i}][name]", upload.name %>
<%= label_tag "committed_files[#{i}][name]", l(:label_filename) %>
<%= text_field_tag "committed_files[#{i}][name]", upload.name, readonly: true %>
</p>
</div>
</div>
Expand All @@ -55,12 +52,12 @@
revision_or_upload: upload } %>
</p>
<p>
<%= label_tag '', l(:label_mime) %>
<%= text_field_tag :name, h(upload.mime_type), readonly: true %>
<%= label_tag "committed_files[#{i}][mime_type]", l(:label_mime) %>
<%= text_field_tag "committed_files[#{i}][mime_type]", h(upload.mime_type), readonly: true %>
</p>
<p>
<%= label_tag '', l(:label_size) %>
<%= text_field_tag :name, number_to_human_size(upload.size), readonly: true %>
<%= label_tag "committed_files[#{i}][size]", l(:label_size) %>
<%= text_field_tag "committed_files[#{i}][size]", number_to_human_size(upload.size), readonly: true %>
</p>
</div>
<div class="splitcontentright">
Expand Down
2 changes: 1 addition & 1 deletion app/views/dmsf_upload/upload_files.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<% end %>
<% end %>
<div class="form-actions">
<%= submit_tag l(:label_upload),
<%= submit_tag l(:label_dmsf_commit),
data: { cy: 'button__submit__upload-file--project' },
class: 'button-positive',
onclick: "$('#ajax-indicator').show();" %>
Expand Down
59 changes: 48 additions & 11 deletions assets/javascripts/attachments_dmsf.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ function dmsfAddLink(linksSpan, linkId, linkName, title, project, awf) {

dmsfAddLink.nextLinkId = 1000;

/* Remove the extension and replace underscores with spaces, 'after_init.rb' -> 'after init' */
function filenameToTitle(filename) {
return filename.replace(/\.[^/.]+$/, "").replace(/_+/g, " ");
}

/* File size to human readable file size, 1024 -> 1.00 KB */
function humanFileSize(bytes) {
var u = 0, s= 1024;
while (bytes >= s || -bytes >= s) {
bytes /= s;
u++;
}
return (u ? bytes.toFixed(2) + ' ' : bytes) + ' KMGTPEZY'[u] + 'B';
}

function dmsfAddFile(inputEl, file, eagerUpload) {

let attachments = $('#dmsf_attachments_fields');
Expand All @@ -62,16 +77,6 @@ function dmsfAddFile(inputEl, file, eagerUpload) {

if($(inputEl).attr('multiple') == 'multiple') {

if($(inputEl).data('description')) {

let description = $('<input>', {type: 'text', 'class': 'description',
name: 'dmsf_attachments[' + attachmentId + '][description]', maxlength: 255,
placeholder: $(inputEl).data('description-placeholder')
}).toggle(!eagerUpload);

fileSpan.append(description);
}

fileSpan.append(iconDel.click(dmsfRemoveFileLbl));

if($(inputEl).data('awf')) {
Expand All @@ -83,13 +88,45 @@ function dmsfAddFile(inputEl, file, eagerUpload) {
fileSpan.append(iconWf);
}

attachments.append(fileSpan);
// Details
let detailsForm = $(inputEl).data('dmsf-file-details-form');
if(detailsForm) {
let detailsDiv = $('<div>').attr({id: 'dmsf_attachments_details_' + attachmentId});
let detailsArrow = $('<a>');
detailsArrow.attr({href: '#', 'class': 'icon-only icon-sorted-asc', title: 'Details'});
detailsArrow.attr(
{
onclick: "$('#dmsf_attachments_details_" + attachmentId + "').toggle();" +
"$(this).toggleClass('icon-sorted-asc');$(this).toggleClass('icon-sorted-desc');" +
"return false;"
});
detailsForm = detailsForm.replace(/\[0\]/g, '[' + attachmentId + ']');
detailsForm = detailsForm.replace(/_0/g, '_' + attachmentId);
detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_name" value=""',
'id="committed_files_' + attachmentId + '_name" value="' + file.name + '"');
detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_title"',
'id="committed_files_' + attachmentId + '_title" value = "' + filenameToTitle(file.name) + '"');
detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_size"',
'id="committed_files_' + attachmentId + '_size" value = "' + humanFileSize(file.size) + '"');
detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_mime_type"',
'id="committed_files_' + attachmentId + '_mime_type" value = "' + file.type + '"');
detailsDiv.append(detailsForm);
detailsDiv.hide();

fileSpan.append(detailsArrow)
attachments.append(fileSpan);
attachments.append(detailsDiv);
}
else {
attachments.append(fileSpan);
}
}
else{
fileSpan.append(iconDel.click(dmsfRemoveFileLbl));
attachments.append(fileSpan);
$('#dmsf_file_revision_name').val(file.name);
}
attachments.append('<br>');

if(eagerUpload) {
dmsfAjaxUpload(file, attachmentId, fileSpan, inputEl);
Expand Down
Loading

0 comments on commit e0532a8

Please sign in to comment.