]> source.dussan.org Git - redmine.git/commitdiff
Don't reload the page when adding/removing a block.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 13 Mar 2017 20:02:34 +0000 (20:02 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Mon, 13 Mar 2017 20:02:34 +0000 (20:02 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@16394 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/my_controller.rb
app/helpers/my_helper.rb
app/views/my/add_block.js.erb [new file with mode: 0644]
app/views/my/page.html.erb
app/views/my/remove_block.js.erb [new file with mode: 0644]
test/functional/my_controller_test.rb

index 7d87707761fd5869a6b58227d84783622da06459..66bd965baf4cdd7be9dcd6c030da44a1f8f9779a 100644 (file)
@@ -147,18 +147,29 @@ class MyController < ApplicationController
   # 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
index 3c0313f9082f863f59feb786b9543eaacccffa77..042209cac2fb8623a4b1a504dc4b53a9acc2ef9f 100644 (file)
@@ -35,7 +35,7 @@ module MyHelper
     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}")
@@ -64,7 +64,7 @@ module MyHelper
     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)
diff --git a/app/views/my/add_block.js.erb b/app/views/my/add_block.js.erb
new file mode 100644 (file)
index 0000000..c2382ee
--- /dev/null
@@ -0,0 +1,3 @@
+$("#block-<%= escape_javascript @block %>").remove();
+$("#list-top").prepend("<%= escape_javascript render_blocks([@block], @user) %>");
+$("#block-select").replaceWith("<%= escape_javascript block_select_tag(@user) %>");
index 9fe143516de111150a121bf27971fe403cc7b85f..1210c471a3dbfd1a371ace9d054245bf307332f0 100644 (file)
@@ -1,5 +1,5 @@
 <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 %>
diff --git a/app/views/my/remove_block.js.erb b/app/views/my/remove_block.js.erb
new file mode 100644 (file)
index 0000000..275e9e1
--- /dev/null
@@ -0,0 +1,2 @@
+$("#block-<%= escape_javascript @block %>").remove();
+$("#block-select").replaceWith("<%= escape_javascript block_select_tag(@user) %>");
index e40dd91e31f259e1c312d11d4b71cf9ee913f8dd..76237dfb52d37621c4e34ed6321145218dc187ae 100644 (file)
@@ -227,7 +227,7 @@ class MyControllerTest < Redmine::ControllerTest
 
     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'))
@@ -239,9 +239,15 @@ class MyControllerTest < Redmine::ControllerTest
     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
@@ -250,6 +256,13 @@ class MyControllerTest < Redmine::ControllerTest
     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