From 453a726a9a666627670215cd8e8ce982cc2a2634 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 7 Nov 2022 01:09:41 +0100 Subject: [PATCH] Postload JSZip Close #18 --- public/assets/js/csvxmlV2.js | 54 ++++++++++++++++++++++++------------ public/index.php | 2 -- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/public/assets/js/csvxmlV2.js b/public/assets/js/csvxmlV2.js index 4c9333a..f158b0a 100644 --- a/public/assets/js/csvxmlV2.js +++ b/public/assets/js/csvxmlV2.js @@ -450,28 +450,48 @@ class CsvxmlPage { zipUploadToXml(validator) { - let zip = new JSZip(); + // Wrap zipping in function to postload it + function runZipping() { + + let zip = new JSZip(); + + let xmlFiles = validator.generateXml(); + const serializer = new XMLSerializer(); + let lineCounter = 0; + for (let xml of xmlFiles) { + zip.file(lineCounter + ".xml", serializer.serializeToString(xml)); + lineCounter++; + } + + zip.generateAsync({type:"blob"}) + .then(function(content) { + const triggerLink = document.createElement('a'); + triggerLink.href = window.URL.createObjectURL(content); + triggerLink.setAttribute('download', "csvxml.zip"); + + triggerLink.style.display = 'none'; + + document.body.appendChild(triggerLink); + triggerLink.click(); + document.body.removeChild(triggerLink); + }); - let xmlFiles = validator.generateXml(); - const serializer = new XMLSerializer(); - let lineCounter = 0; - for (let xml of xmlFiles) { - zip.file(lineCounter + ".xml", serializer.serializeToString(xml)); - lineCounter++; } - zip.generateAsync({type:"blob"}) - .then(function(content) { - const triggerLink = document.createElement('a'); - triggerLink.href = window.URL.createObjectURL(content); - triggerLink.setAttribute('download', "csvxml.zip"); + if (typeof JSZip === "undefined") { - triggerLink.style.display = 'none'; + const loadScript = document.createElement("script"); + loadScript.setAttribute("src", "assets/js/jszip/dist/jszip.min.js"); + loadScript.addEventListener('load', function() { + // console.log("Post-loaded OpenLayers"); + runZipping(); + }, {passive: true, once: true}); - document.body.appendChild(triggerLink); - triggerLink.click(); - document.body.removeChild(triggerLink); - }); + document.body.appendChild(loadScript); + } + else { + runZipping(); + } } diff --git a/public/index.php b/public/index.php index db2edde..6704ccf 100644 --- a/public/index.php +++ b/public/index.php @@ -40,8 +40,6 @@ echo ' - -