edit-meal.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var chooseRecipePopup = null;
  2. function searchForRecipe(webContext,rowIndex) {
  3. var url = webContext +'/chooseRecipeForMeal.do?mode='+rowIndex;
  4. var width = 500;
  5. var height = 300;
  6. var top = (screen.height - height ) / 2;
  7. var left = (screen.width - width ) / 2;
  8. var windowOpts = "titlebar,toolbar=no,resizable,scrollbars,left=" +left
  9. +",top=" +top +",width=" +width +",height=" +height;
  10. if ( chooseRecipePopup != null && !chooseRecipePopup.closed ) {
  11. chooseRecipePopup.close();
  12. }
  13. chooseRecipePopup = window.open(url, "choose_recipe", windowOpts);
  14. chooseRecipePopup.focus();
  15. }
  16. function addRecipe() {
  17. // get the recipe table
  18. var row = document.getElementById('recipe_row');
  19. // get the add row to insert the cloned row into
  20. var addRow = document.getElementById('recipe_add_row');
  21. // clone the recipe row
  22. var newRow = row.cloneNode(true);
  23. // remove 'id' attribute
  24. newRow.removeAttribute('id');
  25. // clear out quantity
  26. var quantity = findNodeWithAttributeValue(newRow,'name','\\.quantity$');
  27. if ( !quantity ) {
  28. alert("Did not find quantity!");
  29. } else {
  30. quantity.value = "1";
  31. }
  32. // set new course to selected index 0
  33. var course = findNodeWithAttributeValue(newRow,'name','\\.courseId$');
  34. if ( !course ) {
  35. alert("Did not find course!");
  36. } else {
  37. course.selectedIndex = 0;
  38. }
  39. // remove any selected recipe name
  40. var recipe = findNodeWithAttributeValue(newRow,'name','\\.recipeId$');
  41. if ( !recipe ) {
  42. alert("Did not find recipe!");
  43. } else {
  44. recipe.value = 0;
  45. }
  46. var recipeSpan = findNodeWithAttributeValue(newRow,'id','\\.name$');
  47. if ( !recipeSpan ) {
  48. alert("Did not find recipe name span!");
  49. } else {
  50. removeChildren(recipeSpan);
  51. recipeSpan.appendChild(document.createTextNode('('+msg_setRecipe+')'));
  52. }
  53. // find all 'name' attributes, and increment index values
  54. updateRecipeRowIndexValues(newRow,numRecipes);
  55. // insert into table
  56. addRow.parentNode.insertBefore(newRow,addRow);
  57. numRecipes++;
  58. }
  59. function updateRecipeRowIndexValues(row,index) {
  60. replaceAttribute(row,'name','recipe\\[\\d+\\]',
  61. 'recipe[' +index +']');
  62. replaceAttribute(row,'id','recipe\\[\\d+\\]',
  63. 'recipe[' +index +']');
  64. replaceAttribute(row,'href','\\d+',
  65. index);
  66. }
  67. function chooseRecipeForMeal(doc,rowNum,recipeId,recipeName) {
  68. var recipeNameSpan = doc.getElementById('meal.recipe['+rowNum+'].recipe.name');
  69. if ( !recipeNameSpan ) {
  70. alert("Could not get recipe name span!");
  71. return;
  72. }
  73. var recipeIdHidden = doc.forms[0].elements['meal.recipe['+rowNum+'].recipe.recipeId'];
  74. if ( !recipeIdHidden ) {
  75. alert("Could not get recipe id hidden element!");
  76. return;
  77. }
  78. // delete any current children, then insert name
  79. removeChildren(recipeNameSpan);
  80. // insert recipe name into span if name available
  81. if ( recipeName ) {
  82. recipeNameSpan.appendChild(doc.createTextNode(recipeName));
  83. }
  84. // set hidden select value to recipeId
  85. recipeIdHidden.value = recipeId;
  86. }
  87. function removeRecipe(recipeIndex) {
  88. // loop through all rows in recipe table, removing the
  89. // row in question and then re-naming any subsequent rows
  90. // so index values decrease
  91. if ( recipeIndex < 1 ) {
  92. alert(getMessage('msg_noDeleteFirstRecipe'));
  93. return;
  94. }
  95. // forward to proper row
  96. var rowIdx = 0;
  97. var recipeRow = document.getElementById('recipe_row');
  98. if ( !recipeRow ) {
  99. alert("Could not get recipe_row!");
  100. return;
  101. }
  102. while ( recipeRow != null && rowIdx < recipeIndex ) {
  103. recipeRow = recipeRow.nextSibling;
  104. if ( recipeRow.nodeName.toLowerCase() == 'tr' ) {
  105. rowIdx++;
  106. }
  107. }
  108. var currRow = recipeRow.nextSibling;
  109. for ( var currIndex = recipeIndex; currIndex < (numRecipes - 1) && currRow != null
  110. && currRow.nextSibling != null; currRow = currRow.nextSibling )
  111. {
  112. if ( currRow.nodeName.toLowerCase() != 'tr' ) continue;
  113. updateRecipeRowIndexValues(currRow,currIndex);
  114. currIndex++;
  115. }
  116. // now remove recipeRow from table
  117. if ( recipeRow.parentNode.removeChild(recipeRow) ) {
  118. numRecipes--;
  119. } else {
  120. alert("Unable to remove recipe node!");
  121. }
  122. }