From f40d47781e79f557089e7e0b0520bc624ca26cb1 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 14 May 2012 16:14:43 +0200 Subject: [PATCH] SONAR-3461 fix actions to order widgets. Sometimes several clicks are required to really change the order of dashboards. --- .../admin_dashboards_controller.rb | 64 +++++++------------ .../app/controllers/dashboards_controller.rb | 22 +++---- .../app/views/dashboards/index.html.erb | 4 +- 3 files changed, 35 insertions(+), 55 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/admin_dashboards_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/admin_dashboards_controller.rb index a03291fc2ae..06be304d77d 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/admin_dashboards_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/admin_dashboards_controller.rb @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public # License along with Sonar; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 -# +# class AdminDashboardsController < ApplicationController SECTION=Navigation::SECTION_CONFIGURATION @@ -33,52 +33,18 @@ class AdminDashboardsController < ApplicationController end end - def up - dashboard_index=-1 - dashboard=nil - @actives.each_index do |index| - if @actives[index].id==params[:id].to_i - dashboard_index=index - dashboard=@actives[index] - end - end - if dashboard && dashboard_index>0 - @actives[dashboard_index]=@actives[dashboard_index-1] - @actives[dashboard_index-1]=dashboard - - @actives.each_index do |index| - @actives[index].order_index=index+1 - @actives[index].save - end - end - redirect_to :action => 'index' - end - def down - dashboard_index=-1 - dashboard=nil - @actives.each_index do |index| - if @actives[index].id==params[:id].to_i - dashboard_index=index - dashboard=@actives[index] - end - end - if dashboard && dashboard_index<@actives.size-1 - @actives[dashboard_index]=@actives[dashboard_index+1] - @actives[dashboard_index+1]=dashboard + position(+1) + end - @actives.each_index do |index| - @actives[index].order_index=index+1 - @actives[index].save - end - end - redirect_to :action => 'index' + def up + position(-1) end def add dashboard=::Dashboard.find(:first, :conditions => ['shared=? and id=?', true, params[:id].to_i()]) if dashboard - ActiveDashboard.create(:dashboard => dashboard, :user => nil, :order_index => @actives.size+1) + ActiveDashboard.create(:dashboard => dashboard, :user => nil, :order_index => (@actives.max_by(&:order_index).order_index+1)) flash[:notice]='Default dashboard added.' end redirect_to :action => 'index' @@ -117,4 +83,22 @@ class AdminDashboardsController < ApplicationController def load_default_dashboards @actives=ActiveDashboard.default_dashboards end + + def position(offset) + to_move = @actives.find { |a| a.id == params[:id].to_i} + if to_move + dashboards_same_type=@actives.select { |a| (a.global? == to_move.global?) }.sort_by(&:order_index) + + index = dashboards_same_type.index(to_move) + dashboards_same_type[index], dashboards_same_type[index + offset] = dashboards_same_type[index + offset], dashboards_same_type[index] + + dashboards_same_type.each_with_index do |a,i| + a.order_index=i+1 + a.save + end + end + + redirect_to :action => 'index' + end + end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboards_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboards_controller.rb index 3909090aff1..0cf468771ee 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboards_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboards_controller.rb @@ -59,7 +59,7 @@ class DashboardsController < ApplicationController else if @dashboard.save add_default_dashboards_if_first_user_dashboard - last_active_dashboard=current_user.active_dashboards.max { |x, y| x.order_index<=>y.order_index } + last_active_dashboard=current_user.active_dashboards.max_by(&:order_index) current_user.active_dashboards.create(:dashboard => @dashboard, :user_id => current_user.id, :order_index => (last_active_dashboard ? last_active_dashboard.order_index+1 : 1)) redirect_to :controller => 'dashboard', :action => 'configure', :did => @dashboard.id, :id => (params[:resource] unless @dashboard.global) else @@ -152,20 +152,16 @@ class DashboardsController < ApplicationController dashboards=current_user.active_dashboards.to_a - to_move = dashboards.find { |a| a.dashboard_id == params[:id].to_i} + to_move = dashboards.find { |a| a.id == params[:id].to_i} if to_move - dashboards_with_same_type=dashboards.select { |a| (a.global? == to_move.global?) } + dashboards_same_type=dashboards.select { |a| (a.global? == to_move.global?) }.sort_by(&:order_index) - switch_with=nil - if offset < 0 - switch_with = dashboards_with_same_type.select { |a| a.order_index <= (to_move.order_index + offset) }.max_by(&:order_index) - elsif offset > 0 - switch_with = dashboards_with_same_type.select { |a| a.order_index >= (to_move.order_index + offset) }.min_by(&:order_index) - end - if switch_with - switch_with.order_index, to_move.order_index = to_move.order_index, switch_with.order_index - to_move.save - switch_with.save + index = dashboards_same_type.index(to_move) + dashboards_same_type[index], dashboards_same_type[index + offset] = dashboards_same_type[index + offset], dashboards_same_type[index] + + dashboards_same_type.each_with_index do |a,i| + a.order_index=i+1 + a.save end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboards/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboards/index.html.erb index b46e6e9497c..fc41d6de894 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboards/index.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboards/index.html.erb @@ -34,12 +34,12 @@ <% end %> <% if index > 0 %> - <%= link_to image_tag('blue-up.png'), {:action => 'up', :id => active.dashboard_id, :resource => params[:resource]}, :method => :post, :id => "up-#{u active.name}" %> + <%= link_to image_tag('blue-up.png'), {:action => 'up', :id => active.id, :resource => params[:resource]}, :method => :post, :id => "up-#{u active.name}" %> <% else %> <%= image_tag('transparent_16.gif') %> <% end %> <% if index < @actives.size-1 %> - <%= link_to image_tag('blue-down.png'), {:action => 'down', :id => active.dashboard.id, :resource => params[:resource]}, :method => :post, :id => "down-#{u active.name}" %> + <%= link_to image_tag('blue-down.png'), {:action => 'down', :id => active.id, :resource => params[:resource]}, :method => :post, :id => "down-#{u active.name}" %> <% end %> -- 2.39.5