Started working on user-defined-fields for all entities (references #176)

This commit is contained in:
Bernd Bestel
2019-04-22 22:16:35 +02:00
parent 77f3b80540
commit fc11da3c3f
45 changed files with 1161 additions and 78 deletions

View File

@@ -10,7 +10,11 @@
Grocy.Api.Post('objects/batteries', jsonData,
function(result)
{
window.location.href = U('/batteries');
Grocy.EditObjectId = result.created_object_id;
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/batteries');
});
},
function(xhr)
{
@@ -24,7 +28,10 @@
Grocy.Api.Put('objects/batteries/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/batteries');
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/batteries');
});
},
function(xhr)
{
@@ -57,5 +64,6 @@ $('#battery-form input').keydown(function(event)
}
});
Grocy.Components.UserfieldsForm.Load();
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('battery-form');

View File

@@ -10,7 +10,11 @@
Grocy.Api.Post('objects/chores', jsonData,
function(result)
{
window.location.href = U('/chores');
Grocy.EditObjectId = result.created_object_id;
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/chores');
});
},
function(xhr)
{
@@ -24,7 +28,10 @@
Grocy.Api.Put('objects/chores/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/chores');
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/chores');
});
},
function(xhr)
{
@@ -66,6 +73,7 @@ for (var i = 0; i < checkboxValues.length; i++)
}
}
Grocy.Components.UserfieldsForm.Load();
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('chore-form');

View File

@@ -0,0 +1,69 @@
Grocy.Components.UserfieldsForm = { };
Grocy.Components.UserfieldsForm.Save = function(success, error)
{
var jsonData = { };
$("#userfields-form .userfield-input").each(function()
{
var input = $(this);
var fieldName = input.attr("id");
var fieldValue = input.val();
if (input.attr("type") == "checkbox")
{
jsonData[fieldName] = "0";
if (input.is(":checked"))
{
jsonData[fieldName] = "1";
}
}
else
{
jsonData[fieldName] = fieldValue;
}
});
Grocy.Api.Put('userfields/' + $("#userfields-form").data("entity") + '/' + Grocy.EditObjectId, jsonData,
function(result)
{
if (success)
{
success();
}
},
function(xhr)
{
if (error)
{
error();
}
}
);
}
Grocy.Components.UserfieldsForm.Load = function()
{
Grocy.Api.Get('userfields/' + $("#userfields-form").data("entity") + '/' + Grocy.EditObjectId,
function(result)
{
$.each(result, function(key, value)
{
var input = $("#" + key + ".userfield-input");
if (input.attr("type") == "checkbox" && value == 1)
{
input.prop("checked", true);
}
else
{
input.val(value);
}
});
},
function(xhr)
{
console.log(xhr);
}
);
}

View File

@@ -10,7 +10,11 @@
Grocy.Api.Post('objects/locations', jsonData,
function(result)
{
window.location.href = U('/locations');
Grocy.EditObjectId = result.created_object_id;
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/locations');
});
},
function(xhr)
{
@@ -24,7 +28,10 @@
Grocy.Api.Put('objects/locations/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/locations');
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/locations');
});
},
function(xhr)
{
@@ -57,5 +64,6 @@ $('#location-form input').keydown(function (event)
}
});
Grocy.Components.UserfieldsForm.Load();
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('location-form');

View File

@@ -1,4 +1,4 @@
$('#save-product-button').on('click', function (e)
$('#save-product-button').on('click', function(e)
{
e.preventDefault();
@@ -26,26 +26,30 @@
if (Grocy.EditMode === 'create')
{
Grocy.Api.Post('objects/products', jsonData,
function (result)
function(result)
{
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteProductPictureOnSave)
Grocy.EditObjectId = result.created_object_id;
Grocy.Components.UserfieldsForm.Save(function()
{
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
function (result)
{
window.location.href = redirectDestination;
},
function (xhr)
{
Grocy.FrontendHelpers.EndUiBusy("product-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = redirectDestination;
}
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteProductPictureOnSave)
{
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
function(result)
{
window.location.href = redirectDestination;
},
function (xhr)
{
Grocy.FrontendHelpers.EndUiBusy("product-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = redirectDestination;
}
});
},
function (xhr)
{
@@ -74,24 +78,27 @@
Grocy.Api.Put('objects/products/' + Grocy.EditObjectId, jsonData,
function(result)
{
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteProductPictureOnSave)
Grocy.Components.UserfieldsForm.Save(function()
{
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
function(result)
{
window.location.href = redirectDestination;
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("product-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = redirectDestination;
}
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteProductPictureOnSave)
{
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
function(result)
{
window.location.href = redirectDestination;
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("product-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = redirectDestination;
}
});
},
function(xhr)
{
@@ -258,6 +265,7 @@ if (Grocy.EditMode === 'create')
}
}
Grocy.Components.UserfieldsForm.Load();
$('#name').focus();
$('.input-group-qu').trigger('change');
Grocy.FrontendHelpers.ValidateForm('product-form');

View File

@@ -10,7 +10,11 @@
Grocy.Api.Post('objects/product_groups', jsonData,
function(result)
{
window.location.href = U('/productgroups');
Grocy.EditObjectId = result.created_object_id;
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/productgroups');
});
},
function(xhr)
{
@@ -24,7 +28,10 @@
Grocy.Api.Put('objects/product_groups/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/productgroups');
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/productgroups');
});
},
function(xhr)
{
@@ -57,5 +64,6 @@ $('#product-group-form input').keydown(function (event)
}
});
Grocy.Components.UserfieldsForm.Load();
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('product-group-form');

View File

@@ -10,7 +10,11 @@
Grocy.Api.Post('objects/quantity_units', jsonData,
function(result)
{
window.location.href = U('/quantityunits');
Grocy.EditObjectId = result.created_object_id;
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/quantityunits');
});
},
function(xhr)
{
@@ -24,7 +28,10 @@
Grocy.Api.Put('objects/quantity_units/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/quantityunits');
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/quantityunits');
});
},
function(xhr)
{
@@ -57,5 +64,6 @@ $('#quantityunit-form input').keydown(function(event)
}
});
Grocy.Components.UserfieldsForm.Load();
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('quantityunit-form');

