Class: SimpleNavigation::Item

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

Instance Method Summary (collapse)

Constructor Details

- (Item) initialize(container, key, name, url_or_options = {}, options_or_nil = {}, items = nil, &sub_nav_block)

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
# File 'lib/simple_navigation/core/item.rb', line 11

def initialize(container, key, name, url_or_options = {}, options_or_nil={}, items=nil, &sub_nav_block)
  @container = container
  options = setup_url_and_options(url_or_options, options_or_nil)
  @container.dom_class = options.delete(:container_class) if options[:container_class]
  @container.dom_id = options.delete(:container_id) if options[:container_id]
  @key = key
  @method = options.delete(:method)
  @name = name
  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

- (Object) highlights_on (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

- (Object) html_options

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.



51
52
53
54
55
56
57
# File 'lib/simple_navigation/core/item.rb', line 51

def html_options
  default_options = self.autogenerate_item_ids? ? {:id => autogenerated_item_id} : {}
  options = default_options.merge(@html_options)
  options[:class] = [@html_options[:class], self.selected_class, self.active_leaf_class].flatten.compact.join(' ')
  options.delete(:class) if options[:class].nil? || options[:class] == ''
  options
end

- (Object) key (readonly)

Returns the value of attribute key



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

def key
  @key
end

- (Object) method (readonly)

Returns the value of attribute method



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

def method
  @method
end

Returns the value of attribute sub_navigation



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

def sub_navigation
  @sub_navigation
end

- (Object) url (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

- (Object) active_leaf_class

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



61
62
63
# File 'lib/simple_navigation/core/item.rb', line 61

def active_leaf_class
  !selected_by_subnav? && selected_by_condition? ? SimpleNavigation.config.active_leaf_class : nil
end

- (Object) name(options = {})

Returns the item's name. If option :apply_generator is set to true (default), the name will be passed to the name_generator specified in the configuration.



29
30
31
32
33
34
35
36
# File 'lib/simple_navigation/core/item.rb', line 29

def name(options = {})
  options.reverse_merge!(:apply_generator => true)
  if (options[:apply_generator])
    SimpleNavigation.config.name_generator.call(@name)
  else
    @name
  end
end

- (Boolean) selected?

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)

Returns:

  • (Boolean)


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

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

- (Boolean) selected_by_config?

Returns:

  • (Boolean)


78
79
80
# File 'lib/simple_navigation/core/item.rb', line 78

def selected_by_config?
  false
end

- (Object) selected_class

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



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

def selected_class
  selected? ? SimpleNavigation.config.selected_class : nil
end