]> git.proxmox.com Git - rustc.git/blame - src/rustbook/static/rustbook.js
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / rustbook / static / rustbook.js
CommitLineData
b039eaaf 1// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
c1a9b12d
SL
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
b039eaaf
SL
11/*jslint browser: true, es5: true */
12/*globals $: true, rootPath: true */
c1a9b12d 13
b039eaaf
SL
14document.addEventListener('DOMContentLoaded', function() {
15 'use strict';
c1a9b12d 16
b039eaaf
SL
17 document.getElementById('toggle-nav').onclick = function(e) {
18 var toc = document.getElementById('toc');
19 var pagewrapper = document.getElementById('page-wrapper');
20 toggleClass(toc, 'mobile-hidden');
21 toggleClass(pagewrapper, 'mobile-hidden');
22 };
c1a9b12d 23
b039eaaf
SL
24 function toggleClass(el, className) {
25 // from http://youmightnotneedjquery.com/
26 if (el.classList) {
27 el.classList.toggle(className);
28 } else {
29 var classes = el.className.split(' ');
30 var existingIndex = classes.indexOf(className);
c1a9b12d 31
b039eaaf
SL
32 if (existingIndex >= 0) {
33 classes.splice(existingIndex, 1);
34 } else {
35 classes.push(className);
36 }
c1a9b12d 37
b039eaaf
SL
38 el.className = classes.join(' ');
39 }
40 }
c1a9b12d 41
b039eaaf
SL
42 // The below code is used to add prev and next navigation links to the
43 // bottom of each of the sections.
44 // It works by extracting the current page based on the url and iterates
45 // over the menu links until it finds the menu item for the current page. We
46 // then create a copy of the preceding and following menu links and add the
47 // correct css class and insert them into the bottom of the page.
48 var toc = document.getElementById('toc').getElementsByTagName('a');
49 var href = document.location.pathname.split('/').pop();
c1a9b12d 50
b039eaaf
SL
51 if (href === 'index.html' || href === '') {
52 href = 'README.html';
c1a9b12d 53 }
c1a9b12d 54
b039eaaf
SL
55 for (var i = 0; i < toc.length; i++) {
56 if (toc[i].attributes.href.value.split('/').pop() === href) {
57 var nav = document.createElement('p');
58
59 if (i > 0) {
60 var prevNode = toc[i-1].cloneNode(true);
61 prevNode.className = 'left';
62 prevNode.setAttribute('rel', 'prev');
63 nav.appendChild(prevNode);
64 }
65
66 if (i < toc.length - 1) {
67 var nextNode = toc[i+1].cloneNode(true);
68 nextNode.className = 'right';
69 nextNode.setAttribute('rel', 'next');
70 nav.appendChild(nextNode);
71 }
72
73 document.getElementById('page').appendChild(nav);
74
75 break;
76 }
77 }
c1a9b12d 78});