openwebrx-clone/htdocs/lib/settings/ImageUpload.js

45 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-02-08 22:29:24 +00:00
$.fn.imageUpload = function() {
$.each(this, function(){
2021-02-10 20:29:46 +00:00
var $uploadButton = $(this).find('button.upload');
var $restoreButton = $(this).find('button.restore');
var $img = $(this).find('img');
2021-02-10 20:29:46 +00:00
var originalUrl = $img.prop('src');
var $input = $(this).find('input');
var id = $input.prop('id');
2021-02-10 20:29:46 +00:00
$uploadButton.click(function(){
$uploadButton.prop('disabled', true);
var input = document.createElement('input');
input.type = 'file';
input.accept = 'image/jpeg, image/png';
2021-02-08 22:29:24 +00:00
input.onchange = function(e) {
var reader = new FileReader()
// TODO: implement file size check
reader.readAsArrayBuffer(e.target.files[0]);
reader.onload = function(e) {
$.ajax({
url: '/imageupload?id=' + id,
type: 'POST',
data: e.target.result,
processData: false,
contentType: 'application/octet-stream',
}).done(function(data){
$input.val(data.file);
$img.prop('src', '/imageupload?file=' + data.file);
}).always(function(){
2021-02-10 20:29:46 +00:00
$uploadButton.prop('disabled', false);
});
}
};
2021-02-08 22:29:24 +00:00
input.click();
return false;
});
2021-02-10 20:29:46 +00:00
$restoreButton.click(function(){
$input.val('restore');
$img.prop('src', originalUrl + "&mapped=false");
return false;
});
2021-02-08 22:29:24 +00:00
});
}