summaryrefslogtreecommitdiff
path: root/js/main.js
blob: 5e6816e843f769e9b5ab2207fca8b1a998e76f8f (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
/* jshint browser:true, devel:true */
(function() {
    'use strict';

    /* 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;
    }

    /* tries to navigate to a page */
    function selectPage(page) {
        var pages = 'home studying curriculum campus career'.split(' ');
        if (pages.indexOf(page) == -1) {
            // TODO: 404
            console.log("404 " + page);
            return false;
        }
        console.log("Loading " + page);
        var http = new XMLHttpRequest();
        http.onload = function() {
            loadContent(page, http.responseText);
        };
        http.open('get', 'pages/' + page + '.html');
        http.send(null);
        return true;
    }

    addEventListener('hashchange', function(ev) {
        var m = /#\/(.+)/.exec(ev.newURL);
        if (!m)
            return;
        selectPage(m[1]);
    });

    // Tries to load the current page, falling back to "home" for unknown URLs.
    selectPage(location.hash.replace(/^#\//, '')) || selectPage('home');
})();