Class: SimpleNavigation::ItemContainer

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

Overview

Holds the Items for a navigation ‘level’.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = 1) ⇒ ItemContainer

:nodoc:



16
17
18
19
20
21
22
# File 'lib/simple_navigation/item_container.rb', line 16

def initialize(level = 1) # :nodoc:
  @level = level
  @items ||= []
  @renderer = SimpleNavigation.config.renderer
  @auto_highlight = true
  @dom_attributes = {}
end

Instance Attribute Details

#auto_highlightObject

rubocop:disable Metrics/ClassLength



6
7
8
# File 'lib/simple_navigation/item_container.rb', line 6

def auto_highlight
  @auto_highlight
end

#dom_attributesObject



24
25
26
27
28
29
30
31
32
# File 'lib/simple_navigation/item_container.rb', line 24

def dom_attributes
  # backward compability for #dom_id and #dom_class
  dom_id_and_class = {
    id: dom_id,
    class: dom_class
  }.compact

  @dom_attributes.merge(dom_id_and_class)
end

#dom_classObject

rubocop:disable Metrics/ClassLength



6
7
8
# File 'lib/simple_navigation/item_container.rb', line 6

def dom_class
  @dom_class
end

#dom_idObject

rubocop:disable Metrics/ClassLength



6
7
8
# File 'lib/simple_navigation/item_container.rb', line 6

def dom_id
  @dom_id
end

#itemsObject

Returns the value of attribute items.



12
13
14
# File 'lib/simple_navigation/item_container.rb', line 12

def items
  @items
end

#levelObject (readonly)

Returns the value of attribute level.



12
13
14
# File 'lib/simple_navigation/item_container.rb', line 12

def level
  @level
end

#rendererObject

rubocop:disable Metrics/ClassLength



6
7
8
# File 'lib/simple_navigation/item_container.rb', line 6

def renderer
  @renderer
end

#selected_classObject

rubocop:disable Metrics/ClassLength



6
7
8
# File 'lib/simple_navigation/item_container.rb', line 6

def selected_class
  @selected_class
end

Instance Method Details

#[](navi_key) ⇒ Object

Returns the Item with the specified key, nil otherwise.



84
85
86
# File 'lib/simple_navigation/item_container.rb', line 84

def [](navi_key)
  items.find { |item| item.key == navi_key }
end

#active_item_container_for(desired_level) ⇒ Object

Returns the active item_container for the specified level (recursively looks up items in selected sub_navigation if level is deeper than this container’s level).



128
129
130
131
132
133
134
# File 'lib/simple_navigation/item_container.rb', line 128

def active_item_container_for(desired_level)
  if level == desired_level
    self
  elsif selected_sub_navigation?
    selected_item.sub_navigation.active_item_container_for(desired_level)
  end
end

#active_leaf_containerObject

Returns the deepest possible active item_container. (recursively searches in the sub_navigation if this container has a selected sub_navigation).



139
140
141
142
143
144
145
# File 'lib/simple_navigation/item_container.rb', line 139

def active_leaf_container
  if selected_sub_navigation?
    selected_item.sub_navigation.active_leaf_container
  else
    self
  end
end

#empty?Boolean

Returns true if there are no items defined for this container.

Returns:

  • (Boolean)


148
149
150
# File 'lib/simple_navigation/item_container.rb', line 148

def empty?
  items.empty?
end

#item(key, name, url = nil, options = {}, &block) ⇒ Object

Creates a new navigation item.

The key is a symbol which uniquely defines your navigation item in the scope of the primary_navigation or the sub_navigation.

The name will be displayed in the rendered navigation. This can also be a call to your I18n-framework.

The url is the address that the generated item points to. You can also use url_helpers (named routes, restful routes helper, url_for, etc). url is optional - items without URLs should not be rendered as links.

The options can be used to specify the following things:

  • any html_attributes - will be included in the rendered navigation item (e.g. id, class etc.)

  • :if - Specifies a proc to call to determine if the item should be rendered (e.g. if: Proc.new { current_user.admin? }). The proc should evaluate to a true or false value and is evaluated in the context of the view.

  • :unless - Specifies a proc to call to determine if the item should not be rendered (e.g. unless: Proc.new { current_user.admin? }). The proc should evaluate to a true or false value and is evaluated in the context of the view.

  • :method - Specifies the http-method for the generated link - default is :get.

  • :highlights_on - if autohighlighting is turned off and/or you want to explicitly specify when the item should be highlighted, you can set a regexp which is matched againstthe current URI.

The block - if specified - will hold the item’s sub_navigation.



66
67
68
69
70
71
# File 'lib/simple_navigation/item_container.rb', line 66

def item(key, name, url = nil, options = {}, &block)
  return unless should_add_item?(options)

  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.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simple_navigation/item_container.rb', line 93

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
  nil
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)



109
110
111
# File 'lib/simple_navigation/item_container.rb', line 109

def render(options = {})
  renderer_instance(options).render(self)
end

#selected?Boolean

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

Returns:

  • (Boolean)


115
116
117
# File 'lib/simple_navigation/item_container.rb', line 115

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

#selected_itemObject

Returns the currently selected item, nil if no item is selected.



121
122
123
# File 'lib/simple_navigation/item_container.rb', line 121

def selected_item
  items.find(&:selected?)
end