app/controllers/my_controller.rb | 66 +++++++++++++++++++++++++++++++++------ app/views/my/page_layout.html.erb | 18 ++++++----- config/routes.rb | 2 ++ 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb index 9ee82c0..ee8ecb4 100644 --- a/app/controllers/my_controller.rb +++ b/app/controllers/my_controller.rb @@ -134,9 +134,15 @@ class MyController < ApplicationController def page_layout @user = User.current @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup + blockpairs = { "top_deleted" => "top", "right_deleted" => "right", "left_deleted" => "left", + "top_deleted" => "top_unsaved", "right_deleted" => "right_unsaved", "left_deleted" => "left_unsaved" } + blockpairs.each { |parent, child| + @blocks[child] = @blocks[child] - @blocks[parent] + } + @block_options = [] BLOCKS.each do |k, v| - unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)} + unless %w(top top_unsaved left left_unsaved right right_unsaved).detect {|f| (@blocks[f] ||= []).include?(k)} @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize] end end @@ -151,9 +157,9 @@ class MyController < ApplicationController @user = User.current layout = @user.pref[:my_page_layout] || {} # remove if already present in a group - %w(top left right).each {|f| (layout[f] ||= []).delete block } + %w(top top_unsaved top_deleted left left_unsaved left_deleted right right_unsaved right_deleted).each {|f| (layout[f] ||= []).delete block } # add it on top - layout['top'].unshift block + layout['top_unsaved'].unshift block @user.pref[:my_page_layout] = layout @user.pref.save redirect_to my_page_layout_path @@ -166,7 +172,15 @@ class MyController < ApplicationController @user = User.current # remove block in all groups layout = @user.pref[:my_page_layout] || {} - %w(top left right).each {|f| (layout[f] ||= []).delete block } + + blockpairs = { "top" => "top_deleted", "right" => "right_deleted", "left" => "left_deleted", + "top_unsaved" => "top_deleted", "right_unsaved" => "right_deleted", "left_unsaved" => "left_deleted" + } + blockpairs.each { |parent, child| + layout[child] = layout[child] | block.to_a + layout[parent] = layout[parent] - layout[child] + } + @user.pref[:my_page_layout] = layout @user.pref.save redirect_to my_page_layout_path @@ -183,15 +197,49 @@ class MyController < ApplicationController group_items.each {|s| s.sub!(/^block_/, '')} if group_items and group_items.is_a? Array layout = @user.pref[:my_page_layout] || {} - # remove group blocks if they are presents in other groups - %w(top left right).each {|f| - layout[f] = (layout[f] || []) - group_items - } - layout[group] = group_items + layout[group.to_s + "_unsaved"] = group_items + @user.pref[:my_page_layout] = layout @user.pref.save end end render :nothing => true end + + # Save my Page changes + def save_layout + @user = User.current + blocks = @user.pref[:my_page_layout] || {} + + blockpairs = { "top" => "top_unsaved", "right" => "right_unsaved", "left" => "left_unsaved" } + blockpairs.each { |parent, child| + blocks[parent] = blocks[parent] | blocks[child] + blocks[child] = [] + } + blockdelete = ["top_deleted", "left_deleted", "right_deleted"] + blockdelete.each{|block| + blocks[block] = [] + } + @user.pref[:my_page_layout] = blocks + @user.pref.save + + redirect_to my_page_path + end + + # Save my Page changes + def cancel_layout + @user = User.current + blocks = @user.pref[:my_page_layout] || {} + + blockpairs = { "top" => "top_deleted", "right" => "right_deleted", "left" => "left_deleted" } + blockpairs.each { |parent, child| + blocks[parent] = blocks[parent] | blocks[child] + blocks[child] = [] + } + + @user.pref[:my_page_layout] = blocks + @user.pref.save + + redirect_to my_page_path + end end diff --git a/app/views/my/page_layout.html.erb b/app/views/my/page_layout.html.erb index 4a60d97..3f35a3b 100644 --- a/app/views/my/page_layout.html.erb +++ b/app/views/my/page_layout.html.erb @@ -8,30 +8,34 @@ <%= link_to l(:button_add), '#', :onclick => '$("#block-form").submit()', :class => 'icon icon-add' %> <% end %> <% end %> -<%= link_to l(:button_back), {:action => 'page'}, :class => 'icon icon-cancel' %> +<%= link_to l(:button_save), {:action => 'save_layout'}, :class => 'icon icon-save' %> +<%= link_to l(:button_cancel), {:action => 'cancel_layout'}, :class => 'icon icon-cancel' %>

<%=l(:label_my_page)%>

- <% @blocks['top'].each do |b| + <% blocklist = @blocks['top'].to_a | @blocks['top_unsaved'].to_a %> + <% blocklist.each do |b| next unless MyController::BLOCKS.keys.include? b %> <%= render :partial => 'block', :locals => {:user => @user, :block_name => b} %> - <% end if @blocks['top'] %> + <% end if blocklist %>
- <% @blocks['left'].each do |b| + <% blocklist = @blocks['left'].to_a | @blocks['left_unsaved'].to_a %> + <% blocklist.each do |b| next unless MyController::BLOCKS.keys.include? b %> <%= render :partial => 'block', :locals => {:user => @user, :block_name => b} %> - <% end if @blocks['left'] %> + <% end if blocklist %>
- <% @blocks['right'].each do |b| + <% blocklist = @blocks['right'].to_a | @blocks['right_unsaved'].to_a %> + <% blocklist.each do |b| next unless MyController::BLOCKS.keys.include? b %> <%= render :partial => 'block', :locals => {:user => @user, :block_name => b} %> - <% end if @blocks['right'] %> + <% end if blocklist %>
<%= javascript_tag "initMyPageSortable('top', '#{ escape_javascript url_for(:action => "order_blocks", :group => "top") }');" %> diff --git a/config/routes.rb b/config/routes.rb index 14bf9ed..353e1bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,6 +63,8 @@ RedmineApp::Application.routes.draw do match 'my/account', :controller => 'my', :action => 'account', :via => [:get, :post] match 'my/account/destroy', :controller => 'my', :action => 'destroy', :via => [:get, :post] match 'my/page', :controller => 'my', :action => 'page', :via => :get + match 'my/save_layout', :controller => 'my', :action => 'save_layout', :via => :get + match 'my/cancel_layout', :controller => 'my', :action => 'cancel_layout', :via => :get match 'my', :controller => 'my', :action => 'index', :via => :get # Redirects to my/page match 'my/reset_rss_key', :controller => 'my', :action => 'reset_rss_key', :via => :post match 'my/reset_api_key', :controller => 'my', :action => 'reset_api_key', :via => :post