|
| 1 | +$( document ).ready(function() { |
| 2 | + |
| 3 | + // url |
| 4 | + var url = window.location.pathname; |
| 5 | + |
| 6 | + // Fix back button cache problem |
| 7 | + window.onunload = function(){}; |
| 8 | + |
| 9 | + // Set theme |
| 10 | + var theme = localStorage.getItem('theme'); |
| 11 | + if (theme === null) { theme = 'light'; } |
| 12 | + |
| 13 | + set_theme(theme); |
| 14 | + |
| 15 | + |
| 16 | + // Syntax highlighting Configuration |
| 17 | + hljs.configure({ |
| 18 | + tabReplace: ' ', // 4 spaces |
| 19 | + languages: [], // Languages used for auto-detection |
| 20 | + }); |
| 21 | + |
| 22 | + $('code').each(function(i, block) { |
| 23 | + hljs.highlightBlock(block); |
| 24 | + }); |
| 25 | + |
| 26 | + var KEY_CODES = { |
| 27 | + PREVIOUS_KEY: 37, |
| 28 | + NEXT_KEY: 39 |
| 29 | + }; |
| 30 | + |
| 31 | + $(document).on('keydown', function (e) { |
| 32 | + switch (e.keyCode) { |
| 33 | + case KEY_CODES.NEXT_KEY: |
| 34 | + e.preventDefault(); |
| 35 | + if($('.nav-chapters.next').length) { |
| 36 | + window.location.href = $('.nav-chapters.next').attr('href'); |
| 37 | + } |
| 38 | + break; |
| 39 | + case KEY_CODES.PREVIOUS_KEY: |
| 40 | + e.preventDefault(); |
| 41 | + if($('.nav-chapters.previous').length) { |
| 42 | + window.location.href = $('.nav-chapters.previous').attr('href'); |
| 43 | + } |
| 44 | + break; |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + // Interesting DOM Elements |
| 49 | + var html = $("html"); |
| 50 | + var sidebar = $("#sidebar"); |
| 51 | + var page_wrapper = $("#page-wrapper"); |
| 52 | + var content = $("#content"); |
| 53 | + |
| 54 | + |
| 55 | + // Add anchors for all content headers |
| 56 | + content.find("h1, h2, h3, h4, h5").wrap(function(){ |
| 57 | + var wrapper = $("<a class=\"header\">"); |
| 58 | + wrapper.attr("name", $(this).text()); |
| 59 | + // Add so that when you click the link actually shows up in the url bar... |
| 60 | + // Remove any existing anchor then append the new one |
| 61 | + // ensuring eg. no spaces are present within it ie. they become %20 |
| 62 | + wrapper.attr("href", $(location).attr('href').split("#")[0] + "#" + encodeURIComponent($(this).text().trim()) ); |
| 63 | + return wrapper; |
| 64 | + }); |
| 65 | + |
| 66 | + |
| 67 | + // Toggle sidebar |
| 68 | + $("#sidebar-toggle").click(function(event){ |
| 69 | + if ( html.hasClass("sidebar-hidden") ) { |
| 70 | + html.removeClass("sidebar-hidden").addClass("sidebar-visible"); |
| 71 | + localStorage.setItem('sidebar', 'visible'); |
| 72 | + } else if ( html.hasClass("sidebar-visible") ) { |
| 73 | + html.removeClass("sidebar-visible").addClass("sidebar-hidden"); |
| 74 | + localStorage.setItem('sidebar', 'hidden'); |
| 75 | + } else { |
| 76 | + if(sidebar.position().left === 0){ |
| 77 | + html.addClass("sidebar-hidden"); |
| 78 | + localStorage.setItem('sidebar', 'hidden'); |
| 79 | + } else { |
| 80 | + html.addClass("sidebar-visible"); |
| 81 | + localStorage.setItem('sidebar', 'visible'); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + |
| 87 | + // Scroll sidebar to current active section |
| 88 | + var activeSection = sidebar.find(".active"); |
| 89 | + if(activeSection.length) { |
| 90 | + sidebar.scrollTop(activeSection.offset().top); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + // Print button |
| 95 | + $("#print-button").click(function(){ |
| 96 | + var printWindow = window.open("print.html"); |
| 97 | + }); |
| 98 | + |
| 99 | + if( url.substring(url.lastIndexOf('/')+1) == "print.html" ) { |
| 100 | + window.print(); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + // Theme button |
| 105 | + $("#theme-toggle").click(function(){ |
| 106 | + if($('.theme-popup').length) { |
| 107 | + $('.theme-popup').remove(); |
| 108 | + } else { |
| 109 | + var popup = $('<div class="theme-popup"></div>') |
| 110 | + .append($('<div class="theme" id="light">Light <span class="default">(default)</span><div>')) |
| 111 | + .append($('<div class="theme" id="rust">Rust</div>')) |
| 112 | + .append($('<div class="theme" id="coal">Coal</div>')) |
| 113 | + .append($('<div class="theme" id="navy">Navy</div>')); |
| 114 | + |
| 115 | + |
| 116 | + popup.insertAfter(this); |
| 117 | + |
| 118 | + $('.theme').click(function(){ |
| 119 | + var theme = $(this).attr('id'); |
| 120 | + |
| 121 | + set_theme(theme); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + }); |
| 126 | + |
| 127 | + function set_theme(theme) { |
| 128 | + if (theme == 'coal' || theme == 'navy') { |
| 129 | + $("[href='tomorrow-night.css']").prop('disabled', false); |
| 130 | + $("[href='highlight.css']").prop('disabled', true); |
| 131 | + } else { |
| 132 | + $("[href='tomorrow-night.css']").prop('disabled', true); |
| 133 | + $("[href='highlight.css']").prop('disabled', false); |
| 134 | + } |
| 135 | + |
| 136 | + localStorage.setItem('theme', theme); |
| 137 | + |
| 138 | + $('body').removeClass().addClass(theme); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + // Hide Rust code lines prepended with a specific character |
| 143 | + var hiding_character = "#"; |
| 144 | + |
| 145 | + $("code.language-rust").each(function(i, block){ |
| 146 | + |
| 147 | + var code_block = $(this); |
| 148 | + var pre_block = $(this).parent(); |
| 149 | + // hide lines |
| 150 | + var lines = code_block.html().split("\n"); |
| 151 | + var first_non_hidden_line = false; |
| 152 | + var lines_hidden = false; |
| 153 | + |
| 154 | + for(var n = 0; n < lines.length; n++){ |
| 155 | + if($.trim(lines[n])[0] == hiding_character){ |
| 156 | + if(first_non_hidden_line){ |
| 157 | + lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)#/, "$1") + "</span>"; |
| 158 | + } |
| 159 | + else { |
| 160 | + lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)#/, "$1") + "\n" + "</span>"; |
| 161 | + } |
| 162 | + lines_hidden = true; |
| 163 | + } |
| 164 | + else if(first_non_hidden_line) { |
| 165 | + lines[n] = "\n" + lines[n]; |
| 166 | + } |
| 167 | + else { |
| 168 | + first_non_hidden_line = true; |
| 169 | + } |
| 170 | + } |
| 171 | + code_block.html(lines.join("")); |
| 172 | + |
| 173 | + // If no lines were hidden, return |
| 174 | + if(!lines_hidden) { return; } |
| 175 | + |
| 176 | + // add expand button |
| 177 | + pre_block.prepend("<div class=\"buttons\"><i class=\"fa fa-expand\"></i></div>"); |
| 178 | + |
| 179 | + pre_block.find("i").click(function(e){ |
| 180 | + if( $(this).hasClass("fa-expand") ) { |
| 181 | + $(this).removeClass("fa-expand").addClass("fa-compress"); |
| 182 | + pre_block.find("span.hidden").removeClass("hidden").addClass("unhidden"); |
| 183 | + } |
| 184 | + else { |
| 185 | + $(this).removeClass("fa-compress").addClass("fa-expand"); |
| 186 | + pre_block.find("span.unhidden").removeClass("unhidden").addClass("hidden"); |
| 187 | + } |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + |
| 192 | + // Process playpen code blocks |
| 193 | + $(".playpen").each(function(block){ |
| 194 | + var pre_block = $(this); |
| 195 | + // Add play button |
| 196 | + var buttons = pre_block.find(".buttons"); |
| 197 | + if( buttons.length === 0 ) { |
| 198 | + pre_block.prepend("<div class=\"buttons\"></div>"); |
| 199 | + buttons = pre_block.find(".buttons"); |
| 200 | + } |
| 201 | + buttons.prepend("<i class=\"fa fa-play play-button\"></i>"); |
| 202 | + |
| 203 | + buttons.find(".play-button").click(function(e){ |
| 204 | + run_rust_code(pre_block); |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + |
| 209 | +}); |
| 210 | + |
| 211 | + |
| 212 | +function run_rust_code(code_block) { |
| 213 | + var result_block = code_block.find(".result"); |
| 214 | + if(result_block.length === 0) { |
| 215 | + code_block.append("<code class=\"result hljs language-bash\"></code>"); |
| 216 | + result_block = code_block.find(".result"); |
| 217 | + } |
| 218 | + |
| 219 | + result_block.text("Running..."); |
| 220 | + |
| 221 | + $.ajax({ |
| 222 | + url: "https://play.rust-lang.org/evaluate.json", |
| 223 | + method: "POST", |
| 224 | + crossDomain: true, |
| 225 | + dataType: "json", |
| 226 | + contentType: "application/json", |
| 227 | + data: JSON.stringify({version: "stable", optimize: "0", code: code_block.find(".language-rust").text() }), |
| 228 | + success: function(response){ |
| 229 | + result_block.text(response.result); |
| 230 | + } |
| 231 | + }); |
| 232 | +} |
0 commit comments