aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-12-12 10:19:48 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-12-12 10:19:48 +0100
commit35ed9868ed703c04cf40b48eff239eef34fdae13 (patch)
tree9e9d0e1fa5261c85317b3a0c6e18fdb435b43748
parent2de6b4a37197ab978c104a87b7a8409bf0591dbd (diff)
downloadsonarqube-35ed9868ed703c04cf40b48eff239eef34fdae13.tar.gz
sonarqube-35ed9868ed703c04cf40b48eff239eef34fdae13.zip
Refactor the ruby API that adds pagination to tables
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/all_projects_controller.rb2
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb59
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/helpers/measures_helper.rb2
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter.rb7
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_list.rb3
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_treemap.rb4
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/all_projects/index.html.erb7
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display_list.html.erb33
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_ajax_pagination.html.erb48
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_pagination.html.erb13
10 files changed, 89 insertions, 89 deletions
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
"<a href='#{url}' modal-width='#{width}' class='open-modal #{clazz}' #{id}>#{h label}</a>"
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 = '<tfoot'
+ html += " id='#{options[:id]}'" if options[:id]
+ html += "><tr><td"
+ html += " colspan='#{options[:colspan]}'" if options[:colspan]
+ html += '>'
+ if options[:include_loading_icon] && options[:id]
+ html += "<img src='#{ApplicationController.root_context}/images/loading-small.gif' style='display: none' id='#{options[:id]}_loading'>"
+ end
+ html += '<div'
+ html += " id='#{options[:id]}_pages'" if options[:id]
+ html += '>'
+ html += message('x_results', :params => [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+= '</div></td></tr></tfoot>'
+ 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<rows.size)
+ @pagination = Api::Pagination.new
+ @pagination.per_page=(criteria(:pageSize)||999999).to_i
+ @pagination.page=(criteria(:page)||1).to_i
@pagination.count = snapshot_ids.size
- snapshot_ids[@pagination.offset ... (@pagination.offset+@pagination.limit)]
+ snapshot_ids[@pagination.offset ... (@pagination.offset+@pagination.limit)] || []
end
def load_results(snapshot_ids)
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_list.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_list.rb
index 87486f132ed..20a64295ace 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_list.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_list.rb
@@ -87,8 +87,7 @@ class MeasureFilterDisplayList < MeasureFilterDisplay
filter.set_criteria_default_value(:sort, 'name')
filter.set_criteria_default_value(:asc, 'true')
filter.set_criteria_default_value(:pageSize, '30')
- filter.pagination.per_page = [filter.criteria[:pageSize].to_i, 200].min
- filter.pagination.page = (filter.criteria[:page] || 1).to_i
+ filter.set_criteria_value(:pageSize, [filter.criteria[:pageSize].to_i, 200].min)
@columns = []
metrics = []
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_treemap.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_treemap.rb
index 6fed8141b91..ea7b5ec85f9 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_treemap.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/models/measure_filter_display_treemap.rb
@@ -36,8 +36,8 @@ class MeasureFilterDisplayTreemap < MeasureFilterDisplay
filter.set_criteria_value(:sort, "metric:#{@size_metric.key}") if @size_metric
filter.set_criteria_value(:asc, 'true')
- filter.pagination.per_page = MAX_RESULTS
- filter.pagination.page = 1
+ filter.set_criteria_value(:pageSize, MAX_RESULTS)
+ filter.set_criteria_value(:page, 1)
end
def html
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/all_projects/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/all_projects/index.html.erb
index 9f174c9e692..1193aac805d 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/views/all_projects/index.html.erb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/all_projects/index.html.erb
@@ -65,8 +65,11 @@
</tr>
<% end %>
</tbody>
-
- <%= 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)
+ }
+ -%>
</table>
</div>
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 @@
<div id="measure_filter_list<%= widget_id -%>">
<% content_for :script do %>
<script>
- function refreshUrl<%= widget_id -%>(url) {
+ function refreshList<%= widget_id -%>(sort, asc, page) {
$j('#measure_filter_foot<%= widget_id -%>_pages').hide();
$j('#measure_filter_foot<%= widget_id -%>_loading').show();
+
+ var url = decodeURI('<%= url_for filter.criteria.merge(:controller => 'measures', :action => 'search', :page => nil, :sort => nil, :asc => nil, :search => nil, :widget_id => widget_id, :trailing_slash => true) -%>');
+ url += '&sort=' + sort + '&asc=' + asc + '&page=' + page;
+
<% if widget_id %>
$j('#measure_filter_list<%= widget_id -%>').load(url);
<% else %>
@@ -11,16 +15,6 @@
<% end %>
return false;
}
- function sortList<%= widget_id -%>(sort, asc) {
- var url = decodeURI('<%= url_for filter.criteria.merge(:controller => 'measures', :action => 'search', :sort => nil, :asc => nil, :search => nil, :widget_id => widget_id, :trailing_slash => true) -%>');
- url += '&sort=' + sort + '&asc=' + asc;
- return refreshUrl<%= widget_id -%>(url);
- }
- function pageList<%= widget_id -%>(page) {
- var url = decodeURI('<%= url_for filter.criteria.merge(:controller => 'measures', :action => 'search', :page => nil, :search => nil, :widget_id => widget_id, :trailing_slash => true) -%>');
- url += '&page=' + page;
- return refreshUrl<%= widget_id -%>(url);
- }
</script>
<%
end
@@ -213,13 +207,14 @@
<% end %>
</tbody>
- <% 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 %>
</table>
</div> \ 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 @@
-<tfoot <%= "id=#{id}" if id -%>>
-<tr>
- <td colspan="<%= colspan || 1 -%>">
- <div <%= "id=#{id}_pages" if id -%>>
- <% 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
- %>
- </div>
- <% if id && local_assigns[:include_loading] %>
- <img src="<%= ApplicationController.root_context -%>/images/loading-small.gif" style="display: none" id="<%= id -%>_loading">
- <% end %>
- </td>
-</tr>
-</tfoot> \ 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 @@
-<tfoot <%= "id=#{id}" if id -%>>
+<tfoot>
<tr>
<td colspan="<%= colspan || 1 -%>">
- <div <%= "id=#{id}_pages" if id -%>>
<% 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
%>
- </div>
- <% if id && local_assigns[:include_loading] %>
- <img src="<%= ApplicationController.root_context -%>/images/loading-small.gif" style="display: none" id="<%= id -%>_loading">
- <% end %>
</td>
</tr>
</tfoot> \ No newline at end of file