martiLQ/source/golang/server/static/js/martilq_app.js

79 lines
2.1 KiB
JavaScript

"use strict";
const output = document.querySelector(".output");
var localJsonFile = "data/marti_test_asic.json";
var btn = document.getElementById("loadBtn");
btn.onclick = dataLoadFunction;
function dataLoadFunction() {
var loadDef = document.getElementById("loaddefinition")
if (loadDef.value.startsWith("http://") || loadDef.value.startsWith("https://")) {
fetchData(loadDef.value);
} else {
fetchData("data/"+ loadDef.value);
}
}
var definition_must = "";
var jdata = {};
window.addEventListener("DOMContentLoaded", () => {
var urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('martilq')) {
var loadDef = urlParams.get('martilq');
if (loadDef.startsWith("http://") || loadDef.startsWith("https://")) {
fetchData(loadDef);
} else {
fetchData("data/"+ loadDef);
}
var ld = document.getElementById("loaddefinition")
ld.value = loadDef
} else {
output.innerHTML = "Please supply a MartiLQ definition to load, such as \"<a href=\"?martilq=martilq_asic.json\">martilq_asic.json</a>\"";
}
});
function fetchData(dataFile) {
output.textContent = "Loading....";
fetch(dataFile)
.then((response) => response.json())
.then((data) => {
var template = "data/martilq_def.must";
jdata["item"] = data;
jdata["describe"] = data.description.replace(/\r\n/g, "<br>");
if (data["custom"]) {
data.custom.forEach((el) => {
if (el.extension == "template" && el.renderer == "MartiReference:Mustache") {
template = el.url;
}
});
}
// Use the template
fetchMust(template);
});
}
function fetchMust(mustTemplateFile) {
fetch(mustTemplateFile)
.then((response) => response.text())
.then((data) => {
definition_must = data
const dMust = document.createElement("div");
dMust.innerHTML = Mustache.render(definition_must, jdata);
output.innerHTML = "";
output.append(dMust);
});
}