Skip to content

Instantly share code, notes, and snippets.

@jeffutter
Forked from tmaier/bootstrap_topbar_list.rb
Created March 8, 2012 07:27
Show Gist options
  • Save jeffutter/1999394 to your computer and use it in GitHub Desktop.
Save jeffutter/1999394 to your computer and use it in GitHub Desktop.
BootstrapTopbarList for simple-navigation and Twitter Bootstrap integration
# This sort-of works. It got unnecessarily complicated though. Suggestions welcome.
# Renders an ItemContainer as a <ul> element and its containing items as <li> elements.
# Prepared to use inside the topbar of Twitter Bootstrap http://twitter.github.com/bootstrap/#navigation
#
# Register the renderer and use following code in your view:
# render_navigation(level: 1..2, renderer: :bootstrap_topbar_list, expand_all: true)
class BootstrapTopbarList < SimpleNavigation::Renderer::Base
def render(item_container)
if options[:is_subnavigation]
ul_class = "dropdown-menu"
else
ul_class = "nav"
end
list_content = ''
collapseable_list_content = ''
item_container.items.each do |item|
li_options = item.html_options.reject {|k, v| k == :link}
if include_sub_navigation?(item)
li_options[:class] = [li_options[:class], "dropdown"].flatten.compact.join(' ')
end
li_content = tag_for(item)
if include_sub_navigation?(item)
li_content << render_sub_navigation_for(item)
end
li = content_tag(:li, li_content, li_options)
# binding.pry
if item.html_options[:collapseable] and not options[:is_subnavigation]
collapseable_list_content << li
else
list_content << li
end
end
if skip_if_empty? && item_container.empty?
''
else
body = content_tag(:ul, list_content, { :id => item_container.dom_id, :class => [item_container.dom_class, ul_class].flatten.compact.join(' ') })
if options[:is_subnavigation]
body
else
icon_bar = content_tag(:span, nil, {:class => 'icon-bar'})
nav_collapse_button = content_tag(:a, icon_bar*3, {:class => 'btn btn-navbar', 'data-toggle' => 'collapse', 'data-target' => '.nav-collapse'})
nav_collapsed_nav = content_tag(:ul, collapseable_list_content, { :id => item_container.dom_id, :class => [item_container.dom_class, ul_class].flatten.compact.join(' ') })
nav_collapsed = content_tag(:div, nav_collapsed_nav, {:class=>"nav-collapse"})
container = content_tag(:div, [nav_collapse_button, body, nav_collapsed].join(''), {:class => 'container-fluid'})
inner = content_tag(:div, container, {:class => 'navbar-inner'})
content_tag(:div, inner, {:class => 'navbar navbar-fixed-top'})
end
end
end
def render_sub_navigation_for(item)
item.sub_navigation.render(self.options.merge(:is_subnavigation => true))
end
protected
def tag_for(item)
if item.url.nil?
items = ''
items << item.name
if include_sub_navigation?(item) && !options[:is_subnavigation]
items << content_tag(:b, nil, {:class => 'caret'})
end
content_tag('a', items, link_options_for(item).except(:method))
else
link_to(item.name, item.url, link_options_for(item))
end
end
# Extracts the options relevant for the generated link
#
def link_options_for(item)
special_options = {:method => item.method}.reject {|k, v| v.nil? }
link_options = item.html_options[:link] || {}
opts = special_options.merge(link_options)
opts[:class] = [link_options[:class], item.selected_class, dropdown_link_class(item)].flatten.compact.join(' ')
if include_sub_navigation?(item) && !options[:is_subnavigation]
opts['data-toggle'] = "dropdown"
end
if item.url.nil?
opts[:href] = '#'
end
opts.delete(:class) if opts[:class].nil? || opts[:class] == ''
opts
end
def dropdown_link_class(item)
if include_sub_navigation?(item) && !options[:is_subnavigation]
"dropdown-toggle"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment