Skip to content

Commit

Permalink
POC : Improve nested pages admin tree
Browse files Browse the repository at this point in the history
  • Loading branch information
bricesanchez committed Feb 2, 2016
1 parent 6d5ac0a commit 4c8a03b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
70 changes: 70 additions & 0 deletions pages/app/helpers/refinery/admin/pages_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
module Refinery
module Admin
module PagesHelper

def sorted_nested_li(objects, order, &block)
nested_li sort_list(objects, order), &block
end

def nested_li(objects, &block)
objects = objects.order(:lft) if objects.is_a? Class

return '' if objects.size == 0

output = "<li class='clearfix record items' id='#{dom_id(objects.first)}' >"
path = [nil]

objects.each_with_index do |o, i|
if o.parent_id != path.last
# We are on a new level, did we descend or ascend?
if path.include?(o.parent_id)
# Remove the wrong trailing path elements
while path.last != o.parent_id
path.pop
output << "</li></ul>"
end
output << "</li><li class='clearfix record items' id='#{dom_id(o)}' >"
else
path << o.parent_id
output << "<ul class='nested' data-ajax-content='#{refinery.admin_children_pages_path(o.nested_url)}'><li class='clearfix record items' id='#{dom_id(o)}' >"
end
elsif i != 0
output << "</li><li class='clearfix record items' id='#{dom_id(o)}' >"
end
output << capture(o, path.size - 1, &block)
end

output << '</li>' * path.length
output.html_safe
end

def parent_id_nested_set_options(current_page)
pages = []
nested_set_options(::Refinery::Page, current_page) { |page| pages << page}
Expand Down Expand Up @@ -53,6 +90,39 @@ def page_title_with_translations(page)
page.title.presence || page.translations.detect { |t| t.title.present?}.title
end

private

def sort_list(objects, order)
objects = objects.order(:lft) if objects.is_a? Class

# Partition the results
children_of = {}
objects.each do |o|
children_of[ o.parent_id ] ||= []
children_of[ o.parent_id ] << o
end

# Sort each sub-list individually
children_of.each_value do |children|
children.sort_by! &order
end

# Re-join them into a single list
results = []
recombine_lists(results, children_of, nil)

results
end

def recombine_lists(results, children_of, parent_id)
if children_of[parent_id]
children_of[parent_id].each do |o|
results << o
recombine_lists(results, children_of, o.id)
end
end
end

end
end
end
51 changes: 50 additions & 1 deletion pages/app/views/refinery/admin/pages/_sortable_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
<ul id='sortable_list'>
<%= render :partial => 'page', :collection => @pages.roots %>
<%= sorted_nested_li(@pages, :title) do |page| %>
<% # setup params for various action links
add_url = refinery.new_admin_page_path(parent_id: page.id)
edit_url = refinery.admin_edit_page_path(page.nested_url, switch_locale: (page.translations.first.locale unless page.translated_to_default_locale?))
delete_url = refinery.admin_delete_page_path(page.nested_url)
delete_options = {
class: "cancel confirm-delete",
data: {confirm: t('message', scope: 'refinery.admin.delete', title: page_title_with_translations(page))}
}
%>

<div class='clearfix'>
<% if page.children.present? %>
<span class="item icon toggle <%= 'expanded' if Refinery::Pages.auto_expand_admin_tree %>"
title="<%= t('expand_collapse', scope: 'refinery.admin.pages')%>">
</span>
<% else %>
<span class="item"></span>
<% end %>

<span class='title <%= 'toggle' if page.children.present? %>'>
<%= page_title_with_translations page %>
<%= page_meta_information page %>
</span>

<% if Refinery::I18n.frontend_locales.many? %>
<span class='locales'>
<% page.translations.sort_by{ |t| Refinery::I18n.frontend_locales.index(t.locale)}.each do |translation| %>
<% if translation.title.present? %>
<%= link_to refinery.admin_edit_page_path(page.nested_url, switch_locale: translation.locale),
class: 'locale', title: translation.locale.upcase do %>

<div class="<%=translation.locale %> locale_marker">
<%= locale_text_icon(translation.locale.upcase) %>
</div>
<% end %>
<% end %>
<% end %>
</span>
<% end %>

<span class='actions'>
<%= action_icon(:preview, page.url, t('.view_live_html')) %>
<%= action_icon(:add, add_url, t('new', scope: 'refinery.admin.pages' ) ) %>
<%= action_icon(:edit, edit_url , t('edit', scope: 'refinery.admin.pages' ) ) %>
<%= action_icon(:delete, delete_url, t('delete', scope: 'refinery.admin.pages' ), delete_options ) if page.deletable? %>
</span>
</div>
<% end %>
</ul>
<%= render '/refinery/admin/sortable_list', :continue_reordering => !!local_assigns[:continue_reordering] %>

0 comments on commit 4c8a03b

Please sign in to comment.