Commit ee45f9287fd8acfe03d36f3218151d733778caa7

Authored by Rodrigo Souto
1 parent efd10d8d

[media-panel-improvements] Published media folder selection filter

app/controllers/my_profile/cms_controller.rb
@@ -329,6 +329,13 @@ class CmsController < MyProfileController @@ -329,6 +329,13 @@ class CmsController < MyProfileController
329 #render :text => article_list_to_json([file]), :content_type => 'text/plain' 329 #render :text => article_list_to_json([file]), :content_type => 'text/plain'
330 end 330 end
331 331
  332 + def published_media_items
  333 + parent = profile.articles.find(params[:parent_id])
  334 + load_recent_files(parent)
  335 + @published_media_items_id = params[:parent_id]
  336 + render :partial => 'published_media_items'
  337 + end
  338 +
332 protected 339 protected
333 340
334 include CmsHelper 341 include CmsHelper
@@ -425,14 +432,21 @@ class CmsController < MyProfileController @@ -425,14 +432,21 @@ class CmsController < MyProfileController
425 {:images => _('Images'), :generics => _('Files')} 432 {:images => _('Images'), :generics => _('Files')}
426 end 433 end
427 434
428 - def load_recent_files 435 + def load_recent_files(parent = nil)
429 #TODO Since we only have special support for images, I'm limiting myself to 436 #TODO Since we only have special support for images, I'm limiting myself to
430 # consider generic files as non-images. In the future, with more supported 437 # consider generic files as non-images. In the future, with more supported
431 # file types we'll need to have a smart way to fetch from the database 438 # file types we'll need to have a smart way to fetch from the database
432 # scopes of each supported type as well as the non-supported types as a 439 # scopes of each supported type as well as the non-supported types as a
433 # whole. 440 # whole.
434 @recent_files = {} 441 @recent_files = {}
435 - files = profile.files.more_recent 442 + if parent.present?
  443 + files = parent.children.files
  444 + @published_media_items_id = parent.id
  445 + else
  446 + files = profile.files
  447 + @published_media_items_id = 'recent-media'
  448 + end
  449 + files = files.more_recent
436 @recent_files[:images] = files.images.limit(6) 450 @recent_files[:images] = files.images.limit(6)
437 @recent_files[:generics] = files.no_images.limit(6) 451 @recent_files[:generics] = files.no_images.limit(6)
438 end 452 end
app/helpers/forms_helper.rb
@@ -265,7 +265,7 @@ module FormsHelper @@ -265,7 +265,7 @@ module FormsHelper
265 ) 265 )
266 end 266 end
267 267
268 - def select_profile_folder(label_text, field_id, profile, default_value='', html_options = {}, js_options = {}, find_options = {}) 268 + def select_profile_folder(label_text, field_id, profile, default_value='', html_options = {}, js_options = {}, find_options = {}, extra_options = {})
269 if find_options.empty? 269 if find_options.empty?
270 folders = profile.folders 270 folders = profile.folders
271 else 271 else
@@ -276,7 +276,7 @@ module FormsHelper @@ -276,7 +276,7 @@ module FormsHelper
276 select_tag( 276 select_tag(
277 field_id, 277 field_id,
278 options_for_select( 278 options_for_select(
279 - [[profile.identifier, '']] + 279 + [[(extra_options[:root_label] || profile.identifier), '']] +
280 folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id.to_s ] }, 280 folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id.to_s ] },
281 default_value.to_s 281 default_value.to_s
282 ), 282 ),
app/models/article.rb
@@ -463,6 +463,7 @@ class Article < ActiveRecord::Base @@ -463,6 +463,7 @@ class Article < ActiveRecord::Base
463 scope :images, :conditions => { :is_image => true } 463 scope :images, :conditions => { :is_image => true }
464 scope :no_images, :conditions => { :is_image => false } 464 scope :no_images, :conditions => { :is_image => false }
465 scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ] 465 scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ]
  466 + scope :files, :conditions => { :type => 'UploadedFile' }
466 scope :with_types, lambda { |types| { :conditions => [ 'articles.type IN (?)', types ] } } 467 scope :with_types, lambda { |types| { :conditions => [ 'articles.type IN (?)', types ] } }
467 468
468 scope :more_popular, :order => 'hits DESC' 469 scope :more_popular, :order => 'hits DESC'
app/models/profile.rb
@@ -200,7 +200,6 @@ class Profile < ActiveRecord::Base @@ -200,7 +200,6 @@ class Profile < ActiveRecord::Base
200 has_many :tasks, :dependent => :destroy, :as => 'target' 200 has_many :tasks, :dependent => :destroy, :as => 'target'
201 201
202 has_many :events, :source => 'articles', :class_name => 'Event', :order => 'start_date' 202 has_many :events, :source => 'articles', :class_name => 'Event', :order => 'start_date'
203 - has_many :files, :source => 'articles', :class_name => 'UploadedFile', :order => 'start_date'  
204 203
205 def find_in_all_tasks(task_id) 204 def find_in_all_tasks(task_id)
206 begin 205 begin
app/views/cms/_published_media_items.html.erb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +<div id='<%= @published_media_items_id %>' class='items'>
  2 + <% file_types.each do |key, header| %>
  3 + <% display = @recent_files[key].present? ? '' : 'none' %>
  4 + <div class='<%= key.to_s %>' style='display: <%= display%>;'>
  5 + <h3><%= header %></h3>
  6 + <% @recent_files[key].each do |file| %>
  7 + <% @file = file %>
  8 + <%= render :partial => "cms/media_panel/#{key.to_s.singularize}" %>
  9 + <% end %>
  10 + </div>
  11 + <% end %>
  12 +</div>
