Module: AgileIndexHelper

Defined in:
app/helpers/agile_index_helper.rb

Overview

AgileIndexHelper module defines helper methods used by AgileRails actions. Output is controlled by data found in 3 major sections of AgileRails form: index, data_set and form sections.

Instance Method Summary collapse

Instance Method Details

#agile_actions_for_dataset(record) ⇒ Object

Creates actions that could be performed on single row of dataset.



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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'app/helpers/agile_index_helper.rb', line 290

def agile_actions_for_dataset(record)
  actions = @form['index']['data_set']['actions']
  return '' if actions.nil? || @form['readonly']

  actions, width, has_check = agile_dataset_actions()
  has_sub_menu = actions.size > 2 || (has_check && actions.size > 1)

  main_menu, sub_menu = '', ''
  actions.sort_by(&:first).each do |num, action|
    session[:form_processing] = "data_set:actions: #{num}=#{action}"
    parms = @form_params.clone

    # if single definition simulate type parameter
    yaml = action.instance_of?(String) ? { 'type' => action } : action

    if %w[ajax link window popup submit].include?(yaml['type'])
      @record = record # otherwise record fields can't be used as parameters
      html = agile_link_ajax_window_submit_action(yaml, record)
    else
      caption = agile_get_caption(yaml) || "agile.#{yaml['type']}"
      title   = t(yaml['help'] || caption, '')
      caption = has_sub_menu ? t(caption, '') : nil
      html = '<li>'
      html += case yaml['type']
      when 'check'
        main_menu += "<li>#{check_box_tag("check-#{record.id}", false, false, { class: 'ar-check' })}</li>"
        next

      when 'edit'
        parms['action'] = 'edit'
        parms['id'] = record.id
        agile_link_to( caption, 'edit-o', parms, title: title )

      when 'show'
        parms['action'] = 'show'
        parms['id'] = record.id
        parms['readonly'] = true
        agile_link_to( caption, 'eye', parms, title: title )

      when 'duplicate'
        parms['id'] = record.id
        # duplicate string will be added to these fields.
        parms['dup_fields'] = yaml['dup_fields'] 
        parms['action'] = 'create'
        agile_link_to( caption, 'content_copy-o', parms, data: { confirm: t('agile.confirm_dup') }, method: :post, title: title )

      when 'delete'
        parms['action'] = 'destroy'
        parms['id'] = record.id
        agile_link_to( caption, 'delete-o', parms, data: { confirm: t('agile.confirm_delete') }, method: :delete, title: title )

      else # error.
        yaml['type'].to_s
      end
      html += '</li>'
    end

    if has_sub_menu
      sub_menu += html
    else
      main_menu += html
    end
  end

  if has_sub_menu
    %(
<ul class="ar-result-actions" style="width: #{width}px;">#{main_menu}
  <li><div class="ar-result-submenu">#{mi_icon('more_vert')}
    <ul id="menu-#{record.id}">#{sub_menu}</ul>
  </div></li>
</ul>)
  else
    %(<ul class="ar-result-actions" style="width: #{width}px;">#{main_menu}</ul>)
  end.html_safe
end

#agile_actions_for_indexObject

Creates action div for AgileRails index action.



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
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
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
173
174
175
176
177
# File 'app/helpers/agile_index_helper.rb', line 35

def agile_actions_for_index
  @js  = @form['script'] || @form['js'] || ''
  @css = @form['css'] || ''
  return '' if @form['index'].nil?

  actions = @form['index']['actions']
  return '' if actions.nil?

  std_actions = Agile.config(:index_standard_actions)
  if actions.instance_of?(String)
    actions = process_standard_actions(actions, std_actions)
  elsif actions['standard']
    actions.merge!(std_actions)
    actions.delete('standard')
  end
  # remove new if readonly
  actions_remove_action(actions, 'new') if @form['readonly']

  html_left, html_right = '', ''
  # Remove actions settings and sort
  only_actions = actions.select{ |k, v| k.instance_of?(Integer) }.sort_by(&:first).map(&:last)
  only_actions.each do |options|
    next if options.nil? # must be

    url    = @form_params.clone
    yaml   = options.instance_of?(String) ? { 'type' => options } : options # if single definition simulate type parameter
    action = yaml['type'].to_s.downcase
    # if return_to is present link directly to URL
    if action == 'link' && yaml['url']
      url = yaml['url']
    else
      url['controller'] = yaml['controller'] if yaml['controller']
      url['action']     = yaml['action'] || action
      url['table']      = yaml['table']  if yaml['table']
      url['form_name']  = yaml['form_name'] if yaml['form_name']
      url['control']    = yaml['control'] if yaml['control']
    end
    # html link options
    html_options = yaml['html'] || {}
    html_options['title'] = yaml['title'] if yaml['title']
    html = case action
           # new
           when 'new'
             caption = yaml['caption'] || 'agile.new'
             html_options['class'] = 'ar-link'
             "<li>#{agile_link_to(caption, 'add', url, html_options)}</li>"

           # filter
           when 'filter'
             # filter off is not present
             next if session.dig(:filters, @form['table'], :filter, :no_off)

             url = ''
             if session.dig(:filters, @form['table'], :filter)
               url = url_for(controller: :agile, action: :run, control: 'agile.filter_off', t: @form['table'], f: AgileHelper.form_param(params))
             end
             yaml['position'] ||= 'right'
             %(
<li>
  <div class="ar-filter" title="#{ArFilter.title_for_filter_off(session.dig(:filters, @form['table']))}" data-url="#{url.html_safe}">
    #{mi_icon(url.blank? ? 'search-o' : 'filter_alt_off-o') }#{ArFilter.filter_menu(self).html_safe}
  </div>
</li>#{ArFilter.get_filter_input_field(self)}).html_safe

           # close
           when 'close'
             %(<li><div class="ar-link" onclick="window.close();"'>#{mi_icon('close')} #{t('agile.close')}</div></li>)

           # back
           when 'back'
             %(<li><div class="ar-link" onclick="history.back();"'>#{mi_icon('arrow_back')} #{t('agile.back')}</div></li>)

           # menu
           when 'menu'
             code = if options['caption']
                      caption = "#{t(options['caption'], options['caption'])}&nbsp;#{mi_icon('caret-down')}"
                      caption + agile_process_eval(options['eval'], self)
                    else # when caption is false, provide own actions
                      agile_process_eval(options['eval'], self)
                    end
             %(<li><div class="ar-link">#{code}</div></li>)
=begin
# reorder      
    when action == 'reorder' then  
      caption = t('agile.reorder')
      parms = @form_params.clone
      parms['operation'] = v
      parms['id']       = params[:ids]
      parms['table']     = @form['table']
      agile_link_to( caption, 'reorder', parms, method: :delete )              
=end

           when 'script'
             agile_script_action(options)

           when 'field'
             yaml['position'] ||= 'right'
             agile_field_action(yaml)

           when 'ajax', 'link', 'window', 'popup', 'submit'
             agile_link_ajax_window_submit_action(options, nil)

           # sort
           when 'sort'
             yaml['position'] ||= 'right'
             choices = [%w[id id]]
             @form['index']['sort']&.split(',')&.each do |e|
               e.strip!
               choices << [t("helpers.label.#{@form['table']}.#{e}"), e]
             end
             data = mi_icon('sort') + select('sort', 'sort', choices, { include_blank: true },
                                             { class: 'ar-sort-select', 'data-table' => @form['table'],
                                               'data-form' => AgileHelper.form_param(params)})
             %(<li title="#{t('agile.sort')}"><div class="ar-sort">#{data}</li>)

           # link
           else
             caption = agile_get_caption(yaml) || t("agile.#{action}")
             icon    = yaml['icon'] || action
             html_options['class'] = 'ar-link'
             code = agile_link_to(caption, icon, url, html_options)
             html_left += %(<li>#{code}</li>)
           end
    yaml['position'] ||= 'left'
    if yaml['position'] == 'left'
      html_left += html
    else
      html_right += html
    end
  end

  %(
<form id="ar-action-menu">
  <span class="ar-spinner">#{mi_icon('settings-o spin')}</span>

  <div class="ar-action-menu">
    <ul class="ar-left">#{html_left}</ul>
    <ul class="ar-right">#{html_right}</ul>
  </div>
<div style="clear: both;"></div>
</form>
  ).html_safe
end

#agile_columns_for_dataset(record) ⇒ Object

Creates column for each field of dataset record.



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'app/helpers/agile_index_helper.rb', line 478

def agile_columns_for_dataset(record)
  data_set = @form['index']['data_set']
  return '' unless data_set['columns']

  sort_data = session.dig(:filters, @form['table'], :sort)
  sort_field, sort_direction = sort_data.to_s.split(' ')
  html, index = '', 0
  data_set['columns'].sort.each do |k, v|
    session[:form_processing] = "data_set:columns: #{k}=#{v}"
    next if v['width'].to_s.match(/hidden|none/i)

    # convert shortcut to hash 
    v = { 'name' => v } if v.instance_of?(String)
    begin
              # as Array (footer)
      value = if record.instance_of?(Array)
                agile_format_value(record[index], v['format']) if record[index]
              # as Hash (ar_memory)
              elsif record.instance_of?(Hash)
                agile_format_value(record[ v['name'] ], v['format'])
              # eval
              elsif v['eval']
                agile_process_column_eval(v, record)
              # as field
              elsif record.respond_to?(v['name'])
                agile_format_value(record.send( v['name'] ), v['format'])
              else
                "??? #{v['name']}"
              end
    rescue Exception => e
      agile_log_exception(e, 'agile_columns_for_dataset')
      value = '!!!Error'
    end
    html += '<div class="spacer"></div>'
    # set column class
    class_ = agile_style_or_class(nil, v['td_class'], value, record)
    class_ += ' is-sorted' if sort_field == v['name']
    # set width and align an additional style
    style = agile_style_or_class(nil, v['td_style'] || v['style'], value, record)
    flex_align  = v['align'].to_s == 'right' ? 'text-align: right;' : ''
    width_align = "width:#{v['width'] || '15%'};#{flex_align}"
    style = %(style="#{width_align}#{style}" )

    html += %(<div class="td #{class_}" #{style}>#{value}</div>)
    index += 1
  end
  html.html_safe
end

#agile_dataset_actionsObject

Determines actions and width of actions column



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'app/helpers/agile_index_helper.rb', line 258

def agile_dataset_actions
  actions = @form['index']['data_set']['actions']
  return [{}, 0, false] if actions.nil? || agile_dont?(actions)

  std_actions = Agile.config(:dataset_standard_actions)
  if actions.instance_of?(String)
    actions = process_standard_actions(actions, std_actions)
  elsif actions['standard']
    actions.merge!(std_actions)
    actions.delete('standard')
  end

  # check must be 0 action
  has_check = actions[0] && actions[0] == 'check'
  width = actions.size == 1 ? 22 : 44
  width = 22 if actions.size > 2 && !has_check
  [actions, width, has_check]
end

Calculates (blank) space required for actions when @footer_record is rendered



280
281
282
283
284
285
# File 'app/helpers/agile_index_helper.rb', line 280

def agile_dataset_actions_for_footer
  return '' unless @form['index']['data_set']['actions']

  ignore, width, ignore2 = agile_dataset_actions()
  %(<div class="ar-result-actions" style="width: #{width}px;"></div>).html_safe
end

#agile_div_filterObject

Paints div for set filter popup



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
# File 'app/helpers/agile_index_helper.rb', line 182

def agile_div_filter
  choices = []
  filter = (@form.dig('index', 'filter') || '').split(',').delete_if { _1 == 'id' }.map(&:strip)
  filter << 'id as text_field' # filter for id is added by default
  filter.each do |f|
    f = f.strip
    name = f.match(' as ') ? f.split(' ').first : f
    # like another field on the form
    choices << [t("helpers.label.#{@form['table']}.#{name}", name), f]
  end
  choices_for_operators = t('agile.choices_for_filter_operators').chomp.split(',').map do |v|
    v.match(':') ? v.split(':') : v
  end
  # currently selected options
  field_name, operators_value = nil, nil
  filter = session.dig(:filters, @form['table'], :filter)
  if filter
    field_name = filter[:field]
    operators_value = filter[:value]
  end
  url_on  = url_for(controller: :agile, action: :run, control: 'agile.filter_on' ,
                    t: AgileHelper.table_param(params), f: AgileHelper.form_param(params), filter_input: 1)
  url_off = url_for(controller: :agile, action: :run, control: 'agile.filter_off',
                    t: AgileHelper.table_param(params), f: AgileHelper.form_param(params))
  %(
  <div id="ar_filter" class="div-hidden">
    <h1>#{t('agile.filter_set')}</h1>

    #{ select(nil, 'filter_field1', options_for_select(choices, field_name), { include_blank: true }) }
    #{ select(nil, 'filter_oper', options_for_select(choices_for_operators, operators_value)) }
    <div class="ar-edit-menu">
      <div class="ar-link ar-filter-set" data-url="#{url_on}">#{mi_icon('done')} #{t('agile.filter_on')}</div>
      <div class="ar-link-ajax" data-url="#{url_off}">
         #{mi_icon('close')}#{t('agile.filter_off')}
      </div>
    </div>
  </div>).html_safe
end

#agile_eval_to_array(expression) ⇒ Object

Split eval expression to array by parameters. Ex. Will split agile_text_for_value(one ,"two") => ['agile_text_for_value', 'one', 'two']



531
532
533
# File 'app/helpers/agile_index_helper.rb', line 531

def agile_eval_to_array(expression)
  expression.split(/[ ,()]/).select(&:present?).map { _1.gsub(/['"]/, '').strip }
end

#agile_filter_popupObject

Creates popup div for setting filter on dataset header.



224
225
226
227
228
229
230
231
232
233
234
# File 'app/helpers/agile_index_helper.rb', line 224

def agile_filter_popup
  html = %(<div class="filter-popup" style="display: none;"><div>#{t('agile.filter_set')}</div><ul>)
  url  = url_for(controller: :agile, action: :run, control: 'agile.filter_on',
                 t: @form['table'], f: params['form_name'], filter_input: 1)

  t('agile.choices_for_filter_operators').chomp.split(',').each do |operator_choice|
    caption, choice = operator_choice.split(':')
    html += %(<li data-operator="#{choice}" data-url="#{url}">#{caption}</li>)
  end 
  "#{html}</ul></div>".html_safe
end

#agile_form_titleObject

Will return title based on @form



239
240
241
242
243
244
245
# File 'app/helpers/agile_index_helper.rb', line 239

def agile_form_title
  return t("helpers.label.#{@form['table']}.table_title", @form['table'])  if @form['title'].nil?
  return t(@form['title'], @form['title']) if @form['title'].instance_of?(String)

  # Hash
  agile_process_eval(@form['title']['eval'], [@form['title']['caption'] || @form['title']['text'], params])
end

#agile_format_value(value, format = nil) ⇒ Object

Formats value according to format supplied or data type. There is lots of things missing here.



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'app/helpers/agile_index_helper.rb', line 445

def agile_format_value(value, format = nil)
  return '' if value.blank?

  klass = value.class.to_s
  return AgileHelper.format_date_time(value, format) if klass.match(/time|date/i)

  format = format.to_s.upcase
  if format[0] == 'N'
    return '' if value.to_f == 0.0 && format.match('Z')

    format.gsub!('Z', '')
    dec = format[1].blank? ? nil : format[1].to_i
    sep = format[2].blank? ? nil : format[2]
    del = format[3].blank? ? nil : format[3]
    cur = format[4].blank? ? nil : format[4]
    agile_format_number(value, dec, sep, del, cur)
  else
    value.to_s
  end
end

#agile_header_for_datasetObject

Creates header div for dataset.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'app/helpers/agile_index_helper.rb', line 369

def agile_header_for_dataset
  html = '<div class="ar-result-header">'
  if @form['index']['data_set']['actions'] && !@form['readonly']
    ignore, width, has_check = agile_dataset_actions()
    check_all = mi_icon('check-box-o', class: 'ar-check-all') if has_check
    html += %(<div class="ar-result-actions" style="width:#{width}px;">#{check_all}</div>)
  end
  # preparation for sort icon
  sort_data = session.dig(:filters, @form['table'], :sort)
  sort_field, sort_direction = sort_data.to_s.split(' ')
  if (columns = @form['index']['data_set']['columns'])
    columns.sort.each do |key, options|
      session[:form_processing] = "data_set:columns: #{key}=#{options}"
      next if options['width'].to_s.match(/hidden|none/i)

      is_sorted = sort_field == options['name'] ? ' is-sorted' : ''
      style = "width:#{options['width'] || '15%'};text-align:#{options['align'] || 'left'}"
      th = %(<div class="th#{is_sorted}" style="#{style}"};" data-name="#{options['name']}")
      label = t_label_for_column(options)

      filter_description = get_filter_description(options['name'])
      icon = 'sort_unset md-18' if filter_description
      icon = nil if session.dig(:filters, @form['table'], :filter, :no_off)
      icon = mi_icon(icon, 'data-filter' => filter_description) if icon
      # no sorting when embedded records or custom filter is active
      sort_ok = !agile_dont?(@form['index']['data_set']['sort'], false)
      sort_ok ||= @form['index'] && @form['index']['sort']
      sort_ok &&= !agile_dont?(options['sort'], false)
      if sort_ok
        url = url_for(controller: :agile, action: :run, control: 'agile.sort', sort: options['name'],
                      t: AgileHelper.table_param(params), f: AgileHelper.form_param(params))
        th += %(><span data-url="#{url}">#{label}</span>#{icon}</div>)
      else
        th += ">#{label}#{icon}</div>"
      end
      html += "<div class=\"spacer\"></div>#{th}"
    end
  end
  "#{html}</div>".html_safe
end

#agile_row_for_dataset(record) ⇒ Object

Creates tr code for each row of dataset.



469
470
471
472
473
# File 'app/helpers/agile_index_helper.rb', line 469

def agile_row_for_dataset(record)
  clas  = "ar-#{cycle('odd','even')} " + agile_style_or_class(nil, @form['index']['data_set']['tr_class'], nil, record)
  style = agile_style_or_class('style', @form['index']['data_set']['tr_style'], nil, record)
  %(<div  id="#{record.id}" class="ar-result-data #{clas}" #{dblclick_on_dataset_action(record)} #{style}>).html_safe
end

#agile_title_for_index(result = nil) ⇒ Object

Creates title div for index action. Title div also includes paging options and help link



251
252
253
# File 'app/helpers/agile_index_helper.rb', line 251

def agile_title_for_index(result = nil)
  agile_dialog_title(agile_form_title(), result)
end

#dblclick_on_dataset_action(record) ⇒ Object

Creates link for single or double click on dataset column



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'app/helpers/agile_index_helper.rb', line 413

def dblclick_on_dataset_action(record)
  html = ''
  if @form['index']['data_set']['dblclick']
    yaml = @form['index']['data_set']['dblclick']
    opts = { id: record.id }
    opts[:controller] = yaml['controller'] || :agile
    opts[:action]     = yaml['action']
    opts[:table]      = yaml['table'] || AgileHelper.table_param(params)
    opts[:form_name]  = yaml['form_name'] || AgileHelper.form_param(params) || opts[:table]
    opts[:method]     = yaml['method'] || 'get'
    opts[:readonly]   = yaml['readonly'] if yaml['readonly']
    opts[:window_close] = yaml['window_close'] if yaml['window_close']
    url_forward_params(opts)

    html += " data-dblclick=#{url_for(opts)}"
  else
    opts = { action: :show,
             controller: :agile,
             id: record.id,
             ids: params[:ids],
             readonly: (params[:readonly] ? 2 : 1),
             table: AgileHelper.table_param(params),
             form_name: AgileHelper.form_param(params) }
    url_forward_params(opts)
    html += " data-dblclick=#{url_for(opts)}" if @form['form']
  end
  html
end