Class: DrgcmsFormFields::TreeSelect

Inherits:
Select show all
Defined in:
app/models/drgcms_form_fields.rb

Overview

Implementation of tree_select DRG CMS form field. Field will provides multiple select functionality displayed as a tree. Might be used for selecting multiple categories in a parent-child tree view.#

Form options:

  • name: field name (required)

  • type: tree_select (required)

  • choices: Values for choices separated by comma. Values can also be specified like description:value.

In this case description will be shown to user, but value will be saved to document.

choices: 'OK:0,Ready:1,Error:2'
choices: Ruby,Pyton,PHP
  • eval: Choices will be provided by evaluating expression eval: ModelName.choices4_field; Model class should define method which will provide data for field. Data returned must be of type Array and have 3 elements. 1 - description text 2 - id value 3 - parent id

  • html: html options which apply to select and text_field fields (optional)

Form example:

10:
  name: categories
  type: tree_select
  eval: 'Categories.all_categories'
  html:
    size: 50x10

Instance Attribute Summary

Attributes inherited from DrgcmsField

#html, #js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Select

#do_eval, #get_choices, #ro_standard

Methods inherited from DrgcmsField

#hash_to_options, #initialize, #record_text_for, #ro_standard, #set_initial_value, #set_style, #t

Constructor Details

This class inherits a constructor from DrgcmsFormFields::DrgcmsField

Class Method Details

.get_data(params, name) ⇒ Object

Return value. Return nil if input field is empty



1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
# File 'app/models/drgcms_form_fields.rb', line 1502

def self.get_data(params, name)
  return nil if params['record'][name].blank?
#
  result = params['record'][name].split(',')
  result.delete_if {|e| e.blank? }
  return nil if result.size == 0
# convert to BSON objects if is BSON object ID
  result = result.map{ |e| BSON::ObjectId.from_string(e) } if BSON::ObjectId.legal?(result.first)
# return only first element if multiple values select was not alowed
  params['record']["#{name}_multiple"] == '1' ? result : result.first  
end

Instance Method Details

#make_tree(parent) ⇒ Object

Prepare choices for tree data rendering.



1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'app/models/drgcms_form_fields.rb', line 1423

def make_tree(parent)
  return '' unless @choices[parent.to_s]
  @html << '<ul>'
  choices = if @choices[parent.to_s].first[3] != 0
    @choices[parent.to_s].sort_by {|e| e[3].to_i } # sort by order if first is not 0
#    @choices[parent.to_s].sort_alphabetical_by(&:first) # use UTF-8 sort
  else  
    @choices[parent.to_s].sort_alphabetical_by(&:first) # use UTF-8 sort
  end
  choices.each do |choice|
    jstree = %Q[{"selected" : #{choice.last ? 'true' : 'false'} }]
# data-jstree must be singe quoted
    @html << %Q[<li data-id="#{choice[1]}" data-jstree='#{jstree}'>#{choice.first}\n]
# call recursively for children     
    make_tree(choice[1]) if @choices[ choice[1].to_s ]
    @html << "</li>"
  end
  @html << '</ul>'  
end

#renderObject

Render tree_select field html code



1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
# File 'app/models/drgcms_form_fields.rb', line 1446

def render
  return ro_standard if @readonly  
  set_initial_value('html','value')
  require 'sort_alphabetical'  
  
  record = record_text_for(@yaml['name'])
  @html << "<div id=\"#{@yaml['name']}\" class=\"tree-select\" #{set_style()} >"
# Fill @choices hash. The key is parent object id
  @choices = {}
  do_eval(@yaml['eval']).each {|data| @choices[ data[2].to_s ] ||= []; @choices[ data[2].to_s ] << (data << false)}
# put current values hash with. To speed up selection when there is a lot of categories
  current_values = {}
  current = @record[@yaml['name']] || []
  current = [current] unless current.class == Array # non array fields
  current.each {|e| current_values[e.to_s] = true}
# set third element of @choices when selected
  @choices.keys.each do |key|
    0.upto( @choices[key].size - 1 ) do |i|
      choice = @choices[key][i]
      choice[choice.size - 1] = true if current_values[ choice[1].to_s ]
    end
  end
  make_tree(nil)
  @html << '</ul></div>'
# add hidden communication field  
  @html << @parent.hidden_field(record, @yaml['name'], value: current.join(','))
# save multiple indicator for data processing on return
  @html << @parent.hidden_field(record, "#{@yaml['name']}_multiple", value: 1) if @yaml['multiple']
# javascript to update hidden record field when tree looses focus
  @js =<<EOJS
$(function(){
  $("##{@yaml['name']}").jstree( {
    "checkbox" : {"three_state" : false},        
    "core" : { "themes" : { "icons": false },
               "multiple" : #{@yaml['multiple'] ? 'true' : 'false'}  },
    "plugins" : ["checkbox"]
  });
});
  
$(document).ready(function() {
  $('##{@yaml['name']}').on('focusout', function(e) {
    var checked_ids = [];
    var checked = $('##{@yaml['name']}').jstree("get_checked", true);
    $.each(checked, function() {
      checked_ids.push( this.data.id );
    });
    $('#record_#{@yaml['name']}').val( checked_ids.join(",") );
  });
});
EOJS
  self
end