From 5912dfe7f381e0c469f7c9c913a70d9952e5707b Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 25 Apr 2019 13:37:41 +0100 Subject: [PATCH] implemted import file by url --- main.js | 42 ++++++++++++++++++++++++++++++++++------ modules/save-and-load.js | 4 ++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index 59e4ed41..39f00de0 100644 --- a/main.js +++ b/main.js @@ -104,11 +104,15 @@ let svgWidth = graphWidth, svgHeight = graphHeight; // svg canvas resolution, c equatorOutput.min = equatorInput.min = graphHeight * -1; equatorOutput.max = equatorInput.max = graphHeight * 2; +const url = new URL(window.location.href); +const params = url.searchParams; + applyDefaultNamesData(); // apply default namesbase on load applyDefaultStyle(); // apply style on load generate(); // generate map on load focusOn(); // based on searchParams focus on point, cell or burg from MFCG addDragToUpload(); // allow map loading by drag and drop +postload(); // postload data from url if exsists // show message on load if required setTimeout(showWelcomeMessage, 8000); @@ -335,9 +339,6 @@ function applyDefaultStyle() { // focus on coordinates, cell or burg provided in searchParams function focusOn() { - const url = new URL(window.location.href); - const params = url.searchParams; - if (params.get("from") === "MFCG") { if (params.get("seed").length === 13) { // show back burg from MFCG @@ -530,12 +531,43 @@ function addDragToUpload() { } // all good - show uploading text and load the map $("#map-dragged > p").text("Uploading..."); - uploadFile(file, function onUploadFinish() { + uploadFile(file, "UTF-8", function onUploadFinish() { $("#map-dragged > p").text("Drop to upload"); }); }); } +function postload() { + + let success = false; + if(params.get("from")==="GIST"){ + console.time("loadURL"); + console.log("Loading URL"); + fetch(params.get("url")) + .then(res => res.blob()) // Gets the response and returns it as a blob + .then(blob => { + console.log("url loaded"); + let objectURL = URL.createObjectURL(blob); + uploadFile(blob, "base64", function onUploadFinish() { + $("#map-dragged > p").text("Drop to upload"); + }); + console.timeEnd("loadURL"); + }) + .catch(err => { + console.log("url problem"); + console.log(err); + alertMessage.innerHTML = 'Error: ' + err; + $("#alert").dialog({ + resizable: false, title: "Invalid url", + width: 400, buttons: { + Close: function() { $(this).dialog("close"); } + }, position: {my: "center", at: "center", of: "svg"} + }); + console.timeEnd("loadURL"); + }); + } +} + function generate() { console.time("TOTAL"); invokeActiveZooming(); @@ -577,8 +609,6 @@ function generate() { // generate map seed (string!) or get it from URL searchParams function generateSeed() { const first = !mapHistory[0]; - const url = new URL(window.location.href); - const params = url.searchParams; const urlSeed = url.searchParams.get("seed"); if (first && params.get("from") === "MFCG" && urlSeed.length === 13) seed = urlSeed.slice(0,-4); else if (first && urlSeed) seed = urlSeed; diff --git a/modules/save-and-load.js b/modules/save-and-load.js index 6a3a1233..214a1548 100644 --- a/modules/save-and-load.js +++ b/modules/save-and-load.js @@ -183,7 +183,7 @@ function saveMap() { console.timeEnd("saveMap"); } -function uploadFile(file, callback) { +function uploadFile(file, type, callback) { console.time("loadMap"); const fileReader = new FileReader(); fileReader.onload = function(fileLoadedEvent) { @@ -213,7 +213,7 @@ function uploadFile(file, callback) { }}); }; - fileReader.readAsText(file, "UTF-8"); + fileReader.readAsText(file, type); if (callback) callback(); }