aboutsummaryrefslogtreecommitdiff
path: root/router.js
blob: 84c8ab1aa64a612c1e152637c97ec4259239bb25 (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
(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);
        }
      }
    };
    try {
      xhr.send();
    } catch(err) {
      errorHandler && errorHandler(err);
    }
  };

  function router() {
    var url = location.hash.slice(2) || 'presentation';
    var contentElement = document.getElementById('content');
    fetchFile('Pages/' + url + '.md', function(contentMd) {
      contentElement.innerHTML = markdown.toHTML(contentMd);
    }, function() {
      var notFoundPage = '<h1>Page non trouvée</h1><a href="#">Retour à l\'accueil</a>';
      contentElement.innerHTML = notFoundPage;
    });
  }

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

})();