View File

@@ -10,7 +10,11 @@
Grocy.Api.Post('objects/task_categories', jsonData,
function(result)
{
window.location.href = U('/taskcategories');
Grocy.EditObjectId = result.created_object_id;
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/taskcategories');
});
},
function(xhr)
{
@@ -24,7 +28,10 @@
Grocy.Api.Put('objects/task_categories/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/taskcategories');
Grocy.Components.UserfieldsForm.Save(function()
{
window.location.href = U('/taskcategories');
});
},
function(xhr)
{
@@ -57,5 +64,6 @@ $('#task-category-form input').keydown(function (event)
}
});
Grocy.Components.UserfieldsForm.Load();
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('task-category-form');

View File

@@ -0,0 +1,74 @@
$('#save-userfield-button').on('click', function(e)
{
e.preventDefault();
var jsonData = $('#userfield-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("userfield-form");
if (Grocy.EditMode === 'create')
{
Grocy.Api.Post('objects/userfields', jsonData,
function(result)
{
window.location.href = U('/userfields');
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("userfield-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
Grocy.Api.Put('objects/userfields/' + Grocy.EditObjectId, jsonData,
function(result)
{
window.location.href = U('/userfields');
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("userfield-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
});
$('#userfield-form input').keyup(function(event)
{
Grocy.FrontendHelpers.ValidateForm('userfield-form');
});
$('#userfield-form select').change(function(event)
{
Grocy.FrontendHelpers.ValidateForm('userfield-form');
});
$('#userfield-form input').keydown(function(event)
{
if (event.keyCode === 13) //Enter
{
event.preventDefault();
if (document.getElementById('userfield-form').checkValidity() === false) //There is at least one validation error
{
return false;
}
else
{
$('#save-userfield-button').click();
}
}
});
$('#entity').focus();
if (typeof GetUriParam("entity") !== "undefined")
{
$("#entity").val(GetUriParam("entity"));
$("#entity").trigger("change");
$('#name').focus();
}
Grocy.FrontendHelpers.ValidateForm('userfield-form');

View File

@@ -0,0 +1,86 @@
var userfieldsTable = $('#userfields-table').DataTable({
'paginate': false,
'order': [[1, 'asc']],
'columnDefs': [
{ 'orderable': false, 'targets': 0 }
],
'language': JSON.parse(L('datatables_localization')),
'scrollY': false,
'colReorder': true,
'stateSave': true,
'stateSaveParams': function(settings, data)
{
data.search.search = "";
data.columns.forEach(column =>
{
column.search.search = "";
});
}
});
$('#userfields-table tbody').removeClass("d-none");
userfieldsTable.columns.adjust().draw();
$("#search").on("keyup", function()
{
var value = $(this).val();
if (value === "all")
{
value = "";
}
userfieldsTable.search(value).draw();
});
$("#entity-filter").on("change", function()
{
var value = $("#entity-filter option:selected").text();
if (value === L("All"))
{
value = "";
}
userfieldsTable.column(1).search(value).draw();
});
$(document).on('click', '.userfield-delete-button', function (e)
{
var objectName = $(e.currentTarget).attr('data-userfield-name');
var objectId = $(e.currentTarget).attr('data-userfield-id');
bootbox.confirm({
message: L('Are you sure to delete user field "#1"?', objectName),
buttons: {
confirm: {
label: L('Yes'),
className: 'btn-success'
},
cancel: {
label: L('No'),
className: 'btn-danger'
}
},
callback: function(result)
{
if (result === true)
{
Grocy.Api.Delete('objects/userfields/' + objectId, {},
function(result)
{
window.location.href = U('/userfields');
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});
if (typeof GetUriParam("entity") !== "undefined")
{
$("#entity-filter").val(GetUriParam("entity"));
$("#entity-filter").trigger("change");
}