Project

General

Profile

Defect #8770 ยป cancel_my_page_v0.1.diff

First Patch - Daniel Felix, 2012-12-15 14:31

View differences:

app/controllers/my_controller.rb
134 134
  def page_layout
135 135
    @user = User.current
136 136
    @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
137
    blockpairs = { "top_deleted" => "top", "right_deleted" => "right", "left_deleted" => "left",
138
                   "top_deleted" => "top_unsaved", "right_deleted" => "right_unsaved", "left_deleted" => "left_unsaved" }
139
    blockpairs.each { |parent, child|
140
      @blocks[child] = @blocks[child] - @blocks[parent]
141
    }
142
    
137 143
    @block_options = []
138 144
    BLOCKS.each do |k, v|
139
      unless %w(top left right).detect {|f| (@blocks[f] ||= []).include?(k)}
145
      unless %w(top top_unsaved left left_unsaved right right_unsaved).detect {|f| (@blocks[f] ||= []).include?(k)}
140 146
        @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]
141 147
      end
142 148
    end
......
151 157
    @user = User.current
152 158
    layout = @user.pref[:my_page_layout] || {}
153 159
    # remove if already present in a group
154
    %w(top left right).each {|f| (layout[f] ||= []).delete block }
160
    %w(top top_unsaved top_deleted left left_unsaved left_deleted right right_unsaved right_deleted).each {|f| (layout[f] ||= []).delete block }
155 161
    # add it on top
156
    layout['top'].unshift block
162
    layout['top_unsaved'].unshift block
157 163
    @user.pref[:my_page_layout] = layout
158 164
    @user.pref.save
159 165
    redirect_to my_page_layout_path
......
166 172
    @user = User.current
167 173
    # remove block in all groups
168 174
    layout = @user.pref[:my_page_layout] || {}
169
    %w(top left right).each {|f| (layout[f] ||= []).delete block }
175
    
176
    blockpairs = { "top" => "top_deleted", "right" => "right_deleted", "left" => "left_deleted",
177
                   "top_unsaved" => "top_deleted", "right_unsaved" => "right_deleted", "left_unsaved" => "left_deleted"
178
    }
179
    blockpairs.each { |parent, child|
180
      layout[child] = layout[child] | block.to_a
181
      layout[parent] = layout[parent] - layout[child]
182
    }
183

  
170 184
    @user.pref[:my_page_layout] = layout
171 185
    @user.pref.save
172 186
    redirect_to my_page_layout_path
......
183 197
      group_items.each {|s| s.sub!(/^block_/, '')}
184 198
      if group_items and group_items.is_a? Array
185 199
        layout = @user.pref[:my_page_layout] || {}
186
        # remove group blocks if they are presents in other groups
187
        %w(top left right).each {|f|
188
          layout[f] = (layout[f] || []) - group_items
189
        }
190
        layout[group] = group_items
200
        layout[group.to_s + "_unsaved"] = group_items
201
        
191 202
        @user.pref[:my_page_layout] = layout
192 203
        @user.pref.save
193 204
      end
194 205
    end
195 206
    render :nothing => true
196 207
  end
208
  
209
  # Save my Page changes
210
  def save_layout
211
    @user = User.current
212
    blocks = @user.pref[:my_page_layout] || {}
213
    
214
    blockpairs = { "top" => "top_unsaved", "right" => "right_unsaved", "left" => "left_unsaved" }
215
    blockpairs.each { |parent, child|
216
      blocks[parent] = blocks[parent] | blocks[child]
217
      blocks[child] = [] 
218
    }
219
    blockdelete = ["top_deleted", "left_deleted", "right_deleted"]
220
    blockdelete.each{|block|
221
      blocks[block] = []
222
    }
223
    @user.pref[:my_page_layout] = blocks
224
    @user.pref.save
225
    
226
    redirect_to my_page_path
227
  end
228
  
