Class: RocketNavigation::ItemContainer
- Inherits:
-
Object
- Object
- RocketNavigation::ItemContainer
- Defined in:
- lib/rocket_navigation/item_container.rb
Instance Attribute Summary collapse
-
#container_html ⇒ Object
Returns the value of attribute container_html.
-
#item_html ⇒ Object
Returns the value of attribute item_html.
-
#items ⇒ Object
Returns the value of attribute items.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#link_html ⇒ Object
Returns the value of attribute link_html.
-
#options ⇒ Object
Returns the value of attribute options.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
-
#selected_class ⇒ Object
Returns the value of attribute selected_class.
-
#view_context ⇒ Object
Returns the value of attribute view_context.
Instance Method Summary collapse
-
#[](navi_key) ⇒ Object
Returns the Item with the specified key, nil otherwise.
- #default_html_options ⇒ Object
-
#empty? ⇒ Boolean
Returns true if there are no items defined for this container.
-
#initialize(level = 1, options = {}) ⇒ ItemContainer
constructor
A new instance of ItemContainer.
- #inspect ⇒ Object
- #item(key, name, url = nil, options = {}, &block) ⇒ Object
-
#level_for_item(navi_key) ⇒ Object
Returns the level of the item specified by navi_key.
- #new_child ⇒ Object
-
#render(options = {}) ⇒ Object
Renders the items in this ItemContainer using the configured renderer.
-
#selected? ⇒ Boolean
Returns true if any of this container’s items is selected.
-
#selected_item ⇒ Object
Returns the currently selected item, nil if no item is selected.
Constructor Details
#initialize(level = 1, options = {}) ⇒ ItemContainer
Returns a new instance of ItemContainer.
22 23 24 25 26 27 28 |
# File 'lib/rocket_navigation/item_container.rb', line 22 def initialize(level = 1, = {}) @level = level @items ||= [] @options = @renderer = RocketNavigation.config.renderer end |
Instance Attribute Details
#container_html ⇒ Object
Returns the value of attribute container_html.
6 7 8 |
# File 'lib/rocket_navigation/item_container.rb', line 6 def container_html @container_html end |
#item_html ⇒ Object
Returns the value of attribute item_html.
6 7 8 |
# File 'lib/rocket_navigation/item_container.rb', line 6 def item_html @item_html end |
#items ⇒ Object
Returns the value of attribute items.
4 5 6 |
# File 'lib/rocket_navigation/item_container.rb', line 4 def items @items end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
4 5 6 |
# File 'lib/rocket_navigation/item_container.rb', line 4 def level @level end |
#link_html ⇒ Object
Returns the value of attribute link_html.
6 7 8 |
# File 'lib/rocket_navigation/item_container.rb', line 6 def link_html @link_html end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/rocket_navigation/item_container.rb', line 3 def @options end |
#renderer ⇒ Object
Returns the value of attribute renderer.
3 4 5 |
# File 'lib/rocket_navigation/item_container.rb', line 3 def renderer @renderer end |
#selected_class ⇒ Object
Returns the value of attribute selected_class.
6 7 8 |
# File 'lib/rocket_navigation/item_container.rb', line 6 def selected_class @selected_class end |
#view_context ⇒ Object
Returns the value of attribute view_context.
3 4 5 |
# File 'lib/rocket_navigation/item_container.rb', line 3 def view_context @view_context end |
Instance Method Details
#[](navi_key) ⇒ Object
Returns the Item with the specified key, nil otherwise.
53 54 55 |
# File 'lib/rocket_navigation/item_container.rb', line 53 def [](navi_key) items.find { |item| item.key == navi_key } end |
#default_html_options ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rocket_navigation/item_container.rb', line 8 def if [:no_default_classes] @container_html = {} @item_html = {} @link_html = {} @selected_class = {} else @container_html = {class: "nav"} @item_html = {class: 'nav-item'} @link_html = {class: 'nav-link'} @selected_class = {branch: "active-branch", item: "active", link: "active"} end end |
#empty? ⇒ Boolean
Returns true if there are no items defined for this container.
94 95 96 |
# File 'lib/rocket_navigation/item_container.rb', line 94 def empty? items.empty? end |
#inspect ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rocket_navigation/item_container.rb', line 98 def inspect "#<RocketNavigation::ItemContainer:#{object_id} @renderer=#{@renderer.inspect} @options=#{@options.inspect} @items=#{@items.inspect} @level=#{@level.inspect} @container_html=#{@container_html.inspect} @item_html=#{@item_html.inspect} @link_html=#{@link_html.inspect} @selected_class=#{@selected_class.inspect} @view_context=#{view_context.nil? ? nil : "[rails view context, hidden from inspect]"} >" end |
#item(key, name, url = nil, options = {}, &block) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/rocket_navigation/item_container.rb', line 36 def item(key, name, url = nil, = {}, &block) return unless should_add_item?() key = url if key.nil? item = Item.new(self, key, name, url, , &block) add_item item, end |
#level_for_item(navi_key) ⇒ Object
Returns the level of the item specified by navi_key. Recursively works its way down the item’s sub_navigations if the desired item is not found directly in this container’s items. Returns nil if item cannot be found.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rocket_navigation/item_container.rb', line 62 def level_for_item(navi_key) return level if self[navi_key] items.each do |item| next unless item. level = item..level_for_item(navi_key) return level if level end return nil end |
#new_child ⇒ Object
30 31 32 33 34 |
# File 'lib/rocket_navigation/item_container.rb', line 30 def new_child child = ItemContainer.new(level + 1, ) child.view_context = view_context child end |
#render(options = {}) ⇒ Object
Renders the items in this ItemContainer using the configured renderer.
The options are the same as in the view’s render_navigation call (they get passed on)
77 78 79 |
# File 'lib/rocket_navigation/item_container.rb', line 77 def render( = {}) renderer_instance().render(self) end |
#selected? ⇒ Boolean
Returns true if any of this container’s items is selected.
83 84 85 |
# File 'lib/rocket_navigation/item_container.rb', line 83 def selected? items.any?(&:selected?) end |
#selected_item ⇒ Object
Returns the currently selected item, nil if no item is selected.
89 90 91 |
# File 'lib/rocket_navigation/item_container.rb', line 89 def selected_item items.find(&:selected?) end |