2017-11-05 12:41:00 +01:00
|
|
|
|
$('#save-battery-button').on('click', function(e)
|
|
|
|
|
{
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2018-11-24 19:40:50 +01:00
|
|
|
|
var jsonData = $('#battery-form').serializeJSON();
|
|
|
|
|
Grocy.FrontendHelpers.BeginUiBusy("battery-form");
|
|
|
|
|
|
2017-11-05 12:41:00 +01:00
|
|
|
|
if (Grocy.EditMode === 'create')
|
|
|
|
|
{
|
2019-01-19 14:51:51 +01:00
|
|
|
|
Grocy.Api.Post('objects/batteries', jsonData,
|
2017-11-05 12:41:00 +01:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2019-04-22 22:16:35 +02:00
|
|
|
|
Grocy.EditObjectId = result.created_object_id;
|
|
|
|
|
Grocy.Components.UserfieldsForm.Save(function()
|
|
|
|
|
{
|
|
|
|
|
window.location.href = U('/batteries');
|
|
|
|
|
});
|
2017-11-05 12:41:00 +01:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("battery-form");
|
2018-09-08 09:26:12 +02:00
|
|
|
|
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
2017-11-05 12:41:00 +01:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-19 14:51:51 +01:00
|
|
|
|
Grocy.Api.Put('objects/batteries/' + Grocy.EditObjectId, jsonData,
|
2017-11-05 12:41:00 +01:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2019-04-22 22:16:35 +02:00
|
|
|
|
Grocy.Components.UserfieldsForm.Save(function()
|
|
|
|
|
{
|
|
|
|
|
window.location.href = U('/batteries');
|
|
|
|
|
});
|
2017-11-05 12:41:00 +01:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
2018-11-24 19:40:50 +01:00
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("battery-form");
|
2018-09-08 09:26:12 +02:00
|
|
|
|
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
2017-11-05 12:41:00 +01:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-07-11 19:43:05 +02:00
|
|
|
|
$('#battery-form input').keyup(function(event)
|
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('battery-form');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#battery-form input').keydown(function(event)
|
|
|
|
|
{
|
|
|
|
|
if (event.keyCode === 13) //Enter
|
|
|
|
|
{
|
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('battery-form').checkValidity() === false) //There is at least one validation error
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$('#save-battery-button').click();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-22 22:16:35 +02:00
|
|
|
|
Grocy.Components.UserfieldsForm.Load();
|
2018-04-16 19:11:32 +02:00
|
|
|
|
$('#name').focus();
|
2018-07-11 19:43:05 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('battery-form');
|