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