re-enable autocomplete per label

This commit is contained in:
Andrew Brown 2018-09-07 21:51:58 -05:00
parent e9f5c961a5
commit be9b39adca
4 changed files with 31 additions and 16 deletions

View File

@ -146,8 +146,8 @@ class ContentController < ApplicationController
'content_type': content_type.name
}) if Rails.env.production?
if params.key? 'image_uploads'
upload_files params['image_uploads'], content_type.name, @content.id
if params.key?('image_uploads')
upload_files(params['image_uploads'], content_type.name, @content.id)
end
if @content.is_a?(Universe) && params.key?('contributors') && @content.user == current_user

View File

@ -4,7 +4,7 @@ class AutocompleteService < Service
# Adding a field name to this switch/case will enable autocompleting
# for that field across any page type.
def self.for(field_name)
def self.for_field_name(field_name)
case field_name
when 'bodytype'
t('body_types')
@ -65,9 +65,19 @@ class AutocompleteService < Service
end.uniq
end
def self.autocompleteable?(field_name)
return false
self.for(field_name).any?
# Adding a field label to this switch/case will enable autocompleting
# for that field across any page type.
def self.for_field_label(field_label)
case field_label.downcase
when 'eye color', 'eyecolor'
t('eye_colors')
else
[]
end.uniq
end
def self.autocompleteable?(field_label)
self.for_field_label(field_label).any?
end
# helper method so we don't have to I18n every time

View File

@ -188,8 +188,8 @@
<% end %>
<% else %>
<% if AutocompleteService.autocompleteable?(field.name) %>
<%= render 'content/form/text_input', f: f, content: content, field: field, autocomplete: AutocompleteService.for(field.name) %>
<% if AutocompleteService.autocompleteable?(field.label) %>
<%= render 'content/form/text_input', f: f, content: content, field: field, autocomplete: AutocompleteService.for_field_label(field.label) %>
<% else %>
<%= render 'content/form/text_input', f: f, content: content, field: field %>
<% end %>

View File

@ -29,20 +29,25 @@
%>
<%= hidden_field_tag "#{content_name}[custom_attribute_values][][name]", field.name %>
<%= text_area_tag "#{content_name}[custom_attribute_values][][value]", value && value.value, class: "materialize-textarea #{'autocomplete' if defined?(autocomplete) && autocomplete}", placeholder: placeholder %>
<%=
text_area_tag "#{content_name}[custom_attribute_values][][value]",
value && value.value,
class: "materialize-textarea #{defined?(autocomplete) && autocomplete ? ('autocomplete ' + 'js-autocomplete-' + field.id.to_s) : ''}",
placeholder: placeholder
%>
<% unless field.name_field? || field.universe_field? %>
<%= render 'content/form/attribute_field_dropdown', field: field %>
<% end %>
</div>
<% if defined?(autocomplete) && autocomplete %>
<%# todo re-enable this %>
<%# content_for :javascript do %>
<!-- $(function() {
<%= content_for :javascript do %>
$(function() {
// This setTimeout is an unfortunate hack to ensure this runs after initializing materialize
setTimeout(function() {
console.log("Initializing autocomplete for #<%= "#{content_name}_#{field.name}" %>");
$('#<%= "#{content_name}_#{field.name}" %>').autocomplete({
console.log("Initializing autocomplete for #<%= "#{content_name}_#{field.label}" %>");
$('.js-autocomplete-<%= field.id.to_s %>').autocomplete({
limit: 5,
data: {
<% autocomplete.each do |autocomplete_option| %>
@ -51,6 +56,6 @@
}
});
}, 1000);
}); -->
<%# end %>
});
<% end %>
<% end %>