Module: CamaleonCms::Frontend::NavMenuHelper

Included in:
Admin::Appearances::NavMenusController, ApplicationHelper
Defined in:
app/helpers/camaleon_cms/frontend/nav_menu_helper.rb

Instance Method Summary collapse

Instance Method Details

add breadcrumb item at the end label => label of the link url: url for the link



196
197
198
199
200
201
202
203
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 196

def breadcrumb_add(label, url = '', prepend = false)
  @_front_breadcrumb ||= []
  if prepend
    @_front_breadcrumb.unshift([label, url])
  else
    @_front_breadcrumb << [label, url]
  end
end

******************* BREADCRUMBS ******************* draw the breadcrumb as html list



181
182
183
184
185
186
187
188
189
190
191
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 181

def breadcrumb_draw
  res = []
  @_front_breadcrumb.each_with_index do |item, index|
    res << if @_front_breadcrumb.size == (index + 1) # last menu
             "<li class='active'>#{item[0]}</li>"
           else
             "<li><a href='#{item[1]}'>#{item[0]}</a></li>"
           end
  end
  res.join('')
end

#cama_menu_draw_items(args, nav_menu, level = 0) ⇒ Object

draw menu items



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 63

def cama_menu_draw_items(args, nav_menu, level = 0)
  html = ''
  parent_current = false
  index = 0
  nav_menu.eager_load(:metas).each do |nav_menu_item|
    _args = args.dup
    data_nav_item = cama_parse_menu_item(nav_menu_item)
    next if data_nav_item == false

    _is_current = data_nav_item[:current] || site_current_path == data_nav_item[:link] || site_current_path == data_nav_item[:link].sub(
      '.html', ''
    )
    has_children = nav_menu_item.have_children? && (args[:levels] == -1 || (args[:levels] != -1 && level <= args[:levels]))
    r = {
      menu_item: nav_menu_item.decorate,
      link: data_nav_item,
      level: level,
      settings: _args,
      has_children: has_children,
      link_attrs: '',
      item_container_attrs: '',
      index: index
    }
    args[:callback_item].call(r)
    _args = r[:settings]

    if has_children
      html_children, current_children = cama_menu_draw_items(args, nav_menu_item.children.reorder(:term_order),
                                                             level + 1)
    else
      html_children = ''
      current_children = false
    end
    parent_current = true if _is_current || current_children

    html += "<#{_args[:item_container]} #{r[:item_container_attrs]} class='#{_args[:item_class]} #{if has_children
                                                                                                     _args[:item_class_parent]
                                                                                                   end} #{if _is_current
                                                                                                            (_args[:item_current]).to_s
                                                                                                          end} #{if current_children
                                                                                                                   'current-menu-ancestor'
                                                                                                                 end}'>#{_args[:link_before]}
          <a #{r[:link_attrs]} #{if nav_menu_item.target.present?
                                   " target='#{nav_menu_item.target}'"
                                 end} href='#{data_nav_item[:link]}' class='#{if _is_current
                                                                                args[:link_current]
                                                                              end} #{if has_children
                                                                                       _args[:link_class_parent]
                                                                                     end} #{_args[:link_class]}' #{if has_children
                                                                                                                     "data-toggle='dropdown'"
                                                                                                                   end} >#{_args[:before]}#{data_nav_item[:name]}#{_args[:after]}</a> #{_args[:link_after]}
          #{html_children}
        </#{_args[:item_container]}>"
    index += 1
  end

  if level.zero?
    html
  else
    html = "<#{args[:sub_container]} class='#{args[:sub_class]} #{if parent_current
                                                                    "parent-#{args[:item_current]}"
                                                                  end} level-#{level}'>#{html}</#{args[:sub_container]}>"
    [html, parent_current]
  end
end

#cama_menu_parse_items(items, max_levels = -1,, internal_level = 0) ⇒ Object

filter and parse all menu items visible for current user and adding the flag for current_parent or current_item max_levels: max levels to iterate return an multidimensional array with all items until level ‘max_levels’ internal_level: ingnore (managed by internal recursion)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 133

