ingredient-search.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function getRecipeIngredientIdField(nameField) {
  2. var fieldId = nameField.id;
  3. var idFieldId = fieldId.replace(/\.([^.]+)\.name$/, '.$1.$1Id');
  4. return document.getElementById(idFieldId);
  5. }
  6. function handleIngredientFocus(event) {
  7. $(this).data('startingValue',$(this).val());
  8. }
  9. function handleIngredientBlur(event) {
  10. console.log("blur: ", this, event, $(this).data('startingValue'), $(this).val());
  11. if ( $(this).data('startingValue') != $(this).val() ) {
  12. var idField = $(getRecipeIngredientIdField(this));
  13. idField.val('0');
  14. console.log("unknown ingredient " +$(this).val());
  15. }
  16. }
  17. function handleIngredientSearchResult(event, data, formatted) {
  18. console.log("chose ingredient result: ", data, "; ", formatted);
  19. $(getRecipeIngredientIdField(this)).val(data[1]);
  20. $(this).data('startingValue', $(this).val());
  21. }
  22. function addIngredientRow() {
  23. var newRow = $('#ingredient_table > tbody > tr:first').clone().insertBefore('#ingredient_add_row');
  24. // remove 'id' attribute
  25. newRow.removeAttr('id');
  26. newRow.find('input:text').val('');
  27. newRow.find('input:hidden').val('0');
  28. // set new unit and ingredient to selected index 0
  29. newRow.find('input:select').each(function() {
  30. this.selectedIndex = 0;
  31. });
  32. // unset any 'optional' flag
  33. newRow.find('input:checkbox').each(function() {
  34. this.checked = false;
  35. });
  36. // make the 'minus' button appear
  37. newRow.find('.minus').css('display', 'block');
  38. updateIngredientRowIndexValues(newRow.get(0));
  39. applyIngredientRowBehaviors(newRow.get(0));
  40. newRow.find('input:text').focus();
  41. return false;
  42. }
  43. function updateIngredientRowIndexValues(row) {
  44. var myIndex = $(row).prevAll('tr.recipe-ingredient').size();
  45. // find all 'name' attributes, and increment index values
  46. replaceAttribute(row,'name','ingredient\\[\\d+\\]',
  47. 'ingredient[' +myIndex +']');
  48. // find all 'id' attributes, and increment index values
  49. replaceAttribute(row,'id','ingredient\\[\\d+\\]',
  50. 'ingredient[' +myIndex +']');
  51. }
  52. function applyAutocomplete(el) {
  53. $(el).find(".ingredient-autocomplete")
  54. .focus(handleIngredientFocus)
  55. .blur(handleIngredientBlur)
  56. .autocomplete("/ieat/ingredientSearch.json", {
  57. extraParams: {
  58. approximateSearch: 'true',
  59. recipeSearch: 'false'
  60. },
  61. dataType: 'json',
  62. autoFill: false,
  63. parse: function (data) {
  64. var searchResults = data[ 'magoffin.matt.xweb.MODEL'];
  65. var results =[];
  66. for (var i = 0; i < searchResults.ingredient.length; i++) {
  67. var ing = searchResults.ingredient[i];
  68. results[results.length] = {
  69. data:[ing.name, ing.ingredientId],
  70. value: ing.name,
  71. result: ing.name
  72. };
  73. }
  74. return results;
  75. }
  76. }).result(handleIngredientSearchResult);
  77. }
  78. function removeIngredientRow() {
  79. var ingredientIndex = $(this).parents('tr.recipe-ingredient').prevAll('tr.recipe-ingredient').size();
  80. console.log("remove ingredient row %d: ", ingredientIndex);
  81. if ( ingredientIndex < 1 ) {
  82. alert(XwebLocale.i18n('noDeleteFirstIngredient'));
  83. return;
  84. }
  85. // remove selected ingredient from table
  86. $(this).parents('tr.recipe-ingredient').remove();
  87. // update form field index values for ingredients *after* the removed one
  88. console.log("update index values >%d: ", ingredientIndex-1);
  89. $('#ingredient_table tr.recipe-ingredient:gt(' +(ingredientIndex-1) +')').each(function() {
  90. updateIngredientRowIndexValues(this);
  91. });
  92. return false;
  93. }
  94. function applyRecipeIngredientRemove(el) {
  95. $(el).find('a.minus').click(removeIngredientRow);
  96. }
  97. function applyIngredientRowBehaviors(el) {
  98. applyAutocomplete(el);
  99. applyRecipeIngredientRemove(el);
  100. }
  101. $(document).ready(function () {
  102. $('#ingredient_add_link').click(addIngredientRow);
  103. applyIngredientRowBehaviors(this);
  104. installTextAreaFocusHandler();
  105. $(document.forms[0].elements['criteria.ingredient[0].ingredient.name']).focus().select();
  106. });