mirror of
https://github.com/24eme/signaturepdf
synced 2024-06-10 01:42:14 +02:00
102 lines
3.4 KiB
JavaScript
102 lines
3.4 KiB
JavaScript
var windowWidth = window.innerWidth;
|
|
var menu = null;
|
|
var menuOffcanvas = null;
|
|
var is_mobile = function() {
|
|
return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none");
|
|
};
|
|
|
|
var createEventsListener = function() {
|
|
|
|
}
|
|
|
|
async function getPDFBlobFromCache(cacheUrl) {
|
|
const cache = await caches.open('pdf');
|
|
let responsePdf = await cache.match(cacheUrl);
|
|
|
|
if(!responsePdf) {
|
|
return null;
|
|
}
|
|
|
|
let pdfBlob = await responsePdf.blob();
|
|
|
|
return pdfBlob;
|
|
}
|
|
|
|
async function uploadFromUrl(url) {
|
|
history.replaceState({}, '', '/metadata');
|
|
var response = await fetch(url);
|
|
if(response.status != 200) {
|
|
return;
|
|
}
|
|
var pdfBlob = await response.blob();
|
|
|
|
if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
|
|
return;
|
|
}
|
|
let dataTransfer = new DataTransfer();
|
|
let filename = url.replace(/^.*\//, '');
|
|
dataTransfer.items.add(new File([pdfBlob], filename, {
|
|
type: 'application/pdf'
|
|
}));
|
|
document.getElementById('input_pdf_upload').files = dataTransfer.files;
|
|
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
|
|
}
|
|
|
|
var pageUpload = async function() {
|
|
document.querySelector('body').classList.remove('bg-light');
|
|
document.getElementById('input_pdf_upload').value = '';
|
|
document.getElementById('page-upload').classList.remove('d-none');
|
|
document.getElementById('page-metadata').classList.add('d-none');
|
|
document.getElementById('input_pdf_upload').focus();
|
|
const cache = await caches.open('pdf');
|
|
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
|
|
if(document.getElementById('input_pdf_upload').files[0].size > maxSize) {
|
|
|
|
alert("Le PDF ne doit pas dépasser " + Math.round(maxSize/1024/1024) + " Mo");
|
|
document.getElementById('input_pdf_upload').value = "";
|
|
return;
|
|
}
|
|
let filename = document.getElementById('input_pdf_upload').files[0].name;
|
|
let response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
|
|
let urlPdf = '/pdf/'+filename;
|
|
await cache.put(urlPdf, response);
|
|
history.pushState({}, '', '/metadata#'+filename);
|
|
pageMetadata(urlPdf)
|
|
});
|
|
}
|
|
|
|
var pageMetadata = async function(url) {
|
|
let filename = url.replace('/pdf/', '');
|
|
document.title = filename + ' - ' + document.title;
|
|
document.querySelector('body').classList.add('bg-light');
|
|
document.getElementById('page-upload').classList.add('d-none');
|
|
document.getElementById('page-metadata').classList.remove('d-none');
|
|
menu = document.getElementById('sidebarTools');
|
|
menuOffcanvas = new bootstrap.Offcanvas(menu);
|
|
responsiveDisplay();
|
|
|
|
let pdfBlob = await getPDFBlobFromCache(url);
|
|
if(!pdfBlob) {
|
|
document.location = '/metadata';
|
|
return;
|
|
}
|
|
|
|
createEventsListener();
|
|
loadPDF(pdfBlob, filename, nbPDF);
|
|
};
|
|
|
|
(function () {
|
|
if(window.location.hash && window.location.hash.match(/^\#http/)) {
|
|
let hashUrl = window.location.hash.replace(/^\#/, '');
|
|
pageUpload();
|
|
uploadFromUrl(hashUrl);
|
|
} else if(window.location.hash) {
|
|
pageMetadata('/pdf/'+window.location.hash.replace(/^\#/, ''));
|
|
} else {
|
|
pageUpload();
|
|
}
|
|
window.addEventListener('hashchange', function() {
|
|
window.location.reload();
|
|
})
|
|
})();
|