Show message after importing biomes
This commit is contained in:
parent
9713603cd5
commit
ec7a07da11
@ -145,11 +145,13 @@ A Voronoi diagram is supposed to be here but for some reason it cannot be displa
|
||||
<h2 class="configHeader"><span class="collapser" id="importHeaderLink">▼</span> Import</h2>
|
||||
<div class="configFrame" id="importContainer">
|
||||
<form id="importForm">
|
||||
<p><b>WARNING</b>: Importing will replace all the biomes!</p>
|
||||
<label for="inputImport">Put JSON text here:</label>
|
||||
<br>
|
||||
<textarea id="inputImport" type="text"></textarea>
|
||||
<br>
|
||||
<button id="inputImportSubmit" type="button">Import</button>
|
||||
<div id="importResultOuter" hidden><br><div id="importResultMessage"></div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
45
mibpov.js
45
mibpov.js
@ -1709,9 +1709,14 @@ inputExportClear.onclick = function() {
|
||||
}
|
||||
/* Import */
|
||||
|
||||
// TODO: Make import cleaner, error checking, messages
|
||||
inputImportSubmit.onclick = function() {
|
||||
let importMessage = function(message) {
|
||||
importResultOuter.hidden = false;
|
||||
importResultMessage.innerText = message;
|
||||
}
|
||||
let importStr = inputImport.value;
|
||||
|
||||
// Do the JSON parse
|
||||
let reviver = function(key, value) {
|
||||
if (key === "name" && ((typeof value) === "string")) {
|
||||
return value;
|
||||
@ -1725,15 +1730,45 @@ inputImportSubmit.onclick = function() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
let parsedJSON = JSON.parse(importStr, reviver);
|
||||
|
||||
let parsedJSON;
|
||||
try {
|
||||
parsedJSON = JSON.parse(importStr, reviver);
|
||||
} catch {
|
||||
importMessage("Import failed. Not a valid JSON object.");
|
||||
}
|
||||
if (typeof parsedJSON !== "object") {
|
||||
importMessage("Import failed. Not a valid JSON object.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Populate the temporary newPoints that MAY
|
||||
// set the biomePoints if successful
|
||||
let newPoints = [];
|
||||
lastBiomeID = 0;
|
||||
let fieldsToCheck = [
|
||||
{ fieldName: "name", type: "string" },
|
||||
{ fieldName: "heat_point", type: "number" },
|
||||
{ fieldName: "humidity_point", type: "number" },
|
||||
{ fieldName: "y_min", type: "number" },
|
||||
{ fieldName: "y_max", type: "number" },
|
||||
]
|
||||
for (let p=0; p<parsedJSON.length; p++) {
|
||||
let parsedPoint = parsedJSON[p];
|
||||
// Type checking
|
||||
for (let f=0; f<fieldsToCheck.length; f++) {
|
||||
let field = fieldsToCheck[f].fieldName;
|
||||
let wantType = fieldsToCheck[f].type;
|
||||
let gotType = typeof parsedPoint[field];
|
||||
if (gotType === "undefined") {
|
||||
importMessage(`Import failed. attribute "${field}" of biome #${p} is undefined.`)
|
||||
return;
|
||||
} else if (gotType !== wantType) {
|
||||
importMessage(`Import failed. attribute "${field}" of biome #${p} is of type "${gotType}" but "${wantType}" expected.`)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let newPoint = {
|
||||
id: lastBiomeID,
|
||||
name: parsedPoint.name,
|
||||
@ -1746,10 +1781,16 @@ inputImportSubmit.onclick = function() {
|
||||
lastBiomeID++;
|
||||
newPoints.push(newPoint);
|
||||
}
|
||||
// Replace the biomes
|
||||
biomePoints = newPoints;
|
||||
repopulateBiomeSelector();
|
||||
updateWidgetStates();
|
||||
draw(true);
|
||||
if (biomePoints.length === 1) {
|
||||
importMessage("Import successful. 1 biome imported.");
|
||||
} else {
|
||||
importMessage(`Import successful. ${biomePoints.length} biomes imported.`);
|
||||
}
|
||||
}
|
||||
|
||||
/* Events for collapsing/extending config section with the arrow thingie */
|
||||
|
Loading…
x
Reference in New Issue
Block a user