mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 12:20:22 +00:00
Finished first version of "pictures for products" (references #58)
This commit is contained in:
@@ -54,3 +54,13 @@ BoolVal = function(test)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
GetFileNameFromPath = function(path)
|
||||
{
|
||||
return path.split("/").pop().split("\\").pop();
|
||||
}
|
||||
|
||||
GetFileExtension = function(pathOrFileName)
|
||||
{
|
||||
return pathOrFileName.split(".").pop();
|
||||
}
|
||||
|
@@ -177,15 +177,10 @@ Grocy.Api.Post = function(apiFunction, jsonData, success, error)
|
||||
xhr.send(JSON.stringify(jsonData));
|
||||
};
|
||||
|
||||
Grocy.Api.UploadFile = function(fileInput, group, success, error)
|
||||
Grocy.Api.UploadFile = function(file, group, fileName, success, error)
|
||||
{
|
||||
if (fileInput[0].files.length === 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
var url = U('/api/files/upload/' + group + '?file_name=' + encodeURIComponent(fileInput[0].files[0].name));
|
||||
var url = U('/api/file/' + group + '?file_name=' + encodeURIComponent(fileName));
|
||||
|
||||
xhr.onreadystatechange = function()
|
||||
{
|
||||
@@ -208,9 +203,40 @@ Grocy.Api.UploadFile = function(fileInput, group, success, error)
|
||||
}
|
||||
};
|
||||
|
||||
xhr.open('POST', url, true);
|
||||
xhr.open('PUT', url, true);
|
||||
xhr.setRequestHeader('Content-type', 'application/octet-stream');
|
||||
xhr.send(fileInput[0].files[0]);
|
||||
xhr.send(file);
|
||||
};
|
||||
|
||||
Grocy.Api.DeleteFile = function(fileName, group, success, error)
|
||||
{
|
||||
var xhr = new XMLHttpRequest();
|
||||
var url = U('/api/file/' + group + '?file_name=' + encodeURIComponent(fileName));
|
||||
|
||||
xhr.onreadystatechange = function()
|
||||
{
|
||||
if (xhr.readyState === XMLHttpRequest.DONE)
|
||||
{
|
||||
if (xhr.status === 200)
|
||||
{
|
||||
if (success)
|
||||
{
|
||||
success(JSON.parse(xhr.responseText));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
error(xhr);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.open('DELETE', url, true);
|
||||
xhr.setRequestHeader('Content-type', 'application/json');
|
||||
xhr.send();
|
||||
};
|
||||
|
||||
Grocy.FrontendHelpers = { };
|
||||
@@ -284,3 +310,15 @@ $(".user-setting-control").on("change", function()
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// Show file name Bootstrap custom file input
|
||||
$('input.custom-file-input').on('change', function()
|
||||
{
|
||||
$(this).next('.custom-file-label').html(GetFileNameFromPath($(this).val()));
|
||||
});
|
||||
|
||||
// Translation of "Browse"-button of Bootstrap custom file input
|
||||
if ($(".custom-file-label").length > 0)
|
||||
{
|
||||
$("<style>").html('.custom-file-label::after { content: "' + L("Select file") + '"; }').appendTo("head");
|
||||
}
|
||||
|
@@ -24,6 +24,18 @@ Grocy.Components.ProductCard.Refresh = function(productId)
|
||||
$('#productcard-product-last-price').text(L('Unknown'));
|
||||
}
|
||||
|
||||
if (productDetails.product.picture_file_name !== null && !productDetails.product.picture_file_name.isEmpty())
|
||||
{
|
||||
$("#productcard-no-product-picture").addClass("d-none");
|
||||
$("#productcard-product-picture").removeClass("d-none");
|
||||
$("#productcard-product-picture").attr("src", U('/api/file/productpictures?file_name=' + productDetails.product.picture_file_name));
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#productcard-no-product-picture").removeClass("d-none");
|
||||
$("#productcard-product-picture").addClass("d-none");
|
||||
}
|
||||
|
||||
EmptyElementWhenMatches('#productcard-product-last-purchased-timeago', L('timeago_nan'));
|
||||
EmptyElementWhenMatches('#productcard-product-last-used-timeago', L('timeago_nan'));
|
||||
},
|
||||
|
@@ -1,4 +1,4 @@
|
||||
$('#save-product-button').on('click', function(e)
|
||||
$('#save-product-button').on('click', function (e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
@@ -12,22 +12,28 @@
|
||||
var jsonData = $('#product-form').serializeJSON();
|
||||
if ($("#product-picture")[0].files.length > 0)
|
||||
{
|
||||
jsonData.picture_file_name = $("#product-picture")[0].files[0].name;
|
||||
var someRandomStuff = Math.random().toString(36).substring(2, 100) + Math.random().toString(36).substring(2, 100);
|
||||
jsonData.picture_file_name = someRandomStuff + $("#product-picture")[0].files[0].name;
|
||||
}
|
||||
|
||||
if (Grocy.DeleteProductPictureOnSave)
|
||||
{
|
||||
jsonData.picture_file_name = null;
|
||||
}
|
||||
|
||||
if (Grocy.EditMode === 'create')
|
||||
{
|
||||
Grocy.Api.Post('add-object/products', jsonData,
|
||||
function(result)
|
||||
function (result)
|
||||
{
|
||||
if (jsonData.hasOwnProperty("picture_file_name"))
|
||||
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteProductPictureOnSave)
|
||||
{
|
||||
Grocy.Api.UploadFile($("#product-picture"), 'productpictures',
|
||||
function(result)
|
||||
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
|
||||
function (result)
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
},
|
||||
function(xhr)
|
||||
function (xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
@@ -38,7 +44,7 @@
|
||||
window.location.href = redirectDestination;
|
||||
}
|
||||
},
|
||||
function(xhr)
|
||||
function (xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
@@ -46,12 +52,26 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Grocy.DeleteProductPictureOnSave)
|
||||
{
|
||||
Grocy.Api.DeleteFile(Grocy.ProductPictureFileName, 'productpictures',
|
||||
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/products/' + Grocy.EditObjectId, jsonData,
|
||||
function(result)
|
||||
{
|
||||
if (jsonData.hasOwnProperty("picture_file_name"))
|
||||
if (jsonData.hasOwnProperty("picture_file_name") && !Grocy.DeleteProductPictureOnSave)
|
||||
{
|
||||
Grocy.Api.UploadFile($("#product-picture"), 'productpictures',
|
||||
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
|
||||
function(result)
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
@@ -150,6 +170,15 @@ $('#product-form input').keydown(function(event)
|
||||
}
|
||||
});
|
||||
|
||||
Grocy.DeleteProductPictureOnSave = false;
|
||||
$('#delete-current-product-picture-button').on('click', function (e)
|
||||
{
|
||||
Grocy.DeleteProductPictureOnSave = true;
|
||||
$("#current-product-picture").addClass("d-none");
|
||||
$("#delete-current-product-picture-on-save-hint").removeClass("d-none");
|
||||
$("#delete-current-product-picture-button").addClass("disabled");
|
||||
});
|
||||
|
||||
$('#name').focus();
|
||||
$('.input-group-qu').trigger('change');
|
||||
Grocy.FrontendHelpers.ValidateForm('product-form');
|
||||
|
@@ -141,15 +141,20 @@ $(document).on('click', '.product-consume-button', function(e)
|
||||
);
|
||||
});
|
||||
|
||||
$(document).on("click", ".show-product-picture-button", function(e)
|
||||
$(document).on("click", ".product-name-cell", function(e)
|
||||
{
|
||||
var pictureUrl = $(e.currentTarget).attr("data-picture-url");
|
||||
var productName = $(e.currentTarget).attr("data-product-name");
|
||||
var productHasPicture = BoolVal($(e.currentTarget).attr("data-product-has-picture"));
|
||||
|
||||
bootbox.alert({
|
||||
title: L("Image of product #1", productName),
|
||||
message: "<img src='" + pictureUrl + "' class='img-fluid'>"
|
||||
});
|
||||
if (productHasPicture)
|
||||
{
|
||||
var pictureUrl = $(e.currentTarget).attr("data-picture-url");
|
||||
var productName = $(e.currentTarget).attr("data-product-name");
|
||||
|
||||
bootbox.alert({
|
||||
title: L("Image of product #1", productName),
|
||||
message: "<img src='" + pictureUrl + "' class='img-fluid'>"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function RefreshStatistics()
|
||||
|
Reference in New Issue
Block a user