init
This commit is contained in:
commit
f4e4e2eae1
30 changed files with 11452 additions and 0 deletions
81
frontend/index.def.html
Normal file
81
frontend/index.def.html
Normal file
|
@ -0,0 +1,81 @@
|
|||
<!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>
|
Loading…
Add table
Add a link
Reference in a new issue