Module: CamaleonCms::Frontend::NavMenuHelper

Included in:
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



243
244
245
246
247
248
249
250
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 243

def breadcrumb_add(label, url = '', prepend = false)
  breadcrumb_items = nav_menu_breadcrumb_items
  if prepend
    breadcrumb_items.unshift([label, url])
  else
    breadcrumb_items << [label, url]
  end
end

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



227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 227

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

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

draw menu items rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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
128
129
130
131
132
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
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 74

def cama_menu_draw_items(args, nav_menu, level = 0)
  items_html = []
  parent_current = false
  index = 0
  # rubocop:disable Rails/FindEach -- Rendering must preserve the relation's configured term_order.
  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 ||= _is_current || current_children

    item_classes = [_args[:item_class]]
    item_classes << _args[:item_class_parent] if has_children
    item_classes << _args[:item_current] if _is_current
    item_classes << 'current-menu-ancestor' if current_children
    item_class_str = item_classes.reject(&:blank?).join(' ')

    item_attrs = []
    item_attrs << r[:item_container_attrs] if r[:item_container_attrs].present?
    item_attrs << "class='#{ERB::Util.html_escape(item_class_str)}'" if item_class_str.present?
    item_attrs_str = item_attrs.reject(&:blank?).join(' ')

    item_open = "<#{_args[:item_container]}"
    item_open << " #{item_attrs_str}" if item_attrs_str.present?
    item_open << '>'

    link_attr_parts = []
    link_attr_parts << r[:link_attrs] if r[:link_attrs].present?
    if nav_menu_item.target.present?
      link_attr_parts << "target='#{ERB::Util.html_escape(nav_menu_item.target.to_s)}'"
    end
    link_attr_parts << "href='#{ERB::Util.html_escape(data_nav_item[:link].to_s)}'"
    link_classes = []
    link_classes << args[:link_current] if _is_current
    link_classes << _args[:link_class_parent] if has_children
    link_classes << _args[:link_class]
    link_class_str = link_classes.reject(&:blank?).join(' ')
    link_attr_parts << "class='#{ERB::Util.html_escape(link_class_str)}'" if link_class_str.present?
    link_attr_parts << "data-toggle='dropdown'" if has_children
    link_attrs_str = link_attr_parts.reject(&:blank?).join(' ')

    # Menu labels intentionally support trusted HTML snippets (icons, buttons, embeds) configured by site admins.
    link_inner = [_args[:before], data_nav_item[:name].to_s, _args[:after]].reject(&:blank?).join('')
    link_tag = "<a #{link_attrs_str}>#{link_inner}</a>"

    item_inner = [
      _args[:link_before],
      link_tag,
      _args[:link_after],
      html_children
    ].reject(&:blank?).join('')
    item_html = "#{item_open}#{item_inner}</#{_args[:item_container]}>"
    # rubocop:disable Rails/OutputSafety -- User-controlled fields are escaped before this trusted menu markup is marked safe.
    items_html << item_html.html_safe
    # rubocop:enable Rails/OutputSafety

    index += 1
  end
  # rubocop:enable Rails/FindEach

  if level == 0
    safe_join(items_html)
  else
    sub_classes = [args[:sub_class]]
    sub_classes << "parent-#{args[:item_current]}" if parent_current
    sub_classes << "level-#{level}"
    sub_container_class = sub_classes.reject(&:blank?).join(' ')
    sub_html = "<#{args[:sub_container]} class='#{sub_container_class}'>" \
               "#{safe_join(items_html)}</#{args[:sub_container]}>"
    [sub_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 a multidimensional array with all items until level 'max_levels' internal_level: ignore (managed by internal recursion)



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 179

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 > 0 && 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 == 0
    res
  else
    [res, is_current_parent, levels.max]
  end
end

#cama_parse_menu_item(nav_menu_item, is_from_backend = false) ⇒ Object



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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 252

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 }
        unless is_from_backend
          visited_post = nav_menu_visited_state(:frontend_visited_post)
          result[:current] = visited_post.present? && visited_post.id == post.id
        end
      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
        visited_category = nav_menu_visited_state(:frontend_visited_category)
        result[:current] = visited_category.present? && 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 }
      unless is_from_backend
        visited_tag = nav_menu_visited_state(:frontend_visited_tag)
        result[:current] = visited_tag.present? && visited_tag.id == .id
      end
    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
        visited_post_type = nav_menu_visited_state(:frontend_visited_post_type)
        result[:current] = visited_post_type.present? && 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_url(id: my_model.id)
      #     }
      #     res[:current] = site_current_path == my_model_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 customizing data, like: current, title, ... of parsed menu item or
  # skip menu item by assigning false into :parsed_menu
  return result if is_from_backend

  _args = { menu_item: nav_menu_item, parsed_menu: result }
  hooks_run('on_render_front_menu_item', _args)
  _args[:parsed_menu]
end

#draw_menu(args = {}) ⇒ Object

draw menu as 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
61
62
63
64
65
66
67
68
69
70
# 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 do |args|
    #     args[:settings][:after] = "<span class='caret'></span>" if args[:has_children]
    #     args[:link_attrs] = "id='#{menu_item.id}'"
    #   end
    # sample:
    #   lambda do |args|
    #     args[:settings][:before] = "<i class='fa fa-home'></i>" if args[:level] == 0 && args[:index] == 0
    #   end
    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]) # rubocop:disable Rails/DynamicFindBy
  nav_menu ||= current_site.nav_menus.first
  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 a 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