diff --git a/app/controllers/dmsf_upload_controller.rb b/app/controllers/dmsf_upload_controller.rb index 1690e00b..d243d15c 100644 --- a/app/controllers/dmsf_upload_controller.rb +++ b/app/controllers/dmsf_upload_controller.rb @@ -41,13 +41,25 @@ class DmsfUploadController < ApplicationController def upload_files uploaded_files = params[:dmsf_attachments] - @uploads = [] - # standard file input uploads - uploaded_files&.each do |_, uploaded_file| - upload = DmsfUpload.create_from_uploaded_attachment(@project, @folder, uploaded_file) - @uploads.push(upload) if upload + # Commit + if params[:commit] == l(:label_dmsf_commit) + uploaded_files&.each do |key, uploaded_file| + upload = DmsfUpload.create_from_uploaded_attachment(@project, @folder, uploaded_file) + params[:committed_files][key][:disk_filename] = upload.disk_filename + params[:committed_files][key][:digest] = upload.digest + params[:committed_files][key][:tempfile_path] = upload.tempfile_path + end + commit_files + # Upload + else + @uploads = [] + # standard file input uploads + uploaded_files&.each do |_, uploaded_file| + upload = DmsfUpload.create_from_uploaded_attachment(@project, @folder, uploaded_file) + @uploads.push(upload) if upload + end + flash.now[:error] = "#{l(:label_attachment)} #{l('activerecord.errors.messages.invalid')}" if @uploads.empty? end - flash.now[:error] = "#{l(:label_attachment)} #{l('activerecord.errors.messages.invalid')}" if @uploads.empty? end # REST API and Redmine attachment form diff --git a/app/views/dmsf_upload/_form.html.erb b/app/views/dmsf_upload/_form.html.erb index b019bab1..a3fb198e 100644 --- a/app/views/dmsf_upload/_form.html.erb +++ b/app/views/dmsf_upload/_form.html.erb @@ -68,6 +68,21 @@ + <% + if defined?(container) && container + project = container.project + folder = container.system_folder + else + project = @project + folder = @folder + end + project_or_folder = folder ? folder : project + files = [] + project_or_folder.dmsf_files.visible.each do |dmsf_file| + rev = dmsf_file.last_revision + files << [dmsf_file.name, rev.major_version, rev.minor_version, rev.patch_version ] if rev + end + %> <%= file_field_tag 'dmsf_attachments[dummy][file]', id: nil, class: 'file_selector', @@ -80,17 +95,19 @@ max_concurrent_uploads: Redmine::Configuration['max_concurrent_ajax_uploads'].to_i, upload_path: dmsf_uploads_path(format: 'js'), description_placeholder: l(:label_optional_description), - project: @project ? "#{@project.identifier}" : '', + project: project.identifier, awf: awf, - dmsf_file_details_form: container ? controller.send( :render_to_string, + dmsf_file_details_form: controller.send(:render_to_string, { partial: 'dmsf_upload/upload_file', - locals: { upload: DmsfUpload.new(container.project, nil, nil), i: 0 } }) : nil - } %> + locals: { upload: DmsfUpload.new(project, folder, nil), i: 0 } }), + files: JSON.generate(files) + } + %> <% if defined?(container) && container %> <%= link_to l(:label_link_from), - new_dmsf_link_path(project_id: container.project.id, type: 'link_from', container: container.class.name), + new_dmsf_link_path(project_id: project.id, type: 'link_from', container: container.class.name), title: l(:title_create_link), class: 'icon icon-add file_selector', remote: true %> <% end %> diff --git a/app/views/dmsf_upload/multi_upload.html.erb b/app/views/dmsf_upload/multi_upload.html.erb index a1de735b..d336d4be 100644 --- a/app/views/dmsf_upload/multi_upload.html.erb +++ b/app/views/dmsf_upload/multi_upload.html.erb @@ -18,6 +18,8 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. %> +<% heads_for_wiki_formatter %> + <%= render partial: '/dmsf/path', locals: { folder: @folder, filename: nil, title: l(:label_attachment_new) } %> @@ -25,13 +27,14 @@ <%= form_tag({ controller: 'dmsf_upload', action: 'upload_files', id: @project, folder_id: @folder }, { id: 'uploadform', multipart: true }) do %> -
- - <%= render partial: 'dmsf_upload/form', - locals: { multiple: true, container: nil, description: true, awf: false } %> - -
+ + <%= render partial: 'dmsf_upload/form', + locals: { multiple: true, container: nil, description: true, awf: false } %> +
- <%= submit_tag l(:label_upload), data: { cy: 'button__submit--dmsf-upload--project' }, class: 'button-positive' %> + <%= submit_tag l(:label_upload), data: { cy: 'button__submit--dmsf-upload--project' }, class: 'button-positive', + id: "dmsf-upload-button" %> + <%= submit_tag l(:label_dmsf_commit), data: { cy: 'button__submit--dmsf-commit--project' }, + class: 'button-positive' %>
<% end %> diff --git a/app/views/dmsf_upload/upload_files.html.erb b/app/views/dmsf_upload/upload_files.html.erb index fb7bc0f9..f9940a74 100644 --- a/app/views/dmsf_upload/upload_files.html.erb +++ b/app/views/dmsf_upload/upload_files.html.erb @@ -42,7 +42,7 @@ <% end %>
<%= submit_tag l(:label_dmsf_commit), - data: { cy: 'button__submit__upload-file--project' }, + data: { cy: 'button__submit__commit-file--project' }, class: 'button-positive', onclick: "$('#ajax-indicator').show();" %>
diff --git a/assets/javascripts/attachments_dmsf.js b/assets/javascripts/attachments_dmsf.js index 1739b2f5..7ac8b73c 100644 --- a/assets/javascripts/attachments_dmsf.js +++ b/assets/javascripts/attachments_dmsf.js @@ -60,6 +60,63 @@ function humanFileSize(bytes) { return (u ? bytes.toFixed(2) + ' ' : bytes) + ' KMGTPEZY'[u] + 'B'; } +/* Increase version */ +function increaseVersion(version, max) { + let res; + if (version >= 0) { + if ((version + 1) < max) { + res = ++version; + } else { + res = version; + } + } else { + if (-(version - 1) < 90 /* 'Z' */) { + res = --version; + } else + res = version; + } + if (res < 0) { + res = String.fromCharCode(-res); // -65 => 'A' + } + return res; +} + +/* Get next version */ +function getNextVersion(filename, files) { + for(let i = 0; i < files.length; i++) { + if(filename === files[i][0]) { + if(files[i][3] && (files[i][3] >= 0)) { + return [files[i][1], files[i][2], increaseVersion(files[i][3], 1000)]; + } + if(files[i][2] && (files[i][2] >= 0)) { + return [files[i][1], increaseVersion(files[i][2], 1000), null]; + } + return [increaseVersion(files[i][1], 100), null, null]; + } + } + return [0, 1, null]; +} + +/* Replace selected version */ +function replaceVersion(detailsForm, attachmentId, name, version) { + let index = detailsForm.search('id="committed_files_' + attachmentId + '_version_' + name + '"'); + if (index != -1) { + let str = detailsForm.substring(index); + // Remove the original selection + str = str.replace('selected="selected" ', ''); + // Select new version + if (version != null) { + str = str.replace('', ''); + } + else { + let c = String.fromCharCode(160); //   + str = str.replace('', ''); + } + detailsForm = detailsForm.substring(0, index) + str; + } + return detailsForm; +} + function dmsfAddFile(inputEl, file, eagerUpload) { let attachments = $('#dmsf_attachments_fields'); @@ -90,36 +147,44 @@ function dmsfAddFile(inputEl, file, eagerUpload) { // Details let detailsForm = $(inputEl).data('dmsf-file-details-form'); - if(detailsForm) { - let detailsDiv = $('
').attr({id: 'dmsf_attachments_details_' + attachmentId}); - let detailsArrow = $(''); - 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(); + let detailsDiv = $('
').attr({id: 'dmsf_attachments_details_' + attachmentId}); + let detailsArrow = $(''); - fileSpan.append(detailsArrow) - attachments.append(fileSpan); - attachments.append(detailsDiv); - } - else { - attachments.append(fileSpan); - } + 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');" + + "$('#dmsf-upload-button').hide();" + + "return false;" + }); + // Index + detailsForm = detailsForm.replace(/\[0\]/g, '[' + attachmentId + ']'); + detailsForm = detailsForm.replace(/_0/g, '_' + attachmentId); + // Name + detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_name" value=""', + 'id="committed_files_' + attachmentId + '_name" value="' + file.name + '"'); + // Title + detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_title"', + 'id="committed_files_' + attachmentId + '_title" value = "' + filenameToTitle(file.name) + '"'); + // Size + detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_size"', + 'id="committed_files_' + attachmentId + '_size" value = "' + humanFileSize(file.size) + '"'); + // Mime type + detailsForm = detailsForm.replace('id="committed_files_' + attachmentId + '_mime_type"', + 'id="committed_files_' + attachmentId + '_mime_type" value = "' + file.type + '"'); + // Version + let version = getNextVersion(file.name, $(inputEl).data('files')); + detailsForm = replaceVersion(detailsForm, attachmentId, 'patch', version[2]); + detailsForm = replaceVersion(detailsForm, attachmentId, 'minor', version[1]); + detailsForm = replaceVersion(detailsForm, attachmentId, 'major', version[0]); + + detailsDiv.append(detailsForm); + detailsDiv.hide(); + + fileSpan.append(detailsArrow) + attachments.append(fileSpan); + attachments.append(detailsDiv); } else{ fileSpan.append(iconDel.click(dmsfRemoveFileLbl));