2018-01-19 23:41:50 +08:00
|
|
|
var html, sidebar, sidebarLinks, sidebarToggleButton, themeToggleButton, themePopup;
|
2018-01-13 02:44:13 +08:00
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
2015-07-29 19:34:48 +08:00
|
|
|
|
2015-09-05 23:26:17 +08:00
|
|
|
// url
|
|
|
|
var url = window.location.pathname;
|
|
|
|
|
2015-09-25 04:32:41 +08:00
|
|
|
// Fix back button cache problem
|
|
|
|
window.onunload = function(){};
|
2015-09-24 22:47:33 +08:00
|
|
|
|
2015-09-14 14:39:53 +08:00
|
|
|
// Set theme
|
2017-09-19 02:55:59 +08:00
|
|
|
var theme = store.get('mdbook-theme');
|
2017-06-12 03:54:09 +08:00
|
|
|
if (theme === null || theme === undefined) { theme = 'light'; }
|
2015-09-14 14:39:53 +08:00
|
|
|
|
|
|
|
set_theme(theme);
|
|
|
|
|
2015-08-06 00:28:59 +08:00
|
|
|
// Syntax highlighting Configuration
|
|
|
|
hljs.configure({
|
|
|
|
tabReplace: ' ', // 4 spaces
|
|
|
|
languages: [], // Languages used for auto-detection
|
|
|
|
});
|
2017-08-08 06:36:38 +08:00
|
|
|
|
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'); });
|
2015-08-06 00:28:59 +08:00
|
|
|
|
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
|
|
|
}
|
2017-08-08 06:36:38 +08:00
|
|
|
|
Add hljs class to all code blocks, regardless of highlighting
Fixes #179.
Highlight.js does not apply syntax highlighting to code blocks marked
no-highlight, nohighlight, plain, or text. When it finds blocks of those
languages, it does not add the `hljs` class to those code blocks either.
highlight.css and tomorrow-night.css use the `hljs` class to give code
blocks their backrgound color and text color, and we want that to apply
even if the code doesn't get syntax highlighting markup.
This is a somewhat hacky solution to get just that behavior! After this
commit, code blocks with no-highlight, nohighlight, plain, or text
language set on them will indeed get the hljs colors.
2016-11-14 10:14:00 +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-08-06 00:28:59 +08:00
|
|
|
|
2015-12-30 23:30:08 +08:00
|
|
|
var KEY_CODES = {
|
|
|
|
PREVIOUS_KEY: 37,
|
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) {
|
2017-03-23 15:59:04 +08:00
|
|
|
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-31 00:19:43 +08:00
|
|
|
}
|
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-31 00:19:43 +08:00
|
|
|
}
|
2015-12-30 23:30:08 +08:00
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
});
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2015-07-29 19:34:48 +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");
|
2018-01-19 23:41:50 +08:00
|
|
|
themeToggleButton = document.getElementById('theme-toggle');
|
|
|
|
themePopup = document.getElementById('theme-list');
|
2017-06-16 03:12:28 +08:00
|
|
|
|
2015-09-24 22:47:33 +08:00
|
|
|
// Toggle sidebar
|
2018-01-13 02:44:13 +08:00
|
|
|
sidebarToggleButton.addEventListener('click', sidebarToggle);
|
2017-06-09 21:51:30 +08:00
|
|
|
|
2015-12-27 11:45:50 +08:00
|
|
|
// 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;
|
2015-12-27 11:45:50 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 08:31:56 +08:00
|
|
|
var firstContact = null;
|
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
document.addEventListener('touchstart', function(e) {
|
2017-10-24 08:31:56 +08:00
|
|
|
firstContact = {
|
2018-01-13 02:44:13 +08:00
|
|
|
x: e.touches[0].clientX,
|
2017-10-24 08:31:56 +08:00
|
|
|
time: Date.now()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
document.addEventListener('touchmove', function(e) {
|
2017-10-24 08:31:56 +08:00
|
|
|
if (!firstContact)
|
|
|
|
return;
|
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
var curX = e.touches[0].clientX;
|
2017-10-24 08:31:56 +08:00
|
|
|
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') {
|
2018-01-15 21:26:53 +08:00
|
|
|
hideThemes();
|
2015-09-11 07:16:29 +08:00
|
|
|
} else {
|
2018-01-15 21:26:53 +08:00
|
|
|
showThemes();
|
2015-09-11 07:16:29 +08:00
|
|
|
}
|
2017-06-23 19:18:06 +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;
|
2018-01-15 21:26:53 +08:00
|
|
|
set_theme(theme);
|
|
|
|
});
|
|
|
|
|
2017-06-23 19:18:06 +08:00
|
|
|
// 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();
|
2017-06-23 19:18:06 +08:00
|
|
|
}
|
2015-09-11 07:16:29 +08:00
|
|
|
});
|
|
|
|
|
2015-09-14 14:39:53 +08:00
|
|
|
function set_theme(theme) {
|
2017-06-29 12:35:20 +08:00
|
|
|
let ace_theme;
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2015-09-14 14:39:53 +08:00
|
|
|
if (theme == 'coal' || theme == 'navy') {
|
2018-01-19 00:12:41 +08:00
|
|
|
document.querySelector("[href='ayu-highlight.css']").disabled = true;
|
|
|
|
document.querySelector("[href='tomorrow-night.css']").disabled = false;
|
|
|
|
document.querySelector("[href='highlight.css']").disabled = true;
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2017-06-29 12:35:20 +08:00
|
|
|
ace_theme = "ace/theme/tomorrow_night";
|
2017-06-07 04:35:44 +08:00
|
|
|
} else if (theme == 'ayu') {
|
2018-01-19 00:12:41 +08:00
|
|
|
document.querySelector("[href='ayu-highlight.css']").disabled = false;
|
|
|
|
document.querySelector("[href='tomorrow-night.css']").disabled = true;
|
|
|
|
document.querySelector("[href='highlight.css']").disabled = true;
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2017-06-29 12:35:20 +08:00
|
|
|
ace_theme = "ace/theme/tomorrow_night";
|
2015-09-14 14:39:53 +08:00
|
|
|
} else {
|
2018-01-19 00:12:41 +08:00
|
|
|
document.querySelector("[href='ayu-highlight.css']").disabled = true;
|
|
|
|
document.querySelector("[href='tomorrow-night.css']").disabled = true;
|
|
|
|
document.querySelector("[href='highlight.css']").disabled = false;
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2017-06-29 12:35:20 +08:00
|
|
|
ace_theme = "ace/theme/dawn";
|
|
|
|
}
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-01-16 20:29:20 +08:00
|
|
|
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);
|
|
|
|
});
|
2015-09-14 14:39:53 +08:00
|
|
|
}
|
|
|
|
|
2017-09-19 02:55:59 +08:00
|
|
|
store.set('mdbook-theme', theme);
|
2015-09-14 14:39:53 +08:00
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
document.body.className = theme;
|
2015-09-14 14:39:53 +08:00
|
|
|
}
|
2015-12-29 06:52:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 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){
|
2015-12-29 20:08:25 +08:00
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
var code_block = block;
|
|
|
|
var pre_block = block.parentNode;
|
2015-12-29 20:08:25 +08:00
|
|
|
// 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;
|
2015-12-29 20:08:25 +08:00
|
|
|
var lines_hidden = false;
|
2015-12-29 06:52:05 +08:00
|
|
|
|
|
|
|
for(var n = 0; n < lines.length; n++){
|
2018-01-13 02:44:13 +08:00
|
|
|
if(lines[n].trim()[0] == hiding_character){
|
2015-12-29 20:08:25 +08:00
|
|
|
if(first_non_hidden_line){
|
2017-05-18 06:04:09 +08:00
|
|
|
lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)# ?/, "$1") + "</span>";
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
else {
|
2017-05-18 06:04:09 +08:00
|
|
|
lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)# ?/, "$1") + "\n" + "</span>";
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
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;
|
2015-12-29 06:52:05 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-13 02:44:13 +08:00
|
|
|
code_block.innerHTML = lines.join("");
|
2015-12-29 20:08:25 +08:00
|
|
|
|
|
|
|
// 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>";
|
2015-12-29 20:08:25 +08:00
|
|
|
|
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');
|
|
|
|
});
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
});
|
2015-12-29 06:52:05 +08:00
|
|
|
});
|
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);
|
2017-09-19 02:55:59 +08:00
|
|
|
}
|
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){
|
2017-09-19 02:55:59 +08:00
|
|
|
hideTooltip(e.currentTarget);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-12-29 06:52:05 +08:00
|
|
|
|
2016-01-01 07:32:12 +08:00
|
|
|
// Process playpen code blocks
|
2018-01-13 02:44:13 +08:00
|
|
|
Array.from(document.querySelectorAll(".playpen")).forEach(function(pre_block){
|
2016-01-01 07:32:12 +08:00
|
|
|
// 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);
|
2016-01-01 07:32:12 +08:00
|
|
|
}
|
|
|
|
|
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){
|
2016-01-01 07:32:12 +08:00
|
|
|
run_rust_code(pre_block);
|
|
|
|
});
|
2018-01-13 02:44:13 +08:00
|
|
|
copyCodeClipboardButton.addEventListener('mouseout', function(e){
|
2017-06-01 03:07:30 +08:00
|
|
|
hideTooltip(e.currentTarget);
|
2017-05-31 07:14:01 +08:00
|
|
|
});
|
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();
|
|
|
|
});
|
|
|
|
}
|
2016-01-01 07:32:12 +08:00
|
|
|
});
|
|
|
|
|
2017-05-31 07:14:01 +08:00
|
|
|
var clipboardSnippets = new Clipboard('.clip-button', {
|
|
|
|
text: function(trigger) {
|
2017-06-01 03:07:30 +08:00
|
|
|
hideTooltip(trigger);
|
2018-01-13 02:44:13 +08:00
|
|
|
let playpen = trigger.closest("pre");
|
2017-08-08 06:05:33 +08:00
|
|
|
return playpen_text(playpen);
|
2017-05-31 07:14:01 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
clipboardSnippets.on('success', function(e) {
|
|
|
|
e.clearSelection();
|
2017-06-01 03:07:30 +08:00
|
|
|
showTooltip(e.trigger, "Copied!");
|
2017-05-31 07:14:01 +08:00
|
|
|
});
|
|
|
|
clipboardSnippets.on('error', function(e) {
|
2017-06-01 03:07:30 +08:00
|
|
|
showTooltip(e.trigger, "Clipboard error!");
|
2017-05-31 07:14:01 +08:00
|
|
|
});
|
2017-08-08 06:05:33 +08:00
|
|
|
|
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) {
|
2017-08-08 06:36:38 +08:00
|
|
|
// 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);
|
2017-08-08 06:05:33 +08:00
|
|
|
});
|
2018-01-13 02:44:13 +08:00
|
|
|
});
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2015-07-29 19:34:48 +08:00
|
|
|
});
|
2016-01-01 07:32:12 +08:00
|
|
|
|
2017-08-08 06:05:33 +08:00
|
|
|
function playpen_text(playpen) {
|
2018-01-13 02:44:13 +08:00
|
|
|
let code_block = playpen.querySelector("code");
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
if (window.ace && code_block.classList.contains("editable")) {
|
|
|
|
let editor = window.ace.edit(code_block);
|
2017-08-08 06:05:33 +08:00
|
|
|
return editor.getValue();
|
|
|
|
} else {
|
2018-01-13 02:44:13 +08:00
|
|
|
return code_block.textContent;
|
2017-08-08 06:05:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 06:36:38 +08:00
|
|
|
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){
|
2017-08-08 06:36:38 +08:00
|
|
|
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");
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2017-08-08 06:36:38 +08:00
|
|
|
// 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");
|
2017-08-08 06:36:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2017-08-08 06:36:38 +08:00
|
|
|
// get list of `extern crate`'s from snippet
|
|
|
|
var txt = playpen_text(pre_block);
|
2017-08-08 06:05:33 +08:00
|
|
|
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
|
|
|
|
var snippet_crates = [];
|
2017-08-08 06:36:38 +08:00
|
|
|
while (item = re.exec(txt)) {
|
2017-08-08 06:05:33 +08:00
|
|
|
snippet_crates.push(item[1]);
|
2017-08-08 06:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
});
|
2017-08-08 06:05:33 +08:00
|
|
|
|
|
|
|
if (all_available) {
|
2018-01-13 02:44:13 +08:00
|
|
|
play_button.classList.remove("hidden");
|
2017-08-08 06:05:33 +08:00
|
|
|
} else {
|
2018-01-13 02:44:13 +08:00
|
|
|
play_button.classList.add("hidden");
|
2017-08-08 06:05:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 03:07:30 +08:00
|
|
|
function hideTooltip(elem) {
|
|
|
|
elem.firstChild.innerText="";
|
2018-01-13 02:44:13 +08:00
|
|
|
elem.className = 'fa fa-copy clip-button';
|
2017-06-01 03:07:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-31 07:14:01 +08:00
|
|
|
function showTooltip(elem, msg) {
|
2017-06-01 03:07:30 +08:00
|
|
|
elem.firstChild.innerText=msg;
|
2018-01-13 02:44:13 +08:00
|
|
|
elem.className = 'fa fa-copy tooltipped';
|
2017-05-31 07:14:01 +08:00
|
|
|
}
|
2016-01-01 07:32:12 +08:00
|
|
|
|
2017-06-09 21:51:30 +08:00
|
|
|
function sidebarToggle() {
|
2018-01-13 02:44:13 +08:00
|
|
|
var html = document.querySelector("html");
|
|
|
|
if (html.classList.contains("sidebar-hidden")) {
|
2017-10-24 08:31:56 +08:00
|
|
|
showSidebar();
|
2018-01-13 02:44:13 +08:00
|
|
|
} else if (html.classList.contains("sidebar-visible")) {
|
2017-10-24 08:31:56 +08:00
|
|
|
hideSidebar();
|
2017-06-09 21:51:30 +08:00
|
|
|
} else {
|
2018-01-13 02:44:13 +08:00
|
|
|
if (getComputedStyle(sidebar)['transform'] === 'none'){
|
2017-10-24 08:31:56 +08:00
|
|
|
hideSidebar();
|
2017-06-09 21:51:30 +08:00
|
|
|
} else {
|
2017-10-24 08:31:56 +08:00
|
|
|
showSidebar();
|
2017-06-09 21:51:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 23:41:50 +08:00
|
|
|
function showThemes() {
|
|
|
|
themePopup.style.display = 'block';
|
|
|
|
themeToggleButton.setAttribute('aria-expanded', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideThemes() {
|
|
|
|
themePopup.style.display = 'none';
|
|
|
|
themeToggleButton.setAttribute('aria-expanded', false);
|
|
|
|
}
|
|
|
|
|
2017-10-24 08:31:56 +08:00
|
|
|
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);
|
2017-10-24 08:31:56 +08:00
|
|
|
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);
|
2017-10-24 08:31:56 +08:00
|
|
|
store.set('mdbook-sidebar', 'hidden');
|
|
|
|
}
|
|
|
|
|
2016-01-01 07:32:12 +08:00
|
|
|
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);
|
2016-01-01 07:32:12 +08:00
|
|
|
}
|
|
|
|
|
2017-09-19 02:55:59 +08:00
|
|
|
let text = playpen_text(code_block);
|
|
|
|
|
2017-06-10 02:59:29 +08:00
|
|
|
var params = {
|
2017-09-19 02:55:59 +08:00
|
|
|
channel: "stable",
|
|
|
|
mode: "debug",
|
|
|
|
crateType: "bin",
|
|
|
|
tests: false,
|
|
|
|
code: text,
|
|
|
|
}
|
2017-04-01 05:06:03 +08:00
|
|
|
|
2017-06-10 03:48:57 +08:00
|
|
|
if(text.indexOf("#![feature") !== -1) {
|
2017-09-19 02:55:59 +08:00
|
|
|
params.channel = "nightly";
|
2017-04-01 05:06:03 +08:00
|
|
|
}
|
|
|
|
|
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",
|
2017-08-06 20:59:03 +08:00
|
|
|
},
|
2018-01-13 02:44:13 +08:00
|
|
|
method: 'POST',
|
|
|
|
mode: 'cors',
|
|
|
|
body: JSON.stringify(params)
|
2016-01-01 07:32:12 +08:00
|
|
|
});
|
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; });
|
2016-01-01 07:32:12 +08:00
|
|
|
}
|
2018-01-19 23:41:50 +08:00
|
|
|
|
|
|
|
(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 });
|
|
|
|
})();
|