wahoo 0.1.0

Static site generator based on Tera templating engine
<script>
let session = "";
function init_session(session_id) {
    // const current = localStorage.getItem('session');
}

function current() {
    let file = window.location.pathname.split('/').slice(2).join('/');
    let dot = file.lastIndexOf('.');
    return file.substring(0, dot);
}

function trunk(file, lang) {
    let dot = file.lastIndexOf('.');
    return file.substring(0, dot);
}

function reset_hash() {
    if ('pushState' in history) {
        history.pushState("", document.title, window.location.pathname + window.location.search);
    } else {
        window.location.hash = '';
    }
}

function getScrollElement() {
    return document.getElementById("main");
}

function store_scroll_data() {
    let scroll = getScrollElement().scrollTop;
    localStorage.setItem('scroll', scroll);
    localStorage.setItem('page', window.location.pathname);

    // also in server mode, remove the hash to prevent auto-section scrolling on reload
    reset_hash();
}


function restore_scroll_data() {
    let scroll = localStorage.getItem('scroll');
    let page = localStorage.getItem('page');
    if (scroll && scroll.length > 0) {
        localStorage.removeItem('scroll');
        localStorage.removeItem('page');
        
        if (page == window.location.pathname) {

            reset_hash();

            document.onreadystatechange = () => {
                if (document.readyState === "complete") {
                    setTimeout(() => {
                        console.log("(actual) scrolling main to",scroll);
                        getScrollElement().scrollTop = scroll;
                    }, 0);
                }
            };
        }
    }
}

function reload() {
    reset_hash();
    window.location.reload();
}

addEventListener('beforeunload', (event) => {
        store_scroll_data();
});


let was_open = false;
let ws_connected = false;
function wahoo_connect() {
    let url = window.location.origin.replace("http","ws") + '/wahoo';
    let socket = new WebSocket(url);

    socket.onopen = function(e) {
        console.log("[open] Connection established");
        ws_connected = true;
        if (was_open) {
            reload();
        } else {
            was_open = true;
        }
    };

    socket.onmessage = function(event) {
        // console.log(event.data);
        // return;
        let notification = JSON.parse(event.data);

        switch (notification.method) {
            case "session": {
                init_session(notification.params);
            } break;
            case "update": {
                handle_update_notification(notification.params);
            } break;
        }
    };

    socket.onclose = function(event) {
        ws_connected = false;
        setTimeout(() => {
            wahoo_connect();
        }, 1000);
    };

    socket.onerror = function(error) {
        // console.log(`[error]`);
        socket.close();
    };
}

const ROOT = ["","home","index"];

function handle_update_notification(files) {
    let path = current();
    for (let file of files) {
        let update = trunk(file);
        if (
            path == update 
            || (ROOT.includes(path) && ROOT.includes(update))
            || file.endsWith(".toml")
            || file.endsWith(".css")
            || file.includes("partial")
            || file.includes("menu")
            || file.includes("sections")
        )
        {
            reload();
        } else {
            console.log("ignoring update - current:",path,"update:",update);
        }
    }

}

let last_poll_id = null;
function poll() {
    httpRequest = new XMLHttpRequest();

    if (!httpRequest) {
        alert("Giving up :( Cannot create an XMLHTTP instance");
        return false;
    }

    httpRequest.onreadystatechange = () => {
        if (ws_connected) {
            return;
        }

        if (httpRequest.readyState === XMLHttpRequest.DONE) {
            if (httpRequest.status === 200) {
                let notification = JSON.parse(httpRequest.responseText);
                if (notification.method == "update") {
                    if (notification.id != last_poll_id) {
                        if (last_poll_id != null) {
                            handle_update_notification(notification.params);
                        }
                        last_poll_id = notification.id;
                    }
                }
            }

            setTimeout(() => {
                poll();
            }, 1000);
        }
    };
    httpRequest.open("GET", "/__wahoo.json");
    httpRequest.send();

}

restore_scroll_data();
wahoo_connect();
// poll();
</script>