mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 12:20:22 +00:00
Implement custom entities / objects (closes #242)
This commit is contained in:
69
public/viewjs/userentities.js
Normal file
69
public/viewjs/userentities.js
Normal file
@@ -0,0 +1,69 @@
|
||||
var userentitiesTable = $('#userentities-table').DataTable({
|
||||
'paginate': false,
|
||||
'order': [[1, 'asc']],
|
||||
'columnDefs': [
|
||||
{ 'orderable': false, 'targets': 0 }
|
||||
],
|
||||
'language': JSON.parse(__t('datatables_localization')),
|
||||
'scrollY': false,
|
||||
'colReorder': true,
|
||||
'stateSave': true,
|
||||
'stateSaveParams': function(settings, data)
|
||||
{
|
||||
data.search.search = "";
|
||||
|
||||
data.columns.forEach(column =>
|
||||
{
|
||||
column.search.search = "";
|
||||
});
|
||||
}
|
||||
});
|
||||
$('#userentities-table tbody').removeClass("d-none");
|
||||
userentitiesTable.columns.adjust().draw();
|
||||
|
||||
$("#search").on("keyup", function()
|
||||
{
|
||||
var value = $(this).val();
|
||||
if (value === "all")
|
||||
{
|
||||
value = "";
|
||||
}
|
||||
|
||||
userentitiesTable.search(value).draw();
|
||||
});
|
||||
|
||||
$(document).on('click', '.userentity-delete-button', function (e)
|
||||
{
|
||||
var objectName = $(e.currentTarget).attr('data-userentity-name');
|
||||
var objectId = $(e.currentTarget).attr('data-userentity-id');
|
||||
|
||||
bootbox.confirm({
|
||||
message: __t('Are you sure to delete userentity "%s"?', objectName),
|
||||
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/userentities/' + objectId, { },
|
||||
function(result)
|
||||
{
|
||||
window.location.href = U('/userentities');
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
68
public/viewjs/userentityform.js
Normal file
68
public/viewjs/userentityform.js
Normal file
@@ -0,0 +1,68 @@
|
||||
$('#save-userentity-button').on('click', function(e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
var jsonData = $('#userentity-form').serializeJSON();
|
||||
Grocy.FrontendHelpers.BeginUiBusy("userentity-form");
|
||||
|
||||
var redirectUrl = U("/userentities");
|
||||
|
||||
if (Grocy.EditMode === 'create')
|
||||
{
|
||||
Grocy.Api.Post('objects/userentities', jsonData,
|
||||
function(result)
|
||||
{
|
||||
window.location.href = redirectUrl;
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.EndUiBusy("userentity-form");
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.Api.Put('objects/userentities/' + Grocy.EditObjectId, jsonData,
|
||||
function(result)
|
||||
{
|
||||
window.location.href = redirectUrl;
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.EndUiBusy("userentity-form");
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$('#userentity-form input').keyup(function(event)
|
||||
{
|
||||
Grocy.FrontendHelpers.ValidateForm('userentity-form');
|
||||
});
|
||||
|
||||
$('#userentity-form select').change(function(event)
|
||||
{
|
||||
Grocy.FrontendHelpers.ValidateForm('userentity-form');
|
||||
});
|
||||
|
||||
$('#userentity-form input').keydown(function(event)
|
||||
{
|
||||
if (event.keyCode === 13) //Enter
|
||||
{
|
||||
event.preventDefault();
|
||||
|
||||
if (document.getElementById('userentity-form').checkValidity() === false) //There is at least one validation error
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$('#save-userentity-button').click();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$('#name').focus();
|
||||
Grocy.FrontendHelpers.ValidateForm('userentity-form');
|
47
public/viewjs/userobjectform.js
Normal file
47
public/viewjs/userobjectform.js
Normal file
@@ -0,0 +1,47 @@
|
||||
$('#save-userobject-button').on('click', function(e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
var jsonData = {};
|
||||
jsonData.userentity_id = Grocy.EditObjectParentId;
|
||||
console.log(jsonData);
|
||||
Grocy.FrontendHelpers.BeginUiBusy("userobject-form");
|
||||
|
||||
if (Grocy.EditMode === 'create')
|
||||
{
|
||||
Grocy.Api.Post('objects/userobjects', jsonData,
|
||||
function(result)
|
||||
{
|
||||
Grocy.EditObjectId = result.created_object_id;
|
||||
Grocy.Components.UserfieldsForm.Save(function()
|
||||
{
|
||||
window.location.href = U('/userobjects/' + Grocy.EditObjectParentName);
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.EndUiBusy("userobject-form");
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.Api.Put('objects/userobjects/' + Grocy.EditObjectId, jsonData,
|
||||
function(result)
|
||||
{
|
||||
Grocy.Components.UserfieldsForm.Save(function()
|
||||
{
|
||||
window.location.href = U('/userobjects/' + Grocy.EditObjectParentName);
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.EndUiBusy("userobject-form");
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Grocy.Components.UserfieldsForm.Load();
|
68
public/viewjs/userobjects.js
Normal file
68
public/viewjs/userobjects.js
Normal file
@@ -0,0 +1,68 @@
|
||||
var userobjectsTable = $('#userobjects-table').DataTable({
|
||||
'paginate': false,
|
||||
'order': [[1, 'asc']],
|
||||
'columnDefs': [
|
||||
{ 'orderable': false, 'targets': 0 }
|
||||
],
|
||||
'language': JSON.parse(__t('datatables_localization')),
|
||||
'scrollY': false,
|
||||
'colReorder': true,
|
||||
'stateSave': true,
|
||||
'stateSaveParams': function(settings, data)
|
||||
{
|
||||
data.search.search = "";
|
||||
|
||||
data.columns.forEach(column =>
|
||||
{
|
||||
column.search.search = "";
|
||||
});
|
||||
}
|
||||
});
|
||||
$('#userobjects-table tbody').removeClass("d-none");
|
||||
userobjectsTable.columns.adjust().draw();
|
||||
|
||||
$("#search").on("keyup", function()
|
||||
{
|
||||
var value = $(this).val();
|
||||
if (value === "all")
|
||||
{
|
||||
value = "";
|
||||
}
|
||||
|
||||
userobjectsTable.search(value).draw();
|
||||
});
|
||||
|
||||
$(document).on('click', '.userobject-delete-button', function (e)
|
||||
{
|
||||
var objectId = $(e.currentTarget).attr('data-userobject-id');
|
||||
|
||||
bootbox.confirm({
|
||||
message: __t('Are you sure to delete this userobject?'),
|
||||
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/userobjects/' + objectId, { },
|
||||
function(result)
|
||||
{
|
||||
window.location.reload();
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user