# Show user's page
def page
@user = User.current
+ @groups = @user.pref.my_page_groups
@blocks = @user.pref.my_page_layout
end
# params[:blocks] : array of block ids of the group
def order_blocks
@user = User.current
- group = params[:group].to_s
- if %w(top left right).include? group
- group_items = (params[:blocks] || []).collect(&:underscore)
- # remove group blocks if they are presents in other groups
- group_items.each {|s| @user.pref.remove_block(s)}
- @user.pref.my_page_layout[group] = group_items
- @user.pref.save
- end
+ @user.pref.order_blocks params[:group], params[:blocks]
+ @user.pref.save
head 200
end
end
def textarea_font; self[:textarea_font] end
def textarea_font=(value); self[:textarea_font]=value; end
+ # Returns the names of groups that are displayed on user's page
+ # Example:
+ # preferences.my_page_groups
+ # # => ['top', 'left, 'right']
+ def my_page_groups
+ Redmine::MyPage.groups
+ end
+
def my_page_layout
self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
end
end
# Removes block from the user page layout
+ # Example:
+ # preferences.remove_block('news')
def remove_block(block)
block = block.to_s.underscore
- %w(top left right).each do |f|
- (my_page_layout[f] ||= []).delete(block)
+ my_page_layout.keys.each do |group|
+ my_page_layout[group].delete(block)
end
my_page_layout
end
return unless Redmine::MyPage.valid_block?(block, my_page_layout.values.flatten)
remove_block(block)
- # add it on top
- my_page_layout['top'] ||= []
- my_page_layout['top'].unshift(block)
+ # add it to the first group
+ group = my_page_groups.first
+ my_page_layout[group] ||= []
+ my_page_layout[group].unshift(block)
+ end
+
+ # Sets the block order for the given group.
+ # Example:
+ # preferences.order_blocks('left', ['issueswatched', 'news'])
+ def order_blocks(group, blocks)
+ group = group.to_s
+ if Redmine::MyPage.groups.include?(group) && blocks.present?
+ blocks = blocks.map(&:underscore) & my_page_layout.values.flatten
+ blocks.each {|block| remove_block(block)}
+ my_page_layout[group] = blocks
+ end
end
def update_block_settings(block, settings)
<h2><%=l(:label_my_page)%></h2>
<div id="my-page">
-<div id="list-top" class="block-receiver">
- <%= render_blocks(@blocks['top'], @user) %>
-</div>
-
-<div id="list-left" class="splitcontentleft block-receiver">
- <%= render_blocks(@blocks['left'], @user) %>
-</div>
-
-<div id="list-right" class="splitcontentright block-receiver">
- <%= render_blocks(@blocks['right'], @user) %>
-</div>
+<% @groups.each do |group| %>
+ <div id="list-<%= group %>" class="block-receiver">
+ <%= render_blocks(@blocks[group], @user) %>
+ </div>
+<% end %>
</div>
<%= context_menu %>
<%= javascript_tag do %>
$(document).ready(function(){
$('#block-select').val('');
- $('#list-top, #list-left, #list-right').sortable({
+ $('.block-receiver').sortable({
connectWith: '.block-receiver',
tolerance: 'pointer',
handle: '.sort-handle',
module MyPage
include Redmine::I18n
+ CORE_GROUPS = ['top', 'left', 'right']
+
CORE_BLOCKS = {
'issuesassignedtome' => {:label => :label_assigned_to_me_issues},
'issuesreportedbyme' => {:label => :label_reported_issues},
'timelog' => {:label => :label_spent_time}
}
+ def self.groups
+ CORE_GROUPS.dup.freeze
+ end
+
# Returns the available blocks
def self.blocks
CORE_BLOCKS.merge(additional_blocks).freeze
.message .contextual { margin-top: 0; }
.splitcontent {overflow:auto;}
-.splitcontentleft{float:left; width:49%;}
-.splitcontentright{float:right; width:49%;}
+.splitcontentleft, #list-left {float:left; width:49%;}
+.splitcontentright, #list-right {float:right; width:49%;}
form {display: inline;}
input, select {vertical-align: middle; margin-top: 1px; margin-bottom: 1px;}
input[type="submit"] { -webkit-appearance: button; }
display: none;
}
- .splitcontentleft {
+ .splitcontentleft, #list-left {
width: 100%;
}
- .splitcontentright {
+ .splitcontentright, #list-right {
width: 100%;
}
end
def test_order_blocks
- xhr :post, :order_blocks, :group => 'left', 'blocks' => ['documents', 'calendar', 'latestnews']
+ pref = User.find(2).pref
+ pref.my_page_layout = {'left' => ['news', 'calendar','documents']}
+ pref.save!
+
+ xhr :post, :order_blocks, :group => 'left', :blocks => ['documents', 'calendar', 'news']
assert_response :success
- assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left']
+ assert_equal ['documents', 'calendar', 'news'], User.find(2).pref.my_page_layout['left']
+ end
+
+ def test_move_block
+ pref = User.find(2).pref
+ pref.my_page_layout = {'left' => ['news','documents'], 'right' => ['calendar']}
+ pref.save!
+
+ xhr :post, :order_blocks, :group => 'left', :blocks => ['news', 'calendar', 'documents']
+ assert_response :success
+ assert_equal({'left' => ['news', 'calendar', 'documents'], 'right' => []}, User.find(2).pref.my_page_layout)
end
def test_reset_rss_key_with_existing_key