Class: DcBigMenuRenderer

Inherits:
Object
  • Object
show all
Includes:
CmsCommonHelper, DcApplicationHelper
Defined in:
app/renderers/dc_big_menu_renderer.rb

Overview

dc_big_menu is experimental menu system which can be used as alternative to dc_menu when sub menu menu level is larger than two levels. Idea is to have top two levels of menu displayed on standard top of page position with other sub levels displayed vertically on left or right part of page. Renderer also provides path method, which can be used to show menu path of currently displayed page.

Instance Attribute Summary

Attributes included from DcApplicationHelper

#design, #form, #ids, #json_ld, #menu, #menu_item, #options, #page, #page_title, #part, #parts, #record, #record_footer, #site, #tables

Instance Method Summary collapse

Methods included from CmsCommonHelper

#dc_choices4_field, #dc_choices_for_field, dc_choices_for_field, #dc_date_time, #dc_format_date_time, dc_format_date_time, #dc_format_number, dc_format_number, #dc_help_body, #dc_help_button, #dc_help_fields, #dc_help_for_tab, #dc_icon4_boolean, #dc_icon_for_boolean, #dc_name4_id, #dc_name4_value, #dc_name_for_id, #dc_name_for_value, dc_name_for_value, #dc_steps_menu_get, #t, t, #t_label_for_column, #t_label_for_field, #t_tablename

Methods included from DcApplicationHelper

#_origin, #dc_add2_record_cookie, #dc_add_json_ld, #dc_add_meta_tag, #dc_big_table, #dc_big_table_name_for_value, #dc_choices4, #dc_choices4_all_collections, #dc_choices4_folders_list, #dc_choices4_menu, #dc_choices4_site_policies, #dc_cms_menu, #dc_deprecate, #dc_document_path, #dc_dont?, #dc_edit_mode?, #dc_edit_title, #dc_error_messages_for, #dc_flash_messages, #dc_get_json_ld, #dc_get_link_canonical_tag, #dc_get_seo_meta_tags, #dc_get_site, #dc_icon_for_link, #dc_iframe_edit, #dc_img_alt, #dc_img_alt_tag, #dc_internal_var, #dc_limit_string, #dc_link_for_create, #dc_link_for_edit, #dc_link_for_edit1, #dc_link_menu_tag, #dc_link_to, #dc_menu_class, #dc_new_title, #dc_page_bottom, #dc_page_class, #dc_page_edit_menu, #dc_page_top, #dc_render, #dc_render_design, #dc_render_design_part, #dc_render_from_site, #dc_render_partial, #dc_replace_in_design, #dc_submit_tag, #dc_table_title, #dc_user_can_view, #dc_user_has_role, #dc_warning_messages_for, #decamelize_type

Constructor Details

#initialize(parent, opts) ⇒ DcBigMenuRenderer

Object initialization.



38
39
40
41
42
43
# File 'app/renderers/dc_big_menu_renderer.rb', line 38

def initialize( parent, opts ) #:nodoc:
  @parent = parent
  @site = parent.dc_get_site
  @opts = opts
  self
end

Instance Method Details

#defaultObject

Default methods renders top two levels of menu on top of page.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/renderers/dc_big_menu_renderer.rb', line 139

def default
  html = '<div class="menu0-div"><ul>'
  @selected = find_selected
  level_0 = DcBigMenu.where(dc_site_id: @site._id, parent: nil, active: true).sort(order: 1).to_a
  level_0.each do |item|
    # menu can be hidden from user
    can_view, msg = dc_user_can_view(@parent, item)
    next unless can_view

    klas = item.id == @selected.id ? "menu0-selected" : "menu0-item"
    html << "<li class='#{klas}'>#{ link_4menu(item) }</li>\n"
  end
  html << "</ul></div>"
  # submenu
  level_1 = DcBigMenu.where(dc_site_id: @site._id, parent: @selected.id, active: true).sort(order: 1).to_a
  html << "<div class='menu1-div'><ul>\n"
  level_1.each do |item1|
    can_view, msg = dc_user_can_view(@parent, item1)
    next unless can_view

    html << "  <li class='menu1-item'>#{link_4menu(item1)}</li>\n"
  end
  html << '</ul></div>'
