Skip to content

Commit

Permalink
Declaring functions according to good practices and using let instead…
Browse files Browse the repository at this point in the history
… of var
  • Loading branch information
wincelau committed Oct 1, 2024
1 parent 55176b9 commit f6946ba
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 122 deletions.
54 changes: 20 additions & 34 deletions public/js/metadata.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var windowWidth = window.innerWidth;
var menu = null;
var menuOffcanvas = null;
var is_mobile = function() {
return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none");
};
let pages = [];
let pdfRenderTasks = [];
let pdffile = null
let deletedMetadata = [];

function responsiveDisplay() {
let menu = document.getElementById('sidebarTools');
let menuOffcanvas = new bootstrap.Offcanvas(menu);

var responsiveDisplay = function() {
if(is_mobile()) {
menu.classList.remove('show');
menuOffcanvas.hide();
Expand All @@ -16,27 +17,12 @@ var responsiveDisplay = function() {
menu.classList.remove('d-none');
};

var canUseCache = async function() {
try {
cache = await caches.open('pdf');
return true;
} catch (e) {
return false;
}
};

var nbPDF = 0;
var pages = [];
var pdfRenderTasks = [];
let filename = null
let pdffile = null
let deletedMetadata = [];

var loadPDF = async function(pdfBlob, filename) {
async function loadPDF(pdfBlob) {
let filename = pdfBlob.name;
let url = await URL.createObjectURL(pdfBlob);
document.title = filename + ' - ' + document.title;

pdffile = pdfBlob

let loadingTask = pdfjsLib.getDocument(url);
document.querySelector('#text_document_name span').innerText = filename;
await loadingTask.promise.then(function(pdf) {
Expand Down Expand Up @@ -80,7 +66,7 @@ var loadPDF = async function(pdfBlob, filename) {
return loadingTask;
}

var pageRender = async function(pageIndex) {
async function pageRender(pageIndex) {

let page = pages[pageIndex];

Expand All @@ -107,7 +93,7 @@ var pageRender = async function(pageIndex) {
});
}

var addMetadata = function(key, value, type, focus) {
function addMetadata(key, value, type, focus) {
let input = document.querySelector('.input-metadata input[name="'+key+'"]');

if(input && !input.value) {
Expand Down Expand Up @@ -147,7 +133,7 @@ var addMetadata = function(key, value, type, focus) {
}
}

const deleteMetadata = function(el) {
function deleteMetadata(el) {
if (confirm("Souhaitez-vous supprimer ce champ ?") === false) return;

const input = el.closest('.input-metadata')
Expand All @@ -156,16 +142,16 @@ const deleteMetadata = function(el) {
input.remove()
}

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

const save = async function () {
async function save() {
const PDFDocument = window['PDFLib'].PDFDocument
const PDFHexString = window['PDFLib'].PDFHexString
const PDFName = window['PDFLib'].PDFName
Expand Down Expand Up @@ -194,10 +180,10 @@ const save = async function () {
});
return ;
}
DL(newPDF, filename)
download(newPDF, document.getElementById('input_pdf_upload').files[0].name)
}

var createEventsListener = function() {
function createEventsListener() {
document.getElementById('form_metadata_add').addEventListener('submit', function(e) {
let formData = new FormData(this);
addMetadata(formData.get('metadata_key'), "", "text", true);
Expand Down
Loading

0 comments on commit f6946ba

Please sign in to comment.