Skip to content

Commit

Permalink
Modernise the menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed Dec 3, 2010
1 parent eeca523 commit bcb75bd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
16 changes: 11 additions & 5 deletions vendor/refinerycms/core/app/views/shared/_menu.html.erb
Expand Up @@ -3,14 +3,19 @@
css ||= 'menu'
hide_children = RefinerySetting.find_or_set(:menu_hide_children, false) unless defined?(hide_children)
cache_key = [Refinery.base_cache_key]
cache_key << dom_id
cache_key << (action_suffix ||= RefinerySetting.find_or_set(:refinery_menu_cache_action_suffix, "site_menu"))
cache_key << extra_suffix if defined?(extra_suffix) and extra_suffix.present?
cache_key << request.path
cache(cache_key.compact.join('_')) do
# Select top menu items unless 'roots' is passed in.
cache_if(RefinerySetting.find_or_set(:menu_cache_enabled, true), cache_key.compact.join('_')) do
# Select top menu items unless 'roots' is supplied.
collection ||= @menu_pages
roots ||= collection.select { |m| m.parent_id == nil }
if roots.any?
# In order to match items that aren't shown in menu and highlight their associations.
# This can be supplied if the logic different in your case.
unless defined?(selected_item)
selected_item = collection.detect{|page| selected_page?(page)}
selected_item = @page if selected_item.nil?
end
if (roots ||= collection.select { |m| m.parent_id.nil? }).any?
sibling_count = roots.size - 1
-%>
<nav id='<%= dom_id %>' class='<%= %W(#{css} clearfix).join(' ') %>'>
Expand All @@ -21,6 +26,7 @@
:hide_children => hide_children,
:sibling_count => sibling_count,
:collection => collection,
:selected_item => selected_item,
:apply_css => true #if you don't care about class='first' class='last' or class='selected' set apply_css to false for speed.
}
-%>
Expand Down
16 changes: 8 additions & 8 deletions vendor/refinerycms/core/app/views/shared/_menu_branch.html.erb
@@ -1,26 +1,26 @@
<%
css = if ((defined?(apply_css) && apply_css) || !defined?(apply_css)) and
(classes = css_for_menu_branch(menu_branch, menu_branch_counter, sibling_count||=nil)).any?
"class='#{classes.join(' ')}'"
if !!local_assigns[:apply_css] and (classes = css_for_menu_branch(menu_branch, menu_branch_counter, sibling_count||=nil, collection, selected_item ||= nil)).any?
css = "class='#{classes.join(' ')}'"
end
dom_id = ("id='item_#{menu_branch_counter}'" if menu_branch.parent_id.nil? and menu_branch.title.present?)

hide_children = (defined?(hide_children) && hide_children)
children = hide_children ? [] : collection.select { |p| p.parent_id == menu_branch.id && p.in_menu? }
-%>
<li<%= ['', css, dom_id].join(' ').gsub(/\ *$/, '') %>>
<%= link_to menu_branch.title, menu_branch.url %>
<% if children.present? %>
<li<%= ['', css, dom_id].compact.join(' ').gsub(/\ *$/, '') %>>
<%= link_to menu_branch.title, menu_branch.url -%>
<% if children.present? -%>
<ul class='clearfix'>
<%=
render :partial => "/shared/menu_branch",
:collection => children,
:locals => {
:apply_css => local_assigns[:apply_css],
:hide_children => hide_children,
:collection => collection,
:selected_item => selected_item,
:sibling_count => children.size - 1
} -%>
</ul>
<% end %>
<% end -%>
</li>
38 changes: 34 additions & 4 deletions vendor/refinerycms/core/lib/refinery/helpers/menu_helper.rb
Expand Up @@ -2,20 +2,50 @@ module Refinery
module Helpers
module MenuHelper

# Adds conditional caching
def cache_if(condition, name = {}, &block)
if condition
cache(name, &block)
else
yield
end

# for <%= style helpers vs <% style
nil
end

# This was extracted from REFINERY_ROOT/vendor/plugins/refinery/app/views/shared/_menu_branch.html.erb
# to remove the complexity of that template by reducing logic in the view.
def css_for_menu_branch(menu_branch, menu_branch_counter, sibling_count = nil)
def css_for_menu_branch(menu_branch, menu_branch_counter, sibling_count = nil, collection = [], selected_item = nil)
css = []
css << "selected" if selected_page?(menu_branch) or descendant_page_selected?(menu_branch)
css << "selected" if selected_page_or_descendant_page_selected?(menu_branch, collection, selected_item)
css << "first" if menu_branch_counter == 0
css << "last" if menu_branch_counter == (sibling_count ||= menu_branch.shown_siblings.size)
css
end

# Determines whether any page underneath the supplied page is the current page according to rails.
# Just calls selected_page? for each descendant of the supplied page.
def descendant_page_selected?(page)
page.has_descendants? and page.descendants.any? {|descendant| selected_page?(descendant) }
# if you pass a collection it won't check its own descendants but use the collection supplied.
def descendant_page_selected?(page, collection = [], selected_item = nil)
return false unless page.has_descendants? or (selected_item && !selected_item.in_menu?)

descendants = if collection.present? and (!selected_item or (selected_item && selected_item.in_menu?))
collection.select{ |item| item.parent_id == page.id }
else
page.descendants
end

descendants.any? do |descendant|
selected_item ? selected_item == descendant : selected_page?(descendant)
end
end

def selected_page_or_descendant_page_selected?(page, collection = [], selected_item = nil)
selected = false
selected = selected_item ? selected_item === page : selected_page?(page)
selected = descendant_page_selected?(page, collection, selected_item) unless selected
selected
end

# Determine whether the supplied page is the currently open page according to Refinery.
Expand Down

0 comments on commit bcb75bd

Please sign in to comment.