mdBook/src/theme/book.js

530 lines
18 KiB
JavaScript
Raw Normal View History

var html, sidebar, sidebarLinks, sidebarToggleButton, themeToggleButton, themePopup;
2018-01-13 02:44:13 +08:00
document.addEventListener('DOMContentLoaded', function() {
// url
var url = window.location.pathname;
2015-09-25 04:32:41 +08:00
// Fix back button cache problem
window.onunload = function(){};
// Set theme
var theme = store.get('mdbook-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
});
2017-06-29 12:35:20 +08:00
if (window.ace) {
// language-rust class needs to be removed for editable
// blocks or highlightjs will capture events
2018-01-13 02:44:13 +08:00
Array
.from(document.querySelectorAll('code.editable'))
.forEach(function(block) { block.classList.remove('language-rust'); });
2018-01-13 02:44:13 +08:00
Array
.from(document.querySelectorAll('code:not(.editable)'))
.forEach(function(block) { hljs.highlightBlock(block); });
2017-06-29 12:35:20 +08:00
} else {
2018-01-13 02:44:13 +08:00
Array
.from(document.querySelectorAll('code'))
.forEach(function(block) { hljs.highlightBlock(block); });
2017-06-29 12:35:20 +08:00
}
// Adding the hljs class gives code blocks the color css
// even if highlighting doesn't apply
2018-01-13 02:44:13 +08:00
Array
.from(document.querySelectorAll('code'))
.forEach(function(block) { block.classList.add('hljs'); });
2015-12-30 23:30:08 +08:00
var KEY_CODES = {
PREVIOUS_KEY: 37,
Improve accessibility (#535) * fix(theme/index): Use nav element for Table of Content * fix(renderer/html_handlebars/helpers/toc): Use ol instead of ul Chapters and sections are ordered, so we should use the appropriate HTML tag. * fix(renderer/html_handlebars/helpers/toc): Hide section number from screen readers Screen readers have this functionality build-in, no need to present this. Ideally, this should not even be in the DOM tree, since the numbers can be shown by using CSS. * fix(theme/index): Remove tabIndex="-1" from .page Divs are not focusable by default * fix(theme): Make sidebar accessible Using aria-hidden (together with tabIndex) takes the links out of the tab order. http://heydonworks.com/practical_aria_examples/#progressive-collapsibles * fix(theme/index): Wrap content inside main tag The main tag helps users skip additional content on the page. * fix(theme/book): Don't focus .page on page load The main content is identified by the main tag, not by auto-focusing it on page load. * fix(theme/index): Make page controls accessible * fix: Make theme selector accessible - Use ul and li (since it is a list) - Add aria-expanded and aria-haspopup to the toggle button - Use button instead of div (buttons are accessible by default) - Handle Esc key (close popup) - Adjust CSS to keep same visual style * fix(theme/stylus/sidebar): Make link clickable area wider Links now expand to fill the entire row. * fix(theme): Wrap header buttons and improve animation performance Previously, the header had a fixed height, which meant that sometimes the print button was not visible. Animating the left property is expensive, which lead to laggy animations - transform is much cheaper and has the same effect. * fix(theme/stylus/theme-popup): Theme button inherits color Bug introduced while making the popup accessible * fix(theme/book): Handle edge case when toggling sidebar Bug introduced when switching from animating left to using transform.
2018-01-15 21:26:53 +08:00
NEXT_KEY: 39,
ESCAPE_KEY: 27,
2015-12-30 23:30:08 +08:00
};
2018-01-13 02:44:13 +08:00
document.addEventListener('keydown', function (e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
2015-12-30 23:30:08 +08:00
switch (e.keyCode) {
case KEY_CODES.NEXT_KEY:
e.preventDefault();
2018-01-13 02:44:13 +08:00
var nextButton = document.querySelector('.nav-chapters.next');
if(nextButton) {
window.location.href = nextButton.href;
}
2015-12-30 23:30:08 +08:00
break;
case KEY_CODES.PREVIOUS_KEY:
e.preventDefault();
2018-01-13 02:44:13 +08:00
var previousButton = document.querySelector('.nav-chapters.previous');
if(previousButton) {
window.location.href = previousButton.href;
}
2015-12-30 23:30:08 +08:00
break;
Improve accessibility (#535) * fix(theme/index): Use nav element for Table of Content * fix(renderer/html_handlebars/helpers/toc): Use ol instead of ul Chapters and sections are ordered, so we should use the appropriate HTML tag. * fix(renderer/html_handlebars/helpers/toc): Hide section number from screen readers Screen readers have this functionality build-in, no need to present this. Ideally, this should not even be in the DOM tree, since the numbers can be shown by using CSS. * fix(theme/index): Remove tabIndex="-1" from .page Divs are not focusable by default * fix(theme): Make sidebar accessible Using aria-hidden (together with tabIndex) takes the links out of the tab order. http://heydonworks.com/practical_aria_examples/#progressive-collapsibles * fix(theme/index): Wrap content inside main tag The main tag helps users skip additional content on the page. * fix(theme/book): Don't focus .page on page load The main content is identified by the main tag, not by auto-focusing it on page load. * fix(theme/index): Make page controls accessible * fix: Make theme selector accessible - Use ul and li (since it is a list) - Add aria-expanded and aria-haspopup to the toggle button - Use button instead of div (buttons are accessible by default) - Handle Esc key (close popup) - Adjust CSS to keep same visual style * fix(theme/stylus/sidebar): Make link clickable area wider Links now expand to fill the entire row. * fix(theme): Wrap header buttons and improve animation performance Previously, the header had a fixed height, which meant that sometimes the print button was not visible. Animating the left property is expensive, which lead to laggy animations - transform is much cheaper and has the same effect. * fix(theme/stylus/theme-popup): Theme button inherits color Bug introduced while making the popup accessible * fix(theme/book): Handle edge case when toggling sidebar Bug introduced when switching from animating left to using transform.
2018-01-15 21:26:53 +08:00
case KEY_CODES.ESCAPE_KEY:
e.preventDefault();
hideThemes();
break;
2015-12-30 23:30:08 +08:00
}
});
// Interesting DOM Elements
2018-01-13 02:44:13 +08:00
html = document.querySelector('html');
sidebar = document.getElementById("sidebar");
sidebarLinks = document.querySelectorAll('#sidebar a');
sidebarToggleButton = document.getElementById("sidebar-toggle");
themeToggleButton = document.getElementById('theme-toggle');
themePopup = document.getElementById('theme-list');
// Toggle sidebar
2018-01-13 02:44:13 +08:00
sidebarToggleButton.addEventListener('click', sidebarToggle);
// Scroll sidebar to current active section
2018-01-13 02:44:13 +08:00
var activeSection = sidebar.querySelector(".active");
if(activeSection) {
sidebar.scrollTop = activeSection.offsetTop;
}
var firstContact = null;
2018-01-13 02:44:13 +08:00
document.addEventListener('touchstart', function(e) {
firstContact = {
2018-01-13 02:44:13 +08:00
x: e.touches[0].clientX,
time: Date.now()
};
});
2018-01-13 02:44:13 +08:00
document.addEventListener('touchmove', function(e) {
if (!firstContact)
return;
2018-01-13 02:44:13 +08:00
var curX = e.touches[0].clientX;
var xDiff = curX - firstContact.x,
tDiff = Date.now() - firstContact.time;
if (tDiff < 250 && Math.abs(xDiff) >= 150) {
if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300))
showSidebar();
else if (xDiff < 0 && curX < 300)
hideSidebar();
firstContact = null;
}
});
2015-09-11 07:16:29 +08:00
// Theme button
2018-01-13 02:44:13 +08:00
themeToggleButton.addEventListener('click', function(){
if (themePopup.style.display === 'block') {
Improve accessibility (#535) * fix(theme/index): Use nav element for Table of Content * fix(renderer/html_handlebars/helpers/toc): Use ol instead of ul Chapters and sections are ordered, so we should use the appropriate HTML tag. * fix(renderer/html_handlebars/helpers/toc): Hide section number from screen readers Screen readers have this functionality build-in, no need to present this. Ideally, this should not even be in the DOM tree, since the numbers can be shown by using CSS. * fix(theme/index): Remove tabIndex="-1" from .page Divs are not focusable by default * fix(theme): Make sidebar accessible Using aria-hidden (together with tabIndex) takes the links out of the tab order. http://heydonworks.com/practical_aria_examples/#progressive-collapsibles * fix(theme/index): Wrap content inside main tag The main tag helps users skip additional content on the page. * fix(theme/book): Don't focus .page on page load The main content is identified by the main tag, not by auto-focusing it on page load. * fix(theme/index): Make page controls accessible * fix: Make theme selector accessible - Use ul and li (since it is a list) - Add aria-expanded and aria-haspopup to the toggle button - Use button instead of div (buttons are accessible by default) - Handle Esc key (close popup) - Adjust CSS to keep same visual style * fix(theme/stylus/sidebar): Make link clickable area wider Links now expand to fill the entire row. * fix(theme): Wrap header buttons and improve animation performance Previously, the header had a fixed height, which meant that sometimes the print button was not visible. Animating the left property is expensive, which lead to laggy animations - transform is much cheaper and has the same effect. * fix(theme/stylus/theme-popup): Theme button inherits color Bug introduced while making the popup accessible * fix(theme/book): Handle edge case when toggling sidebar Bug introduced when switching from animating left to using transform.
2018-01-15 21:26:53 +08:00
hideThemes();
2015-09-11 07:16:29 +08:00
} else {
Improve accessibility (#535) * fix(theme/index): Use nav element for Table of Content * fix(renderer/html_handlebars/helpers/toc): Use ol instead of ul Chapters and sections are ordered, so we should use the appropriate HTML tag. * fix(renderer/html_handlebars/helpers/toc): Hide section number from screen readers Screen readers have this functionality build-in, no need to present this. Ideally, this should not even be in the DOM tree, since the numbers can be shown by using CSS. * fix(theme/index): Remove tabIndex="-1" from .page Divs are not focusable by default * fix(theme): Make sidebar accessible Using aria-hidden (together with tabIndex) takes the links out of the tab order. http://heydonworks.com/practical_aria_examples/#progressive-collapsibles * fix(theme/index): Wrap content inside main tag The main tag helps users skip additional content on the page. * fix(theme/book): Don't focus .page on page load The main content is identified by the main tag, not by auto-focusing it on page load. * fix(theme/index): Make page controls accessible * fix: Make theme selector accessible - Use ul and li (since it is a list) - Add aria-expanded and aria-haspopup to the toggle button - Use button instead of div (buttons are accessible by default) - Handle Esc key (close popup) - Adjust CSS to keep same visual style * fix(theme/stylus/sidebar): Make link clickable area wider Links now expand to fill the entire row. * fix(theme): Wrap header buttons and improve animation performance Previously, the header had a fixed height, which meant that sometimes the print button was not visible. Animating the left property is expensive, which lead to laggy animations - transform is much cheaper and has the same effect. * fix(theme/stylus/theme-popup): Theme button inherits color Bug introduced while making the popup accessible * fix(theme/book): Handle edge case when toggling sidebar Bug introduced when switching from animating left to using transform.
2018-01-15 21:26:53 +08:00
showThemes();
2015-09-11 07:16:29 +08:00
}
});
2015-09-11 07:16:29 +08:00
2018-01-13 02:44:13 +08:00
themePopup.addEventListener('click', function(e){
var theme = e.target.id || e.target.parentElement.id;
Improve accessibility (#535) * fix(theme/index): Use nav element for Table of Content * fix(renderer/html_handlebars/helpers/toc): Use ol instead of ul Chapters and sections are ordered, so we should use the appropriate HTML tag. * fix(renderer/html_handlebars/helpers/toc): Hide section number from screen readers Screen readers have this functionality build-in, no need to present this. Ideally, this should not even be in the DOM tree, since the numbers can be shown by using CSS. * fix(theme/index): Remove tabIndex="-1" from .page Divs are not focusable by default * fix(theme): Make sidebar accessible Using aria-hidden (together with tabIndex) takes the links out of the tab order. http://heydonworks.com/practical_aria_examples/#progressive-collapsibles * fix(theme/index): Wrap content inside main tag The main tag helps users skip additional content on the page. * fix(theme/book): Don't focus .page on page load The main content is identified by the main tag, not by auto-focusing it on page load. * fix(theme/index): Make page controls accessible * fix: Make theme selector accessible - Use ul and li (since it is a list) - Add aria-expanded and aria-haspopup to the toggle button - Use button instead of div (buttons are accessible by default) - Handle Esc key (close popup) - Adjust CSS to keep same visual style * fix(theme/stylus/sidebar): Make link clickable area wider Links now expand to fill the entire row. * fix(theme): Wrap header buttons and improve animation performance Previously, the header had a fixed height, which meant that sometimes the print button was not visible. Animating the left property is expensive, which lead to laggy animations - transform is much cheaper and has the same effect. * fix(theme/stylus/theme-popup): Theme button inherits color Bug introduced while making the popup accessible * fix(theme/book): Handle edge case when toggling sidebar Bug introduced when switching from animating left to using transform.
2018-01-15 21:26:53 +08:00
set_theme(theme);
});
// Hide theme selector popup when clicking outside of it
2018-01-13 02:44:13 +08:00
document.addEventListener('click', function(event){
if(themePopup.style.display === 'block' && !themeToggleButton.contains(event.target) && !themePopup.contains(event.target)) {
hideThemes();
}
2015-09-11 07:16:29 +08:00
});
function set_theme(theme) {
2017-06-29 12:35:20 +08:00
let ace_theme;
if (theme == 'coal' || theme == 'navy') {
document.querySelector("[href='ayu-highlight.css']").disabled = true;
document.querySelector("[href='tomorrow-night.css']").disabled = false;
document.querySelector("[href='highlight.css']").disabled = true;
2017-06-29 12:35:20 +08:00
ace_theme = "ace/theme/tomorrow_night";
} else if (theme == 'ayu') {
document.querySelector("[href='ayu-highlight.css']").disabled = false;
document.querySelector("[href='tomorrow-night.css']").disabled = true;
document.querySelector("[href='highlight.css']").disabled = true;
2017-06-29 12:35:20 +08:00
ace_theme = "ace/theme/tomorrow_night";
} else {
document.querySelector("[href='ayu-highlight.css']").disabled = true;
document.querySelector("[href='tomorrow-night.css']").disabled = true;
document.querySelector("[href='highlight.css']").disabled = false;
2017-06-29 12:35:20 +08:00
ace_theme = "ace/theme/dawn";
}
setTimeout(function() {
document.querySelector('meta[name="theme-color"]').content = getComputedStyle(document.body).backgroundColor;
}, 1);
2017-06-29 12:35:20 +08:00
if (window.ace && window.editors) {
window.editors.forEach(function(editor) {
editor.setTheme(ace_theme);
});
}
store.set('mdbook-theme', theme);
2018-01-13 02:44:13 +08:00
document.body.className = theme;
}
// Hide Rust code lines prepended with a specific character
var hiding_character = "#";
2018-01-13 02:44:13 +08:00
Array.from(document.querySelectorAll("code.language-rust")).forEach(function(block){
2018-01-13 02:44:13 +08:00
var code_block = block;
var pre_block = block.parentNode;
// hide lines
2018-01-13 02:44:13 +08:00
var lines = code_block.innerHTML.split("\n");
2015-12-29 19:26:32 +08:00
var first_non_hidden_line = false;
var lines_hidden = false;
for(var n = 0; n < lines.length; n++){
2018-01-13 02:44:13 +08:00
if(lines[n].trim()[0] == hiding_character){
if(first_non_hidden_line){
lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)# ?/, "$1") + "</span>";
}
else {
lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)# ?/, "$1") + "\n" + "</span>";
}
lines_hidden = true;
2015-12-29 19:26:32 +08:00
}
else if(first_non_hidden_line) {
lines[n] = "\n" + lines[n];
}
else {
first_non_hidden_line = true;
}
}
2018-01-13 02:44:13 +08:00
code_block.innerHTML = lines.join("");
// If no lines were hidden, return
if(!lines_hidden) { return; }
2018-01-13 02:44:13 +08:00
var buttons = document.createElement('div');
buttons.className = 'buttons';
buttons.innerHTML = "<i class=\"fa fa-expand\" title=\"Show hidden lines\"></i>";
2018-01-13 02:44:13 +08:00
// add expand button
pre_block.prepend(buttons);
pre_block.querySelector('.buttons').addEventListener('click', function(e) {
if (e.target.classList.contains('fa-expand')) {
var lines = pre_block.querySelectorAll('span.hidden');
e.target.classList.remove('fa-expand');
e.target.classList.add('fa-compress');
e.target.title = 'Hide lines';
Array.from(lines).forEach(function(line) {
line.classList.remove('hidden');
line.classList.add('unhidden');
});
} else if (e.target.classList.contains('fa-compress')) {
var lines = pre_block.querySelectorAll('span.unhidden');
e.target.classList.remove('fa-compress');
e.target.classList.add('fa-expand');
e.target.title = 'Show hidden lines';
Array.from(lines).forEach(function(line) {
line.classList.remove('unhidden');
line.classList.add('hidden');
});
}
});
});
2018-01-13 02:44:13 +08:00
Array.from(document.querySelectorAll('pre code')).forEach(function(block) {
var pre_block = block.parentNode;
if( !pre_block.classList.contains('playpen') ) {
var buttons = pre_block.querySelector(".buttons");
if(!buttons) {
buttons = document.createElement('div');
buttons.className = 'buttons';
pre_block.prepend(buttons);
}
2018-01-13 02:44:13 +08:00
var clipButton = document.createElement('button');
clipButton.className = 'fa fa-copy clip-button';
clipButton.title = 'Copy to clipboard';
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
buttons.prepend(clipButton);
clipButton.addEventListener('mouseout', function(e){
hideTooltip(e.currentTarget);
});
}
});
// Process playpen code blocks
2018-01-13 02:44:13 +08:00
Array.from(document.querySelectorAll(".playpen")).forEach(function(pre_block){
// Add play button
2018-01-13 02:44:13 +08:00
var buttons = pre_block.querySelector(".buttons");
if(!buttons) {
buttons = document.createElement('div');
buttons.className = 'buttons';
pre_block.prepend(buttons);
}
2018-01-13 02:44:13 +08:00
var runCodeButton = document.createElement('button');
runCodeButton.className = 'fa fa-play play-button';
runCodeButton.hidden = true;
runCodeButton.title = 'Run this code';
var copyCodeClipboardButton = document.createElement('button');
copyCodeClipboardButton.className = 'fa fa-copy clip-button';
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
copyCodeClipboardButton.title = 'Copy to clipboard';
buttons.prepend(runCodeButton);
buttons.prepend(copyCodeClipboardButton);
2017-06-29 12:35:20 +08:00
2018-01-13 02:44:13 +08:00
runCodeButton.addEventListener('click', function(e){
run_rust_code(pre_block);
});
2018-01-13 02:44:13 +08:00
copyCodeClipboardButton.addEventListener('mouseout', function(e){
hideTooltip(e.currentTarget);
});
2018-01-13 02:44:13 +08:00
let code_block = pre_block.querySelector("code");
if (window.ace && code_block.classList.contains("editable")) {
var undoChangesButton = document.createElement('button');
undoChangesButton.className = 'fa fa-history reset-button';
undoChangesButton.title = 'Undo changes';
buttons.prepend(undoChangesButton);
undoChangesButton.addEventListener('click', function() {
let editor = window.ace.edit(code_block);
editor.setValue(editor.originalCode);
editor.clearSelection();
});
}
});
var clipboardSnippets = new Clipboard('.clip-button', {
text: function(trigger) {
hideTooltip(trigger);
2018-01-13 02:44:13 +08:00
let playpen = trigger.closest("pre");
return playpen_text(playpen);
}
});
clipboardSnippets.on('success', function(e) {
e.clearSelection();
showTooltip(e.trigger, "Copied!");
});
clipboardSnippets.on('error', function(e) {
showTooltip(e.trigger, "Clipboard error!");
});
2018-01-13 02:44:13 +08:00
var request = fetch("https://play.rust-lang.org/meta/crates", {
headers: {
'Content-Type': "application/json",
},
method: 'POST',
mode: 'cors',
});
request
.then(function(response) { return response.json(); })
.then(function(response) {
// get list of crates available in the rust playground
let playground_crates = response.crates.map(function(item) {return item["id"];} );
2018-01-13 02:44:13 +08:00
Array.from(document.querySelectorAll(".playpen")).forEach(function(block) {
handle_crate_list_update(block, playground_crates);
});
2018-01-13 02:44:13 +08:00
});
});
function playpen_text(playpen) {
2018-01-13 02:44:13 +08:00
let code_block = playpen.querySelector("code");
2018-01-13 02:44:13 +08:00
if (window.ace && code_block.classList.contains("editable")) {
let editor = window.ace.edit(code_block);
return editor.getValue();
} else {
2018-01-13 02:44:13 +08:00
return code_block.textContent;
}
}
function handle_crate_list_update(playpen_block, playground_crates) {
// update the play buttons after receiving the response
update_play_button(playpen_block, playground_crates);
// and install on change listener to dynamically update ACE editors
if (window.ace) {
2018-01-13 02:44:13 +08:00
let code_block = playpen_block.querySelector("code");
if (code_block.classList.contains("editable")) {
let editor = window.ace.edit(code_block);
editor.addEventListener("change", function(e){
update_play_button(playpen_block, playground_crates);
});
}
}
}
// updates the visibility of play button based on `no_run` class and
// used crates vs ones available on http://play.rust-lang.org
function update_play_button(pre_block, playground_crates) {
2018-01-13 02:44:13 +08:00
var play_button = pre_block.querySelector(".play-button");
// skip if code is `no_run`
2018-01-13 02:44:13 +08:00
if (pre_block.querySelector('code').classList.contains("no_run")) {
play_button.classList.add("hidden");
return;
}
// get list of `extern crate`'s from snippet
var txt = playpen_text(pre_block);
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
var snippet_crates = [];
while (item = re.exec(txt)) {
snippet_crates.push(item[1]);
}
// check if all used crates are available on play.rust-lang.org
var all_available = snippet_crates.every(function(elem) {
return playground_crates.indexOf(elem) > -1;
});
if (all_available) {
2018-01-13 02:44:13 +08:00
play_button.classList.remove("hidden");
} else {
2018-01-13 02:44:13 +08:00
play_button.classList.add("hidden");
}
}
function hideTooltip(elem) {
elem.firstChild.innerText="";
2018-01-13 02:44:13 +08:00
elem.className = 'fa fa-copy clip-button';
}
function showTooltip(elem, msg) {
elem.firstChild.innerText=msg;
2018-01-13 02:44:13 +08:00
elem.className = 'fa fa-copy tooltipped';
}
function sidebarToggle() {
2018-01-13 02:44:13 +08:00
var html = document.querySelector("html");
if (html.classList.contains("sidebar-hidden")) {
showSidebar();
2018-01-13 02:44:13 +08:00
} else if (html.classList.contains("sidebar-visible")) {
hideSidebar();
} else {
2018-01-13 02:44:13 +08:00
if (getComputedStyle(sidebar)['transform'] === 'none'){
hideSidebar();
} else {
showSidebar();
}
}
}
function showThemes() {
themePopup.style.display = 'block';
themeToggleButton.setAttribute('aria-expanded', true);
}
function hideThemes() {
themePopup.style.display = 'none';
themeToggleButton.setAttribute('aria-expanded', false);
}
function showSidebar() {
2018-01-13 02:44:13 +08:00
html.classList.remove('sidebar-hidden')
html.classList.add('sidebar-visible');
Array.from(sidebarLinks).forEach(function(link) {
link.setAttribute('tabIndex', 0);
});
sidebarToggleButton.setAttribute('aria-expanded', true);
sidebar.setAttribute('aria-hidden', false);
store.set('mdbook-sidebar', 'visible');
}
function hideSidebar() {
2018-01-13 02:44:13 +08:00
html.classList.remove('sidebar-visible')
html.classList.add('sidebar-hidden');
Array.from(sidebarLinks).forEach(function(link) {
link.setAttribute('tabIndex', -1);
});
sidebarToggleButton.setAttribute('aria-expanded', false);
sidebar.setAttribute('aria-hidden', true);
store.set('mdbook-sidebar', 'hidden');
}
function run_rust_code(code_block) {
2018-01-13 02:44:13 +08:00
var result_block = code_block.querySelector(".result");
if(!result_block) {
result_block = document.createElement('code');
result_block.className = 'result hljs language-bash';
code_block.append(result_block);
}
let text = playpen_text(code_block);
2017-06-10 02:59:29 +08:00
var params = {
channel: "stable",
mode: "debug",
crateType: "bin",
tests: false,
code: text,
}
2017-06-10 03:48:57 +08:00
if(text.indexOf("#![feature") !== -1) {
params.channel = "nightly";
}
2018-01-13 02:44:13 +08:00
result_block.innerText = "Running...";
var request = fetch("https://play.rust-lang.org/execute", {
headers: {
'Content-Type': "application/json",
},
2018-01-13 02:44:13 +08:00
method: 'POST',
mode: 'cors',
body: JSON.stringify(params)
});
2018-01-13 02:44:13 +08:00
request
.then(function(response) { return response.json(); })
.then(function(response) { result_block.innerText = response.success ? response.stdout : response.stderr; })
.catch(function(error) { result_block.innerText = "Playground communication" + error.message; });
}
(function autoHideMenu() {
var scrollingContainer = document.querySelector('html');
var menu = document.getElementById('menu-bar');
var previousScrollTop = scrollingContainer.scrollTop;
document.addEventListener('scroll', function() {
if (menu.classList.contains('folded') && scrollingContainer.scrollTop < previousScrollTop) {
menu.classList.remove('folded');
} else if (!menu.classList.contains('folded') && scrollingContainer.scrollTop > previousScrollTop) {
menu.classList.add('folded');
}
if (!menu.classList.contains('bordered') && scrollingContainer.scrollTop > 0) {
menu.classList.add('bordered');
}
if (menu.classList.contains('bordered') && scrollingContainer.scrollTop === 0) {
menu.classList.remove('bordered');
}
previousScrollTop = scrollingContainer.scrollTop;
hideThemes();
}, { passive: true });
})();