Added a quick mockup for equipment / instruction manuals (references #25)

This commit is contained in:
Bernd Bestel
2018-10-02 20:03:08 +02:00
parent f90faca62e
commit edb986ce24
11 changed files with 385 additions and 2 deletions

View File

@@ -0,0 +1,81 @@
var equipmentTable = $('#equipment-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 = "";
});
},
'select': 'single',
'initComplete': function()
{
this.api().row({ order: 'current' }, 0).select();
}
});
equipmentTable.on('select', function(e, dt, type, indexes)
{
if (type === 'row')
{
var selectedEquipmentId = $(equipmentTable.row(indexes[0]).node()).data("equipment-id");
console.log(selectedEquipmentId);
}
});
$("#search").on("keyup", function()
{
var value = $(this).val();
if (value === "all")
{
value = "";
}
equipmentTable.search(value).draw();
});
$(document).on('click', '.equipment-delete-button', function (e)
{
var objectName = $(e.currentTarget).attr('data-equipment-name');
var objectId = $(e.currentTarget).attr('data-equipment-id');
bootbox.confirm({
message: L('Are you sure to delete equipment "#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.Get('delete-object/equipment/' + objectId,
function(result)
{
window.location.href = U('/equipment');
},
function(xhr)
{
console.error(xhr);
}
);
}
}
});
});

View File

@@ -0,0 +1,123 @@
$('#save-equipment-button').on('click', function(e)
{
e.preventDefault();
var jsonData = $('#equipment-form').serializeJSON();
if ($("#instruction-manual")[0].files.length > 0)
{
var someRandomStuff = Math.random().toString(36).substring(2, 100) + Math.random().toString(36).substring(2, 100);
jsonData.instruction_manual_file_name = someRandomStuff + $("#instruction-manual")[0].files[0].name;
}
if (Grocy.DeleteInstructionManualOnSave)
{
jsonData.instruction_manual_file_name = null;
}
if (Grocy.EditMode === 'create')
{
Grocy.Api.Post('add-object/equipment', jsonData,
function(result)
{
if (jsonData.hasOwnProperty("instruction_manual_file_name") && !Grocy.DeleteInstructionManualOnSave)
{
Grocy.Api.UploadFile($("#instruction-manual")[0].files[0], 'equipmentmanuals', jsonData.instruction_manual_file_name,
function(result)
{
window.location.href = U('/equipment');
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = U('/equipment');
}
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
if (Grocy.DeleteInstructionManualOnSave)
{
Grocy.Api.DeleteFile(Grocy.InstructionManualFileNameName, 'equipmentmanuals',
function(result)
{
// Nothing to do
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
};
Grocy.Api.Post('edit-object/equipment/' + Grocy.EditObjectId, jsonData,
function(result)
{
if (jsonData.hasOwnProperty("instruction_manual_file_name") && !Grocy.DeleteInstructionManualOnSave)
{
Grocy.Api.UploadFile($("#instruction-manual")[0].files[0], 'equipmentmanuals', jsonData.instruction_manual_file_name,
function(result)
{
window.location.href = U('/equipment');;
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
else
{
window.location.href = U('/equipment');;
}
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}
});
$('#equipment-form input').keyup(function(event)
{
Grocy.FrontendHelpers.ValidateForm('equipment-form');
});
$('#equipment-form input').keydown(function(event)
{
if (event.keyCode === 13) //Enter
{
event.preventDefault();
if (document.getElementById('equipment-form').checkValidity() === false) //There is at least one validation error
{
return false;
}
else
{
$('#save-equipment-button').click();
}
}
});
Grocy.DeleteInstructionManualOnSave = false;
$('#delete-current-instruction-manual-button').on('click', function (e)
{
Grocy.DeleteInstructionManualOnSave = true;
//$("#current-instruction-manual").addClass("d-none");
$("#delete-current-instruction-manual-on-save-hint").removeClass("d-none");
$("#delete-current-instruction-manual-button").addClass("disabled");
});
$('#name').focus();
Grocy.FrontendHelpers.ValidateForm('equipment-form');