Class: RocketNavigation::ItemContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/rocket_navigation/item_container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @level = level
  @items ||= []
  @options = options
  @renderer = RocketNavigation.config.renderer
  default_html_options
end

Instance Attribute Details

#container_htmlObject

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_htmlObject

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

#itemsObject

Returns the value of attribute items.



4
5
6
# File 'lib/rocket_navigation/item_container.rb', line 4

def items
  @items
end

#levelObject (readonly)

Returns the value of attribute level.



4
5
6
# File 'lib/rocket_navigation/item_container.rb', line 4

def level
  @level
end

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

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/rocket_navigation/item_container.rb', line 3

def options
  @options
end

#rendererObject

Returns the value of attribute renderer.



3
4
5
# File 'lib/rocket_navigation/item_container.rb', line 3

def renderer
  @renderer
end

#selected_classObject

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_contextObject

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_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rocket_navigation/item_container.rb', line 8

def default_html_options
  if options[: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.

Returns:

  • (Boolean)


94
95
96
# File 'lib/rocket_navigation/item_container.rb', line 94

def empty?
  items.empty?
end

#inspectObject



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, options = {}, &block)
  return unless should_add_item?(options)
  key = url if key.nil?
  item = Item.new(self, key, name, url, options, &block)
  add_item item, options
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.sub_navigation
    level = item.sub_navigation.level_for_item(navi_key)
    return level if level
  end
  return nil
end

#new_childObject



30
31
32
33
34
# File 'lib/rocket_navigation/item_container.rb', line 30

def new_child
  child = ItemContainer.new(level + 1, options)
  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(options = {})
  renderer_instance(options).render(self)
end

#selected?Boolean

Returns true if any of this container’s items is selected.

Returns:

  • (Boolean)


83
84
85
# File 'lib/rocket_navigation/item_container.rb', line 83

def selected?
  items.any?(&:selected?)
end

#selected_itemObject

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