From 2de6b4a37197ab978c104a87b7a8409bf0591dbd Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 12 Dec 2012 07:54:42 +0100 Subject: [PATCH] SONAR-3825 widget should support column sorting and pagination --- .../core/widgets/measure_filter_list.html.erb | 2 +- .../widgets/measure_filter_treemap.html.erb | 12 +- .../app/controllers/measures_controller.rb | 6 +- .../WEB-INF/app/helpers/application_helper.rb | 3 + .../WEB-INF/app/helpers/measures_helper.rb | 4 +- .../app/views/measures/_display.html.erb | 11 ++ .../app/views/measures/_display_list.html.erb | 123 +++++++++++------- .../app/views/measures/search.html.erb | 12 +- .../utils/_tfoot_ajax_pagination.html.erb | 48 +++++++ .../views/utils/_tfoot_pagination.html.erb | 13 +- 10 files changed, 164 insertions(+), 70 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display.html.erb create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_ajax_pagination.html.erb diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_list.html.erb b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_list.html.erb index dc3fcc37f0c..7ec7a3bbe7b 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_list.html.erb +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_list.html.erb @@ -10,7 +10,7 @@ filter.execute(self, :user => current_user) @widget_title = link_to h(filter.name), {:controller => 'measures', :action => 'filter', :id => filter.id, :display => 'list'} %> - <%= render :partial => "measures/display_#{filter.display.key}", :locals => {:edit_mode => false, :widget => widget, :filter => filter} %> + <%= render :partial => "measures/display_#{filter.display.key}", :locals => {:edit_mode => false, :filter => filter, :widget_id => widget.id} -%> <% end else diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_treemap.html.erb b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_treemap.html.erb index 9b8ac706d1e..53c2fae8967 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_treemap.html.erb +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/measure_filter_treemap.html.erb @@ -6,14 +6,14 @@ if filter url_options = {:controller => 'measures', :action => 'filter', :id => filter.id, :display => 'treemap'} filter.load_criteria_from_data - filter.set_criteria_value('display', 'treemap') + filter.set_criteria_value(:display, 'treemap') if size_metric - filter.set_criteria_value('tmSize', size_metric.key) - url_options['tmSize']=size_metric.key + filter.set_criteria_value(:tmSize, size_metric.key) + url_options[:tmSize]=size_metric.key end if color_metric - filter.set_criteria_value('tmColor', color_metric.key) - url_options['tmColor']=color_metric.key + filter.set_criteria_value(:tmColor, color_metric.key) + url_options[:tmColor]=color_metric.key end if !filter.require_authentication? || logged_in? @@ -21,7 +21,7 @@ @widget_title = link_to h(filter.name), url_options %> - <%= render :partial => "measures/display_#{filter.display.key}", :locals => {:edit_mode => false, :widget => widget, :filter => filter} %> + <%= render :partial => "measures/display_#{filter.display.key}", :locals => {:edit_mode => false, :widget_id => widget.id, :filter => filter} %> <% end else diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb index 3447146fbff..a63af45542a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/measures_controller.rb @@ -31,9 +31,13 @@ class MeasuresController < ApplicationController else @filter = MeasureFilter.new end - @filter.criteria=(params) + @filter.criteria=(params.merge({:controller => nil, :action => nil, :search => nil, :widget_id => nil})) @filter.enable_default_display @filter.execute(self, :user => current_user) + + if request.xhr? + render :partial => 'measures/display', :locals => {:filter => @filter, :edit_mode => false, :widget_id => params[:widget_id]} + end end # Load existing filter 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 81684742838..99a440fe75c 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 @@ -688,4 +688,7 @@ module ApplicationHelper "#{h label}" end + def link_to_function_if(condition, name, *args) + condition ? link_to_function(name, args) : name + 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 05b3e471d39..64ccbf731d3 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 @@ -19,9 +19,9 @@ # module MeasuresHelper - def list_column_html(filter, column) + def list_column_html(filter, column, widget_id) if column.sort? - html = link_to_function(h(column.title_label), "reloadParameters({asc:'#{(!filter.sort_asc?).to_s}', sort:'#{column.key}'})", :title => h(column.tooltip)) + html = link_to_function(h(column.title_label), "sortList#{widget_id}('#{escape_javascript column.key}',#{!filter.sort_asc?})", :title => h(column.tooltip)) else html=h(column.title_label) end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display.html.erb new file mode 100644 index 00000000000..09d7858646a --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/_display.html.erb @@ -0,0 +1,11 @@ +<% if filter.security_exclusions %> +

