From 375bb2ab33eaba807ff3c894f87dfc19cb7de471 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 23 May 2012 08:19:43 +0200 Subject: [PATCH] Fix Pagination and sorting in filter widget --- .../plugins/core/widgets/filter.html.erb | 2 +- .../app/controllers/filters_controller.rb | 9 +- .../WEB-INF/app/helpers/filters_helper.rb | 85 ++++++++++--------- .../WEB-INF/app/views/filters/_list.html.erb | 39 +++++++-- 4 files changed, 86 insertions(+), 49 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/filter.html.erb b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/filter.html.erb index cecb95ce2eb..10cf311564e 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/filter.html.erb +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/filter.html.erb @@ -3,4 +3,4 @@ @filter_context=Filters.execute(@filter, self, params) %> -<%= render :partial => "filters/#{@filter.default_view}", :locals => {:edit_mode => false} %> +<%= render :partial => "filters/#{@filter.default_view}", :locals => {:edit_mode => false, :widget => widget} %> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb index f06a27a086e..bdfd14524a6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/filters_controller.rb @@ -25,7 +25,7 @@ class FiltersController < ApplicationController SECTION=Navigation::SECTION_CONFIGURATION verify :method => :post, :only => [:create, :delete, :up, :down, :activate, :deactivate, :up_column, :down_column, :add_column, :delete_column, :set_sorted_column, :set_view, :set_columns, :set_page_size], :redirect_to => {:action => :index} - before_filter :login_required, :except => %w(index treemap) + before_filter :login_required, :except => %w(index treemap list) def manage if is_admin? @@ -36,6 +36,13 @@ class FiltersController < ApplicationController @filters.sort! { |a,b| a.name.downcase <=> b.name.downcase } end + def list + @filter=::Filter.find(params[:id]) + @filter_context=Filters.execute(@filter, self, params) + + render :partial => 'list', :locals => {:edit_mode => params[:edit_mode], :update_id => params[:update_id]} + end + def new @filter=::Filter.new() @filter.criteria< {:asc => (!(column.ascending?)).to_s, :sort => column.id}) - else - html=h(column.display_name) - end - if column.variation - html=" #{html}" - end + def goto_page(msg, filter, update_id, override={}) + link_to_remote msg, {:update => "filter-#{update_id}", :url => params.merge({:controller => :filters, :action => :list, :id => filter.id, :edit_mode => @edit_mode, :update_id => update_id}.merge(override))} + end + + def column_title(column, filter, update_id) + if column.sortable? + html=goto_page(h(column.display_name), filter, update_id, {:asc => (!(column.ascending?)).to_s, :sort => column.id}) + else + html=h(column.display_name) + end + if column.variation + html=" #{html}" + end - if filter.sorted_column==column - html << (column.ascending? ? image_tag("asc12.png") : image_tag("desc12.png")) - end - html + if filter.sorted_column==column + html << (column.ascending? ? image_tag("asc12.png") : image_tag("desc12.png")) + end + html end def column_align(column) - (column.on_name? || column.on_key?) ? 'left' : 'right' + (column.on_name? || column.on_key?) ? 'left' : 'right' end def treemap_metrics(filter) - metrics=filter.measure_columns.map{|col| col.metric} - size_metric=(metrics.size>=1 ? metrics[0] : Metric.by_key('ncloc')) - color_metric=nil - if metrics.size>=2 - color_metric=metrics[1] - end - if color_metric.nil? || !color_metric.treemap_color? - color_metric=Metric.by_key('violations_density') - end - [size_metric, color_metric] + metrics=filter.measure_columns.map { |col| col.metric } + size_metric=(metrics.size>=1 ? metrics[0] : Metric.by_key('ncloc')) + color_metric=nil + if metrics.size>=2 + color_metric=metrics[1] + end + if color_metric.nil? || !color_metric.treemap_color? + color_metric=Metric.by_key('violations_density') + end + [size_metric, color_metric] end def period_names - p1=Property.value('sonar.timemachine.period1', nil, 'previous_analysis') - p2=Property.value('sonar.timemachine.period2', nil, '5') - p3=Property.value('sonar.timemachine.period3', nil, '30') - [period_name(p1), period_name(p2), period_name(p3)] + p1=Property.value('sonar.timemachine.period1', nil, 'previous_analysis') + p2=Property.value('sonar.timemachine.period2', nil, '5') + p3=Property.value('sonar.timemachine.period3', nil, '30') + [period_name(p1), period_name(p2), period_name(p3)] end private def period_name(property) - if property=='previous_analysis' - message('delta_since_previous_analysis') - elsif property =~ /^[\d]+(\.[\d]+){0,1}$/ - # is integer - message('delta_over_x_days', :params => property) - elsif property =~ /\d{4}-\d{2}-\d{2}/ - message('delta_since', :params => property) - elsif !property.blank? - message('delta_since_version', :params => property) - else - nil - end + if property=='previous_analysis' + message('delta_since_previous_analysis') + elsif property =~ /^[\d]+(\.[\d]+){0,1}$/ + # is integer + message('delta_over_x_days', :params => property) + elsif property =~ /\d{4}-\d{2}-\d{2}/ + message('delta_since', :params => property) + elsif !property.blank? + message('delta_since_version', :params => property) + else + nil + end end + end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/filters/_list.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/filters/_list.html.erb index 679e9c5c9fe..4eb53431780 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/filters/_list.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/filters/_list.html.erb @@ -1,12 +1,21 @@ <% filter=@filter_context.filter %> -
+<% @edit_mode=edit_mode %> + +<% unless defined? update_id %> + <% if defined? widget %> + <% update_id=widget.id %> + <% else %> + <% end %> +<% end %> + +
<% filter.columns.each do |column| %> <% end %> @@ -49,15 +58,31 @@ %> | <% if max_pages > 20 && start_page > 1 %> - <%= link_to message('paging_first'), {:overwrite_params => {:page_id => 1}} -%> + <%= goto_page message('paging_previous'), filter, update_id, :page_id => 1 %> + <% end %> + + <% if current_page>start_page %> + <%= goto_page message('paging_previous'), filter, update_id, :page_id => current_page-1 %> + <% else %> + <%= message('paging_previous') -%> <% end %> - <%= link_to_if current_page>start_page, message('paging_previous'), {:overwrite_params => {:page_id => current_page-1}} %> + <% for index in start_page..end_page %> - <%= link_to_unless index==current_page, index.to_s, {:overwrite_params => {:page_id => index}} %> + <% unless index==current_page %> + <%= goto_page index.to_s, filter, update_id, :page_id => index %> + <% else %> + <%= index.to_s -%> + <% end %> <% end %> - <%= link_to_if current_page {:page_id => 1+current_page}} %> + + <% if current_page + <%= goto_page message('paging_next'), filter, update_id, :page_id => current_page+1 %> + <% else %> + <%= message('paging_next') -%> + <% end %> + <% if max_pages > 20 && end_page < max_pages %> - <%= link_to message('paging_last'), {:overwrite_params => {:page_id => max_pages}} -%> + <%= goto_page message('paging_last'), filter, update_id, :page_id => max_pages %> <% end %> <% end %> -- 2.39.5
class="<%= column_align(column) -%>"> - <%= column_title(column, filter) %> + <%= column_title(column, filter, update_id) %>