# 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
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'
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
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
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
<% 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">