Class: DrgcmsFormFields::TextAutocomplete

Inherits:
DrgcmsField
  • Object
show all
Defined in:
app/models/drgcms_form_fields/text_autocomplete.rb

Overview

Implementation of text_autocomplete DRG CMS form field.

Form options:

  • name: field name (required)

  • type: text_autocomplete (required)

  • table Collection (table) name. When defined search must contain field name

  • with_new Will add an icon for shortcut to add new document to related collection

  • with_edit Will add an icon for shortcut to edit (view) related document

  • is_id Field value represent value as id. If false, field will use entered value and not value selected with autocomplete. Default is true.

  • search: Search may consist of three parameters from which are separated either by dot (.)

    • search_field_name; when table option is defined search must define field name which will be used for search query

    • collection_name.search_field_name; Same as above except that table options must be ommited.

    • collection_name.search_field_name.method_name; When searching is more complex custom search

    method may be defined in CollectionName model which will provide result set for search.

Form example:

10:
  name: user_id
  type: text_autocomplete
  search: dc_user.name
  is_id: false
  size: 30
  with_new: user
  with_edit: user

Instance Attribute Summary

Attributes inherited from DrgcmsField

#css, #js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DrgcmsField

#__css_code, #hash_to_options, #html, #initialize, #record_text_for, #ro_standard, #set_css_code, #set_default_value, #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



165
166
167
# File 'app/models/drgcms_form_fields/text_autocomplete.rb', line 165

def self.get_data(params, name)
  params['record']["_#{name}"].blank? ? nil : params['record'][name]
end

Instance Method Details

#renderObject

Render text_autocomplete field html code



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
# File 'app/models/drgcms_form_fields/text_autocomplete.rb', line 56

def render
  # Return descriptive text and put it into input field
  # search field name
  if @yaml['search'].class == Hash
    table    = @yaml['search']['table']
    ret_name = @yaml['search']['field']
    method   = @yaml['search']['method']
  elsif @yaml['search'].match(/\.|\,| /)
    table, ret_name, method = @yaml['search'].split(/\.|\,| /).map(&:strip)
  else
    ret_name = @yaml['search']
  end
  # determine table name
  if @yaml['table']
    table = if @yaml['table'].class == String
      @yaml['table']
    elsif @yaml['table']['eval']
      eval @yaml['table']['eval']
    else
      Rails.logger.error "Field #{ @yaml['name'] }: Invalid table parameter!"
      nil
    end
  end
  return ro_standard 'Table or field keyword not defined!' unless (table && ret_name)
  # TODO check if table exists
  model_klass = table.classify.constantize
  # find record and return value of field
  value_send_as = 'p_' + @yaml['name']
  value = if @parent.params[value_send_as]
            @parent.params[value_send_as]
          elsif @record && @record[@yaml['name']]
            @record[@yaml['name']]
          end
  not_id = @parent.dc_dont?(@yaml['is_id'], false)
  # Found value to be written in field. If field is not found write out value.
  value_displayed = value
  if value && !not_id
    if BSON::ObjectId.legal?(value)
      record = model_klass.find(value)
      value_displayed = record.send(ret_name) if record
    else
      value_displayed = model_klass.send(ret_name, method, @parent, value)
    end
  end
  return ro_standard(value_displayed) if @readonly

  # Add method back, so autocomplete will know that it must search for method inside class
  ret_name = "#{ret_name}.#{method}" if method
  @yaml['html'] ||= {}
  @yaml['html']['value'] = value_displayed
  @yaml['html']['placeholder'] ||= t('drgcms.search_placeholder') || nil

  _name = '_' + @yaml['name']
  record = record_text_for(@yaml['name'])  
  @html << '<span class="dc-text-autocomplete">' + @parent.text_field(record, _name, @yaml['html']) + '<span></span>'
  # with new icon
  if @yaml['with_new']
    @html << ' ' + @parent.fa_icon('plus-square-o', class: 'in-edit-add', title: t('drgcms.new'),
             style: "vertical-align: top;", 'data-table' => @yaml['with_new'] )
  end
  # with edit icon
  if @yaml['with_edit'] && @record[@yaml['name']].present?
    @html << ' ' + @parent.fa_icon('edit-o', class: 'in-edit-add', title: t('drgcms.edit'),
             style: "vertical-align: top;", 'data-table' => @yaml['with_edit'], 'data-id' => @record[@yaml['name']] )
  end
  @html << '</span>' + @parent.hidden_field(record, @yaml['name'], value: value) # actual value will be in hidden field

  # JS stuff
  # allow unselected values on is_id: false option
  not_id_code = %(
if (ui.item == null) {  
$("##{record}_#{@yaml['name']}").val($("##{record}__#{@yaml['name']}").val() );
return;
} ) if not_id

  @js << <<EOJS
$(document).ready(function() {
  $("##{record}_#{_name}").autocomplete( {
    source: function(request, response) {
      $.ajax({
        url: "/dc_common/autocomplete",
        type: "POST",
        dataType: "json",
        data: { input: request.term, table: "#{table}", search: "#{ret_name}" #{(',id: "'+@yaml['id'] + '"') if @yaml['id']} },
        success: function(data) {
          response( $.map( data, function(key) {
            return key;
          }));
        }
      });
    },

    change: function (event, ui) { 
      #{not_id_code}
      if (ui.item == null) return;
      $("##{record}_#{@yaml['name']}").val(ui.item.id);
    },

    minLength: 2
  });
});
EOJS
    
  self 
end