2017-04-21 15:36:04 +02:00
|
|
|
|
$('#save-shoppinglist-button').on('click', function(e)
|
|
|
|
|
{
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2018-11-24 19:40:50 +01:00
|
|
|
|
var jsonData = $('#shoppinglist-form').serializeJSON();
|
|
|
|
|
Grocy.FrontendHelpers.BeginUiBusy("shoppinglist-form");
|
|
|
|
|
|
2017-04-21 15:36:04 +02:00
|
|
|
|
if (Grocy.EditMode === 'create')
|
|
|
|
|
{
|
2019-01-19 14:51:51 +01:00
|
|
|
|
Grocy.Api.Post('objects/shopping_list', jsonData,
|
2017-04-21 15:36:04 +02:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2018-04-18 19:03:39 +02:00
|
|
|
|
window.location.href = U('/shoppinglist');
|
2017-04-21 15:36:04 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("shoppinglist-form");
|
2017-04-21 15:36:04 +02:00
|
|
|
|
console.error(xhr);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-19 14:51:51 +01:00
|
|
|
|
Grocy.Api.Put('objects/shopping_list/' + Grocy.EditObjectId, jsonData,
|
2017-04-21 15:36:04 +02:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2018-04-18 19:03:39 +02:00
|
|
|
|
window.location.href = U('/shoppinglist');
|
2017-04-21 15:36:04 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("shoppinglist-form");
|
2017-04-21 15:36:04 +02:00
|
|
|
|
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);
|
2019-01-19 00:37:21 -07:00
|
|
|
|
|
2019-01-19 14:51:51 +01:00
|
|
|
|
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);
|
2018-07-12 19:12:31 +02:00
|
|
|
|
$('#amount').focus();
|
2018-07-15 08:29:26 +02:00
|
|
|
|
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');
|
2018-11-26 19:37:17 +01:00
|
|
|
|
Grocy.Components.ProductPicker.GetInputElement().focus();
|
2017-04-21 19:02:00 +02:00
|
|
|
|
|
2018-11-26 19:37:17 +01:00
|
|
|
|
if (Grocy.EditMode === "edit")
|
2018-04-16 19:11:32 +02:00
|
|
|
|
{
|
2019-01-19 00:37:21 -07: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)
|
2017-04-21 15:36:04 +02:00
|
|
|
|
{
|
2018-07-14 14:43:57 +02:00
|
|
|
|
Grocy.Components.ProductPicker.GetInputElement().focus();
|
2018-04-16 19:11:32 +02:00
|
|
|
|
}
|
2018-07-12 19:12:31 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$(this).select();
|
|
|
|
|
}
|
2018-04-16 19:11:32 +02:00
|
|
|
|
});
|
2017-04-21 15:36:04 +02:00
|
|
|
|
|
2018-07-11 19:43:05 +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
|
2017-04-21 15:36:04 +02:00
|
|
|
|
{
|
2018-09-29 13:41:56 +02:00
|
|
|
|
event.preventDefault();
|
2019-01-19 00:37:21 -07:00
|
|
|
|
|
2018-07-11 19:43:05 +02:00
|
|
|
|
if (document.getElementById('shoppinglist-form').checkValidity() === false) //There is at least one validation error
|
2017-04-21 15:36:04 +02:00
|
|
|
|
{
|
2018-04-16 19:11:32 +02:00
|
|
|
|
return false;
|
2017-04-21 15:36:04 +02:00
|
|
|
|
}
|
2018-07-11 19:43:05 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$('#save-shoppinglist-button').click();
|
|
|
|
|
}
|
2018-04-16 19:11:32 +02:00
|
|
|
|
}
|
2017-04-21 15:36:04 +02:00
|
|
|
|
});
|