Module: CamaleonCms::Frontend::NavMenuHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/camaleon_cms/frontend/nav_menu_helper.rb

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: owenperedo@gmail.com
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Instance Method Summary collapse

Instance Method Details

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


136
137
138
139
140
141
142
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 136

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

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


121
122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 121

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

#cama_is_current_menu?(menu_item) ⇒ Boolean

check if menu is the current menu

Returns:

  • (Boolean)

114
115
116
117
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 114

def cama_is_current_menu?(menu_item)
  r = _get_link_nav_menu(menu_item)
  r[:current] || site_current_path == r[:link] || site_current_path.sub(".html", "") == r[:link].sub(".html", "") if r.present?
end

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

draw menu items


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
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 69

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 = _get_link_nav_menu(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: '',
      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, level + 1)
    else
      html_children, current_children = "", false
    end
    parent_current = true if _is_current || current_children

    html += "<#{_args[:item_container]} class='#{_args[:item_class]} #{_args[:item_class_parent] if has_children} #{"#{_args[:item_current]}" if _is_current} #{"current-menu-ancestor" if current_children }'>#{_args[:link_before]}
              <a #{r[:link_attrs]} href='#{data_nav_item[:link]}' class='#{args[:link_current] if _is_current} #{_args[:link_class_parent] if has_children} #{_args[:link_class]}' >#{_args[:before]}#{data_nav_item[:name]}#{_args[:after]}</a> #{_args[:link_after]}
              #{ html_children }
            </#{_args[:item_container]}>"
    index += 1
  end

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

#draw_menu(args = {}) ⇒ Object

draw menu as an html default configurations is for bootstrap support


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
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 20

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:    lambda{|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]}'
                  # 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 = html.sub("{__}", cama_menu_draw_items(args, nav_menu.children))
  else
    html = html.sub("{__}", "")
  end
  html
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)


14
15
16
# File 'app/helpers/camaleon_cms/frontend/nav_menu_helper.rb', line 14

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