From 35ed9868ed703c04cf40b48eff239eef34fdae13 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 12 Dec 2012 10:19:48 +0100 Subject: [PATCH] Refactor the ruby API that adds pagination to tables --- .../controllers/all_projects_controller.rb | 2 +- .../WEB-INF/app/helpers/application_helper.rb | 59 ++++++++++++++++++- .../WEB-INF/app/helpers/measures_helper.rb | 2 +- .../WEB-INF/app/models/measure_filter.rb | 7 ++- .../app/models/measure_filter_display_list.rb | 3 +- .../models/measure_filter_display_treemap.rb | 4 +- .../app/views/all_projects/index.html.erb | 7 ++- .../app/views/measures/_display_list.html.erb | 33 +++++------ .../utils/_tfoot_ajax_pagination.html.erb | 48 --------------- .../views/utils/_tfoot_pagination.html.erb | 13 ++-- 10 files changed, 89 insertions(+), 89 deletions(-) delete mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_ajax_pagination.html.erb diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/all_projects_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/all_projects_controller.rb index 232e2438976..366ae034cc7 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/all_projects_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/all_projects_controller.rb @@ -26,7 +26,7 @@ class AllProjectsController < ApplicationController bad_request("The 'qualifier' parameter is not valid. It must reference a root type.") unless Project.root_qualifiers.include?(@qualifier) @filter = MeasureFilter.new - @filter.criteria={:qualifiers => @qualifier, :sort => 'name', :asc => (params[:asc]!='false')} + @filter.criteria={:qualifiers => @qualifier, :sort => 'name', :asc => (params[:asc]!='false'), :pageSize=>200, :page => params[:page]} @filter.require_links=true @filter.execute(self, :user => current_user) end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 99a440fe75c..6b5798a4620 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -260,7 +260,7 @@ module ApplicationHelper if options[:line] anchor= 'L' + options[:line].to_s end - link_to(name || resource.name, {:controller => 'resource', :action => 'index', :anchor => anchor, :id => resource.id, :period => period_index, :tab => options[:tab], :rule => options[:rule], + link_to(name || resource.name, {:controller => 'resource', :action => 'index', :anchor => anchor, :id => resource.id, :period => period_index, :tab => options[:tab], :rule => options[:rule], :metric => options[:metric], :display_title => true}, :popup => ['resource', 'height=800,width=900,scrollbars=1,resizable=1'], :title => options[:title]) end end @@ -688,7 +688,60 @@ module ApplicationHelper "#{h label}" end - def link_to_function_if(condition, name, *args) - condition ? link_to_function(name, args) : name + # Options : + # :id + # :colspan + # :include_loading_icon - only if :id is set as well + def table_pagination(pagination, options={}, &block) + html = ' [pagination.count]) if pagination.count>0 + + if pagination.pages > 1 + max_pages = pagination.pages + current_page = pagination.page + start_page = 1 + end_page = max_pages + if max_pages > 20 + if current_page < 12 + start_page = 1 + end_page = 20 + elsif current_page > max_pages-10 + start_page = max_pages-20 + end_page = max_pages + else + start_page = current_page-10 + end_page = current_page+9 + end + end + + html += ' | ' + if max_pages > 20 && start_page > 1 + html += (current_page!=1 ? yield(message('paging_first'), 1) : message('paging_first')) + end + html += (pagination.previous? ? yield(message('paging_previous'), current_page-1) : message('paging_previous')) + html += ' ' + for index in start_page..end_page + html += (index != current_page ? yield(index.to_s, index) : index.to_s) + html += ' ' + end + html += (pagination.next? ? yield(message('paging_next'), current_page+1) : message('paging_next')) + html += ' ' + if max_pages > 20 && end_page < max_pages + html += (current_page != max_pages ? yield(message('paging_last'), max_pages) : message('paging_last')) + html += ' ' + end + end + html+= '' + html end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb index 64ccbf731d3..311ec82e9d0 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb @@ -21,7 +21,7 @@ module MeasuresHelper def list_column_html(filter, column, widget_id) if column.sort? - html = link_to_function(h(column.title_label), "sortList#{widget_id}('#{escape_javascript column.key}',#{!filter.sort_asc?})", :title => h(column.tooltip)) + html = link_to_function(h(column.title_label), "refreshList#{widget_id}('#{escape_javascript column.key}',#{!filter.sort_asc?}, #{filter.criteria[:page]||1})", :title => h(column.tooltip)) else html=h(column.title_label) end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb index b7ab2cdd13c..587b4eea749 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb @@ -206,7 +206,7 @@ class MeasureFilter < ActiveRecord::Base private def init_results - @pagination = Api::Pagination.new + @pagination = nil @security_exclusions = nil @rows = nil @base_row = nil @@ -222,8 +222,11 @@ class MeasureFilter < ActiveRecord::Base authorized_project_ids = controller.select_authorized(:user, project_ids) snapshot_ids = rows.map { |row| row.getSnapshotId() if authorized_project_ids.include?(row.getResourceRootId()) }.compact @security_exclusions = (snapshot_ids.size <% end %> - - <%= render :partial => 'utils/tfoot_pagination', :locals => {:pagination => @filter.pagination, :colspan => colspan} %> + + <%= table_pagination(@filter.pagination, :colspan => colspan) { |label, page_id| + link_to(label, :action => 'index', :qualifier => @qualifier, :asc => @filter.criteria[:asc], :page => page_id) + } + -%> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display_list.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display_list.html.erb index 8e9f33549c4..601e3c9330c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display_list.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display_list.html.erb @@ -1,9 +1,13 @@
<% content_for :script do %> <% end @@ -213,13 +207,14 @@ <% end %> - <% pagination_partial = widget_id ? 'utils/tfoot_ajax_pagination' : 'utils/tfoot_pagination' %> - <%= render :partial => pagination_partial, :locals => { - :pagination => filter.pagination, - :colspan => colspan, - :id => "measure_filter_foot#{widget_id}", - :include_loading => true, - :js_function => "pageList#{widget_id}" - } -%> + <% if widget_id %> + <%= table_pagination(filter.pagination, :colspan => colspan, :id => "measure_filter_foot#{widget_id}", :include_loading_icon => true) { |label, page_id| + link_to_function label, "refreshList#{widget_id}('#{filter.criteria[:sort]}', #{filter.criteria[:asc]}, #{page_id})" + } -%> + <% else %> + <%= table_pagination(filter.pagination, :colspan => colspan, :id => "measure_filter_foot#{widget_id}", :include_loading_icon => true) { |label, page_id| + link_to(label, filter.criteria.merge({:page => page_id})) + } -%> + <% end %>
\ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_ajax_pagination.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_ajax_pagination.html.erb deleted file mode 100644 index 2ee7bb04433..00000000000 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_ajax_pagination.html.erb +++ /dev/null @@ -1,48 +0,0 @@ -> - - -
> - <% if pagination.count > 0 %> - <%= message('x_results', :params => [pagination.count]) -%> - <% end %> - <% - if pagination.pages > 1 - max_pages = pagination.pages - current_page = pagination.page - start_page = 1 - end_page = max_pages - if max_pages > 20 - if current_page < 12 - start_page = 1 - end_page = 20 - elsif current_page > max_pages-10 - start_page = max_pages-20 - end_page = max_pages - else - start_page = current_page-10 - end_page = current_page+9 - end - end - %> - | - <% if max_pages > 20 && start_page > 1 %> - <%= link_to_function_if current_page != 1, message('paging_first'), "#{js_function}(1)" -%> - <% end %> - <%= link_to_function_if pagination.previous?, message('paging_previous'), "#{js_function}(#{current_page-1})" -%> - <% for index in start_page..end_page %> - <%= link_to_function_if index != current_page, index.to_s, "#{js_function}(#{index})" -%> - <% end %> - <%= link_to_function_if pagination.next?, message('paging_next'), "#{js_function}(#{current_page+1})" -%> - <% if max_pages > 20 && end_page < max_pages %> - <%= link_to_function_if current_page != max_pages, message('paging_last'), "#{js_function}(#{max_pages})" -%> - <% - end - end - %> -
- <% if id && local_assigns[:include_loading] %> - - <% end %> - - - \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_pagination.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_pagination.html.erb index b35a2976be0..f50dafc2aa1 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_pagination.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_pagination.html.erb @@ -1,7 +1,6 @@ -> + -
> <% if pagination.count > 0 %> <%= message('x_results', :params => [pagination.count]) -%> <% end %> @@ -26,7 +25,7 @@ %> | <% if max_pages > 20 && start_page > 1 %> - <%= link_to_if current_page != 1, message('paging_first'), params.merge(:page => 1) %> + <%= link_to_if current_page != 1, message('paging_first'), params.merge(:page => 1) %> <% end %> <%= link_to_if pagination.previous?, message('paging_previous'), params.merge(:page => current_page-1) %> <% for index in start_page..end_page %> @@ -34,15 +33,11 @@ <% end %> <%= link_to_if pagination.next?, message('paging_next'), params.merge(:page => current_page+1) %> <% if max_pages > 20 && end_page < max_pages %> - <%= link_to_if current_page != max_pages, message('paging_last'), params.merge(:page => max_pages) %> + <%= link_to_if current_page != max_pages, message('paging_last'), params.merge(:page => max_pages) %> <% end %> - <% + <% end %> -
- <% if id && local_assigns[:include_loading] %> - - <% end %> \ No newline at end of file -- 2.39.5