diff --git a/app/assets/javascripts/ingredients.js b/app/assets/javascripts/ingredients.js index 14afb46..baa04f7 100644 --- a/app/assets/javascripts/ingredients.js +++ b/app/assets/javascripts/ingredients.js @@ -1,65 +1,69 @@ + +window.INGREDIENT_API = {}; + (function($) { function initializeEditor($ingredientForm) { usdaFoodSearchEngine.initialize(false); var $typeahead = $ingredientForm.find(".ndbn_typeahead"); - var $usdaModal = $("#link_ndbn_modal"); - var $name = $ingredientForm.find(".name"); - var $ndbn = $ingredientForm.find("input.ndbn"); - var $ndbn_group = $ingredientForm.find(".ndbn_group"); - - if ($ndbn.val()) { - } $typeahead.typeahead_search({ searchUrl: '/ingredients/usda_food_search.html', - resultsContainer: '#link_ndbn_modal .results' + resultsContainer: $ingredientForm.find(".ndbn_results") },{ name: 'usdaFoods', source: usdaFoodSearchEngine, + limit: 10, display: function(datum) { return datum.name; } }); $typeahead.on("typeahead_search:selected", function(evt, item) { - selectNdbn(item.ndbn); + selectNdbn($ingredientForm, item.ndbn); }); - $usdaModal.on("shown.bs.modal", function() { - var $this = $(this); - $typeahead.typeahead("val", $name.val()); - $typeahead.focus(); - $typeahead.select(); - }); - - $ingredientForm.on("click", "#link_ndbn_modal .results .food_result", function(evt) { + $ingredientForm.on("click", ".ndbn_results .food_result", function(evt) { var $item = $(evt.target); var ndbn = $item.data("ndbn"); - selectNdbn(ndbn); + selectNdbn($ingredientForm, ndbn); }); } - function selectNdbn(ndbn) { - var $ingredientForm = $("#ingredient_form"); + function selectNdbn($ingredientForm, ndbn) { var id = $ingredientForm.find("input.id").val(); $ingredientForm.find("input.ndbn").val(ndbn); $ingredientForm.attr("action", "/ingredients/" + id + "/select_ndbn").attr("data-remote", "true"); - $("#link_ndbn_modal").modal('hide').on('hidden.bs.modal', function() { - $ingredientForm.submit(); - }); + $ingredientForm.submit(); } + function customTokenizer(str) { + if (str) { + var cleaned = str.replace(/,/g, ""); + return Bloodhound.tokenizers.whitespace(cleaned); + } else { + return []; + } + } + + window.INGREDIENT_API.initialize = function() { + var $ingredientForm = $("#ingredient_form"); + + if ($ingredientForm.length) { + initializeEditor($ingredientForm); + } + }; + var usdaFoodSearchEngine = new Bloodhound({ initialize: false, datumTokenizer: function(datum) { var str = datum ? datum.name : null; - return str ? str.split(/[\s,]+/) : []; + return customTokenizer(str); }, - queryTokenizer: Bloodhound.tokenizers.whitespace, + queryTokenizer: customTokenizer, identify: function(datum) { return datum.ndbn; }, sorter: function(a, b) { if (a.name < b.name) { @@ -77,11 +81,7 @@ }); $(document).on("ready page:load", function() { - var $ingredientForm = $("#ingredient_form"); - - if ($ingredientForm.length) { - initializeEditor($ingredientForm); - } + window.INGREDIENT_API.initialize(); }); })(jQuery); \ No newline at end of file diff --git a/app/models/usda_food.rb b/app/models/usda_food.rb index 41b32d1..5bfb420 100644 --- a/app/models/usda_food.rb +++ b/app/models/usda_food.rb @@ -4,7 +4,7 @@ class UsdaFood < ActiveRecord::Base has_many :usda_food_weights def self.search(query) - tokens = query.to_s.split(' ') + tokens = query.to_s.gsub(',', '').split(' ') if tokens.empty? UsdaFood.none diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb index a028562..cf84fca 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -13,21 +13,19 @@ <%= f.text_field :name, class: 'form-control name', autofocus: true %> -
+
<%= f.label :ndbn, "Nutrient Databank Number", class: 'control-label' %>
-
- -
- <%= f.text_field :usda_food_name, class: 'form-control', readonly: true %> + +
-
- <%= f.label :notes, class: 'control-label' %> - <%= f.text_area :notes, class: 'form-control' %> +
@@ -35,9 +33,14 @@ <%= f.text_field :density, class: 'form-control', disabled: has_ndbn %>
+
+ <%= f.label :notes, class: 'control-label' %> + <%= f.text_area :notes, class: 'form-control' %> +
+
-

Per 100 grams

+

Nutrition Per 100 grams

> @@ -100,37 +103,8 @@
- -
<%= f.submit class: 'btn btn-primary' %>
- - <% end %> diff --git a/app/views/ingredients/select_ndbn.js.erb b/app/views/ingredients/select_ndbn.js.erb index 8826196..4eb7e32 100644 --- a/app/views/ingredients/select_ndbn.js.erb +++ b/app/views/ingredients/select_ndbn.js.erb @@ -1,2 +1,3 @@ -$("#ingredient_form").replaceWith($("<%= escape_javascript(render(partial: 'ingredients/form')) %>")); \ No newline at end of file +$("#ingredient_form").replaceWith($("<%= escape_javascript(render(partial: 'ingredients/form')) %>")); +window.INGREDIENT_API.initialize(); \ No newline at end of file