def cama_menu_parse_items(items, max_levels = -1, internal_level = 0)
  res = []
  is_current_parent = false
  levels = [0]
  items.reorder(:term_order).each_with_index do |nav_menu_item, index|
    data_nav_item = cama_parse_menu_item(nav_menu_item)
    next if data_nav_item == false

    _is_current = data_nav_item[:current] || site_current_path == data_nav_item[:link] || site_current_path == data_nav_item[:link].sub(
      '.html', ''
    )
    has_children = nav_menu_item.have_children?
    has_children = false if max_levels.positive? && max_levels == internal_level
    data_nav_item[:label] = data_nav_item[:name]
    data_nav_item[:url] = data_nav_item[:link]
    r = {
      menu_item: nav_menu_item.decorate,
      level: internal_level,
      has_children: has_children,
      index: index,
      current_item: _is_current,
      current_parent: false,
      levels: 0
    }.merge(data_nav_item.except(:current, :name, :link))

    if has_children
      r[:children], _is_current_parent, r[:levels] = cama_menu_parse_items(nav_menu_item.children, max_levels,
                                                                           internal_level + 1)
      if _is_current_parent
        is_current_parent = true
        r[:current_parent] = true
      end
      r[:levels] = r[:levels] + 1
    end
    is_current_parent = true if r[:current_item]
    levels << r[:levels]
    res << r
  end

  if internal_level.zero?
    res
  else
    [res, is_current_parent, levels.max]
  end
end

#cama_parse_menu_item(nav_menu_item, is_from_backend = false) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 205

def cama_parse_menu_item(nav_menu_item, is_from_backend = false)
  type_menu = nav_menu_item.kind
  result = false
  begin
    case type_menu
    when 'post'
      post = CamaleonCms::Post.find(nav_menu_item.url).decorate
      if is_from_backend || post.can_visit?
        result = { link: post.the_url(as_path: true), name: post.the_title, type_menu: type_menu,
                   url_edit: post.the_edit_url }
        result[:current] = @cama_visited_post.present? && @cama_visited_post.id == post.id unless is_from_backend
      end
    when 'category'
      category = CamaleonCms::Category.find(nav_menu_item.url).decorate
      result = { link: category.the_url(as_path: true), name: category.the_title,
                 url_edit: category.the_edit_url }
      unless is_from_backend
        result[:current] =
          @cama_visited_category.present? && @cama_visited_category.id == category.id
      end
    when 'post_tag'
       = CamaleonCms::PostTag.find(nav_menu_item.url).decorate
      result = { link: .the_url(as_path: true), name: .the_title,
                 url_edit: .the_edit_url }
      result[:current] = @cama_visited_tag.present? && @cama_visited_tag.id == .id unless is_from_backend
    when 'post_type'
      post_type = CamaleonCms::PostType.find(nav_menu_item.url).decorate
      result = { link: post_type.the_url(as_path: true), name: post_type.the_title,
                 url_edit: post_type.the_edit_url }
      unless is_from_backend
        result[:current] =
          @cama_visited_post_type.present? && @cama_visited_post_type.id == post_type.id
      end
    when 'external'
      result = { link: nav_menu_item.url.to_s.translate, name: nav_menu_item.name.to_s.translate, current: false }
      # permit to customize or mark as current menu
      # _args: (HASH) {menu_item: Model Menu Item, parsed_menu: Parsed Menu }
      #   Sample parsed_menu: {link: "url of the link", name: "Text of the menu", current: Boolean (true => is current menu, false => not current menu item)}
      unless is_from_backend
        result[:link] = cama_root_path if result[:link] == 'root_url'
        result[:link] = site_current_path if site_current_path == "#{current_site.the_path}#{result[:link]}"
        result[:current] = result[:link] == site_current_url || result[:link] == site_current_path
        _args = { menu_item: nav_menu_item, parsed_menu: result }
        hooks_run('on_external_menu', _args)
        result = _args[:parsed_menu]
      end
    else
      # permit to build custom menu items registered as Custom Menu by hook "nav_menu_custom"
      # sample: def my_parse_custom_menu_item_listener(args);
      #   if args[:menu_item].kind == 'MyModelClass'
      #     my_model = MyModelClass.find(args[:menu_item].url)
      #     res = {name: my_model.name, url_edit: my_model_edit_url(id: my_model.id), link: my_model_public_url(id: my_model.id)}
      #     res[:current] = site_current_path == my_model_public_url(id: my_model.id) unless args[:is_from_backend]
      #     args[:parsed_menu] = res
      #   end
      # end
      hook_args = { menu_item: nav_menu_item, parsed_menu: false, is_from_backend: is_from_backend }
      hooks_run('parse_custom_menu_item', hook_args)
      result = hook_args[:parsed_menu]
    end
  rescue StandardError => e
    Rails.logger.error "Camaleon CMS - Menu Item Not Found => Skipped menu for: #{e.message} (#{nav_menu_item.inspect})".cama_log_style(:red)
  end

  # permit to customize data, like: current, title, ... of parsed menu item or skip menu item by assigning false into :parsed_menu
  if is_from_backend
    result
  else
    _args = { menu_item: nav_menu_item, parsed_menu: result }
    hooks_run('on_render_front_menu_item', _args)
    _args[:parsed_menu]
  end
