main.js 5.74 KB
Newer Older
1
function initSidebar() {
2
  var filename = document.location.pathname;
3

4 5 6 7
  if (filename.slice(0, 5) == "/next") {
    filename = filename.slice(5);
  }

8 9 10 11 12 13 14 15 16 17 18
  if (filename == "/") {
    filename = "/index.html";
  } else if (filename.slice(0, 6) == "/news/") {
    filename = "/news/";
  }

  var menu = document.getElementById("menu");
  var setMenuLayout = function() {
    if (window.innerWidth < 900) {
      document.body.className = "narrow";
      menu.className = "";
19
      document.getElementById("main_content").style.minHeight = "0";
20 21 22 23 24 25 26
    } else {
      if (document.body.clientWidth < 1340) {
        document.body.className = "normal";
      } else {
        document.body.className = "wide";
      }

27 28 29
      var y = (window.pageYOffset !== undefined) ? window.pageYOffset :
        (document.documentElement || document.body.parentNode || document.body).scrollTop;

30
      if (y < 444 || window.innerHeight < menu.clientHeight + 100) {
31 32 33 34
        menu.className = "";
      } else {
        menu.className = "floating";
      }
35 36 37 38

      setTimeout(function () {
        document.getElementById("main_content").style.minHeight = menu.clientHeight + 100 + "px";
      }, 10);
39 40 41 42 43 44 45 46 47 48 49 50 51
    }
  };
  setMenuLayout();
  window.onresize = setMenuLayout;
  window.onscroll = setMenuLayout;

  var items = menu.getElementsByTagName("li");
  var toc = null;
  for (var i = 0; i < items.length; i++) {
    var link = items[i].getElementsByTagName("a")[0];
    var href = link.href;
    if (href.lastIndexOf(filename) >= 0) {
      var parent = link.parentNode;
52 53 54 55

      while (link.childNodes.length > 0) {
        var child = link.childNodes[0];
        link.removeChild(child);
56
        parent.appendChild(child);
57 58
      }
      parent.removeChild(link);
59 60 61 62 63 64 65 66 67 68 69
      items[i].className = "selected";
      toc = document.createElement("ul");
      toc.id = "toc";
      items[i].appendChild(toc);
    }
  }

  return toc;
}

function setupSidebar() {
70 71 72 73 74
  if (window.CAPNP_NEWS_SIDEBAR) {
    setupNewsSidebar(CAPNP_NEWS_SIDEBAR);
    return;
  }

75 76 77 78 79 80 81
  var filename = document.location.pathname;

  if (filename.slice(0, 5) == "/next") {
    filename = filename.slice(5);
  }

  var isNews = filename.slice(0, 6) == "/news/";
Kenton Varda's avatar
Kenton Varda committed
82

83 84 85 86 87 88 89
  var toc = initSidebar();
  if (toc) {
    var content = document.getElementById("main_content").childNodes;
    var headings = [];

    for (var i = 0; i < content.length; i++) {
      if (content[i].tagName == "H2" ||
Kenton Varda's avatar
Kenton Varda committed
90
          (!isNews && (content[i].tagName == "H3" || content[i].tagName == "H4"))) {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        headings.push(content[i]);
      }
    }

    var levels = [toc];
    for (var i in headings) {
      var hl = headings[i].tagName.slice(1) - 1;
      while (hl > levels.length) {
        var parent = levels[levels.length - 1];
        var item = parent.childNodes[parent.childNodes.length - 1];
        var sublist = document.createElement("ul");
        item.appendChild(sublist);
        levels.push(sublist);
      }
      while (hl < levels.length) {
        levels.pop();
      }

      var parent = levels[levels.length - 1];
      var item = document.createElement("li");
      var p = document.createElement("p");
      var link = document.createElement("a");
113
      p.appendChild(document.createTextNode(headings[i].innerText || headings[i].textContent));
114 115 116 117 118 119
      var hlinks = headings[i].getElementsByTagName("a");
      if (hlinks.length == 1) {
        link.href = hlinks[0].href;
      } else {
        link.href = "#" + headings[i].id;
      }
120 121
      link.appendChild(p);
      item.appendChild(link);
122 123 124 125 126 127 128 129 130 131 132 133
      parent.appendChild(item);
    }
  }
}

function setupNewsSidebar(items) {
  var toc = initSidebar();
  if (toc) {
    for (var i in items) {
      var item = document.createElement("li");
      var p = document.createElement("p");
      var link = document.createElement("a");
134
      p.appendChild(document.createTextNode(items[i].title));
135
      link.href = items[i].url;
136 137
      link.appendChild(p);
      item.appendChild(link);
138 139 140 141
      toc.appendChild(item);
    }
  }
}
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

function setupSlides() {
  var slides = document.querySelectorAll("body.slides main section");
  var headerTitle = document.querySelector("body.slides header .title");
  var slideNum = document.querySelector("#slide-num");

  var current = 0;
  var hash = document.location.hash;
  if (hash) {
    current = parseInt(hash.slice(1)) - 1;
  }
  slides[current].className = "current";
  headerTitle.textContent = slides[current].dataset.title || "";
  slideNum.textContent = window.location.hash;

  function navSlide(diff) {
    slides[current].className = "";
    current = Math.min(slides.length - 1, Math.max(0, current + diff));
    slides[current].className = "current";

    headerTitle.textContent = slides[current].dataset.title || "";
    if (current) {
      history.replaceState({}, "", "#" + (current + 1));
      slideNum.textContent = "#" + (current + 1);
    } else {
      history.replaceState({}, "", window.location.pathname);
      slideNum.textContent = "";
    }
  }

  document.body.addEventListener("keydown", event => {
    if (event.keyCode == 39) {
      navSlide(1);
    } else if (event.keyCode == 37) {
      navSlide(-1);
    }
  });

  document.querySelector("body.slides footer button.back").addEventListener("click", event => {
    navSlide(-1);
  });
  document.querySelector("body.slides footer button.forward").addEventListener("click", event => {
    navSlide(1);
  });

  if (document.location.hostname === "localhost") {
    var lastModified = new Date(document.lastModified);
    setInterval(function () {
      var req = new Request(".", {headers: {
          "If-Modified-Since": lastModified.toUTCString()}});
      fetch(req).then(response => {
        if (response.status == 200 &&
            new Date(response.headers.get("Last-Modified")) > lastModified) {
          document.location.reload();
        }
      });
    }, 1000);
  }
}