viewjs: Code deduplication, Grocy. -> GrocyConfig.

This commit is contained in:
Katharina Bogad 2021-06-20 13:06:15 +02:00
parent 87ee45f0b1
commit 64b8195f62
12 changed files with 185 additions and 403 deletions

View File

@ -230,6 +230,32 @@ class GrocyFrontendHelpers
});
}
MakeYesNoBox(message, selector, callback)
{
var self = this;
$(document).on('click', selector, function(e)
{
message = message instanceof Function ? message(e) : message;
bootbox.confirm({
message: message,
closeButton: false,
buttons: {
confirm: {
label: self.Grocy.translate('Yes'),
className: 'btn-success'
},
cancel: {
label: self.Grocy.translate('No'),
className: 'btn-danger'
}
},
callback: (result) => callback(result, e)
});
});
}
MakeDeleteConfirmBox(message, selector, attrName, attrId, apiEndpoint, redirectUrl)
{
if (!apiEndpoint.endsWith('/'))
@ -245,17 +271,9 @@ class GrocyFrontendHelpers
$(document).on('click', selector, function(e)
{
var target = $(e.currentTarget);
var objectName = target.attr(attrName);
var objectId = target.attr(attrId);
if (message instanceof Function)
{
message = message(objectId, objectName);
}
else
{
message = self.Grocy.translate(message, objectName)
}
var objectName = attrName instanceof Function ? attrName(target) : target.attr(attrName);
var objectId = attrId instanceof Function ? attrId(target) : target.attr(attrId);
message = message instanceof Function ? message(objectId, objectName) : self.Grocy.translate(message, objectName);
bootbox.confirm({
message: message,

View File

@ -7,62 +7,21 @@
].concat($.fn.dataTable.defaults.columnDefs)
});
$('#batteries-table tbody').removeClass("d-none");
batteriesTable.columns.adjust().draw();
$("#search").on("keyup", Grocy.FrontendHelpers.Delay(function()
{
var value = $(this).val();
if (value === "all")
{
value = "";
}
batteriesTable.search(value).draw();
}, 200));
$("#clear-filter-button").on("click", function()
Grocy.FrontendHelpers.InitDataTable(batteriesTable, null, function()
{
$("#search").val("");
batteriesTable.search("").draw();
$("#show-disabled").prop('checked', false);
});
$(document).on('click', '.battery-delete-button', function(e)
{
var objectName = $(e.currentTarget).attr('data-battery-name');
var objectId = $(e.currentTarget).attr('data-battery-id');
bootbox.confirm({
message: __t('Are you sure to delete battery "%s"?', objectName),
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
closeButton: false,
callback: function(result)
{
if (result === true)
{
Grocy.Api.Delete('objects/batteries/' + objectId, {},
function(result)
{
window.location.href = U('/batteries');
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});
Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'Are you sure to delete battery "%s"?',
'.battery-delete-button',
'data-battery-name',
'data-battery-id',
'objects/batteries/',
'/batteries'
);
$("#show-disabled").change(function()
{

View File

@ -7,38 +7,8 @@
].concat($.fn.dataTable.defaults.columnDefs)
});
$('#batteries-journal-table tbody').removeClass("d-none");
batteriesJournalTable.columns.adjust().draw();
$("#battery-filter").on("change", function()
{
var value = $(this).val();
var text = $("#battery-filter option:selected").text();
if (value === "all")
{
text = "";
}
batteriesJournalTable.column(1).search(text).draw();
});
$("#search").on("keyup", Grocy.FrontendHelpers.Delay(function()
{
var value = $(this).val();
if (value === "all")
{
value = "";
}
batteriesJournalTable.search(value).draw();
}, 200));
$("#clear-filter-button").on("click", function()
{
$("#search").val("");
$("#battery-filter").val("all");
batteriesJournalTable.column(1).search("").draw();
batteriesJournalTable.search("").draw();
});
Grocy.FrontendHelpers.InitDataTable(batteriesJournalTable);
Grocy.FrontendHelpers.MakeFilterForColumn("#battery-filter", 1, batteriesJournalTable);
if (typeof GetUriParam("battery") !== "undefined")
{

View File

@ -13,42 +13,14 @@ Grocy.FrontendHelpers.InitDataTable(choresTable, null, function()
$("#show-disabled").prop('checked', false);
});
$(document).on('click', '.chore-delete-button', function(e)
{
var objectName = $(e.currentTarget).attr('data-chore-name');
var objectId = $(e.currentTarget).attr('data-chore-id');
bootbox.confirm({
message: __t('Are you sure to delete chore "%s"?', objectName),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
{
if (result === true)
{
Grocy.Api.Delete('objects/chores/' + objectId, {},
function(result)
{
window.location.href = U('/chores');
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});
Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'Are you sure to delete chore "%s"?',
'.core-delete-button',
'data-chore-name',
'data-chore-id',
'objects/chores/',
'/chroes'
);
$("#show-disabled").change(function()
{

View File

@ -148,41 +148,14 @@ $("#name").trigger("keyup");
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('quantityunit-form');
$(document).on('click', '.qu-conversion-delete-button', function(e)
{
var objectId = $(e.currentTarget).attr('data-qu-conversion-id');
bootbox.confirm({
message: __t('Are you sure to remove this conversion?'),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
{
if (result === true)
{
Grocy.Api.Delete('objects/quantity_unit_conversions/' + objectId, {},
function(result)
{
window.location.reload();
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});
Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'Are you sure to remove this conversion?',
'.qu-conversion-delete-button',
'data-qu-conversion-id',
'data-qu-conversion-id',
'objects/quantity_unit_conversions/',
() => window.location.reload(),
);
$("#test-quantityunit-plural-forms-button").on("click", function(e)
{

View File

@ -126,79 +126,23 @@ $('#recipe-form input').keydown(function(event)
}
});
$(document).on('click', '.recipe-pos-delete-button', function(e)
{
var objectName = $(e.currentTarget).attr('data-recipe-pos-name');
var objectId = $(e.currentTarget).attr('data-recipe-pos-id');
Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'Are you sure to delete recipe ingredient "%s"?',
'.recipe-pos-delete-button',
'data-recipe-pos-name',
'data-recipe-pos-id',
'objects/recipes_pos/',
() => window.postMessage(WindowMessageBag("IngredientsChanged"), Grocy.BaseUrl)
);
bootbox.confirm({
message: __t('Are you sure to delete recipe ingredient "%s"?', objectName),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
{
if (result === true)
{
Grocy.Api.Delete('objects/recipes_pos/' + objectId, {},
function(result)
{
window.postMessage(WindowMessageBag("IngredientsChanged"), Grocy.BaseUrl);
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});
$(document).on('click', '.recipe-include-delete-button', function(e)
{
var objectName = $(e.currentTarget).attr('data-recipe-include-name');
var objectId = $(e.currentTarget).attr('data-recipe-include-id');
bootbox.confirm({
message: __t('Are you sure to remove the included recipe "%s"?', objectName),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
{
if (result === true)
{
Grocy.Api.Delete('objects/recipes_nestings/' + objectId, {},
function(result)
{
window.postMessage(WindowMessageBag("IngredientsChanged"), Grocy.BaseUrl);
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});
Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'Are you sure to remove the included recipe "%s"?',
'.recipe-include-delete-button',
'data-recipe-include-name',
'data-recipe-include-id',
'objects/recipes_nesting/',
() => window.postMessage(WindowMessageBag("IngredientsChanged"), Grocy.BaseUrl)
);
$(document).on('click', '.recipe-pos-show-note-button', function(e)
{

View File

@ -108,93 +108,77 @@ Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'/recipes'
);
$(document).on('click', '.recipe-shopping-list', function(e)
{
var objectName = $(e.currentTarget).attr('data-recipe-name');
var objectId = $(e.currentTarget).attr('data-recipe-id');
bootbox.confirm({
message: __t('Are you sure to put all missing ingredients for recipe "%s" on the shopping list?', objectName) + "<br><br>" + __t("Uncheck ingredients to not put them on the shopping list") + ":" + $("#missing-recipe-pos-list")[0].outerHTML.replace("d-none", ""),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
Grocy.FrontendHelpers.MakeYesNoBox(
(e) =>
{
var objectName = $(e.currentTarget).attr('data-recipe-name');
return __t('Are you sure to put all missing ingredients for recipe "%s" on the shopping list?', objectName) +
"<br><br>" +
__t("Uncheck ingredients to not put them on the shopping list") +
":" +
$("#missing-recipe-pos-list")[0].outerHTML.replace("d-none", "");
},
'.recipe-shopping-list',
(result, e) =>
{
var objectId = $(e.currentTarget).attr('data-recipe-id');
if (result === true)
{
if (result === true)
{
Grocy.FrontendHelpers.BeginUiBusy();
Grocy.FrontendHelpers.BeginUiBusy();
var excludedProductIds = new Array();
$(".missing-recipe-pos-product-checkbox:checkbox:not(:checked)").each(function()
var excludedProductIds = new Array();
$(".missing-recipe-pos-product-checkbox:checkbox:not(:checked)").each(function()
{
excludedProductIds.push($(this).data("product-id"));
});
Grocy.Api.Post('recipes/' + objectId + '/add-not-fulfilled-products-to-shoppinglist', { "excludedProductIds": excludedProductIds },
function(result)
{
excludedProductIds.push($(this).data("product-id"));
});
Grocy.Api.Post('recipes/' + objectId + '/add-not-fulfilled-products-to-shoppinglist', { "excludedProductIds": excludedProductIds },
function(result)
{
window.location.href = U('/recipes');
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
}
window.location.href = U('/recipes');
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
}
});
});
}
);
$(".recipe-consume").on('click', function(e)
{
var objectName = $(e.currentTarget).attr('data-recipe-name');
var objectId = $(e.currentTarget).attr('data-recipe-id');
bootbox.confirm({
message: __t('Are you sure to consume all ingredients needed by recipe "%s" (ingredients marked with "only check if any amount is in stock" will be ignored)?', objectName),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
Grocy.FrontendHelpers.MakeYesNoBox(
(e) =>
{
var objectName = $(e.currentTarget).attr('data-recipe-name');
return __t('Are you sure to consume all ingredients needed by recipe "%s" (ingredients marked with "only check if any amount is in stock" will be ignored)?', objectName);
},
'.recipe-consume',
(result, e) =>
{
var target = $(e.currentTarget);
var objectName = target.attr('data-recipe-name');
var objectId = target.attr('data-recipe-id');
if (result === true)
{
if (result === true)
{
Grocy.FrontendHelpers.BeginUiBusy();
Grocy.FrontendHelpers.BeginUiBusy();
Grocy.Api.Post('recipes/' + objectId + '/consume', {},
function(result)
{
Grocy.FrontendHelpers.EndUiBusy();
toastr.success(__t('Removed all ingredients of recipe "%s" from stock', objectName));
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
toastr.warning(__t('Not all ingredients of recipe "%s" are in stock, nothing removed', objectName));
console.error(xhr);
}
);
}
Grocy.Api.Post('recipes/' + objectId + '/consume', {},
function(result)
{
Grocy.FrontendHelpers.EndUiBusy();
toastr.success(__t('Removed all ingredients of recipe "%s" from stock', objectName));
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
toastr.warning(__t('Not all ingredients of recipe "%s" are in stock, nothing removed', objectName));
console.error(xhr);
}
);
}
});
});
}
);
recipesTables.on('select', function(e, dt, type, indexes)
{

View File

@ -48,42 +48,14 @@ $("#selected-shopping-list").on("change", function()
window.location.href = U('/shoppinglist?list=' + value);
});
$("#delete-selected-shopping-list").on("click", function()
{
var objectName = $("#selected-shopping-list option:selected").text();
var objectId = $("#selected-shopping-list").val();
bootbox.confirm({
message: __t('Are you sure to delete shopping list "%s"?', objectName),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
{
if (result === true)
{
Grocy.Api.Delete('objects/shopping_lists/' + objectId, {},
function(result)
{
window.location.href = U('/shoppinglist');
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});
Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'Are you sure to delete shopping list "%s"?',
'#delete-selected-shopping-list',
() => $("#selected-shopping-list option:selected").text(),
() => $("#selected-shopping-list").val(),
'objects/shopping_lists/',
'/shoppinglist'
);
$(document).on('click', '.shoppinglist-delete-button', function(e)
{
@ -160,47 +132,34 @@ $(document).on('click', '#add-overdue-expired-products', function(e)
);
});
$(document).on('click', '#clear-shopping-list', function(e)
{
bootbox.confirm({
message: __t('Are you sure to empty shopping list "%s"?', $("#selected-shopping-list option:selected").text()),
closeButton: false,
buttons: {
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
Grocy.FrontendHelpers.MakeYesNoBox(
() => __t('Are you sure to empty shopping list "%s"?', $("#selected-shopping-list option:selected").text()),
'#clear-shopping-list',
(result) =>
{
if (result === true)
{
if (result === true)
{
Grocy.FrontendHelpers.BeginUiBusy();
Grocy.FrontendHelpers.BeginUiBusy();
Grocy.Api.Post('stock/shoppinglist/clear', { "list_id": $("#selected-shopping-list").val() },
function(result)
{
animateCSS("#shoppinglist-table tbody tr", "fadeOut", function()
{
Grocy.FrontendHelpers.EndUiBusy();
$("#shoppinglist-table tbody tr").remove();
OnListItemRemoved();
});
},
function(xhr)
Grocy.Api.Post('stock/shoppinglist/clear', { "list_id": $("#selected-shopping-list").val() },
function(result)
{
animateCSS("#shoppinglist-table tbody tr", "fadeOut", function()
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
}
$("#shoppinglist-table tbody tr").remove();
OnListItemRemoved();
});
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
}
});
});
}
);
$(document).on('click', '.shopping-list-stock-add-workflow-list-item-button', function(e)
{

View File

@ -55,10 +55,12 @@ $(document).on('click', '.product-consume-button', function(e)
Grocy.FrontendHelpers.BeginUiBusy();
var productId = $(e.currentTarget).attr('data-product-id');
var consumeAmount = $(e.currentTarget).attr('data-consume-amount');
var originalTotalStockAmount = $(e.currentTarget).attr('data-original-total-stock-amount');
var wasSpoiled = $(e.currentTarget).hasClass("product-consume-button-spoiled");
var target = $(e.currentTarget);
var productId = target.attr('data-product-id');
var consumeAmount = target.attr('data-consume-amount');
var originalTotalStockAmount = target.attr('data-original-total-stock-amount');
var wasSpoiled = target.hasClass("product-consume-button-spoiled");
Grocy.Api.Post('stock/products/' + productId + '/consume', { 'amount': consumeAmount, 'spoiled': wasSpoiled, 'allow_subproduct_substitution': true },
function(bookingResponse)
@ -111,12 +113,13 @@ $(document).on('click', '.product-open-button', function(e)
Grocy.FrontendHelpers.BeginUiBusy();
var productId = $(e.currentTarget).attr('data-product-id');
var productName = $(e.currentTarget).attr('data-product-name');
var productQuName = $(e.currentTarget).attr('data-product-qu-name');
var amount = $(e.currentTarget).attr('data-open-amount');
var button = $(e.currentTarget);
var productId = button.attr('data-product-id');
var productName = button.attr('data-product-name');
var productQuName = button.attr('data-product-qu-name');
var amount = button.attr('data-open-amount');
Grocy.Api.Post('stock/products/' + productId + '/open', { 'amount': amount, 'allow_subproduct_substitution': true },
function(bookingResponse)
{

View File

@ -37,7 +37,7 @@
@endif>
@else
<script>
Grocy.UserSettings.scan_mode_consume_enabled = false;
GrocyConfig.UserSettings.scan_mode_consume_enabled = false;
</script>
@endif
</div>

View File

@ -37,7 +37,7 @@
@endif>
@else
<script>
Grocy.UserSettings.scan_mode_purchase_enabled = false;
GrocyConfig.UserSettings.scan_mode_purchase_enabled = false;
</script>
@endif
</div>

View File

@ -29,16 +29,16 @@
<div class="row">
<div class="col-xs-12 col-md-6 col-xl-5 pb-3">
<script>
Grocy.EditMode = '{{ $mode }}';
Grocy.EditObjectParentId = {{ $recipe->id }};
Grocy.EditObject = {!! json_encode($recipePos) !!};
Grocy.QuantityUnits = {!! json_encode($quantityUnits) !!};
Grocy.QuantityUnitConversionsResolved = {!! json_encode($quantityUnitConversionsResolved) !!};
GrocyConfig.EditMode = '{{ $mode }}';
GrocyConfig.EditObjectParentId = {{ $recipe->id }};
GrocyConfig.EditObject = {!! json_encode($recipePos) !!};
GrocyConfig.QuantityUnits = {!! json_encode($quantityUnits) !!};
GrocyConfig.QuantityUnitConversionsResolved = {!! json_encode($quantityUnitConversionsResolved) !!};
</script>
@if($mode == 'edit')
<script>
Grocy.EditObjectId = {{ $recipePos->id }};
GrocyConfig.EditObjectId = {{ $recipePos->id }};
</script>
@endif