Class: DcManualRenderer

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

Instance Method Summary collapse

Constructor Details

#initialize(parent, opts = {}) ⇒ DcManualRenderer

Returns a new instance of DcManualRenderer.



34
35
36
37
38
# File 'app/renderers/dc_manual_renderer.rb', line 34

def initialize( parent, opts={} )
  @parent = parent
  @opts   = opts
  self
end

Instance Method Details

#body_for_examplesObject



131
132
133
# File 'app/renderers/dc_manual_renderer.rb', line 131

def body_for_examples
  @manual_page.body
end

#defaultObject

Render manual data



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/renderers/dc_manual_renderer.rb', line 138

def default
  @path = @opts[:path]
  return list if @path.size == 1 and @opts.dig(:settings, 'manual', 'manual_id').nil?
  # link for manual settings on the page
  edit_html = ''
  if dc_edit_mode?
    edit_html << @parent.dc_link_for_edit(table: 'dc_memory', title: 'dc_manual.settings', 
                             form_name: 'dc_manual_settings', icon: 'book lg', 
                             location: @parent.page.class.to_s,
                             field_name: 'params', id: @parent.page.id,
                             action: 'new', element: 'manual' )
  end
  find_document()
  return "ERROR! #{@parent.params[:path]} #{edit_html}" if @manual.nil? or @manual_page.nil?

  if dc_edit_mode?
    opts = { action: 'edit', table: 'dc_manual', 
             id: @manual._id, title: 'dc_manual.edit' }
    edit_html << dc_link_for_edit(opts)    
  end

  html = %Q[
<div class="wrap">
<div class="row manual">
  <div class="column small-12 medium-3 large-4">
    #{edit_html}#{manual_menu()}
  </div>
  <div class="column small-12 medium-9 large-8 body">]

  # ;add dc_manual_pages
  no_subsections = @manual_ids.split(';').size
  table = 'dc_manual' + ';dc_manual_page'*no_subsections
  form_name = @manual_ids.blank? ? 'dc_manual' : 'dc_manual_page'
  if dc_edit_mode?
    unless @manual_ids.blank?
      opts = { action: 'edit', form_name: form_name, table: table,
               ids: @manual_ids, 'id' => @manual_page.id, 
               title: I18n.t('dc_manual.edit_chapter', title: @manual_page.title) }
      html << dc_link_for_edit(opts) + '<br>'.html_safe
    end
    # Add new subchapter
    ids = @manual_ids + (@manual_ids.blank? ? '' : ';') + @manual_page.id.to_s
    table = 'dc_manual' + ';dc_manual_page'*(no_subsections+1)
    opts = { controller: 'cmsedit', action: 'new', form_name: 'dc_manual_page', 
             table: table, ids: ids, 
             title: I18n.t('dc_manual.new_chapter', title: @manual_page.title) }
    html << dc_link_for_create(opts) + '<br>'.html_safe  
  end

  can_view, msg = dc_user_can_view(@parent, @manual_page)
  # process body before it is outputed to browser
  body = @opts[:body_render] ? send(@opts[:body_render]) : @manual_page.body
  html << if can_view
%Q[<h1>#{@manual_page.title}</h1>#{body}<br>
<div class='updated'>
  #{t('dc_manual.updated')} <b>#{@manual_page.updated_at.strftime('%d.%m.%Y')}</b>
</div>]
  else
    msg
  end
  html << '</div>'*3
end

#find_documentObject

Find document.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/renderers/dc_manual_renderer.rb', line 56

def find_document
  # found bookname in path link
  book_name_ix = @path.index(@parent.page.link)
  @manual      = DcManual.find_by(link: @path[book_name_ix+1])
  if @manual
    @prepand_path = @path.shift(book_name_ix+2)
  else
    # manual_id should be set in page settings
    manual_id     = @opts.dig(:settings, 'manual', 'manual_id')
    @prepand_path = @path.shift(book_name_ix+1)
    @manual       = DcManual.find(manual_id) if manual_id
  end  
  return if @manual.nil?
  # ids for editing manual page
  @manual_ids  = '' 
  # Determine selected page
  @manual_page = @manual
  while (link_page = @path.shift) != nil do
    @manual_ids << (@manual_ids.blank? ? '' : ';') + "#{@manual_page.id}"
    @manual_page = @manual_page.dc_manual_pages.find_by(link: link_page)
    if @manual_page.nil?
      # select first page if path is in error
      @manual_page = @manual
      break 
    end      
  end
end

#listObject

List all manuals



43
44
45
46
47
48
49
50
51
# File 'app/renderers/dc_manual_renderer.rb', line 43

def list
  html = "<div class='#{@opts[:div] || 'list-manuals'}'>"
  if @opts[:edit_mode] > 1
    html << dc_link_for_create({ controller: 'cmsedit', table: 'dc_manual', title: t('dc_manual.new') }) 
  end
  manuals = DcManual.where(active: true).sort(title: 1).to_a
  html << @parent.render( partial: 'dc_manual/list', locals: { manuals: manuals }, formats: [:html] )
  html << '</div>'
end

#manual_menuObject

Create menu



119
120
121
122
123
124
125
126
# File 'app/renderers/dc_manual_renderer.rb', line 119

def manual_menu
  menu = @manual.choices4_all_as_tree    
  html = "<div class=\"title\">#{menu_link_for(menu.first,[])}</div>"
  html << submenu(menu.first, menu, [])
%Q[<ul id="manual-menu" class="is-active menu multilevel-accordion-menu " data-accordion-menu data-submenu-toggle="true">
  #{html}
</ul>]
end

Create link for one page



87
88
89
90
91
92
# File 'app/renderers/dc_manual_renderer.rb', line 87

def menu_link_for(menu, parent_path)
  document_link = parent_path + [menu[4]]
  document_link.shift # remove link to document name
  link = "/#{@prepand_path.join('/')}/#{document_link.join('/')}"
  @parent.link_to(menu[0],link)
end

#render_cssObject



214
215
216
# File 'app/renderers/dc_manual_renderer.rb', line 214

def render_css
  @manual.try :css
end

#render_htmlObject



204
205
206
207
208
209
# File 'app/renderers/dc_manual_renderer.rb', line 204

def render_html
  method = @opts[:method] || 'default'
  return "#{self.class}. Method #{method} not defined!" unless method
  
  send method
end

Create submenus for a menu



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/renderers/dc_manual_renderer.rb', line 97

def submenu(menu_item, menu, parent_path)
  # is active when ids path matches parent path or last (my id) matches manual_page
  last_item = menu_item[1].split(';').last
  is_active = (@manual_ids.match(menu_item[1]) or last_item.match(@manual_page.id.to_s)) ? ' is-active' : '' 
  
  html = parent_path.blank? ? '' : "<li class=\"#{is_active}\">#{menu_link_for(menu_item, parent_path)}" 
  # select subpages
  sub_pages = menu.select {|e| e[2] == menu_item[1]}
  return html if sub_pages.size == 0

  sub_pages.sort! {|a,b| a[3] <=> b[3]}
  sub_pages.each do |page|
    html << %Q[<ul class="menu vertical nested #{is_active}" >]
    html << submenu(page, menu, parent_path + [menu_item[4]] )
    html << '</ul>'
  end
  html << '</li>'
end