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) MakeDeleteConfirmBox(message, selector, attrName, attrId, apiEndpoint, redirectUrl)
{ {
if (!apiEndpoint.endsWith('/')) if (!apiEndpoint.endsWith('/'))
@ -245,17 +271,9 @@ class GrocyFrontendHelpers
$(document).on('click', selector, function(e) $(document).on('click', selector, function(e)
{ {
var target = $(e.currentTarget); var target = $(e.currentTarget);
var objectName = target.attr(attrName); var objectName = attrName instanceof Function ? attrName(target) : target.attr(attrName);
var objectId = target.attr(attrId); var objectId = attrId instanceof Function ? attrId(target) : target.attr(attrId);
message = message instanceof Function ? message(objectId, objectName) : self.Grocy.translate(message, objectName);
if (message instanceof Function)
{
message = message(objectId, objectName);
}
else
{
message = self.Grocy.translate(message, objectName)
}
bootbox.confirm({ bootbox.confirm({
message: message, message: message,

View File

@ -7,62 +7,21 @@
].concat($.fn.dataTable.defaults.columnDefs) ].concat($.fn.dataTable.defaults.columnDefs)
}); });
$('#batteries-table tbody').removeClass("d-none"); $('#batteries-table tbody').removeClass("d-none");
batteriesTable.columns.adjust().draw(); Grocy.FrontendHelpers.InitDataTable(batteriesTable, null, function()
$("#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()
{ {
$("#search").val(""); $("#search").val("");
batteriesTable.search("").draw(); batteriesTable.search("").draw();
$("#show-disabled").prop('checked', false); $("#show-disabled").prop('checked', false);
}); });
$(document).on('click', '.battery-delete-button', function(e) Grocy.FrontendHelpers.MakeDeleteConfirmBox(
{ 'Are you sure to delete battery "%s"?',
var objectName = $(e.currentTarget).attr('data-battery-name'); '.battery-delete-button',
var objectId = $(e.currentTarget).attr('data-battery-id'); 'data-battery-name',
'data-battery-id',
bootbox.confirm({ 'objects/batteries/',
message: __t('Are you sure to delete battery "%s"?', objectName), '/batteries'
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);
}
);
}
}
});
});
$("#show-disabled").change(function() $("#show-disabled").change(function()
{ {

View File

@ -7,38 +7,8 @@
].concat($.fn.dataTable.defaults.columnDefs) ].concat($.fn.dataTable.defaults.columnDefs)
}); });
$('#batteries-journal-table tbody').removeClass("d-none"); $('#batteries-journal-table tbody').removeClass("d-none");
batteriesJournalTable.columns.adjust().draw(); Grocy.FrontendHelpers.InitDataTable(batteriesJournalTable);
Grocy.FrontendHelpers.MakeFilterForColumn("#battery-filter", 1, batteriesJournalTable);
$("#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();
});
if (typeof GetUriParam("battery") !== "undefined") if (typeof GetUriParam("battery") !== "undefined")
{ {

View File

@ -13,42 +13,14 @@ Grocy.FrontendHelpers.InitDataTable(choresTable, null, function()
$("#show-disabled").prop('checked', false); $("#show-disabled").prop('checked', false);
}); });
$(document).on('click', '.chore-delete-button', function(e) Grocy.FrontendHelpers.MakeDeleteConfirmBox(
{ 'Are you sure to delete chore "%s"?',
var objectName = $(e.currentTarget).attr('data-chore-name'); '.core-delete-button',
var objectId = $(e.currentTarget).attr('data-chore-id'); 'data-chore-name',
'data-chore-id',
bootbox.confirm({ 'objects/chores/',
message: __t('Are you sure to delete chore "%s"?', objectName), '/chroes'
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);
}
);
}
}
});
});
$("#show-disabled").change(function() $("#show-disabled").change(function()
{ {

View File

@ -148,41 +148,14 @@ $("#name").trigger("keyup");
$('#name').focus(); $('#name').focus();
Grocy.FrontendHelpers.ValidateForm('quantityunit-form'); Grocy.FrontendHelpers.ValidateForm('quantityunit-form');
$(document).on('click', '.qu-conversion-delete-button', function(e) Grocy.FrontendHelpers.MakeDeleteConfirmBox(
{ 'Are you sure to remove this conversion?',
var objectId = $(e.currentTarget).attr('data-qu-conversion-id'); '.qu-conversion-delete-button',
'data-qu-conversion-id',
bootbox.confirm({ 'data-qu-conversion-id',
message: __t('Are you sure to remove this conversion?'), 'objects/quantity_unit_conversions/',
closeButton: false, () => window.location.reload(),
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);
}
);
}
}
});
});
$("#test-quantityunit-plural-forms-button").on("click", function(e) $("#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) Grocy.FrontendHelpers.MakeDeleteConfirmBox(
{ 'Are you sure to delete recipe ingredient "%s"?',
var objectName = $(e.currentTarget).attr('data-recipe-pos-name'); '.recipe-pos-delete-button',
var objectId = $(e.currentTarget).attr('data-recipe-pos-id'); 'data-recipe-pos-name',
'data-recipe-pos-id',
'objects/recipes_pos/',
() => window.postMessage(WindowMessageBag("IngredientsChanged"), Grocy.BaseUrl)
);
bootbox.confirm({ Grocy.FrontendHelpers.MakeDeleteConfirmBox(
message: __t('Are you sure to delete recipe ingredient "%s"?', objectName), 'Are you sure to remove the included recipe "%s"?',
closeButton: false, '.recipe-include-delete-button',
buttons: { 'data-recipe-include-name',
confirm: { 'data-recipe-include-id',
label: __t('Yes'), 'objects/recipes_nesting/',
className: 'btn-success' () => window.postMessage(WindowMessageBag("IngredientsChanged"), Grocy.BaseUrl)
}, );
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);
}
);
}
}
});
});
$(document).on('click', '.recipe-pos-show-note-button', function(e) $(document).on('click', '.recipe-pos-show-note-button', function(e)
{ {

View File

@ -108,93 +108,77 @@ Grocy.FrontendHelpers.MakeDeleteConfirmBox(
'/recipes' '/recipes'
); );
$(document).on('click', '.recipe-shopping-list', function(e) Grocy.FrontendHelpers.MakeYesNoBox(
{ (e) =>
var objectName = $(e.currentTarget).attr('data-recipe-name'); {
var objectId = $(e.currentTarget).attr('data-recipe-id'); 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) +
bootbox.confirm({ "<br><br>" +
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", ""), __t("Uncheck ingredients to not put them on the shopping list") +
closeButton: false, ":" +
buttons: { $("#missing-recipe-pos-list")[0].outerHTML.replace("d-none", "");
confirm: { },
label: __t('Yes'), '.recipe-shopping-list',
className: 'btn-success' (result, e) =>
}, {
cancel: { var objectId = $(e.currentTarget).attr('data-recipe-id');
label: __t('No'), if (result === true)
className: 'btn-danger'
}
},
callback: function(result)
{ {
if (result === true) Grocy.FrontendHelpers.BeginUiBusy();
{
Grocy.FrontendHelpers.BeginUiBusy();
var excludedProductIds = new Array(); var excludedProductIds = new Array();
$(".missing-recipe-pos-product-checkbox:checkbox:not(:checked)").each(function() $(".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")); window.location.href = U('/recipes');
}); },
function(xhr)
Grocy.Api.Post('recipes/' + objectId + '/add-not-fulfilled-products-to-shoppinglist', { "excludedProductIds": excludedProductIds }, {
function(result) 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) Grocy.FrontendHelpers.MakeYesNoBox(
{ (e) =>
var objectName = $(e.currentTarget).attr('data-recipe-name'); {
var objectId = $(e.currentTarget).attr('data-recipe-id'); 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);
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), '.recipe-consume',
closeButton: false, (result, e) =>
buttons: { {
confirm: { var target = $(e.currentTarget);
label: __t('Yes'), var objectName = target.attr('data-recipe-name');
className: 'btn-success' var objectId = target.attr('data-recipe-id');
}, if (result === true)
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
{ {
if (result === true) Grocy.FrontendHelpers.BeginUiBusy();
{
Grocy.FrontendHelpers.BeginUiBusy();
Grocy.Api.Post('recipes/' + objectId + '/consume', {}, Grocy.Api.Post('recipes/' + objectId + '/consume', {},
function(result) function(result)
{ {
Grocy.FrontendHelpers.EndUiBusy(); Grocy.FrontendHelpers.EndUiBusy();
toastr.success(__t('Removed all ingredients of recipe "%s" from stock', objectName)); toastr.success(__t('Removed all ingredients of recipe "%s" from stock', objectName));
}, },
function(xhr) function(xhr)
{ {
Grocy.FrontendHelpers.EndUiBusy(); Grocy.FrontendHelpers.EndUiBusy();
toastr.warning(__t('Not all ingredients of recipe "%s" are in stock, nothing removed', objectName)); toastr.warning(__t('Not all ingredients of recipe "%s" are in stock, nothing removed', objectName));
console.error(xhr); console.error(xhr);
} }
); );
}
} }
}); }
}); );
recipesTables.on('select', function(e, dt, type, indexes) 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); window.location.href = U('/shoppinglist?list=' + value);
}); });
$("#delete-selected-shopping-list").on("click", function() Grocy.FrontendHelpers.MakeDeleteConfirmBox(
{ 'Are you sure to delete shopping list "%s"?',
var objectName = $("#selected-shopping-list option:selected").text(); '#delete-selected-shopping-list',
var objectId = $("#selected-shopping-list").val(); () => $("#selected-shopping-list option:selected").text(),
() => $("#selected-shopping-list").val(),
bootbox.confirm({ 'objects/shopping_lists/',
message: __t('Are you sure to delete shopping list "%s"?', objectName), '/shoppinglist'
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);
}
);
}
}
});
});
$(document).on('click', '.shoppinglist-delete-button', function(e) $(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) Grocy.FrontendHelpers.MakeYesNoBox(
{ () => __t('Are you sure to empty shopping list "%s"?', $("#selected-shopping-list option:selected").text()),
bootbox.confirm({ '#clear-shopping-list',
message: __t('Are you sure to empty shopping list "%s"?', $("#selected-shopping-list option:selected").text()), (result) =>
closeButton: false, {
buttons: { if (result === true)
confirm: {
label: __t('Yes'),
className: 'btn-success'
},
cancel: {
label: __t('No'),
className: 'btn-danger'
}
},
callback: function(result)
{ {
if (result === true) Grocy.FrontendHelpers.BeginUiBusy();
{
Grocy.FrontendHelpers.BeginUiBusy();
Grocy.Api.Post('stock/shoppinglist/clear', { "list_id": $("#selected-shopping-list").val() }, Grocy.Api.Post('stock/shoppinglist/clear', { "list_id": $("#selected-shopping-list").val() },
function(result) function(result)
{ {
animateCSS("#shoppinglist-table tbody tr", "fadeOut", function() animateCSS("#shoppinglist-table tbody tr", "fadeOut", function()
{
Grocy.FrontendHelpers.EndUiBusy();
$("#shoppinglist-table tbody tr").remove();
OnListItemRemoved();
});
},
function(xhr)
{ {
Grocy.FrontendHelpers.EndUiBusy(); 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) $(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(); Grocy.FrontendHelpers.BeginUiBusy();
var productId = $(e.currentTarget).attr('data-product-id'); var target = $(e.currentTarget);
var consumeAmount = $(e.currentTarget).attr('data-consume-amount');
var originalTotalStockAmount = $(e.currentTarget).attr('data-original-total-stock-amount'); var productId = target.attr('data-product-id');
var wasSpoiled = $(e.currentTarget).hasClass("product-consume-button-spoiled"); 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 }, Grocy.Api.Post('stock/products/' + productId + '/consume', { 'amount': consumeAmount, 'spoiled': wasSpoiled, 'allow_subproduct_substitution': true },
function(bookingResponse) function(bookingResponse)
@ -111,12 +113,13 @@ $(document).on('click', '.product-open-button', function(e)
Grocy.FrontendHelpers.BeginUiBusy(); 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 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 }, Grocy.Api.Post('stock/products/' + productId + '/open', { 'amount': amount, 'allow_subproduct_substitution': true },
function(bookingResponse) function(bookingResponse)
{ {

View File

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

View File

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

View File

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