Skip to content

Commit ae6674b

Browse files
author
Dan Petker
committed
Output buttons for dropdown toggles
1 parent 1cbfa07 commit ae6674b

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

lib/simple_navigation/rendering/renderer/bootstrap.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ module Renderer
33
class Bootstrap < SimpleNavigation::Renderer::Base
44
def render(item_container)
55
return '' if respond_to?(:skip_if_empty?) && skip_if_empty? && item_container.empty?
6+
67
config_selected_class = SimpleNavigation.config.selected_class
78
SimpleNavigation.config.selected_class = 'active'
9+
810
list_content = item_container.items.inject([]) do |list, item|
11+
912
li_options = item.html_options.reject {|k, v| k == :link}
1013
icon = li_options.delete(:icon)
1114
dropdown = item_container.dropdown.nil? ? true : item_container.dropdown
1215
split = item_container.split
1316
split = (include_sub_navigation?(item) and li_options.delete(:split)) if li_options.include?(:split)
1417
dropdown = (include_sub_navigation?(item) and li_options.delete(:dropdown)) if li_options.include?(:dropdown)
1518
li_content = tag_for(item, item.name, icon, split, dropdown)
19+
1620
if include_sub_navigation?(item)
1721
if split
1822
lio = li_options.dup
@@ -22,47 +26,65 @@ def render(item_container)
2226
li_options[:id] = nil
2327
li_content = tag_for(item)
2428
end
29+
2530
item.sub_navigation.dom_class = [item.sub_navigation.dom_class, dropdown ? 'dropdown-menu' : nil, split ? 'pull-right' : nil].flatten.compact.join(' ')
2631
li_content << render_sub_navigation_for(item)
2732
li_options[:class] = [li_options[:class], dropdown ? 'dropdown' : nil, split ? 'dropdown-split-right' : nil].flatten.compact.join(' ')
2833
end
34+
2935
list << content_tag(:li, li_content, li_options)
3036
end.join
37+
3138
SimpleNavigation.config.selected_class = config_selected_class
39+
3240
if item_container.respond_to?(:dom_attributes)
3341
dom_attributes = item_container.dom_attributes
3442
else
3543
# supports simple-navigation before the ItemContainer#dom_attributes
3644
dom_attributes = {:id => item_container.dom_id, :class => item_container.dom_class}
3745
end
46+
3847
content_tag(:ul, list_content, dom_attributes)
3948
end
4049

4150
protected
4251

4352
def tag_for(item, name = '', icon = nil, split = false, dropdown = false)
44-
unless item.url or include_sub_navigation?(item)
53+
include_sub_nav = include_sub_navigation?(item)
54+
55+
unless item.url or include_sub_nav
4556
return item.name
4657
end
58+
4759
url = item.url
4860
link = Array.new
4961
link << content_tag(:i, '', :class => [icon].flatten.compact.join(' ')) unless icon.nil?
5062
link << name
51-
if include_sub_navigation?(item)
63+
64+
if include_sub_nav
5265
item_options = item.html_options
5366
item_options[:link] = Hash.new if item_options[:link].nil?
5467
item_options[:link][:class] = Array.new if item_options[:link][:class].nil?
68+
5569
unless split
5670
if dropdown
5771
item_options[:link][:class] << 'dropdown-toggle'
5872
item_options[:link][:'data-toggle'] = 'dropdown'
59-
item_options[:link][:'data-target'] = '#'
73+
item_options[:link][:type] = 'button'
74+
item_options[:link][:'aria-haspopup'] = true
75+
item_options[:link][:'aria-expanded'] = false
6076
end
6177
link << content_tag(:b, '', :class => 'caret')
6278
end
79+
6380
item.html_options = item_options
6481
end
65-
link_to(link.join(" ").html_safe, url, options_for(item))
82+
83+
if include_sub_nav && dropdown
84+
content_tag(:button, link.join(" ").html_safe, options_for(item))
85+
else
86+
link_to(link.join(" ").html_safe, url, options_for(item))
87+
end
6688
end
6789

6890
end

0 commit comments

Comments
 (0)