Class: SimpleNavigation::Item

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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



15
16
17
18
19
20
21
22
23
# File 'lib/simple_navigation/item.rb', line 15

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

Instance Attribute Details

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

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.



30
31
32
# File 'lib/simple_navigation/item.rb', line 30

def name
  @name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def options
  @options
end

Returns the value of attribute sub_navigation.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def sub_navigation
  @sub_navigation
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/simple_navigation/item.rb', line 5

def url
  @url
end

Instance Method Details

#active_leaf_classObject

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



65
66
67
68
69
# File 'lib/simple_navigation/item.rb', line 65

def active_leaf_class
  if !selected_by_subnav? && selected_by_condition?
    config.active_leaf_class
  end
end

#highlights_onObject

Returns the :highlights_on option as set at initialization



80
81
82
# File 'lib/simple_navigation/item.rb', line 80

def highlights_on
  @highlights_on ||= options[:highlights_on]
end

#html_optionsObject

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.



52
53
54
55
56
57
58
59
60
61
# File 'lib/simple_navigation/item.rb', line 52

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

Returns the html attributes for the link as set with the :link_html option at initialization



91
92
93
# File 'lib/simple_navigation/item.rb', line 91

def link_html_options
  @link_html_options ||= options[:link_html]
end

#methodObject

Returns the :method option as set at initialization



85
86
87
# File 'lib/simple_navigation/item.rb', line 85

def method
  @method ||= options[: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)

Returns:

  • (Boolean)


45
46
47
# File 'lib/simple_navigation/item.rb', line 45

def selected?
  @selected ||= selected_by_subnav? || selected_by_condition?
end

#selected_classObject

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



73
74
75
76
77
# File 'lib/simple_navigation/item.rb', line 73

def selected_class
  if selected?
    container.selected_class || config.selected_class
  end
end