$( document ).ready(function() { // url var url = window.location.pathname; // Fix back button cache problem window.onunload = function(){}; // Set theme var theme = store.get('theme'); if (theme === null || theme === undefined) { theme = 'light'; } set_theme(theme); // Syntax highlighting Configuration hljs.configure({ tabReplace: ' ', // 4 spaces languages: [], // Languages used for auto-detection }); if (window.ace) { // language-rust class needs to be removed for editable // blocks or highlightjs will capture events $('code.editable').removeClass('language-rust'); $('code').not('.editable').each(function(i, block) { hljs.highlightBlock(block); }); } else { $('code').each(function(i, block) { hljs.highlightBlock(block); }); } // Adding the hljs class gives code blocks the color css // even if highlighting doesn't apply $('code').addClass('hljs'); var KEY_CODES = { PREVIOUS_KEY: 37, NEXT_KEY: 39 }; $(document).on('keydown', function (e) { if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; } switch (e.keyCode) { case KEY_CODES.NEXT_KEY: e.preventDefault(); if($('.nav-chapters.next').length) { window.location.href = $('.nav-chapters.next').attr('href'); } break; case KEY_CODES.PREVIOUS_KEY: e.preventDefault(); if($('.nav-chapters.previous').length) { window.location.href = $('.nav-chapters.previous').attr('href'); } break; } }); // Interesting DOM Elements var sidebar = $("#sidebar"); // Help keyboard navigation by always focusing on page content $(".page").focus(); // Toggle sidebar $("#sidebar-toggle").click(sidebarToggle); // Hide sidebar on section link click if it occupies large space // in relation to the whole screen (phone in portrait) $("#sidebar a").click(function(event){ if (sidebar.width() > window.screen.width * 0.4) { sidebarToggle(); } }); // Scroll sidebar to current active section var activeSection = sidebar.find(".active"); if(activeSection.length) { sidebar.scrollTop(activeSection.offset().top); } // Print button $("#print-button").click(function(){ var printWindow = window.open("print.html"); }); if( url.substring(url.lastIndexOf('/')+1) == "print.html" ) { window.print(); } // Theme button $("#theme-toggle").click(function(){ if($('.theme-popup').length) { $('.theme-popup').remove(); } else { var popup = $('
') .append($('
");
result_block = code_block.find(".result");
}
let text;
let inner_code_block = code_block.find("code").first();
if (window.ace && inner_code_block.hasClass("editable")) {
let editor = window.ace.edit(inner_code_block.get(0));
text = editor.getValue();
} else {
text = inner_code_block.text();
}
var params = {
version: "stable",
optimize: "0",
code: text,
};
if(text.indexOf("#![feature") !== -1) {
params.version = "nightly";
}
result_block.text("Running...");
$.ajax({
url: "https://play.rust-lang.org/evaluate.json",
method: "POST",
crossDomain: true,
dataType: "json",
contentType: "application/json",
data: JSON.stringify(params),
success: function(response){
result_block.text(response.result);
}
});
}