#-- # Copyright (c) 2012+ Damjan Rems # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ module DrgcmsFormFields ########################################################################### # 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 collection # * +not_id+ Field value represent value not an id. Field will save entered value when not selected wit autocomplete. # * +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 # html: # size: 30 ########################################################################### class TextAutocomplete < DrgcmsField ########################################################################### # Render text_autocomplete field html code ########################################################################### 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('.') else ret_name = @yaml['search'] end # determine table name if @yaml['table'] table = if @yaml['table'].class == String @yaml['table'] # eval(how_to_get_my_table_name) elsif @yaml['table']['eval'] eval @yaml['table']['eval'] else Rails.logger.error "Field #{@yaml['name']}: Invalid table parameter!" nil end end return 'Table or field keyword not defined!' unless (table and ret_name) # TODO check if table exists t = 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 and @record[@yaml['name']] @record[@yaml['name']] end # Found value to be written in field. If field is not found write out value. if value record = t.find(value) unless @yaml['not_id'] # don't if it's is not an id value_displayed = record ? record.send(ret_name) : value end # return if readonly #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 << @parent.text_field(record, _name, @yaml['html']) if @yaml['with_new'] @html << ' ' + @parent.fa_icon('plus-square lg', class: 'in-edit-add', title: t('drgcms.new'), style: "vertical-align: top;", 'data-table' => @yaml['with_new'] ) end @html << @parent.hidden_field(record, @yaml['name'], value: value) # actual value will be in hidden field # JS stuff # allow unselected values on not_id: true option not_id_code = %Q[ if (ui.item == null) { $("##{record}_#{@yaml['name']}").val($("##{record}__#{@yaml['name']}").val() ); return; } ] if @yaml['not_id'] # @js << <