Finished first version of "pictures for products" (references #58)

This commit is contained in:
Bernd Bestel
2018-10-01 20:20:50 +02:00
parent fcdeb33426
commit f1fc0ee549
12 changed files with 252 additions and 56 deletions

View File

@@ -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();
}

View File

@@ -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");
}