implemted import file by url

This commit is contained in:
Lucas 2019-04-25 13:37:41 +01:00
parent 9aa369b6df
commit 5912dfe7f3
2 changed files with 38 additions and 8 deletions

42
main.js
View file

@ -104,11 +104,15 @@ let svgWidth = graphWidth, svgHeight = graphHeight; // svg canvas resolution, c
equatorOutput.min = equatorInput.min = graphHeight * -1; equatorOutput.min = equatorInput.min = graphHeight * -1;
equatorOutput.max = equatorInput.max = graphHeight * 2; equatorOutput.max = equatorInput.max = graphHeight * 2;
const url = new URL(window.location.href);
const params = url.searchParams;
applyDefaultNamesData(); // apply default namesbase on load applyDefaultNamesData(); // apply default namesbase on load
applyDefaultStyle(); // apply style on load applyDefaultStyle(); // apply style on load
generate(); // generate map on load generate(); // generate map on load
focusOn(); // based on searchParams focus on point, cell or burg from MFCG focusOn(); // based on searchParams focus on point, cell or burg from MFCG
addDragToUpload(); // allow map loading by drag and drop addDragToUpload(); // allow map loading by drag and drop
postload(); // postload data from url if exsists
// show message on load if required // show message on load if required
setTimeout(showWelcomeMessage, 8000); setTimeout(showWelcomeMessage, 8000);
@ -335,9 +339,6 @@ function applyDefaultStyle() {
// focus on coordinates, cell or burg provided in searchParams // focus on coordinates, cell or burg provided in searchParams
function focusOn() { function focusOn() {
const url = new URL(window.location.href);
const params = url.searchParams;
if (params.get("from") === "MFCG") { if (params.get("from") === "MFCG") {
if (params.get("seed").length === 13) { if (params.get("seed").length === 13) {
// show back burg from MFCG // show back burg from MFCG
@ -530,12 +531,43 @@ function addDragToUpload() {
} }
// all good - show uploading text and load the map // all good - show uploading text and load the map
$("#map-dragged > p").text("Uploading<span>.</span><span>.</span><span>.</span>"); $("#map-dragged > p").text("Uploading<span>.</span><span>.</span><span>.</span>");
uploadFile(file, function onUploadFinish() { uploadFile(file, "UTF-8", function onUploadFinish() {
$("#map-dragged > p").text("Drop to upload"); $("#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() { function generate() {
console.time("TOTAL"); console.time("TOTAL");
invokeActiveZooming(); invokeActiveZooming();
@ -577,8 +609,6 @@ function generate() {
// generate map seed (string!) or get it from URL searchParams // generate map seed (string!) or get it from URL searchParams
function generateSeed() { function generateSeed() {
const first = !mapHistory[0]; const first = !mapHistory[0];
const url = new URL(window.location.href);
const params = url.searchParams;
const urlSeed = url.searchParams.get("seed"); const urlSeed = url.searchParams.get("seed");
if (first && params.get("from") === "MFCG" && urlSeed.length === 13) seed = urlSeed.slice(0,-4); if (first && params.get("from") === "MFCG" && urlSeed.length === 13) seed = urlSeed.slice(0,-4);
else if (first && urlSeed) seed = urlSeed; else if (first && urlSeed) seed = urlSeed;

View file

@ -183,7 +183,7 @@ function saveMap() {
console.timeEnd("saveMap"); console.timeEnd("saveMap");
} }
function uploadFile(file, callback) { function uploadFile(file, type, callback) {
console.time("loadMap"); console.time("loadMap");
const fileReader = new FileReader(); const fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent) { fileReader.onload = function(fileLoadedEvent) {
@ -213,7 +213,7 @@ function uploadFile(file, callback) {
}}); }});
}; };
fileReader.readAsText(file, "UTF-8"); fileReader.readAsText(file, type);
if (callback) callback(); if (callback) callback();
} }