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:



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

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

Instance Attribute Details

#auto_highlightObject

Returns the value of attribute auto_highlight.



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

def auto_highlight
  @auto_highlight
end

#dom_classObject

Returns the value of attribute dom_class.



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

def dom_class
  @dom_class
end

#dom_idObject

Returns the value of attribute dom_id.



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

def dom_id
  @dom_id
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#rendererObject

Returns the value of attribute renderer.



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

def renderer
  @renderer
end

Instance Method Details

#[](navi_key) ⇒ Object

Returns the Item with the specified key, nil otherwise.



40
41
42
# File 'lib/simple_navigation/item_container.rb', line 40

def [](navi_key)
  items.find {|i| i.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).



89
90
91
92
93
# File 'lib/simple_navigation/item_container.rb', line 89

def active_item_container_for(desired_level)
  return self if self.level == desired_level
  return nil unless selected_sub_navigation?
  return selected_item.sub_navigation.active_item_container_for(desired_level)
end

#current_explicit_navigationObject

Returns the current navigation that has been explicitely defined in the controller for this container’s level. Returns nil if no explicit current navigation has been set.



82
83
84
# File 'lib/simple_navigation/item_container.rb', line 82

def current_explicit_navigation
  SimpleNavigation.current_navigation_for(level)
end

#item(key, name, url, 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.)

The options can be used to specify the following things:

  • 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.

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



34
35
36
# File 'lib/simple_navigation/item_container.rb', line 34

def item(key, name, url, options={}, &block)
  (@items << SimpleNavigation::Item.new(self, key, name, url, options, block)) if should_add_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 item cannot be found.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/simple_navigation/item_container.rb', line 48

def level_for_item(navi_key)
  my_item = self[navi_key]
  return self.level if my_item
  items.each do |i|
    if i.sub_navigation
      level = i.sub_navigation.level_for_item(navi_key)
      return level unless level.nil?
    end
  end
  return nil
end

#render(include_sub_navigation = false, options = {}) ⇒ Object

Renders the items in this ItemContainer using the configured renderer.

Set include_sub_navigation to true if you want to nest the sub_navigation into the active parent_navigation



63
64
65
# File 'lib/simple_navigation/item_container.rb', line 63

def render(include_sub_navigation=false, options={})
  self.renderer.new.render(self, include_sub_navigation, options)
end

#selected?Boolean

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

Returns:

  • (Boolean)


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

def selected?
  items.any? {|i| i.selected?}
end

#selected_itemObject

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



75
76
77
# File 'lib/simple_navigation/item_container.rb', line 75

def selected_item
  self[current_explicit_navigation] || items.find {|i| i.selected?}
end