Recipe updates (#795)

This commit is contained in:
Zack Arnett
2020-04-24 11:41:57 -04:00
committed by GitHub
parent 385e7287fe
commit f076b0d0c6
6 changed files with 440 additions and 350 deletions

View File

@@ -1,4 +1,30 @@
$('#save-recipe-button').on('click', function(e)
function saveRecipePicture(result, location)
{
$recipeId = Grocy.EditObjectId || result.created_object_id;
Grocy.Components.UserfieldsForm.Save(() =>
{
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteRecipePictureOnSave)
{
Grocy.Api.UploadFile($("#recipe-picture")[0].files[0], 'recipepictures', jsonData.picture_file_name,
(result) =>
{
window.location.href = U(location + $recipeId);
},
(xhr) =>
{
Grocy.FrontendHelpers.EndUiBusy("recipe-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = U(location + $recipeId);
}
});
}
$('.save-recipe').on('click', function(e)
{
e.preventDefault();
@@ -11,6 +37,15 @@
jsonData.picture_file_name = someRandomStuff + $("#recipe-picture")[0].files[0].name;
}
const location = $(e.currentTarget).attr('data-location') == 'return' ? '/recipes?recipe=' : '/recipe/';
if(Grocy.EditMode == 'create') {
console.log(jsonData);
Grocy.Api.Post('objects/recipes', jsonData,
(result) => saveRecipePicture(result, location));
return;
}
if (Grocy.DeleteRecipePictureOnSave)
{
jsonData.picture_file_name = null;
@@ -29,30 +64,7 @@
}
Grocy.Api.Put('objects/recipes/' + Grocy.EditObjectId, jsonData,
function(result)
{
Grocy.Components.UserfieldsForm.Save(function()
{
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteRecipePictureOnSave)
{
Grocy.Api.UploadFile($("#recipe-picture")[0].files[0], 'recipepictures', jsonData.picture_file_name,
function (result)
{
window.location.href = U('/recipes?recipe=' + Grocy.EditObjectId);
},
function (xhr)
{
Grocy.FrontendHelpers.EndUiBusy("recipe-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = U('/recipes?recipe=' + Grocy.EditObjectId);
}
});
},
(result) => saveRecipePicture(result, location),
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("recipe-form");
@@ -354,7 +366,7 @@ $(window).on("message", function(e)
Grocy.Api.Put('objects/recipes/' + Grocy.EditObjectId, $('#recipe-form').serializeJSON(),
function(result)
{
window.location.href = U('/recipe/' + Grocy.EditObjectId);
// window.location.href = U('/recipe/' + Grocy.EditObjectId);
},
function(xhr)
{
@@ -364,35 +376,35 @@ $(window).on("message", function(e)
}
});
Grocy.Components.RecipePicker.GetPicker().on('change', function (e)
{
var value = Grocy.Components.RecipePicker.GetValue();
if (value.toString().isEmpty())
{
return;
}
// Grocy.Components.RecipePicker.GetPicker().on('change', function (e)
// {
// var value = Grocy.Components.RecipePicker.GetValue();
// if (value.toString().isEmpty())
// {
// return;
// }
Grocy.Api.Get('objects/recipes/' + value,
function(recipe)
{
$("#includes_servings").val(recipe.servings);
},
function(xhr)
{
console.error(xhr);
}
);
});
// Grocy.Api.Get('objects/recipes/' + value,
// function(recipe)
// {
// $("#includes_servings").val(recipe.servings);
// },
// function(xhr)
// {
// console.error(xhr);
// }
// );
// });
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
// Just save the current recipe on every change of the product picker as a workflow could be started which leaves the page...
Grocy.Api.Put('objects/recipes/' + Grocy.EditObjectId, $('#recipe-form').serializeJSON(), function () { }, function () { });
});
// Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
// {
// // Just save the current recipe on every change of the product picker as a workflow could be started which leaves the page...
// Grocy.Api.Put('objects/recipes/' + Grocy.EditObjectId, $('#recipe-form').serializeJSON(), function () { }, function () { });
// });
// As the /recipe/new route immediately creates a new recipe on load,
// always replace the current location by the created recipes edit page location
if (window.location.pathname.toLowerCase() === "/recipe/new")
{
window.history.replaceState(null, null, U("/recipe/" + Grocy.EditObjectId));
}
// if (window.location.pathname.toLowerCase() === "/recipe/new")
// {
// window.history.replaceState(null, null, U("/recipe/" + Grocy.EditObjectId));
// }

View File

@@ -1,6 +1,8 @@
var recipesTables = $('#recipes-table').DataTable({
'order': [[0, 'asc']],
'order': [[1, 'asc']],
'columnDefs': [
{ 'orderable': false, 'targets': 0 },
{ 'searchable': false, "targets": 0 },
{ 'orderData': 2, 'targets': 1 }
],
'select': 'single',
@@ -24,18 +26,14 @@ if (typeof recipe !== "undefined")
var rowId = "#recipe-row-" + recipe;
$(rowId).addClass("selected")
var cardId = "#recipe-card-" + recipe;
$(cardId).addClass("bg-primary").addClass("text-white");
var cardId = "#RecipeGalleryCard-" + recipe;
$(cardId).addClass("border-primary");
if ($(window).width() < 768)
{
// Scroll to recipe card on mobile
$("#selectedRecipeCard")[0].scrollIntoView();
}
else
{
$(cardId)[0].scrollIntoView();
}
}
if (GetUriParam("search") !== undefined)
@@ -59,8 +57,10 @@ $("#search").on("keyup", Delay(function()
recipesTables.search(value).draw();
$(".recipe-gallery-item-container").removeClass("d-none");
$(".recipe-gallery-item-container .card-title:not(:contains_case_insensitive(" + value + "))").parent().parent().parent().parent().addClass("d-none");
$(".recipe-gallery-item").removeClass("d-none");
console.log( $(".recipe-gallery-item .card-title:not(:contains_case_insensitive(" + value + "))"));
$(".recipe-gallery-item .card-title:not(:contains_case_insensitive(" + value + "))").parent().parent().parent().addClass("d-none");
}, 200));
$("#status-filter").on("change", function()
@@ -71,15 +71,13 @@ $("#status-filter").on("change", function()
value = "";
}
// Transfer CSS classes of selected element to dropdown element (for background)
$(this).attr("class", $("#" + $(this).attr("id") + " option[value='" + value + "']").attr("class") + " form-control");
recipesTables.column(4).search(value).draw();
recipesTables.column(5).search(value).draw();
});
$("#selectedRecipeDeleteButton").on('click', function(e)
$(".recipe-delete").on('click', function(e)
{
e.preventDefault();
var objectName = $(e.currentTarget).attr('data-recipe-name');
var objectId = $(e.currentTarget).attr('data-recipe-id');
@@ -115,7 +113,7 @@ $("#selectedRecipeDeleteButton").on('click', function(e)
});
});
$(document).on('click', '.recipe-order-missing-button', function(e)
$(document).on('click', '.recipe-shopping-list', function(e)
{
var objectName = $(e.currentTarget).attr('data-recipe-name');
var objectId = $(e.currentTarget).attr('data-recipe-id');
@@ -161,7 +159,7 @@ $(document).on('click', '.recipe-order-missing-button', function(e)
});
});
$("#selectedRecipeConsumeButton").on('click', function(e)
$(".recipe-consume").on('click', function(e)
{
var objectName = $(e.currentTarget).attr('data-recipe-name');
var objectId = $(e.currentTarget).attr('data-recipe-id');
@@ -223,7 +221,7 @@ $(".recipe-gallery-item").on("click", function(e)
window.location.href = U('/recipes?tab=gallery&recipe=' + $(this).data("recipe-id"));
});
$("#selectedRecipeToggleFullscreenButton").on('click', function(e)
$(".recipe-fullscreen").on('click', function(e)
{
e.preventDefault();
@@ -242,6 +240,19 @@ $("#selectedRecipeToggleFullscreenButton").on('click', function(e)
}
});
$(".recipe-print").on('click', function(e)
{
e.preventDefault();
$("#selectedRecipeCard").removeClass("fullscreen");
$("body").removeClass("fullscreen-card");
$("#selectedRecipeCard .card-header").removeClass("fixed-top");
$("#selectedRecipeCard .card-body").removeClass("mt-5");
window.history.replaceState(null, null, " ");
window.print();
});
$('#servings-scale').keyup(function(event)
{
var data = { };