end

#find_selectedObject

Find out which top level menu currently displayed page belongs to. Subroutine of default method.



48
49
50
51
52
53
54
# File 'app/renderers/dc_big_menu_renderer.rb', line 48

def find_selected #:nodoc:
  ret = DcBigMenu.find( @parent.page.menu_id ) if @parent.page.menu_id
  while ret and ret.parent != nil
    ret = DcBigMenu.find( ret.parent )
  end
  ret || DcBigMenu.where(dc_site_id: @site._id, parent: nil, active: true).limit(1).first
end

#left_menuObject

Renderer for menu part displayed on left position of page.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/renderers/dc_big_menu_renderer.rb', line 89

def left_menu
  html = ''
  menu = DcBigMenu.find( @parent.page.menu_id )
  # Show menu on same level if selected has no children
  if DcBigMenu.where( parent: @parent.page.menu_id ).limit(1).to_a.size == 0
    menu = DcBigMenu.find( m.parent )
  end    

  html << "<div class='menu-left-item-top'>#{menu.caption}</div>"
  DcBigMenu.where( parent: menu.id ).order(order: 1).each do |item|
    html << (item._id == @parent.page.menu_id ? '<div class="menu-left-item-selected">' : '<div class="menu-left-item">')
    html << link_4menu(item) 
    html << '</div>'
  end

  html << "<div class='menu-left-item-bottom'>"
  if menu.parent
    parent = DcBigMenu.find( menu.parent )
    html << "&#9650; #{link_4menu(parent)}"
  end
  html << '&nbsp;</div>' 
end

Creates edit link if in edit mode.



59
60
61
62
63
64
65
66
67
68
69
# File 'app/renderers/dc_big_menu_renderer.rb', line 59

def link_4edit #:nodoc:
  html = ''
  return html unless @opts[:edit_mode] > 1
  
  @opts[:editparams].merge!( { table: 'dc_big_menu', controller: 'cmsedit', action: 'edit' } )
  title = "#{t('drgcms.edit')}: "
  @opts[:editparams].merge!( { id: @menu.id, title: "#{title}#{@menu.name}" } ) if @menu
  title << t('helpers.label.dc_big_menu.tabletitle')
  @opts[:editparams].merge!( { action: 'index', title: title }) if @menu.nil?
  html << dc_link_for_edit( @opts[:editparams] )
end

Returns html code for single link on a menu.



74
75
76
77
78
79
80
81
82
83
84
# File 'app/renderers/dc_big_menu_renderer.rb', line 74

def link_4menu(item) #:nodoc:
  html = ''
  link = item.link
  link = "/#{@site.route_name}/#{item.page_id}" #if link.blank?

  html << @parent.link_to(item.picture, link) unless item.picture.blank?
  html << if !item.caption.blank?
    # TODO Translation
    @parent.link_to(item.caption, link)
  end
end

#pathObject

Renders menu path for currently selected page.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/renderers/dc_big_menu_renderer.rb', line 115

def path
  html = ''
  a = []
  m = DcBigMenu.find( @parent.page.menu_id )
  a << m
  while m.parent 
    m = DcBigMenu.find( m.parent )
    a << m
  end

  (a.size - 1).downto(0) do |i| 
    html << "<span id=menu-path-#{a.size - 1 - i}>"
    html << link_4menu(a[i]) 
    html << (i > 0 ? ' &raquo; ' : '') #&rsaquo;&#10132;
    html << '</span>'
  end
  # Save level to parents params object
  @parent.params[:menu_level] = a.size
  html
end

#render_cssObject

Return CSS part of code.



175
176
177
# File 'app/renderers/dc_big_menu_renderer.rb', line 175

def render_css
  @menu ? "#{@menu.css}\n #{@selected ? @selected.css : ''}\n" : ''
end

#render_htmlObject

Renderer dispatcher. Method returns HTML part of code.



167
168
169
170
# File 'app/renderers/dc_big_menu_renderer.rb', line 167

def render_html
  method = @opts[:method] || 'default'
  respond_to?(method) ? send(method) : "Error DcBigMenu: Method #{method} doesn't exist!"
end