|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | + |
| 12 | +document.addEventListener("DOMContentLoaded", function(event) { |
| 13 | + |
| 14 | + document.getElementById("toggle-nav").onclick = toggleNav; |
| 15 | + |
| 16 | + function toggleNav() { |
| 17 | + var toc = document.getElementById("toc"); |
| 18 | + var pagewrapper = document.getElementById("page-wrapper"); |
| 19 | + toggleClass(toc, "mobile-hidden"); |
| 20 | + toggleClass(pagewrapper, "mobile-hidden"); |
| 21 | + } |
| 22 | + |
| 23 | + function toggleClass(el, className) { |
| 24 | + // from http://youmightnotneedjquery.com/ |
| 25 | + if (el.classList) { |
| 26 | + el.classList.toggle(className); |
| 27 | + } else { |
| 28 | + var classes = el.className.split(' '); |
| 29 | + var existingIndex = classes.indexOf(className); |
| 30 | + |
| 31 | + if (existingIndex >= 0) { |
| 32 | + classes.splice(existingIndex, 1); |
| 33 | + } else { |
| 34 | + classes.push(className); |
| 35 | + } |
| 36 | + |
| 37 | + el.className = classes.join(' '); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // The below code is used to add prev and next navigation links to the bottom |
| 42 | + // of each of the sections. |
| 43 | + // It works by extracting the current page based on the url and iterates over |
| 44 | + // the menu links until it finds the menu item for the current page. We then |
| 45 | + // create a copy of the preceding and following menu links and add the |
| 46 | + // correct css class and insert them into the bottom of the page. |
| 47 | + var toc = document.getElementById('toc').getElementsByTagName('a'); |
| 48 | + var href = document.location.pathname.split('/').pop(); |
| 49 | + if (href === 'index.html' || href === '') { |
| 50 | + href = 'README.html'; |
| 51 | + } |
| 52 | + |
| 53 | + for (var i = 0; i < toc.length; i++) { |
| 54 | + if (toc[i].attributes.href.value.split('/').pop() === href) { |
| 55 | + var nav = document.createElement('p'); |
| 56 | + if (i > 0) { |
| 57 | + var prevNode = toc[i-1].cloneNode(true); |
| 58 | + prevNode.className = 'left'; |
| 59 | + prevNode.setAttribute('rel', 'prev'); |
| 60 | + nav.appendChild(prevNode); |
| 61 | + } |
| 62 | + if (i < toc.length - 1) { |
| 63 | + var nextNode = toc[i+1].cloneNode(true); |
| 64 | + nextNode.className = 'right'; |
| 65 | + nextNode.setAttribute('rel', 'next'); |
| 66 | + nav.appendChild(nextNode); |
| 67 | + } |
| 68 | + document.getElementById('page').appendChild(nav); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +}); |
0 commit comments