]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3461 fix actions to order widgets.
authorDavid Gageot <david@gageot.net>
Mon, 14 May 2012 14:14:43 +0000 (16:14 +0200)
committerDavid Gageot <david@gageot.net>
Mon, 14 May 2012 14:14:43 +0000 (16:14 +0200)
Sometimes several clicks are required to really change the order of dashboards.

sonar-server/src/main/webapp/WEB-INF/app/controllers/admin_dashboards_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboards_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/views/dashboards/index.html.erb

index a03291fc2ae7ae215617e0ba3ea086392ce764db..06be304d77d7992b9cdd25696396c34fabb0f2ae 100644 (file)
@@ -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
index 3909090aff11e1ca061a1966f75b5942e0da4b84..0cf468771ee1359e2c51546f6be96af5e62451eb 100644 (file)
@@ -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
 
index b46e6e9497c7b2b46db4dafde53dd40ba2d63ace..fc41d6de89493e89a5170cb42d35ac2aa1a8040f 100644 (file)
             <% end %>
             <td>
               <% 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 %>
             </td>
             <td class="thin nowrap right">