Skip to content

Commit

Permalink
Declaring functions according to good practices and using let instead of
Browse files Browse the repository at this point in the history
var and mutualisation function with common.js
  • Loading branch information
wincelau committed Oct 2, 2024
1 parent ed7d2f9 commit b5404a3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 94 deletions.
36 changes: 36 additions & 0 deletions public/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ function is_mobile() {
return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none");
};

function hasTouch() {
return 'ontouchstart' in document.documentElement
|| navigator.maxTouchPoints > 0
|| navigator.msMaxTouchPoints > 0;
}

function disabledHoverStyle() {
try { // prevent exception on browsers not supporting DOM styleSheets properly
for (var si in document.styleSheets) {
var styleSheet = document.styleSheets[si];
if (!styleSheet.rules) continue;

for (var ri = styleSheet.rules.length - 1; ri >= 0; ri--) {
if (!styleSheet.rules[ri].selectorText) continue;

if (styleSheet.rules[ri].selectorText.match(':hover')) {
styleSheet.deleteRule(ri);
}
}
}
} catch (ex) {}
}

async function canUseCache() {
try {
cache = await caches.open('pdf');
Expand Down Expand Up @@ -67,6 +90,15 @@ async function loadFileFromUrl(url, pageUrl, local = null) {
document.getElementById('input_pdf_upload').files = dataTransfer.files;
}

function download(blob, filename) {
let a = document.createElement("a"),
u = URL.createObjectURL(blob);
a.download = filename,
a.href = u,
a.click(),
setTimeout(() => URL.revokeObjectURL(u))
}

function storeSymmetricKeyCookie(hash, symmetricKey) {
if (symmetricKey.length != 15) {
console.error("Erreur taille cle symétrique.");
Expand Down Expand Up @@ -144,3 +176,7 @@ function trimSvgWhitespace(svgContent) {

return svgContent = svgContainer.innerHTML;
}

function getLetter(i) {
return String.fromCharCode(96 + i+1).toUpperCase();
}
9 changes: 0 additions & 9 deletions public/js/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@ function deleteMetadata(el) {
input.remove()
}

function download(blob, filename) {
let a = document.createElement("a"),
u = URL.createObjectURL(blob);
a.download = filename,
a.href = u,
a.click(),
setTimeout(() => URL.revokeObjectURL(u))
}

async function save() {
const PDFDocument = window['PDFLib'].PDFDocument
const PDFHexString = window['PDFLib'].PDFHexString
Expand Down
125 changes: 40 additions & 85 deletions public/js/organization.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
var windowWidth = window.innerWidth;
var menu = null;
var menuOffcanvas = null;
var is_mobile = function() {
return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none");
};
var hasTouch = function() {
return 'ontouchstart' in document.documentElement
|| navigator.maxTouchPoints > 0
|| navigator.msMaxTouchPoints > 0;
}
var disabledHoverStyle = function() {
try { // prevent exception on browsers not supporting DOM styleSheets properly
for (var si in document.styleSheets) {
var styleSheet = document.styleSheets[si];
if (!styleSheet.rules) continue;

for (var ri = styleSheet.rules.length - 1; ri >= 0; ri--) {
if (!styleSheet.rules[ri].selectorText) continue;

if (styleSheet.rules[ri].selectorText.match(':hover')) {
styleSheet.deleteRule(ri);
}
}
}
} catch (ex) {}
}
var responsiveDisplay = function() {
let windowWidth = window.innerWidth;
let menu = null;
let menuOffcanvas = null;

function responsiveDisplay() {
if(is_mobile()) {
document.getElementById('page-organization').classList.remove('decalage-pdf-div');
menu.classList.remove('show');
Expand All @@ -41,23 +18,23 @@ var responsiveDisplay = function() {
menu.classList.remove('d-md-block');
menu.classList.remove('d-none');
};
var isSelectionMode = function() {
function isSelectionMode() {
return document.querySelectorAll('.canvas-container .input-select:checked').length > 0;
}
var isDraggedMode = function() {
function isDraggedMode() {
return document.querySelectorAll('.canvas-container .input-drag:checked').length > 0;
}

var nbPagePerLine = 5;
let nbPagePerLine = 5;
if(is_mobile()) {
nbPagePerLine = 2;
}

var nbPDF = 0;
var pages = [];
var pdfRenderTasks = [];
let nbPDF = 0;
let pages = [];
let pdfRenderTasks = [];

var loadPDF = async function(pdfBlob, filename, pdfIndex) {
async function loadPDF(pdfBlob, filename, pdfIndex) {
let url = await URL.createObjectURL(pdfBlob);

let dataTransfer = new DataTransfer();
Expand Down Expand Up @@ -234,13 +211,13 @@ var loadPDF = async function(pdfBlob, filename, pdfIndex) {
return loadingTask;
};

var pageRenderAll = function() {
function pageRenderAll() {
for(pageIndex in pages) {
pageRender(pageIndex);
}
}

var pageRender = async function(pageIndex) {
async function pageRender(pageIndex) {
let scrollWidth = 12;
if(is_mobile()) {
scrollWidth = -4;
Expand Down Expand Up @@ -278,12 +255,12 @@ var pageRender = async function(pageIndex) {
});
}

var getFileIndex = function(page) {
function getFileIndex(page) {

return page.id.replace('canvas-container-', '').replace(/_.*$/, '');
}

var getFilesStats = function() {
function getFilesStats() {
let files = [];
document.querySelectorAll('.canvas-container').forEach(function(page) {
let fileIndex = getFileIndex(page);
Expand All @@ -305,12 +282,7 @@ var getFilesStats = function() {
return files;
}

const getLetter = function(i) {

return String.fromCharCode(96 + i+1).toUpperCase();
}

var updateListePDF = function() {
function updateListePDF() {
document.querySelector('#list_pdf').innerHTML = "";
let nbFiles = document.querySelector('#input_pdf').files.length;
for (var i = 0; i < nbFiles; i++) {
Expand All @@ -335,7 +307,7 @@ var updateListePDF = function() {
updateGlobalState();
}

var getPagesSelected = function() {
function getPagesSelected() {
let pages = [];
document.querySelectorAll('.canvas-container .input-select:checked').forEach(function(item) {
pages[item.parentNode.id.replace('canvas-container-', '')] = item.parentNode;
Expand All @@ -344,43 +316,43 @@ var getPagesSelected = function() {
return pages;
}

var selectPage = function(page, state) {
function selectPage(page, state) {
page.querySelector('input[type=checkbox].input-select').checked = state;
updatePageState(page);
}

var toggleSelectPage = function(page) {
function toggleSelectPage(page) {
if(isPageDeleted(page) || isPageDragged(page) || isDraggedMode()) {
return;
}
selectPage(page, !isPageSelected(page));
updateGlobalState();
}

var isPageSelected = function(page) {
function isPageSelected(page) {

return page.querySelector('input[type=checkbox].input-select').checked;
}

var dragPage = function(page, state) {
function dragPage(page, state) {
page.querySelector('input[type=checkbox].input-drag').checked = state;
updatePageState(page);
}

var toggleDragPage = function(page) {
function toggleDragPage(page) {
dragPage(page, !isPageDragged(page));
updateGlobalState();
document.querySelectorAll('.canvas-container').forEach(function(page) {
updatePageState(page);
});
}

var isPageDragged = function(page) {
function isPageDragged(page) {

return page.querySelector('input[type=checkbox].input-drag').checked;
}

var movePagesDragged = function(pageHere, position) {
function movePagesDragged(pageHere, position) {
document.querySelectorAll('.canvas-container .input-drag:checked').forEach(function(item) {
let page = item.parentNode;
if(position == 'right') {
Expand All @@ -392,28 +364,26 @@ var movePagesDragged = function(pageHere, position) {
document.getElementById('btn_drag_select').click();
}

var toggleDeletePage = function(page) {
function toggleDeletePage(page) {
deletePage(page, isPageDeleted(page))
updateGlobalState();
}

var deletePage = function(page, state) {
function deletePage(page, state) {
page.querySelector('input[type=checkbox].checkbox-page').checked = state;
page.querySelector('input[type=checkbox].input-select').checked = false;
updatePageState(page);
}

var isPageDeleted = function(page) {

function isPageDeleted(page) {
return !page.querySelector('input[type=checkbox].checkbox-page').checked;
}

var isPageHover = function(page) {

function isPageHover(page) {
return page.querySelector('input[type=checkbox].input-hover').checked;
}

var updatePageState = function(page) {
function updatePageState(page) {
page.classList.remove('border-primary', 'shadow-sm', 'bg-primary', 'border-secondary', 'bg-secondary');
page.classList.add('border-transparent', 'bg-transparent');
page.querySelector('.canvas-pdf').style.opacity = '1';
Expand Down Expand Up @@ -483,7 +453,7 @@ var updatePageState = function(page) {
}
}

var updateFilesState = function() {
function updateFilesState() {
let filesStats = getFilesStats();
for(fileIndex in filesStats) {
let checkbox = document.querySelector('#file_'+fileIndex+' input[type=checkbox]');
Expand All @@ -497,7 +467,7 @@ var updateFilesState = function() {
}
}

var updateGlobalState = function() {
function updateGlobalState() {
updateFilesState();
if(!is_mobile()) {
document.querySelector('#container-btn-zoom').classList.remove('d-none');
Expand Down Expand Up @@ -543,29 +513,14 @@ var updateGlobalState = function() {
}
}

var uploadAndLoadPDF = async function(input_upload) {
const cache = await caches.open('pdf');
async function uploadAndLoadPDF(input_upload) {
for (let i = 0; i < input_upload.files.length; i++) {
let filename = input_upload.files[i].name;
let response = new Response(input_upload.files[i], { "status" : 200, "statusText" : "OK" });
let urlPdf = '/pdf/'+filename;
await cache.put(urlPdf, response);
let pdfBlob = await getPDFBlobFromCache(urlPdf);
nbPDF++;
await loadPDF(pdfBlob, filename, nbPDF);
await loadPDF(input_upload.files[i], input_upload.files[i].name, nbPDF);
}
}

const DL = function (d,f) {
let a = document.createElement("a"),
u = URL.createObjectURL(d);
a.download = f,
a.href = u,
a.click(),
setTimeout(() => URL.revokeObjectURL(u))
}

let saveAll = async function () {
async function saveAll() {
let order = [];
let selectionMode = isSelectionMode();

Expand Down Expand Up @@ -593,7 +548,7 @@ let saveAll = async function () {
await save(order.join(','));
}

let save = async function (order) {
async function save(order) {
const PDFDocument = window['PDFLib'].PDFDocument
const Rotation = window['PDFLib'].Rotation

Expand Down Expand Up @@ -626,10 +581,10 @@ let save = async function (order) {
pdf.addPage(pdfPage);
}
const newPDF = new Blob([await pdf.save()], {type: "application/pdf"});
await DL(newPDF, filename+".pdf");
await download(newPDF, filename+".pdf");
}

var createEventsListener = function() {
function createEventsListener() {
document.getElementById('save-select_mobile').addEventListener('click', async function(event) {
event.preventDefault();
startProcessingMode(document.getElementById('save-select_mobile'));
Expand Down Expand Up @@ -764,7 +719,7 @@ async function uploadFromUrl(url) {
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
}

var pageUpload = async function() {
async function pageUpload() {
document.querySelector('body').classList.remove('bg-light');
document.getElementById('input_pdf_upload').value = '';
document.getElementById('page-upload').classList.remove('d-none');
Expand All @@ -776,7 +731,7 @@ var pageUpload = async function() {
});
}

var pageOrganization = async function() {
async function pageOrganization() {
document.querySelector('body').classList.add('bg-light');
document.getElementById('page-upload').classList.add('d-none');
document.getElementById('page-organization').classList.remove('d-none');
Expand Down
1 change: 1 addition & 0 deletions templates/organization.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
'Page' => _('Page')
]); ?>;
</script>
<script src="<?php echo $REVERSE_PROXY_URL; ?>/js/common.js?<?php echo ($COMMIT) ? $COMMIT : filemtime($ROOT."/public/js/common.js") ?>"></script>
<script src="<?php echo $REVERSE_PROXY_URL; ?>/js/organization.js?<?php echo ($COMMIT) ? $COMMIT : filemtime($ROOT."/public/js/organization.js") ?>"></script>
<script src="<?php echo $REVERSE_PROXY_URL; ?>/vendor/pdf-lib.min.js?1.17.1"></script>
</body>
Expand Down

0 comments on commit b5404a3

Please sign in to comment.