aboutsummaryrefslogtreecommitdiff
path: root/router.js
blob: a14a30813be8e08f3721d14187ae5763c81ad469 (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
(function () {

  function fetchFile(url, successHandler, errorHandler) {
    var xhr = typeof XMLHttpRequest != 'undefined'
      ? new XMLHttpRequest()
      : new ActiveXObject('Microsoft.XMLHTTP');
    xhr.open('get', url, true);
    xhr.responseType = 'text';
    xhr.onreadystatechange = function() {
      var status;
      var data;
      if (xhr.readyState == 4) {
        status = xhr.status;
        if (status === 200 || status === 0) {
          data = xhr.responseText;
          successHandler && successHandler(data);
        } else {
          errorHandler && errorHandler(status);
        }
      }
    };
    xhr.send();
  };

  function router() {
    var url = location.hash.slice(2) || 'presentation';
    var contentElement = document.getElementById('content');
    fetchFile('Pages/' + url + '.md', function(contentMd) {
      fetchFile('Pages/contact.html', function(contactHtml) {
        var contentHtml = markdown.toHTML(contentMd);
        contentElement.innerHTML = contentHtml + contactHtml;
      });
    });
  }

  this.addEventListener('hashchange', router);
  this.addEventListener('load', router);

})();