class SimpleNavigation::Item

Represents an item in your navigation. Gets generated by the item method in the config-file.

Attributes

container[RW]
key[RW]
name[RW]
options[RW]
sub_navigation[RW]
url[RW]

Public Class Methods

new(container, key, name, url = nil, opts = {}, &sub_nav_block) click to toggle source

see SimpleNavigation::ItemContainer#item

The subnavigation (if any) is either provided by a block or passed in directly as items

# File lib/simple_navigation/item.rb, line 14
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.options = opts

  setup_sub_navigation(options[:items], &sub_nav_block)
end

Public Instance Methods

active_leaf_class() click to toggle source

Returns the configured #active_leaf_class if the item is the selected leaf, nil otherwise

# File lib/simple_navigation/item.rb, line 64
def active_leaf_class
  if !selected_by_subnav? && selected_by_condition?
    config.active_leaf_class
  end
end
highlights_on() click to toggle source

Returns the :highlights_on option as set at initialization

# File lib/simple_navigation/item.rb, line 79
def highlights_on
  @highlights_on ||= options[:highlights_on]
end
html_options() click to toggle source

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.

# File lib/simple_navigation/item.rb, line 51
def html_options
  html_opts = options.fetch(:html) { Hash.new }
  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
method() click to toggle source

Returns the :method option as set at initialization

# File lib/simple_navigation/item.rb, line 84
def method
  @method ||= options[:method]
end
selected?() click to toggle source

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)

# File lib/simple_navigation/item.rb, line 44
def selected?
  @selected ||= selected_by_subnav? || selected_by_condition?
end
selected_class() click to toggle source

Returns the configured #selected_class if the item is selected, nil otherwise

# File lib/simple_navigation/item.rb, line 72
def selected_class
  if selected?
    container.selected_class || config.selected_class
  end
end

Protected Instance Methods

auto_highlight?() click to toggle source

Return true if auto_highlight is on for this item.

# File lib/simple_navigation/item.rb, line 118
def auto_highlight?
  config.auto_highlight && container.auto_highlight
end
autogenerated_item_id() click to toggle source

Returns the item's id which is added to the rendered output.

# File lib/simple_navigation/item.rb, line 113
def autogenerated_item_id
  config.id_generator.call(key) if config.autogenerate_item_ids
end
root_path_match?() click to toggle source

Returns true if both the item's url and the request's url are root_path

# File lib/simple_navigation/item.rb, line 108
def root_path_match?
  url == '/' && SimpleNavigation.request_path == '/'
end
selected_by_condition?() click to toggle source

Returns true if the item's url matches the request's current url.

# File lib/simple_navigation/item.rb, line 103
def selected_by_condition?
  highlights_on ? selected_by_highlights_on? : selected_by_autohighlight?
end
selected_by_subnav?() click to toggle source

Returns true if item has a subnavigation and the #sub_navigation is selected

# File lib/simple_navigation/item.rb, line 98
def selected_by_subnav?
  sub_navigation && sub_navigation.selected?
end

Private Instance Methods

autohighlight_by_subpath?() click to toggle source
# File lib/simple_navigation/item.rb, line 162
def autohighlight_by_subpath?
  config.highlight_on_subpath && selected_by_subpath?
end
config() click to toggle source
# File lib/simple_navigation/item.rb, line 132
def config
  SimpleNavigation.config
end
remove_anchors(url_with_anchors) click to toggle source
# File lib/simple_navigation/item.rb, line 140
def remove_anchors(url_with_anchors)
  url_with_anchors && url_with_anchors.split('#').first
end
remove_query_params(url_with_params) click to toggle source
# File lib/simple_navigation/item.rb, line 144
def remove_query_params(url_with_params)
  url_with_params && url_with_params.split('?').first
end
request_uri() click to toggle source
# File lib/simple_navigation/item.rb, line 136
def request_uri
  SimpleNavigation.request_uri
end
selected_by_autohighlight?() click to toggle source
# File lib/simple_navigation/item.rb, line 154
def selected_by_autohighlight?
  return false unless auto_highlight?

  root_path_match? ||
  (url_for_autohighlight && SimpleNavigation.current_page?(url_for_autohighlight)) ||
  autohighlight_by_subpath?
end
selected_by_highlights_on?() click to toggle source
# File lib/simple_navigation/item.rb, line 166
def selected_by_highlights_on?
  case highlights_on
  when Regexp then !!(request_uri =~ highlights_on)
  when Proc then highlights_on.call
  when :subpath then selected_by_subpath?
  else
    fail ArgumentError, ':highlights_on must be a Regexp, Proc or :subpath'
  end
end
selected_by_subpath?() click to toggle source
# File lib/simple_navigation/item.rb, line 176
def selected_by_subpath?
  escaped_url = Regexp.escape(url_for_autohighlight)
  !!(request_uri =~ /^#{escaped_url}(\/|$||\?)/i)
end
setup_sub_navigation(items = nil, &sub_nav_block) click to toggle source
# File lib/simple_navigation/item.rb, line 181
def setup_sub_navigation(items = nil, &sub_nav_block)
  return unless sub_nav_block || items

  self.sub_navigation = ItemContainer.new(container.level + 1)

  if sub_nav_block
    sub_nav_block.call sub_navigation
  else
    sub_navigation.items = items
  end
end
url_for_autohighlight() click to toggle source
# File lib/simple_navigation/item.rb, line 148
def url_for_autohighlight
  relevant_url = remove_anchors(self.url) if config.ignore_anchors_on_auto_highlight
  relevant_url = remove_query_params(relevant_url) if config.ignore_query_params_on_auto_highlight
  relevant_url
end