<!DOCTYPE html>
<html>
    <head>
        <title>Untitled</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script type="text/javascript" src="jokes.js"></script>
    </head>
    <body style="margin: 0;" onload="Open()">
        
        <div style="line-height: 46px;">
            <input id="file" type="file" accept=".json" style="display: none;"/>
            <button id="load">load</button>
            <button id="save">save</button>
            <button onclick="Hide()">Hide</button>
            <input id="title" type="text" value="Untitled">
        </div>
        <div id="calculator" style="position: absolute; top: 46px; bottom: 0; left: 0; right: 0;"></div>
        <script>            
            window.Calc = Desmos.GraphingCalculator(document.querySelector('#calculator'));
            
            function loadDesmosState(jsonString){
                var state = JSON.parse(jsonString);
                window.Calc.setState(state);
            };
            
            document.querySelector('#file').onchange = function(e){
                let file = e.target.files[0];
                name = file.name.replace(/\.(json|JSON)$/, '');
                document.querySelector('#title').value = name;
                document.querySelector('head>title').text = name;
                let reader = new FileReader();
                reader.onload = function(e){
                    loadDesmosState(e.target.result);
                };
                reader.readAsText(file);
            };
            
            document.querySelector('#load').onclick = function(e){
                document.querySelector('#file').click();
            };
            
            function download(content, fileName, contentType) {
                let a = document.createElement('a');
                a.href = URL.createObjectURL(new Blob([content], {type: contentType}));
                a.download = fileName;
                a.click();
            };
            
            document.querySelector('#save').onclick = function(e){
                let state = JSON.stringify(window.Calc.getState());
                let title = document.querySelector('#title').value;
                download(state, title+'.json', 'text/plain');
            };
        </script>
        <script>
function Open() {
var win = window.open()
var url = "./index.html"
var iframe = win.document.createElement('iframe')
window.location.replace("https://google.com",);
iframe.style.width = "100%";
iframe.style.height = "100%";
iframe.style.border = "none";
iframe.src = url
win.document.body.appendChild(iframe)
}
function Hide() {
    window.location.replace("https://google.com",);
}
</script>

    </body>
</html>