summaryrefslogtreecommitdiff
path: root/js/main.js
blob: d37c2289ba83fcdc74286d95cd03fe039ff4804d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
define(function() {
    'use strict';

    var all_pages = 'home studying curriculum campus career disclaimer'.split(' ');

    var cacheBuster = '';
    if (/dev/.test(location.search)) {
        cacheBuster = '?t=' + (new Date()).getTime();
    }

    /* loads a page into view and execute scripts */
    function loadContent(name, text) {
        var contentElement = document.getElementById('content');
        contentElement.innerHTML = text;
        document.body.dataset.page = name;

        // execute scripts in page
        [].forEach.call(contentElement.getElementsByTagName('script'),
                function(origScript) {
            var script = document.createElement('script');
            // copy attributes
            [].forEach.call(origScript.attributes, function(a) {
                script.setAttribute(a.name, a.value);
            });
            // copy contents
            script.textContent = origScript.textContent;
            origScript.parentNode.replaceChild(script, origScript);
        });
    }

    /* tries to navigate to a page */
    var current_page;
    function selectPage(page, anchor) {
        if (all_pages.indexOf(page) == -1) {
            // TODO: 404
            console.log("404 " + page);
            return false;
        }

        if (current_page === page) {
            return;
        }
        current_page = page;

        console.log("Loading " + page);
        var http = new XMLHttpRequest();
        http.onload = function() {
            loadContent(page, http.responseText);

            // focus element when loaded
            var anchor_id = /^#(.+)/.exec(anchor);
            if (anchor_id) {
                var selection = document.getElementById(anchor_id[1]);
                if (selection) {
                    selection.scrollIntoView();
                }
            }
        };
        http.open('get', 'pages/' + page + '.html' + cacheBuster);
        http.send(null);
        return true;
    }

    function changePageHash(hash) {
        var m = /#\/([^\/]+)/.exec(hash);
        if (m) {
            selectPage(m[1], hash);
        } else {
            location.hash = '#/home';
            selectPage('home', hash);
        }
    }

    addEventListener('hashchange', function(ev) {
        changePageHash(ev.newURL);
    });

    // Tries to load the current page, falling back to "home" for unknown URLs.
    changePageHash(location.hash);
});