app/views/cms/_text_editor_sidebar.html.erb
@@ -23,20 +23,10 @@ @@ -23,20 +23,10 @@
23 23
24 <div id='published-media' class='text-editor-sidebar-box'> 24 <div id='published-media' class='text-editor-sidebar-box'>
25 <div class='header'><strong><%= _('Published media') %></strong></div> 25 <div class='header'><strong><%= _('Published media') %></strong></div>
  26 + <%= select_profile_folder(nil, :parent_id, profile, 'recent-media', {'data-url' => url_for({:controller => 'cms', :action => 'published_media_items', :profile => profile.identifier})}, {},
  27 + "type='Folder' or type='Gallery'", {:root_label => _('Recent media')}) %>
26 <%= render :partial => 'drag_and_drop_note' %> 28 <%= render :partial => 'drag_and_drop_note' %>
27 - <div class='items'>  
28 - <% file_types.each do |key, label| %>  
29 - <% if @recent_files[key].present? %>  
30 - <div class='<%= key.to_s%>'>  
31 - <h3><%= label %></h3>  
32 - <% @recent_files[key].each do |file| %>  
33 - <% @file = file %>  
34 - <%= render :partial => "cms/media_panel/#{key.to_s.singularize}" %>  
35 - <% end %>  
36 - </div>  
37 - <% end %>  
38 - <% end %>  
39 - </div> 29 + <%= render :partial => 'published_media_items' %>
40 </div> 30 </div>
41 31
42 <div id='media-search-box' class='text-editor-sidebar-box'> 32 <div id='media-search-box' class='text-editor-sidebar-box'>
@@ -64,4 +54,4 @@ @@ -64,4 +54,4 @@
64 </div> 54 </div>
65 </script> 55 </script>
66 56
67 -<%= javascript_include_tag 'jquery.fileupload.js', 'tmpl.js', 'media-upload.js' %> 57 +<%= javascript_include_tag 'jquery.fileupload.js', 'tmpl.js', 'media-panel.js' %>
app/views/cms/media_upload.js.erb
1 <% if @file.valid? %> 1 <% if @file.valid? %>
2 <% klass = @file.class.name.split('::').last.to_css_class %> 2 <% klass = @file.class.name.split('::').last.to_css_class %>
3 - jQuery("#published-media .items .<%= klass.pluralize %> h3").after("<%= j render :partial => "cms/media_panel/#{klass}" %>");  
4 - jQuery("#published-media .items .<%= klass.pluralize %>").show(); 3 + jQuery("#published-media #recent-media .<%= klass.pluralize %> h3").after("<%= j render :partial => "cms/media_panel/#{klass}" %>");
  4 + jQuery("#published-media #recent-media .<%= klass.pluralize %>").show();
5 <% else %> 5 <% else %>
6 alert("Failed to upload file: <%= j @file.errors.full_messages.join(', ').html_safe %>"); 6 alert("Failed to upload file: <%= j @file.errors.full_messages.join(', ').html_safe %>");
7 <% end %> 7 <% end %>
public/javascripts/media-panel.js 0 → 100644
@@ -0,0 +1,43 @@ @@ -0,0 +1,43 @@
  1 +jQuery('#file').fileupload({
  2 + add: function(e, data){
  3 + data.context = jQuery(tmpl("template-upload", data.files[0]));
  4 + jQuery('#media-upload-form').append(data.context);
  5 + data.submit();
  6 + },
  7 + progress: function (e, data) {
  8 + if (data.context) {
  9 + progress = parseInt(data.loaded / data.total * 100, 10);
  10 + data.context.find('.bar').css('width', progress + '%');
  11 + data.context.find('.percentage').text(progress + '%');
  12 + }
  13 + }
  14 +});
  15 +
  16 +jQuery('#published-media #parent_id').change(function(){
  17 + value = jQuery(this).val();
  18 + if(value == '')
  19 + value = 'recent-media'
  20 + selector = '#published-media #'+value
  21 +
  22 + if (jQuery(selector).length > 0){
  23 + jQuery('#published-media .items').hide();
  24 + jQuery(selector).show();
  25 + } else {
  26 + jQuery('#published-media').addClass('fetching');
  27 + url = jQuery(this).data('url');
  28 + jQuery.ajax({
  29 + url: url,
  30 + data: {"parent_id":value},
  31 + dataType: 'html',
  32 + success: function(response) {
  33 + jQuery('#published-media .items').hide();
  34 + jQuery("#published-media").append('<div id="'+ value +'" class="items">' + response + '</div>');
  35 + jQuery('#published-media').removeClass('fetching');
  36 + },
  37 + error: function(response, textStatus, xhr) {
  38 + console.log(response);
  39 + console.log(textStatus);
  40 + }
  41 + });
  42 + }
  43 +});
public/javascripts/media-upload.js
@@ -1,14 +0,0 @@ @@ -1,14 +0,0 @@
1 -jQuery('#file').fileupload({  
2 - add: function(e, data){  
3 - data.context = jQuery(tmpl("template-upload", data.files[0]));  
4 - jQuery('#media-upload-form').append(data.context);  
5 - data.submit();  
6 - },  
7 - progress: function (e, data) {  
8 - if (data.context) {  
9 - progress = parseInt(data.loaded / data.total * 100, 10);  
10 - data.context.find('.bar').css('width', progress + '%');  
11 - data.context.find('.percentage').text(progress + '%');  
12 - }  
13 - }  
14 -});