# params[:block] : id of the block to add
def add_block
@user = User.current
- @user.pref.add_block params[:block]
- @user.pref.save
- redirect_to my_page_path
+ @block = params[:block]
+ if @user.pref.add_block @block
+ @user.pref.save
+ respond_to do |format|
+ format.html { redirect_to my_page_path }
+ format.js
+ end
+ else
+ render_error :status => 422
+ end
end
# Remove a block to user's page
# params[:block] : id of the block to remove
def remove_block
@user = User.current
- @user.pref.remove_block params[:block]
+ @block = params[:block]
+ @user.pref.remove_block @block
@user.pref.save
- redirect_to my_page_path
+ respond_to do |format|
+ format.html { redirect_to my_page_path }
+ format.js
+ end
end
# Change blocks order on user's page
content = render_block_content(block, user)
if content.present?
handle = content_tag('span', '', :class => 'sort-handle')
- close = link_to(l(:button_delete), {:action => "remove_block", :block => block}, :method => 'post', :class => "icon-only icon-close")
+ close = link_to(l(:button_delete), {:action => "remove_block", :block => block}, :remote => true, :method => 'post', :class => "icon-only icon-close")
content = content_tag('div', handle + close, :class => 'contextual') + content
content_tag('div', content, :class => "mypage-box", :id => "block-#{block}")
Redmine::MyPage.block_options.each do |label, block|
options << content_tag('option', label, :value => block, :disabled => disabled.include?(block))
end
- select_tag('block', options, :id => "block-select", :onchange => "this.form.submit();")
+ select_tag('block', options, :id => "block-select", :onchange => "$('#block-form').submit();")
end
def calendar_items(startdt, enddt)
--- /dev/null
+$("#block-<%= escape_javascript @block %>").remove();
+$("#list-top").prepend("<%= escape_javascript render_blocks([@block], @user) %>");
+$("#block-select").replaceWith("<%= escape_javascript block_select_tag(@user) %>");
<div class="contextual">
- <%= form_tag({:action => "add_block"}, :id => "block-form") do %>
+ <%= form_tag({:action => "add_block"}, :remote => true, :id => "block-form") do %>
<%= label_tag('block-select', l(:button_add)) %>:
<%= block_select_tag(@user) %>
<% end %>
--- /dev/null
+$("#block-<%= escape_javascript @block %>").remove();
+$("#block-select").replaceWith("<%= escape_javascript block_select_tag(@user) %>");
xhr :post, :update_page, :settings => {'timelog' => {'days' => '14'}}
assert_response :success
- assert_include '$("#block-timelog").html(', response.body
+ assert_include '$("#block-timelog").replaceWith(', response.body
assert_include '14 days', response.body
assert_equal({:days => "14"}, user.reload.pref.my_page_settings('timelog'))
assert User.find(2).pref[:my_page_layout]['top'].include?('issuesreportedbyme')
end
- def test_add_invalid_block_should_redirect
+ def test_add_block_xhr
+ xhr :post, :add_block, :block => 'issuesreportedbyme'
+ assert_response :success
+ assert_include 'issuesreportedbyme', User.find(2).pref[:my_page_layout]['top']
+ end
+
+ def test_add_invalid_block_should_error
post :add_block, :block => 'invalid'
- assert_redirected_to '/my/page'
+ assert_response 422
end
def test_remove_block
assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
end
+ def test_remove_block_xhr
+ xhr :post, :remove_block, :block => 'issuesassignedtome'
+ assert_response :success
+ assert_include '$("#block-issuesassignedtome").remove();', response.body
+ assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
+ end
+
def test_order_blocks
xhr :post, :order_blocks, :group => 'left', 'blocks' => ['documents', 'calendar', 'latestnews']
assert_response :success