end

#draw_menu(args = {}) ⇒ Object

draw menu as an html default configurations is for bootstrap support



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 14

def draw_menu(args = {})
  args_def = {
    menu_slug: 'main_menu', # slug for the menu
    container: 'ul', # type of container for the menu
    container_id: '', # container id for the menu
    container_class: 'nav navbar-nav nav-menu', # container class for the menu
    item_container: 'li', # type of container for the items
    item_current: 'current-menu-item', # class for current menu item
    item_class: 'menu-item', # class for all menu items
    item_class_parent: 'dropdown', # class for all menu items that contain sub items
    sub_container: 'ul', # type of container for sub items
    sub_class: 'dropdown-menu', # class for sub container
    callback_item: ->(args) {},
    # callback executed for each item (args = { menu_item, link, level, settings, has_children, link_attrs = "", index}).
    #     menu_item: (Object) Menu object
    #     link: (Hash) link data: {link: '', name: ''}
    #     level: (Integer) current level
    #     has_children: (boolean) if this item contain sub menus
    #     settings: (Hash) menu settings
    #     index: (Integer) Index Position of this menu
    #     link_attrs: (String) Here you can add your custom attrs for current link, sample: id='my_id' data-title='#{args[:link][:name]}'
    #     item_container_attrs: (String) Here you can add your custom attrs for link container.
    # In settings you can change the values for this item, like after, before, ..:
    # sample: lambda{|args| args[:settings][:after] = "<span class='caret'></span>" if args[:has_children]; args[:link_attrs] = "id='#{menu_item.id}'";  }
    # sample: lambda{|args| args[:settings][:before] = "<i class='fa fa-home'></i>" if args[:level] == 0 && args[:index] == 0;  }
    before: '', # content before link text
    after: '', # content after link text
    link_current: 'current-link', # class for current menu link
    link_before: '', # content before link
    link_after: '', # content after link
    link_class: 'menu_link', # class for all menu links
    link_class_parent: 'dropdown-toggle', # class for all menu links that contain sub items
    levels: -1, # max of levels to recover, -1 => return all levels
    container_prepend: '', # content prepend for menu container
    container_append: '' # content append for menu container
  }

  args = args_def.merge(args)
  nav_menu = current_site.nav_menus.find_by_slug(args[:menu_slug])
  nav_menu = current_site.nav_menus.first unless nav_menu.present?
  html = "<#{args[:container]} class='#{args[:container_class]}' id='#{args[:container_id]}'>#{args[:container_prepend]}{__}#{args[:container_append]}</#{args[:container]}>"
  if nav_menu.present?
    html.sub('{__}', cama_menu_draw_items(args, nav_menu.children.reorder(:term_order)))
  else
    html.sub('{__}', '')
  end
end

#get_nav_menu(key = 'main_menu', class_name = 'navigation') ⇒ Object

draw nav menu as html list key: slug for nav menu to register this, go to admin -> appearance -> menus (DEPRECATED)



8
9
10
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 8

def get_nav_menu(key = 'main_menu', class_name = 'navigation')
  draw_menu({ menu_slug: key, container_class: class_name })
end