var windowWidth = window.innerWidth;
var menu = null;
var menuOffcanvas = null;
var is_mobile = function() {
return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none");
};
var responsiveDisplay = function() {
if(is_mobile()) {
document.getElementById('page-organization').style.paddingRight = "inherit";
menu.classList.remove('show');
menuOffcanvas.hide();
document.getElementById('container-pages').classList.remove('vh-100');
} else {
menuOffcanvas.show();
document.getElementById('page-organization').style.paddingRight = "350px";
document.getElementById('container-pages').classList.add('vh-100');
}
menu.classList.remove('d-md-block');
menu.classList.remove('d-none');
};
var isSelectionMode = function() {
return document.querySelectorAll('.canvas-container .input-select:checked').length > 0;
}
var getPagesSelected = function() {
let pages = [];
document.querySelectorAll('.canvas-container .input-select:checked').forEach(function(item) {
pages[item.parentNode.id.replace('canvas-container-', '')] = item.parentNode;
});
return pages;
}
var nbPagePerLine = 5;
if(is_mobile()) {
nbPagePerLine = 1;
}
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdf.worker.js?legacy';
var nbPDF = 0;
var pages = [];
var pdfRenderTasks = [];
var loadPDF = async function(pdfBlob, filename, pdfIndex) {
let url = await URL.createObjectURL(pdfBlob);
let dataTransfer = new DataTransfer();
let i = 0;
for (i = 0; i < document.getElementById('input_pdf').files.length; i++) {
dataTransfer.items.add(document.getElementById('input_pdf').files[i]);
}
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf').files = dataTransfer.files;
updateListePDF();
let pdfLetter = String.fromCharCode(96 + i+1).toUpperCase();
let loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdf) {
for(var pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++ ) {
pdf.getPage(pageNumber).then(function(page) {
let pageIndex = pdfLetter + "_" + (page.pageNumber - 1);
pages[pageIndex] = page;
let pageHTML = '
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
Page '+page.pageNumber+' - '+filename+'
';
pageHTML += '
';
pageHTML += '
';
pageHTML += '
';
document.getElementById('container-pages').insertAdjacentHTML('beforeend', pageHTML);
let canvasContainer = document.getElementById('canvas-container-' + pageIndex);
canvasContainer.addEventListener('dragstart', function(e) {
this.querySelector('.container-resize').classList.add('d-none');
this.querySelector('.canvas-pdf').classList.add('shadow-lg');
this.querySelector('.canvas-pdf').style.border = '2px dashed #777';
e.dataTransfer.setData('element', this.id);
this.style.opacity = 0.4;
});
canvasContainer.addEventListener('dragend', function(e) {
this.querySelector('.container-resize').classList.remove('d-none');
this.querySelector('.canvas-pdf').classList.remove('shadow-lg');
this.querySelector('.canvas-pdf').style.border = '2px solid transparent';
this.style.opacity = 1;
stateCheckbox(this.querySelector('input[type=checkbox]'));
});
canvasContainer.addEventListener('dragover', function(e) {
if (e.preventDefault) {
e.preventDefault();
}
if(e.layerX > e.target.clientWidth / 2) {
this.insertAdjacentElement('beforebegin', document.querySelector('#'+e.dataTransfer.getData('element')));
} else {
this.insertAdjacentElement('afterend', document.querySelector('#'+e.dataTransfer.getData('element')));
}
return false;
});
canvasContainer.querySelector('.btn-delete').addEventListener('click', function(e) {
let pages = getPagesSelected();
if(!pages.length) {
pages[pageIndex] = this.parentNode;
}
for(index in pages) {
let checkbox = pages[index].querySelector('input[type=checkbox]');
checkbox.checked = !checkbox.checked;
stateCheckbox(checkbox);
}
if(isSelectionMode()) {
document.querySelector('#btn_cancel_select').click();
}
});
canvasContainer.querySelector('.btn-select').addEventListener('click', function(e) {
let checkbox = this.parentNode.querySelector('input[type=checkbox].input-select');
checkbox.checked = !checkbox.checked;
let container = this.parentNode;
if(checkbox.checked) {
container.classList.add('border-primary', 'shadow', 'bg-primary');
container.classList.remove('border-transparent', 'shadow-sm', 'bg-white');
} else {
container.classList.remove('border-primary', 'shadow', 'bg-primary');
container.classList.add('border-transparent', 'shadow-sm', 'bg-white');
}
if(document.querySelectorAll('.canvas-container .input-select:checked').length > 0) {
document.querySelector('#container-btn-save-select').classList.remove('d-none');
document.querySelector('#container-btn-save').classList.add('d-none');
} else {
document.querySelector('#container-btn-save-select').classList.add('d-none');
document.querySelector('#container-btn-save').classList.remove('d-none');
}
});
canvasContainer.querySelector('.btn-download').addEventListener('click', function(e) {
if(isSelectionMode()) {
document.querySelector('#save-select').click();
return false;
}
let container = this.parentNode;
let pageValue = container.querySelector('.checkbox-page').value;
let orientation = degreesToOrientation(container.querySelector('.input-rotate').value);
if(orientation) {
pageValue = pageValue + "-" + orientation;
}
document.querySelector('#input_pages').value = pageValue;
document.querySelector('#form_pdf').submit();
});
canvasContainer.querySelector('.btn-rotate').addEventListener('click', function(e) {
let pages = getPagesSelected();
if(!pages.length) {
pages[pageIndex] = this.parentNode;
}
for(index in pages) {
let inputRotate = pages[index].querySelector('.input-rotate');
inputRotate.value = (parseInt(inputRotate.value) + 90) % 360;
pageRender(index);
}
})
pageRender(pageIndex);
});
}
}, function (reason) {
console.error(reason);
});
};
var pageRenderAll = function() {
for(pageIndex in pages) {
pageRender(pageIndex);
}
}
var pageRender = async function(pageIndex) {
let page = pages[pageIndex];
let rotation = parseInt(document.querySelector('#input_rotate_'+pageIndex).value);
let viewport = page.getViewport({scale: 1, rotation: rotation});
let size = Math.floor((document.getElementById('container-pages').offsetWidth - (8*(nbPagePerLine+1)) - 12) / nbPagePerLine);
let scaleWidth = size / viewport.width;
let scaleHeight = size / viewport.height;
let viewportWidth = page.getViewport({scale: scaleWidth, rotation: rotation});
let viewportHeight = page.getViewport({scale: scaleHeight, rotation: rotation});
if(viewportWidth.height > size) {
viewport = viewportHeight;
} else {
viewport = viewportWidth;
}
let canvasContainer = document.getElementById('canvas-container-' + pageIndex);
canvasContainer.style.height = (size + 4) + "px";
canvasContainer.style.width = (size + 4) + "px";
let canvasPDF = canvasContainer.querySelector('.canvas-pdf');
let context = canvasPDF.getContext('2d');
canvasPDF.height = viewport.height;
canvasPDF.width = viewport.width;
if(pdfRenderTasks[pageIndex]) {
pdfRenderTasks[pageIndex].cancel();
}
pdfRenderTasks[pageIndex] = await page.render({
canvasContext: context,
viewport: viewport,
});
}
var stateCheckbox = function(checkbox) {
let checkboxContainer = checkbox.parentNode.parentNode.parentNode;
if(checkbox.checked) {
checkboxContainer.style.opacity = '1'
} else {
checkboxContainer.style.opacity = '0.2';
}
};
var updateListePDF = function() {
document.querySelector('#list_pdf').innerHTML = "";
for (var i = 0; i < document.querySelector('#input_pdf').files.length; i++) {
const pdfFile = document.querySelector('#input_pdf').files.item(i);
document.querySelector('#list_pdf').insertAdjacentHTML('beforeend', '