Class: Releaf::Builders::Page::MenuBuilder

Inherits:
Object
  • Object
show all
Includes:
Base, Template, InstanceCache
Defined in:
app/builders/releaf/builders/page/menu_builder.rb

Instance Attribute Summary

Attributes included from Template

#template

Instance Method Summary collapse

Methods included from Template

#initialize

Methods included from Base

#default_translation_scope, #html_escape, #icon, #layout_settings, #locale_options, #resource_title, #safe_join, #t, #tag, #template_variable, #translate_locale, #wrapper

Methods included from InstanceCache

#instance_cache, #instance_cache_store, #reset_instance_cache

Instance Method Details

#active?(item) ⇒ Boolean

Returns:

  • (Boolean)

11
12
13
14
15
16
17
18
19
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 11

def active?(item)
  instance_cache("active_#{item.class.name}_#{item.name}") do
    if item.group?
      item.controllers.find{|subitem| active?(subitem) }.present?
    else
      item.controller_name == controller.short_name
    end
  end
end

#collapsed_item?(item) ⇒ Boolean

Returns:

  • (Boolean)

48
49
50
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 48

def collapsed_item?(item)
  item.group? && !active?(item) && layout_settings("releaf.menu.collapsed.#{item.name}") == true
end

#compact_side?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 94

def compact_side?
  layout_settings('releaf.side.compact')
end

#compacterObject


106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 106

def compacter
  tag(:div, class: "compacter") do
    if compact_side?
      icon_name = "angle-double-right"
      title_attribute = 'title-expand'
    else
      icon_name = "angle-double-left"
      title_attribute = 'title-collapse'
    end
    button(nil, icon_name, title: compacter_data[title_attribute], data: compacter_data )
  end
end

#compacter_dataObject


119
120
121
122
123
124
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 119

def compacter_data
  {
    'title-expand'   => t("Expand", scope: :admin),
    'title-collapse' => t("Collapse", scope: :admin)
  }
end

#item_attributes(item) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 52

def item_attributes(item)
  attributes = {
    class: item_classes(item),
    data: {
      name: item.name
    }
  }

  attributes.delete(:class) if attributes[:class].empty?
  attributes
end

#item_classes(item) ⇒ Object


64
65
66
67
68
69
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 64

def item_classes(item)
  list = []
  list << "collapsed" if collapsed_item?(item)
  list << "active" if active?(item)
  list
end

#item_collapser(item) ⇒ Object


86
87
88
89
90
91
92
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 86

def item_collapser(item)
  tag(:span, class: "collapser") do
    tag(:button, type: "button") do
      item_collapser_icon(item)
    end
  end
end

#item_collapser_icon(item) ⇒ Object


98
99
100
101
102
103
104
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 98

def item_collapser_icon(item)
  if compact_side?
    icon("chevron-right")
  else
    icon(collapsed_item?(item) ? "chevron-down" : "chevron-up")
  end
end

#item_name_abbreviation(item_full_name) ⇒ Object


78
79
80
81
82
83
84
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 78

def item_name_abbreviation( item_full_name )
  return "" if item_full_name.blank?
  # use the first two letters after the last slash that is not preceded by a space
  # to avoid identical abbreviations for namespaced items in case of missing translations
  # but still use the first word in cases of user-entered slashes, e.g. "Inputs / Outputs"
  item_full_name.split(/(?<!\s)\//).last.to_s[0..1].mb_chars.capitalize
end

#item_name_content(item) ⇒ Object


71
72
73
74
75
76
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 71

def item_name_content(item)
  item_full_name    = item.localized_name
  item_abbreviation = item_name_abbreviation( item_full_name )

  tag(:abbr, item_abbreviation, title: item_full_name) + tag(:span, item_full_name, class: "name")
end

26
27
28
29
30
31
32
33
34
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 26

def menu_item(item)
  tag(:li, item_attributes(item)) do
    if item.group?
      menu_item_group(item)
    else
      menu_item_single(item)
    end
  end
end

42
43
44
45
46
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 42

def menu_item_group(item)
  tag(:span, class: "trigger") do
    item_name_content(item) << item_collapser(item)
  end << menu_level(item.controllers)
end

36
37
38
39
40
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 36

def menu_item_single(item)
  tag(:a, class: "trigger", href: item.path) do
    item_name_content(item)
  end
end

21
22
23
24
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 21

def menu_level(items)
  level_content = items.map{|item| menu_item(item) }.compact
  tag(:ul){ level_content } if level_content.present?
end

#outputObject


7
8
9
# File 'app/builders/releaf/builders/page/menu_builder.rb', line 7

def output
  compacter << tag(:nav, menu_level(Releaf.application.config.menu))
end