<%= message('results_not_display_due_to_security') -%>

+<% end %> + +<% if !filter.errors.empty? %> + <% filter.errors.full_messages.each do |message| %> +
<%= h message %>
+ <% end %> +<% elsif filter.rows && filter.display %> + <%= render :partial => "measures/display_#{filter.display.class::KEY}", :locals => {:filter => filter, :edit_mode => edit_mode, :widget_id => widget_id} -%> +<% end %> 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 df8ad570af0..8e9f33549c4 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,4 +1,30 @@ +
+<% content_for :script do %> + <% + end + display_favourites = logged_in? colspan = filter.display.columns.size colspan += 1 if display_favourites @@ -56,13 +82,48 @@ td1.remove(); }); } + $j(document).ready(function () { + $j("#select-metric").on("change", function (e) { + var selectedKey = $j("#select-metric option:selected").val(); + if (selectedKey.indexOf('metric:') == 0) { + if (selectedKey.indexOf('metric:new_') == 0) { + $j('#select-period option :eq(0)').attr('disabled', 'disabled'); + $j('#select-period ').val('1'); + } else { + $j('#select-period option :eq(0)').removeAttr('disabled'); + $j('#select-period').val(''); + } + $j('#select-period').show(); + } else { + $j('#select-period').hide(); + } + $j("#add-metric").removeAttr('disabled'); + + }); + $j("#add-metric").on("click", function (e) { + var columnKey = $j("#select-metric option:selected").val(); + var period = $j("#select-period option:selected").val(); + if (period.length > 0) { + columnKey += ':' + period; + } + cols.push(columnKey); + window.location = removeUrlAttr(decodeURI(window.location.href), 'cols\\[\\]') + '&' + $j.map(cols,function (a) { + return a ? 'cols[]=' + a : null; + }).join('&'); + }); + $j("#exit-edit").on("click", function (e) { + var url = removeUrlAttr(decodeURI(window.location.href), 'cols\\[\\]'); + url = removeUrlAttr(url, 'edit'); + url += '&' + $j.map(cols,function (a) { + return a ? 'cols[]=' + a : null; + }).join('&'); + window.location = url; + }); + }); <% end - end %> - -<% if edit_mode %>
@@ -88,45 +149,9 @@
- -<% end %> +<% + end +%> @@ -135,7 +160,7 @@ <% end %> <% filter.display.columns.each do |column| %> - <%= list_column_html(filter, column) -%> + <%= list_column_html(filter, column, widget_id) -%> <% end %> @@ -188,5 +213,13 @@ <% end %> - <%= render :partial => 'utils/tfoot_pagination', :locals => {:pagination => filter.pagination, :colspan => colspan} %> -
\ No newline at end of file + <% 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}" + } -%> + +
\ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb index 7129a6b3748..242bb0e6f4e 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/measures/search.html.erb @@ -56,17 +56,7 @@ - <% if @filter.security_exclusions %> -

<%= message('results_not_display_due_to_security') -%>

- <% end %> - - <% if !@filter.errors.empty? %> - <% @filter.errors.full_messages.each do |message| %> -
<%= h message %>
- <% end %> - <% elsif @filter.rows && @filter.display %> - <%= render :partial => "measures/display_#{@filter.display.class::KEY}", :locals => {:filter => @filter, :edit_mode => edit_mode} -%> - <% end %> + <%= render :partial => 'measures/display', :locals => {:filter => @filter, :edit_mode => edit_mode, :widget_id => nil} -%> <% end %> 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 new file mode 100644 index 00000000000..2ee7bb04433 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/utils/_tfoot_ajax_pagination.html.erb @@ -0,0 +1,48 @@ +> + + +
> + <% 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 f50dafc2aa1..b35a2976be0 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,6 +1,7 @@ - +> +
> <% if pagination.count > 0 %> <%= message('x_results', :params => [pagination.count]) -%> <% end %> @@ -25,7 +26,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 %> @@ -33,11 +34,15 @@ <% 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