elmskell/frontend/index.def.html
mtgmonkey f4e4e2eae1 init
2025-04-24 22:32:10 -04:00

81 lines
2.1 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf8">
<title>Main</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000000;
}
</style>
<script src="main.js"></script>
</head>
<body>
<script>
var app;
// call main with data from ./init.json and initialisation function
callWithServerData('./init.json', main, init)
// FUNCTIONS
// takes pertinent data and returns an app
// serverData : Object
// cookies : Object
function init(serverData, cookies) {
const flags = {...serverData, ...cookies};
app = Elm.Main.init({
flags: flags
});
app.ports.setStorage.subscribe(setStorage);
app.ports.refresh.subscribe(main);
}
// main 'loop'
// serverData : Object
// init : Function
function main(serverData, init) {
const cookies = localStorage.getItem('cookies') ? JSON.parse(localStorage.getItem('cookies')) : "";
init(serverData, cookies);
// XXX send test server data to getTestFromServer
// callWithServerData('./test.json', app.ports.getTestFromServer.send);
}
// passes the content of json at path to the function toCall
// path : String
// toCall : Function
function callWithServerData(path, toCall, ...args) {
fetch(path)
.then(response => response.json())
.then(data => {
if (toCall === main) {
toCall(data, args[0]);
} else {
toCall(data, args);
}
})
.catch(err => {
console.error('Err ', err.message);
const data = "";
if (toCall === main) {
toCall(data, args[0]);
} else {
toCall(data, args);
}
});
}
// sets localStorage cookies to passed cookies value
// cookies : Object
function setStorage(cookies) {
localStorage.setItem('cookies', JSON.stringify(cookies));
}
</script>
</body>
</html>