Files
grocy/public/viewjs/shoppinglistform.js

102 lines
2.1 KiB
JavaScript
Raw Normal View History

$('#save-shoppinglist-button').on('click', function(e)
{
e.preventDefault();
var jsonData = $('#shoppinglist-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("shoppinglist-form");
if (Grocy.EditMode === 'create')
{
Grocy.Api.Post('objects/shopping_list', jsonData,
function(result)
{
window.location.href = U('/shoppinglist');
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("shoppinglist-form");
console.error(xhr);
}
);
}
else
{
Grocy.Api.Put('objects/shopping_list/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/shoppinglist');
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("shoppinglist-form");
console.error(xhr);
}
);
}
});
2018-07-14 14:43:57 +02:00
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
2017-04-21 19:02:00 +02:00
{
var productId = $(e.target).val();
if (productId)
{
2018-04-16 19:11:32 +02:00
Grocy.Components.ProductCard.Refresh(productId);
Grocy.Api.Get('stock/products/' + productId,
2017-04-21 19:02:00 +02:00
function (productDetails)
{
$('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name);
$('#amount').focus();
Grocy.FrontendHelpers.ValidateForm('shoppinglist-form');
2017-04-21 19:02:00 +02:00
},
function(xhr)
{
console.error(xhr);
}
);
}
});
2018-07-14 14:43:57 +02:00
Grocy.FrontendHelpers.ValidateForm('shoppinglist-form');
Grocy.Components.ProductPicker.GetInputElement().focus();
2017-04-21 19:02:00 +02:00
if (Grocy.EditMode === "edit")
2018-04-16 19:11:32 +02:00
{
Grocy.Components.ProductPicker.GetPicker().trigger('change');
2018-04-16 19:11:32 +02:00
}
$('#amount').on('focus', function(e)
{
2018-07-14 14:43:57 +02:00
if (Grocy.Components.ProductPicker.GetValue().length === 0)
{
2018-07-14 14:43:57 +02:00
Grocy.Components.ProductPicker.GetInputElement().focus();
2018-04-16 19:11:32 +02:00
}
else
{
$(this).select();
}
2018-04-16 19:11:32 +02:00
});
$('#shoppinglist-form input').keyup(function (event)
{
Grocy.FrontendHelpers.ValidateForm('shoppinglist-form');
});
$('#shoppinglist-form input').keydown(function (event)
2018-04-16 19:11:32 +02:00
{
if (event.keyCode === 13) //Enter
{
event.preventDefault();
if (document.getElementById('shoppinglist-form').checkValidity() === false) //There is at least one validation error
{
2018-04-16 19:11:32 +02:00
return false;
}
else
{
$('#save-shoppinglist-button').click();
}
2018-04-16 19:11:32 +02:00
}
});