diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-12-10 20:09:41 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-12-10 20:09:41 +0000 |
commit | ef25210aca9278e51f81bd15e85a3143c667ff17 (patch) | |
tree | 312d20360571770a231f4fd307c12d0c535d60f2 /app | |
parent | 2304f5d42c2bb1829b1cf9055c2848db116742d3 (diff) | |
download | redmine-ef25210aca9278e51f81bd15e85a3143c667ff17.tar.gz redmine-ef25210aca9278e51f81bd15e85a3143c667ff17.zip |
Merged ajax_upload branch (#3957).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10977 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/application_controller.rb | 10 | ||||
-rw-r--r-- | app/controllers/attachments_controller.rb | 32 | ||||
-rw-r--r-- | app/controllers/messages_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/previews_controller.rb | 4 | ||||
-rw-r--r-- | app/controllers/wiki_controller.rb | 3 | ||||
-rw-r--r-- | app/helpers/application_helper.rb | 5 | ||||
-rw-r--r-- | app/models/attachment.rb | 12 | ||||
-rw-r--r-- | app/views/attachments/_form.html.erb | 34 | ||||
-rw-r--r-- | app/views/attachments/destroy.js.erb | 1 | ||||
-rw-r--r-- | app/views/attachments/upload.js.erb | 9 | ||||
-rw-r--r-- | app/views/common/_preview.html.erb | 2 | ||||
-rw-r--r-- | app/views/previews/issue.html.erb | 4 |
12 files changed, 83 insertions, 35 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 585ac5788..a4c804fd8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -300,6 +300,16 @@ class ApplicationController < ActionController::Base render_404 end + def find_attachments + if (attachments = params[:attachments]).present? + att = attachments.values.collect do |attachment| + Attachment.find_by_token( attachment[:token] ) if attachment[:token].present? + end + att.compact! + end + @attachments = att || [] + end + # make sure that the user is a member of the project (or admin) if project is private # used as a before_filter for actions that do not require any particular permission on the project def check_project_privacy diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 13871c323..53e0fd6d4 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -85,15 +85,17 @@ class AttachmentsController < ApplicationController @attachment = Attachment.new(:file => request.raw_post) @attachment.author = User.current @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) + saved = @attachment.save - if @attachment.save - respond_to do |format| - format.api { render :action => 'upload', :status => :created } - end - else - respond_to do |format| - format.api { render_validation_errors(@attachment) } - end + respond_to do |format| + format.js + format.api { + if saved + render :action => 'upload', :status => :created + else + render_validation_errors(@attachment) + end + } end end @@ -101,9 +103,17 @@ class AttachmentsController < ApplicationController if @attachment.container.respond_to?(:init_journal) @attachment.container.init_journal(User.current) end - # Make sure association callbacks are called - @attachment.container.attachments.delete(@attachment) - redirect_to_referer_or project_path(@project) + if @attachment.container + # Make sure association callbacks are called + @attachment.container.attachments.delete(@attachment) + else + @attachment.destroy + end + + respond_to do |format| + format.html { redirect_to_referer_or project_path(@project) } + format.js + end end private diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 16e3b7c45..82b74dda5 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -19,6 +19,7 @@ class MessagesController < ApplicationController menu_item :boards default_search_scope :messages before_filter :find_board, :only => [:new, :preview] + before_filter :find_attachments, :only => [:preview] before_filter :find_message, :except => [:new, :preview] before_filter :authorize, :except => [:preview, :edit, :destroy] @@ -117,7 +118,6 @@ class MessagesController < ApplicationController def preview message = @board.messages.find_by_id(params[:id]) - @attachements = message.attachments if message @text = (params[:message] || params[:reply])[:content] @previewed = message render :partial => 'common/preview' diff --git a/app/controllers/previews_controller.rb b/app/controllers/previews_controller.rb index 9594a9fe1..4ea945c64 100644 --- a/app/controllers/previews_controller.rb +++ b/app/controllers/previews_controller.rb @@ -16,12 +16,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class PreviewsController < ApplicationController - before_filter :find_project + before_filter :find_project, :find_attachments def issue @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank? if @issue - @attachements = @issue.attachments @description = params[:issue] && params[:issue][:description] if @description && @description.gsub(/(\r?\n|\n\r?)/, "\n") == @issue.description.to_s.gsub(/(\r?\n|\n\r?)/, "\n") @description = nil @@ -37,7 +36,6 @@ class PreviewsController < ApplicationController def news if params[:id].present? && news = News.visible.find_by_id(params[:id]) @previewed = news - @attachments = news.attachments end @text = (params[:news] ? params[:news][:description] : nil) render :partial => 'common/preview' diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index fd7076024..115f61157 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -37,6 +37,7 @@ class WikiController < ApplicationController before_filter :find_existing_or_new_page, :only => [:show, :edit, :update] before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version] accept_api_auth :index, :show, :update, :destroy + before_filter :find_attachments, :only => [:preview] helper :attachments include AttachmentsHelper @@ -293,7 +294,7 @@ class WikiController < ApplicationController # page is nil when previewing a new page return render_403 unless page.nil? || editable?(page) if page - @attachements = page.attachments + @attachments += page.attachments @previewed = page.content end @text = params[:content][:text] diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8173bdb44..1a0c9ea5f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -597,8 +597,9 @@ module ApplicationHelper def parse_inline_attachments(text, project, obj, attr, only_path, options) # when using an image link, try to use an attachment, if possible - if options[:attachments] || (obj && obj.respond_to?(:attachments)) - attachments = options[:attachments] || obj.attachments + if options[:attachments].present? || (obj && obj.respond_to?(:attachments)) + attachments = options[:attachments] || [] + attachments += obj.attachments if obj text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| filename, ext, alt, alttext = $1.downcase, $2, $3, $4 # search for the picture in attachments diff --git a/app/models/attachment.rb b/app/models/attachment.rb index a1c232d16..6fa079c91 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -154,11 +154,19 @@ class Attachment < ActiveRecord::Base end def visible?(user=User.current) - container && container.attachments_visible?(user) + if container_id + container && container.attachments_visible?(user) + else + author == user + end end def deletable?(user=User.current) - container && container.attachments_deletable?(user) + if container_id + container && container.attachments_deletable?(user) + else + author == user + end end def image? diff --git a/app/views/attachments/_form.html.erb b/app/views/attachments/_form.html.erb index e47c288a9..092f68244 100644 --- a/app/views/attachments/_form.html.erb +++ b/app/views/attachments/_form.html.erb @@ -1,18 +1,28 @@ +<span id="attachments_fields"> <% if defined?(container) && container && container.saved_attachments %> <% container.saved_attachments.each_with_index do |attachment, i| %> - <span class="icon icon-attachment" style="display:block; line-height:1.5em;"> - <%= h(attachment.filename) %> (<%= number_to_human_size(attachment.filesize) %>) - <%= hidden_field_tag "attachments[p#{i}][token]", "#{attachment.id}.#{attachment.digest}" %> + <span id="attachments_p<%= i %>"> + <%= text_field_tag("attachments[p#{i}][filename]", attachment.filename, :class => 'filename') + + text_field_tag("attachments[p#{i}][description]", attachment.description, :maxlength => 255, :placeholder => l(:label_optional_description), :class => 'description') + + link_to(' '.html_safe, attachment_path(attachment, :attachment_id => "p#{i}", :format => 'js'), :method => 'delete', :remote => true, :class => 'remove-upload') %> + <%= hidden_field_tag "attachments[p#{i}][token]", "#{attachment.token}" %> </span> <% end %> <% end %> -<span id="attachments_fields"> - <span> - <%= file_field_tag 'attachments[1][file]', :id => nil, :class => 'file', - :onchange => "checkFileSize(this, #{Setting.attachment_max_size.to_i.kilobytes}, '#{escape_javascript(l(:error_attachment_too_big, :max_size => number_to_human_size(Setting.attachment_max_size.to_i.kilobytes)))}');" -%> - <%= text_field_tag 'attachments[1][description]', '', :id => nil, :class => 'description', :maxlength => 255, :placeholder => l(:label_optional_description) %> - <%= link_to_function(image_tag('delete.png'), 'removeFileField(this)', :title => (l(:button_delete))) %> - </span> </span> -<span class="add_attachment"><%= link_to l(:label_add_another_file), '#', :onclick => 'addFileField(); return false;', :class => 'add_attachment' %> -(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)</span> +<span class="add_attachment"> +<%= file_field_tag 'attachments_files', + :id => nil, + :multiple => true, + :onchange => 'addInputFiles(this);', + :data => { + :max_file_size => Setting.attachment_max_size.to_i.kilobytes, + :max_file_size_message => l(:error_attachment_too_big, :max_size => number_to_human_size(Setting.attachment_max_size.to_i.kilobytes)), + :max_concurrent_uploads => Redmine::Configuration['max_concurrent_ajax_uploads'].to_i, + :upload_path => uploads_path(:format => 'js'), + :description_placeholder => l(:label_optional_description) + } %> +(<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>) +</span> + +<%= javascript_include_tag 'attachments' %> diff --git a/app/views/attachments/destroy.js.erb b/app/views/attachments/destroy.js.erb new file mode 100644 index 000000000..3cfb5845f --- /dev/null +++ b/app/views/attachments/destroy.js.erb @@ -0,0 +1 @@ +$('#attachments_<%= j params[:attachment_id] %>').remove(); diff --git a/app/views/attachments/upload.js.erb b/app/views/attachments/upload.js.erb new file mode 100644 index 000000000..04e30b569 --- /dev/null +++ b/app/views/attachments/upload.js.erb @@ -0,0 +1,9 @@ +var fileSpan = $('#attachments_<%= j params[:attachment_id] %>'); +$('<input>', { type: 'hidden', name: 'attachments[<%= j params[:attachment_id] %>][token]' } ).val('<%= j @attachment.token %>').appendTo(fileSpan); +fileSpan.find('a.remove-upload') + .attr({ + "data-remote": true, + "data-method": 'delete', + href: '<%= j attachment_path(@attachment, :attachment_id => params[:attachment_id], :format => 'js') %>' + }) + .off('click'); diff --git a/app/views/common/_preview.html.erb b/app/views/common/_preview.html.erb index fd95f1188..90d83ce8c 100644 --- a/app/views/common/_preview.html.erb +++ b/app/views/common/_preview.html.erb @@ -1,3 +1,3 @@ <fieldset class="preview"><legend><%= l(:label_preview) %></legend> -<%= textilizable @text, :attachments => @attachements, :object => @previewed %> +<%= textilizable @text, :attachments => @attachments, :object => @previewed %> </fieldset> diff --git a/app/views/previews/issue.html.erb b/app/views/previews/issue.html.erb index 60ad2a0ad..a88bec6fc 100644 --- a/app/views/previews/issue.html.erb +++ b/app/views/previews/issue.html.erb @@ -1,11 +1,11 @@ <% if @notes %> <fieldset class="preview"><legend><%= l(:field_notes) %></legend> - <%= textilizable @notes, :attachments => @attachements, :object => @issue %> + <%= textilizable @notes, :attachments => @attachments, :object => @issue %> </fieldset> <% end %> <% if @description %> <fieldset class="preview"><legend><%= l(:field_description) %></legend> - <%= textilizable @description, :attachments => @attachements, :object => @issue %> + <%= textilizable @description, :attachments => @attachments, :object => @issue %> </fieldset> <% end %> |