Set ".active" class to group item when sub item is active
jmuheim opened this issue · comments
Yeah, I agree. Pull request would be accepted.
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.