Class: SimpleNavigation::Item
- Inherits:
-
Object
- Object
- SimpleNavigation::Item
- Defined in:
- lib/simple_navigation/item.rb
Overview
Represents an item in your navigation. Gets generated by the item method in the config-file.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#name(options = {}) ⇒ Object
readonly
Returns the item’s name.
-
#options ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#sub_navigation ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#url ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
Instance Method Summary collapse
-
#active_leaf_class ⇒ Object
Returns the configured active_leaf_class if the item is the selected leaf, nil otherwise.
-
#highlights_on ⇒ Object
Returns the :highlights_on option as set at initialization.
-
#html_options ⇒ Object
Returns the html-options hash for the item, i.e.
-
#initialize(container, key, name, url = nil, opts = {}, &sub_nav_block) ⇒ Item
constructor
see ItemContainer#item.
-
#link_html_options ⇒ Object
Returns the html attributes for the link as set with the :link_html option at initialization.
-
#method ⇒ Object
Returns the :method option as set at initialization.
-
#selected? ⇒ Boolean
Returns true if this navigation item should be rendered as ‘selected’.
-
#selected_class ⇒ Object
Returns the configured selected_class if the item is selected, nil otherwise.
Constructor Details
#initialize(container, key, name, url = nil, opts = {}, &sub_nav_block) ⇒ Item
see ItemContainer#item
The subnavigation (if any) is either provided by a block or passed in directly as items
17 18 19 20 21 22 23 24 25 |
# File 'lib/simple_navigation/item.rb', line 17 def initialize(container, key, name, url = nil, opts = {}, &sub_nav_block) self.container = container self.key = key self.name = name.respond_to?(:call) ? name.call : name self.url = url.respond_to?(:call) ? url.call : url self. = opts ([:items], &sub_nav_block) end |
Instance Attribute Details
#key ⇒ Object
rubocop:disable Metrics/ClassLength
7 8 9 |
# File 'lib/simple_navigation/item.rb', line 7 def key @key end |
#name(options = {}) ⇒ Object
Returns the item’s name. If :apply_generator option is set to true (default), the name will be passed to the name_generator specified in the configuration.
7 8 9 |
# File 'lib/simple_navigation/item.rb', line 7 def name @name end |
#options ⇒ Object
rubocop:disable Metrics/ClassLength
7 8 9 |
# File 'lib/simple_navigation/item.rb', line 7 def @options end |
#sub_navigation ⇒ Object
rubocop:disable Metrics/ClassLength
7 8 9 |
# File 'lib/simple_navigation/item.rb', line 7 def @sub_navigation end |
#url ⇒ Object
rubocop:disable Metrics/ClassLength
7 8 9 |
# File 'lib/simple_navigation/item.rb', line 7 def url @url end |
Instance Method Details
#active_leaf_class ⇒ Object
Returns the configured active_leaf_class if the item is the selected leaf, nil otherwise
67 68 69 70 71 |
# File 'lib/simple_navigation/item.rb', line 67 def active_leaf_class return unless ! && selected_by_condition? config.active_leaf_class end |
#highlights_on ⇒ Object
Returns the :highlights_on option as set at initialization
82 83 84 |
# File 'lib/simple_navigation/item.rb', line 82 def highlights_on @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.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/simple_navigation/item.rb', line 54 def html_opts = .fetch(:html) { {} } html_opts[:id] ||= autogenerated_item_id classes = [html_opts[:class], selected_class, active_leaf_class] classes = classes.flatten.compact.join(' ') html_opts[:class] = classes if classes && !classes.empty? html_opts end |
#link_html_options ⇒ Object
Returns the html attributes for the link as set with the :link_html option at initialization
93 94 95 96 97 98 99 |
# File 'lib/simple_navigation/item.rb', line 93 def @link_html_options ||= begin = [:link_html] || {} [:'aria-current'] = 'page' if ! && selected_by_condition? end end |
#method ⇒ Object
Returns the :method option as set at initialization
87 88 89 |
# File 'lib/simple_navigation/item.rb', line 87 def method @method ||= [:method] end |
#selected? ⇒ Boolean
Returns true if this navigation item should be rendered as ‘selected’. An item is selected if
-
it has a subnavigation and one of its subnavigation items is selected or
-
its url matches the url of the current request (auto highlighting)
47 48 49 |
# File 'lib/simple_navigation/item.rb', line 47 def selected? @selected ||= || selected_by_condition? end |
#selected_class ⇒ Object
Returns the configured selected_class if the item is selected, nil otherwise
75 76 77 78 79 |
# File 'lib/simple_navigation/item.rb', line 75 def selected_class return unless selected? container.selected_class || config.selected_class end |