229
  # Save my Page changes
230
  def cancel_layout
231
    @user = User.current
232
    blocks = @user.pref[:my_page_layout] || {}
233
    
234
    blockpairs = { "top" => "top_deleted", "right" => "right_deleted", "left" => "left_deleted" }
235
    blockpairs.each { |parent, child|
236
      blocks[parent] = blocks[parent] | blocks[child]
237
      blocks[child] = [] 
238
    }
239

  
240
    @user.pref[:my_page_layout] = blocks
241
    @user.pref.save
242
    
243
    redirect_to my_page_path
244
  end
197 245
end
app/views/my/page_layout.html.erb
8 8
  <%= link_to l(:button_add), '#', :onclick => '$("#block-form").submit()', :class => 'icon icon-add' %>
9 9
  <% end %>
10 10
<% end %>
11
<%= link_to l(:button_back), {:action => 'page'}, :class => 'icon icon-cancel' %>
11
<%= link_to l(:button_save), {:action => 'save_layout'}, :class => 'icon icon-save' %>
12
<%= link_to l(:button_cancel), {:action => 'cancel_layout'}, :class => 'icon icon-cancel' %>
12 13
</div>
13 14

  
14 15
<h2><%=l(:label_my_page)%></h2>
15 16

  
16 17
<div id="list-top" class="block-receiver">
17
  <% @blocks['top'].each do |b|
18
  <% blocklist = @blocks['top'].to_a | @blocks['top_unsaved'].to_a %>
19
  <% blocklist.each do |b|
18 20
     next unless MyController::BLOCKS.keys.include? b %>
19 21
  <%= render :partial => 'block', :locals => {:user => @user, :block_name => b} %>
20
  <% end if @blocks['top'] %>
22
  <% end if blocklist %>
21 23
</div>
22 24

  
23 25
<div id="list-left" class="splitcontentleft block-receiver">
24
  <% @blocks['left'].each do |b|
26
  <% blocklist = @blocks['left'].to_a | @blocks['left_unsaved'].to_a %>
27
  <% blocklist.each do |b|
25 28
     next unless MyController::BLOCKS.keys.include? b %>
26 29
  <%= render :partial => 'block', :locals => {:user => @user, :block_name => b} %>
27
  <% end if @blocks['left'] %>
30
  <% end if blocklist %>
28 31
</div>
29 32

  
30 33
<div id="list-right" class="splitcontentright block-receiver">
31
  <% @blocks['right'].each do |b|
34
  <% blocklist = @blocks['right'].to_a | @blocks['right_unsaved'].to_a %>
35
  <% blocklist.each do |b|
32 36
     next unless MyController::BLOCKS.keys.include? b %>
33 37
  <%= render :partial => 'block', :locals => {:user => @user, :block_name => b} %>
34
  <% end if @blocks['right'] %>
38
  <% end if blocklist %>
35 39
</div>
36 40

  
37 41
<%= javascript_tag "initMyPageSortable('top', '#{ escape_javascript url_for(:action => "order_blocks", :group => "top") }');" %>
config/routes.rb
63 63
  match 'my/account', :controller => 'my', :action => 'account', :via => [:get, :post]
64 64
  match 'my/account/destroy', :controller => 'my', :action => 'destroy', :via => [:get, :post]
65 65
  match 'my/page', :controller => 'my', :action => 'page', :via => :get
66
  match 'my/save_layout', :controller => 'my', :action => 'save_layout', :via => :get
67
  match 'my/cancel_layout', :controller => 'my', :action => 'cancel_layout', :via => :get
66 68
  match 'my', :controller => 'my', :action => 'index', :via => :get # Redirects to my/page
67 69
  match 'my/reset_rss_key', :controller => 'my', :action => 'reset_rss_key', :via => :post
68 70
  match 'my/reset_api_key', :controller => 'my', :action => 'reset_api_key', :via => :post
    (1-1/1)