Class: Spark::MenuHelper::Menu

Inherits:
Helper
  • Object
show all
Defined in:
app/helpers/spark/menu_helper.rb

Constant Summary collapse

DEFAULTS =
{
  tag: :div,
  role: :menu,
  base_class: 'menu',
  header_tag: :h4,
  group_tag: :ul,
  item_tag: :li
}

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, options = {}) ⇒ Menu

Returns a new instance of Menu.



13
14
15
16
17
18
# File 'app/helpers/spark/menu_helper.rb', line 13

def initialize( title = nil, options = {} )
  options, title = title, nil if title.is_a? Hash

  @options = add_class( options, config[:base_class] )
  @options['aria-label'] = title if title
end

Instance Method Details

#display(body) ⇒ Object



20
21
22
23
24
25
# File 'app/helpers/spark/menu_helper.rb', line 20

def display( body )
  @options.merge!({ role: config[:role] })
  ( config[:tag], @options ) do
    concat( body )
  end
end

#group(title = nil, options = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/spark/menu_helper.rb', line 47

def group( title=nil, options={}, &block )
  options = add_class( options, config[:base_class] + "-group" )
  options['role'] = 'group'

  content = capture( &block )
  return if content.blank? ||
          ( options.include?('if') && !options.delete('if') ) ||
          ( options.include?('unless') && !!options.delete('unless') )                     

  concat header( title ) if title
  ( config[:group_tag], options, &block )
end

#header(text = nil, options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'app/helpers/spark/menu_helper.rb', line 27

def header( text = nil, options = {}, &block )
  options, text = text, nil if text.is_a?( Hash )
  options = add_class( options, config[:base_class] + "-header" )

  ( :header, options ) {
    concat( ( config[:header_tag], text, class: config[:base_class] + '-title' ) ) if text
    concat( capture( &block ).html_safe ) if block_given?
  }
end

#item(content = nil, url = nil, options = nil, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/spark/menu_helper.rb', line 60

def item( content=nil, url=nil, options=nil, &block )
  content, url, options = parse_item_args( content, url, options, &block)

  return if ( options.include?('if') && !options.delete('if') ) ||
            ( options.include?('unless') && !!options.delete('unless') )

  if url
    ( config[:item_tag], role: 'none', class: config[:base_class] + "-item-wrapper" ) do
      link( url, options ) { content }
    end
  else
    content, options = set_content( content, options )
    options = add_class( options, config[:base_class] + "-item" )
    ( config[:item_tag], options, &content )
  end
end


37
38
39
40
41
42
43
44
45
# File 'app/helpers/spark/menu_helper.rb', line 37

def link( content=nil, url=nil, options=nil, &block )
  content, url, options = parse_item_args( content, url, options, &block)
  content, options = set_content( content, options )
  options = add_class( options, config[:base_class] + "-item" )

  options['role'] ||= 'menuitem'

  link_to( url, options, &content )
end