jejacks0n / navigasmic

Navigasmic: Semantic navigation for Rails using simple view level or configuration definitions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set ".active" class to group item when sub item is active

jmuheim opened this issue · comments

A sub item is active:
image

But the parent item is not active:
image

Yeah, I agree. Pull request would be accepted.

I just added a PR. With it, it looks like this:

image

The active class is set on the <li> of the group, too, when it has an active child.

The mentioned pull request is very dirty and should never be merged into master.

If anybody still wants to use the dirty (but in most cases working) code, one can simply add the following to config/navigasmic.rb:

class BetterListBuilder < Navigasmic::Builder::ListBuilder
  def structure_for(label, link = false, options = {}, &block)
    content = ''

    if block_given?
      merge_classes!(options, @config.has_nested_class)
      content = content_tag(@config.group_tag, capture(&block), {class: @config.is_nested_class})
    end

    merge_classes!(options, 'active') if has_active_child?(content)
    label = label_for(label, link, block_given?, options)
    content_tag(@config.item_tag, "#{label}#{content}".html_safe, options)
  end

  # FIXME: This is a very dirty, error-prone hack, because the groups and items are rendered directly to HTML!
  # We should maintain something like a tree structure of groups and items that can really check upon active children.
  def has_active_child?(content)
    content =~ /<li class="active">/
  end
end

Navigasmic.setup do |config|
  # ...
  config.default_builder = BetterListBuilder
  # ...
end

Still, a proper implementation of this functionality would be very welcome.