codeplant/simple-navigation

Remove selected class from parent of selected item?

Opened this issue · 3 comments

TL;DR: I'm trying to get around the sub-navigation rule of the active navigation item highlighting rules.

With this config/navigation.rb file:

navigation.items do |primary|
  primary.item :events, "Events", events_path, do |events|
    events.item :dashboard, "Dashboard", events_dashboard_path
    events.item :documents, "Documents", events_documents_path do |documents|
      documents.item :invitations, "Invitations", events_documents_path(type: "invitations")
      documents.item :rsvps, "RSVPs", events_documents_path(type: "rsvps")
      documents.item :unanswered, "Unanswered Invitations", events_documents_path(filter: "unanswered")
    end
  end
end

When viewing "Invitations", I'd like "Documents" to not get the selected class, but for "Events" to still get the selected class. I passed in a highlights_on lambda, like this:

    events.item :documents, "Documents", events_documents_path, highlights_on: lambda { current_page_is_documents? } do |documents|
      documents.item :invitations, "Invitations", events_documents_path(type: "invitations"), highlights_on: lambda { current_page_is_invitations? }

But this is overridden by the Item#selected? method, which is used for both tree expansion and the call to selected_class.

Is what I'm trying to do possible with some off-the-shelf configuration options? I've worked up a solution with a custom renderer. Using the public API, highlighting for children can only be turned off globally. To get item-level control (i.e., "Events" gets selected but "Documents" does not), I have this method in my renderer:

def should_highlight?(item)
  child_highlighting = !item.send(:options)[:no_highlight_for_children]
  return false if child_highlighting && !item.selected_class.nil? && item.active_leaf_class.nil?
  true
end

Which I'm not in love with, but gets the job done.

Hi @briancgraham. Another solution would be to set the :highlights_on option the parent with a regex that would exclude the invitations child :)

Thanks - worked great!

Awesome 😄