Class: Uberkit::Menu::NavigationMenu

Inherits:
Displayer
  • Object
show all
Defined in:
lib/uberkit/menu.rb

Instance Method Summary collapse

Methods inherited from Displayer

#is_haml?

Constructor Details

#initialize(template, options = {}) ⇒ NavigationMenu

Returns a new instance of NavigationMenu.



10
11
12
13
14
15
16
# File 'lib/uberkit/menu.rb', line 10

def initialize(template,options = {})
  super(template)
  @actions = []
  @subnavs = []
  @id = options.delete(:id)
  @class_name = options.delete(:class)
end

Instance Method Details

#action(name, options = {}, html_options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/uberkit/menu.rb', line 38

def action(name, options = {}, html_options = {})
  wrapper_options = { 
    :current => html_options.delete(:current), 
    :disabled => html_options.delete(:disabled), 
    :force_current => html_options.delete(:force_current), 
    :url => options,
    :html => html_options.delete(:html)
  }
  @actions << [@template.link_to(name,options,html_options), wrapper_options, options]
end

#action_wrapper(contents, options = {}, url_for_options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/uberkit/menu.rb', line 18

def action_wrapper(contents, options = {}, url_for_options = {})
  classes = Array.new        
  classes << "first" if @actions.first == [contents, options, url_for_options]
  classes << "last" if @actions.last == [contents, options, url_for_options]
  classes << "current" if merits_current?(contents,options,url_for_options)
  classes << "disabled" if options.delete(:disabled)    
  classes << classes.join("_") if classes.size > 1
  classes << options[:html].delete(:class) if options[:html]
  (:li, contents, options[:html].merge(:class => classes.join(" ")))
end

#actionsObject



49
50
51
# File 'lib/uberkit/menu.rb', line 49

def actions
  @actions
end

#any_current?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/uberkit/menu.rb', line 77

def any_current?
  @actions.select{|a| merits_current?(*a)}.any?
end

#buildObject Also known as: to_html



81
82
83
84
85
# File 'lib/uberkit/menu.rb', line 81

def build
   :ul, @actions.collect{|a| action_wrapper(*a)}.join("\n"),
              :id => @id,
              :class => @class_name
end

#custom_action(options = {}, &block) ⇒ Object



58
59
60
61
# File 'lib/uberkit/menu.rb', line 58

def custom_action(options = {}, &block)
  options[:force_current] = true
  @actions << [capture(&block), options, {}]
end

#merits_current?(contents, options = {}, url_for_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/uberkit/menu.rb', line 29

def merits_current?(contents,options={},url_for_options={})
  if options[:force_current]
    return true if options.delete(:current) == true && !options[:disabled]
  else
    return true if (options.delete(:current) == true || (!url_for_options.is_a?(Symbol) && (@template.current_page?(url_for_options)) && url_for_options != {}) and !options[:disabled])
  end
   false
end

#remote_action(name, options = {}, html_options = {}) ⇒ Object



53
54
55
56
# File 'lib/uberkit/menu.rb', line 53

def remote_action(name, options = {}, html_options = {})
  wrapper_options = { :current => options.delete(:current), :disabled => options.delete(:disabled), :force_current => options.delete(:force_current), :url => options[:url] }
  @actions << [@template.link_to_remote(name,options,html_options), wrapper_options, options[:url]]
end

Yields:

  • (subnav)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/uberkit/menu.rb', line 63

def submenu(name, options = {}, html_options = {}, &block)
  subnav = NavigationMenu.new(@template,html_options)
  @subnavs << subnav
  yield subnav

  if subnav.actions.any?
    if options == :delegate
      @actions << [@template.link_to(name, subnav.actions.first[1][:url]) + subnav.to_html, {:current => subnav.any_current?, :url => subnav.actions.first[1][:url]}, {:class => 'submenu'}]
    else
      @actions << [@template.link_to(name,options,html_options) + subnav.to_html, {:current => subnav.any_current?, :url => options}, {:class => 'submenu'}.merge(options)] if subnav.actions.any?
    end
  end
end