Class: SimpleNavigation::Item
- Inherits:
-
Object
- Object
- SimpleNavigation::Item
- Defined in:
- lib/simple_navigation/core/item.rb,
lib/simple_navigation/rails_controller_methods.rb
Overview
Represents an item in your navigation. Gets generated by the item method in the config-file.
Instance Attribute Summary collapse
-
#exclude_highlighting ⇒ Object
readonly
Returns the value of attribute exclude_highlighting.
-
#highlights_on ⇒ Object
readonly
Returns the value of attribute highlights_on.
-
#html_options ⇒ Object
Returns the html-options hash for the item, i.e.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sub_navigation ⇒ Object
readonly
Returns the value of attribute sub_navigation.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(container, key, name, url, options, items = nil, &sub_nav_block) ⇒ Item
constructor
see ItemContainer#item.
- #item_excluded ⇒ Object
-
#selected? ⇒ Boolean
Returns true if this navigation item should be rendered as ‘selected’.
- #selected_by_config? ⇒ Boolean
-
#selected_class ⇒ Object
Returns the configured selected_class if the item is selected, nil otherwise.
Constructor Details
#initialize(container, key, name, url, options, items = nil, &sub_nav_block) ⇒ Item
see ItemContainer#item
The subnavigation (if any) is either provided by a block or passed in directly as items
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/simple_navigation/core/item.rb', line 11 def initialize(container, key, name, url, , items=nil, &sub_nav_block) @container = container @container.dom_class = .delete(:container_class) if [:container_class] @container.dom_id = .delete(:container_id) if [:container_id] @key = key @method = .delete(:method) @name = name @url = url.instance_of?(Proc) ? url.call : url @highlights_on = .delete(:highlights_on) @exclude_highlighting = .delete(:exclude_highlight_if) @html_options = if sub_nav_block || items @sub_navigation = ItemContainer.new(@container.level + 1) sub_nav_block.call @sub_navigation if sub_nav_block @sub_navigation.items = items if items end end |
Instance Attribute Details
#exclude_highlighting ⇒ Object (readonly)
Returns the value of attribute exclude_highlighting.
5 6 7 |
# File 'lib/simple_navigation/core/item.rb', line 5 def exclude_highlighting @exclude_highlighting end |
#highlights_on ⇒ Object (readonly)
Returns the value of attribute highlights_on.
5 6 7 |
# File 'lib/simple_navigation/core/item.rb', line 5 def highlights_on @highlights_on end |
#html_options ⇒ Object
Returns the html-options hash for the item, i.e. the options specified for this item in the config-file. It also adds the ‘selected’ class to the list of classes if necessary.
42 43 44 45 46 47 48 |
# File 'lib/simple_navigation/core/item.rb', line 42 def = self.autogenerate_item_ids? ? {:id => autogenerated_item_id} : {} = .merge(@html_options) [:class] = [@html_options[:class], self.selected_class].flatten.compact.join(' ') .delete(:class) if [:class].nil? || [:class] == '' end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/simple_navigation/core/item.rb', line 5 def key @key end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/simple_navigation/core/item.rb', line 5 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/simple_navigation/core/item.rb', line 5 def name @name end |
#sub_navigation ⇒ Object (readonly)
Returns the value of attribute sub_navigation.
5 6 7 |
# File 'lib/simple_navigation/core/item.rb', line 5 def @sub_navigation end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
5 6 7 |
# File 'lib/simple_navigation/core/item.rb', line 5 def url @url end |
Instance Method Details
#item_excluded ⇒ Object
50 51 52 53 54 55 |
# File 'lib/simple_navigation/core/item.rb', line 50 def item_excluded if exclude_highlighting raise ArgumentError, ':exclude_highlight_if must be a regexp' unless exclude_highlighting.instance_of?(Regexp) SimpleNavigation.request_uri =~ exclude_highlighting end end |
#selected? ⇒ Boolean
Returns true if this navigation item should be rendered as ‘selected’. An item is selected if
-
it has been explicitly selected in a controller or
-
it has a subnavigation and one of its subnavigation items is selected or
-
its url matches the url of the current request (auto highlighting)
36 37 38 |
# File 'lib/simple_navigation/core/item.rb', line 36 def selected? @selected = (@selected || selected_by_config? || || selected_by_url?) end |
#selected_by_config? ⇒ Boolean
74 75 76 |
# File 'lib/simple_navigation/core/item.rb', line 74 def selected_by_config? false end |
#selected_class ⇒ Object
Returns the configured selected_class if the item is selected, nil otherwise
58 59 60 61 62 63 64 65 |
# File 'lib/simple_navigation/core/item.rb', line 58 def selected_class if selected? self.item_excluded ? nil : SimpleNavigation.config.selected_class # SimpleNavigation.config.selected_class else nil end end |