Class: SimpleNavigation::ItemContainer
- Inherits:
-
Object
- Object
- SimpleNavigation::ItemContainer
- Defined in:
- lib/simple_navigation/core/item_container.rb,
lib/simple_navigation/rails_controller_methods.rb
Overview
Holds the Items for a navigation ‘level’.
Instance Attribute Summary collapse
-
#auto_highlight ⇒ Object
Returns the value of attribute auto_highlight.
-
#dom_class ⇒ Object
Returns the value of attribute dom_class.
-
#dom_id ⇒ Object
Returns the value of attribute dom_id.
-
#items ⇒ Object
Returns the value of attribute items.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
Instance Method Summary collapse
-
#[](navi_key) ⇒ Object
Returns the Item with the specified key, nil otherwise.
-
#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).
-
#active_leaf_container ⇒ Object
Returns the deepest possible active item_container.
-
#empty? ⇒ Boolean
Returns true if there are no items defined for this container.
-
#initialize(level = 1) ⇒ ItemContainer
constructor
:nodoc:.
-
#item(key, name, url, options = {}, &block) ⇒ Object
Creates a new navigation item.
-
#level_for_item(navi_key) ⇒ Object
Returns the level of the item specified by navi_key.
-
#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) ⇒ ItemContainer
:nodoc:
9 10 11 12 13 14 |
# File 'lib/simple_navigation/core/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_highlight ⇒ Object
Returns the value of attribute auto_highlight.
7 8 9 |
# File 'lib/simple_navigation/core/item_container.rb', line 7 def auto_highlight @auto_highlight end |
#dom_class ⇒ Object
Returns the value of attribute dom_class.
7 8 9 |
# File 'lib/simple_navigation/core/item_container.rb', line 7 def dom_class @dom_class end |
#dom_id ⇒ Object
Returns the value of attribute dom_id.
7 8 9 |
# File 'lib/simple_navigation/core/item_container.rb', line 7 def dom_id @dom_id end |
#items ⇒ Object
Returns the value of attribute items.
6 7 8 |
# File 'lib/simple_navigation/core/item_container.rb', line 6 def items @items end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
6 7 8 |
# File 'lib/simple_navigation/core/item_container.rb', line 6 def level @level end |
#renderer ⇒ Object
Returns the value of attribute renderer.
7 8 9 |
# File 'lib/simple_navigation/core/item_container.rb', line 7 def renderer @renderer end |
Instance Method Details
#[](navi_key) ⇒ Object
Returns the Item with the specified key, nil otherwise.
50 51 52 |
# File 'lib/simple_navigation/core/item_container.rb', line 50 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).
101 102 103 104 105 |
# File 'lib/simple_navigation/core/item_container.rb', line 101 def active_item_container_for(desired_level) return self if self.level == desired_level return nil unless return selected_item..active_item_container_for(desired_level) end |
#active_leaf_container ⇒ Object
Returns the deepest possible active item_container. (recursively searches in the sub_navigation if this container has a selected sub_navigation).
109 110 111 112 113 114 115 |
# File 'lib/simple_navigation/core/item_container.rb', line 109 def active_leaf_container if selected_item..active_leaf_container else self end end |
#empty? ⇒ Boolean
Returns true if there are no items defined for this container.
118 119 120 |
# File 'lib/simple_navigation/core/item_container.rb', line 118 def empty? items.empty? 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:
-
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.
37 38 39 |
# File 'lib/simple_navigation/core/item_container.rb', line 37 def item(key, name, url, ={}, &block) (@items << SimpleNavigation::Item.new(self, key, name, url, , nil, &block)) if should_add_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 item cannot be found.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/simple_navigation/core/item_container.rb', line 58 def level_for_item(navi_key) my_item = self[navi_key] return self.level if my_item items.each do |i| if i. level = i..level_for_item(navi_key) return level unless level.nil? end end return 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)
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/simple_navigation/core/item_container.rb', line 73 def render(={}) renderer_instance = if [:renderer] if [:renderer].instance_of?(Symbol) && SimpleNavigation.registered_renderers.key?([:renderer]) SimpleNavigation.registered_renderers[[:renderer]].new() else [:renderer].new() end else self.renderer.new() end renderer_instance.render(self) end |
#selected? ⇒ Boolean
Returns true if any of this container’s items is selected.
88 89 90 |
# File 'lib/simple_navigation/core/item_container.rb', line 88 def selected? items.any? {|i| i.selected?} end |
#selected_item ⇒ Object
Returns the currently selected item, nil if no item is selected.
94 95 96 |
# File 'lib/simple_navigation/core/item_container.rb', line 94 def selected_item items.find {|i| i.selected?} end |