]> git.lizzy.rs Git - rust.git/blob - src/rustbook/javascript.rs
rollup merge of #21151: brson/beta
[rust.git] / src / rustbook / javascript.rs
1 // Copyright 2015 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 // The rust-book JavaScript in string form.
12
13 pub static JAVASCRIPT: &'static str = r#"
14 <script type="text/javascript">
15 document.addEventListener("DOMContentLoaded", function(event) {
16   document.getElementById("toggle-nav").onclick = toggleNav;
17   function toggleNav() {
18     var toc = document.getElementById("toc");
19     var pagewrapper = document.getElementById("page-wrapper");
20     toggleClass(toc, "mobile-hidden");
21     toggleClass(pagewrapper, "mobile-hidden");
22   };
23
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);
31
32        if (existingIndex >= 0) {
33          classes.splice(existingIndex, 1);
34        } else {
35          classes.push(className);
36        }
37
38        el.className = classes.join(' ');
39      }
40   }
41 });
42 </script>
43 "#;