123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- "use strict";
- window.onunload = function () { };
- function playground_text(playground) {
- let code_block = playground.querySelector("code");
- if (window.ace && code_block.classList.contains("editable")) {
- let editor = window.ace.edit(code_block);
- return editor.getValue();
- } else {
- return code_block.textContent;
- }
- }
- (function codeSnippets() {
- function fetch_with_timeout(url, options, timeout = 6000) {
- return Promise.race([
- fetch(url, options),
- new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout))
- ]);
- }
- var playgrounds = Array.from(document.querySelectorAll(".playground"));
- if (playgrounds.length > 0) {
- fetch_with_timeout("https://play.rust-lang.org/meta/crates", {
- headers: {
- 'Content-Type': "application/json",
- },
- method: 'POST',
- mode: 'cors',
- })
- .then(response => response.json())
- .then(response => {
-
- let playground_crates = response.crates.map(item => item["id"]);
- playgrounds.forEach(block => handle_crate_list_update(block, playground_crates));
- });
- }
- function handle_crate_list_update(playground_block, playground_crates) {
-
- update_play_button(playground_block, playground_crates);
-
- if (window.ace) {
- let code_block = playground_block.querySelector("code");
- if (code_block.classList.contains("editable")) {
- let editor = window.ace.edit(code_block);
- editor.addEventListener("change", function (e) {
- update_play_button(playground_block, playground_crates);
- });
-
- editor.commands.addCommand({
- name: "run",
- bindKey: {
- win: "Ctrl-Enter",
- mac: "Ctrl-Enter"
- },
- exec: _editor => run_rust_code(playground_block)
- });
- }
- }
- }
-
-
- function update_play_button(pre_block, playground_crates) {
- var play_button = pre_block.querySelector(".play-button");
-
- if (pre_block.querySelector('code').classList.contains("no_run")) {
- play_button.classList.add("hidden");
- return;
- }
-
- var txt = playground_text(pre_block);
- var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
- var snippet_crates = [];
- var item;
- while (item = re.exec(txt)) {
- snippet_crates.push(item[1]);
- }
-
- var all_available = snippet_crates.every(function (elem) {
- return playground_crates.indexOf(elem) > -1;
- });
- if (all_available) {
- play_button.classList.remove("hidden");
- } else {
- play_button.classList.add("hidden");
- }
- }
- function run_rust_code(code_block) {
- 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 = playground_text(code_block);
- let classes = code_block.querySelector('code').classList;
- let has_2018 = classes.contains("edition2018");
- let edition = has_2018 ? "2018" : "2015";
- var params = {
- version: "stable",
- optimize: "0",
- code: text,
- edition: edition
- };
- if (text.indexOf("#![feature") !== -1) {
- params.version = "nightly";
- }
- result_block.innerText = "Running...";
- fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
- headers: {
- 'Content-Type': "application/json",
- },
- method: 'POST',
- mode: 'cors',
- body: JSON.stringify(params)
- })
- .then(response => response.json())
- .then(response => result_block.innerText = response.result)
- .catch(error => result_block.innerText = "Playground Communication: " + error.message);
- }
-
- hljs.configure({
- tabReplace: ' ',
- languages: [],
- });
- let code_nodes = Array
- .from(document.querySelectorAll('code'))
-
- .filter(function (node) {return !node.parentElement.classList.contains("header"); });
- if (window.ace) {
-
-
- Array
- .from(document.querySelectorAll('code.editable'))
- .forEach(function (block) { block.classList.remove('language-rust'); });
- Array
- .from(document.querySelectorAll('code:not(.editable)'))
- .forEach(function (block) { hljs.highlightBlock(block); });
- } else {
- code_nodes.forEach(function (block) { hljs.highlightBlock(block); });
- }
-
-
- code_nodes.forEach(function (block) { block.classList.add('hljs'); });
- Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
- var lines = Array.from(block.querySelectorAll('.boring'));
-
- if (!lines.length) { return; }
- block.classList.add("hide-boring");
- var buttons = document.createElement('div');
- buttons.className = 'buttons';
- buttons.innerHTML = "<button class=\"fa fa-eye\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
-
- var pre_block = block.parentNode;
- pre_block.insertBefore(buttons, pre_block.firstChild);
- pre_block.querySelector('.buttons').addEventListener('click', function (e) {
- if (e.target.classList.contains('fa-eye')) {
- e.target.classList.remove('fa-eye');
- e.target.classList.add('fa-eye-slash');
- e.target.title = 'Hide lines';
- e.target.setAttribute('aria-label', e.target.title);
- block.classList.remove('hide-boring');
- } else if (e.target.classList.contains('fa-eye-slash')) {
- e.target.classList.remove('fa-eye-slash');
- e.target.classList.add('fa-eye');
- e.target.title = 'Show hidden lines';
- e.target.setAttribute('aria-label', e.target.title);
- block.classList.add('hide-boring');
- }
- });
- });
- if (window.playground_copyable) {
- Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
- var pre_block = block.parentNode;
- if (!pre_block.classList.contains('playground')) {
- var buttons = pre_block.querySelector(".buttons");
- if (!buttons) {
- buttons = document.createElement('div');
- buttons.className = 'buttons';
- pre_block.insertBefore(buttons, pre_block.firstChild);
- }
- var clipButton = document.createElement('button');
- clipButton.className = 'fa fa-copy clip-button';
- clipButton.title = 'Copy to clipboard';
- clipButton.setAttribute('aria-label', clipButton.title);
- clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
- buttons.insertBefore(clipButton, buttons.firstChild);
- }
- });
- }
-
- Array.from(document.querySelectorAll(".playground")).forEach(function (pre_block) {
-
- var buttons = pre_block.querySelector(".buttons");
- if (!buttons) {
- buttons = document.createElement('div');
- buttons.className = 'buttons';
- pre_block.insertBefore(buttons, pre_block.firstChild);
- }
- var runCodeButton = document.createElement('button');
- runCodeButton.className = 'fa fa-play play-button';
- runCodeButton.hidden = true;
- runCodeButton.title = 'Run this code';
- runCodeButton.setAttribute('aria-label', runCodeButton.title);
- buttons.insertBefore(runCodeButton, buttons.firstChild);
- runCodeButton.addEventListener('click', function (e) {
- run_rust_code(pre_block);
- });
- if (window.playground_copyable) {
- var copyCodeClipboardButton = document.createElement('button');
- copyCodeClipboardButton.className = 'fa fa-copy clip-button';
- copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
- copyCodeClipboardButton.title = 'Copy to clipboard';
- copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
- buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
- }
- 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';
- undoChangesButton.setAttribute('aria-label', undoChangesButton.title);
- buttons.insertBefore(undoChangesButton, buttons.firstChild);
- undoChangesButton.addEventListener('click', function () {
- let editor = window.ace.edit(code_block);
- editor.setValue(editor.originalCode);
- editor.clearSelection();
- });
- }
- });
- })();
- (function menues() {
- class Menu {
- constructor(selectorPrefix, defaultValue, setter) {
- this.selectorPrefix = selectorPrefix;
- this.defaultValue = defaultValue;
- this.setValue = setter;
- this.init();
- }
- get toggleButton() {
- return document.getElementById(`${this.selectorPrefix}-toggle`);
- }
- get popup() {
- return document.getElementById(`${this.selectorPrefix}-list`);
- }
- get value() {
- let itemValue;
- try {
- itemValue = localStorage.getItem(`mdbook-${this.selectorPrefix}`);
- } catch (e) { }
- if (itemValue === null || itemValue === undefined) {
- return this.defaultValue;
- } else {
- return itemValue;
- }
- }
- showPopup() {
- this.popup.style.display = 'block';
- this.toggleButton.setAttribute('aria-expanded', true);
- this.popup.querySelector(`button[data-value="${this.value}"]`).focus();
- }
- hidePopup() {
- this.popup.style.display = 'none';
- this.toggleButton.setAttribute('aria-expanded', false);
- this.toggleButton.focus();
- }
- init() {
- this.setValue(this.value, false);
- this.toggleButton.addEventListener('click', () => {
- if (this.popup.style.display === 'block') {
- this.hidePopup();
- } else {
- this.showPopup();
- }
- });
- this.popup.addEventListener('click', (e) => {
- const value = e.target.dataset.value || e.target.parentElement.dataset.value;
- this.setValue(value);
- });
- this.popup.addEventListener('focusout', (e) => {
-
- if (!!e.relatedTarget && !this.toggleButton.contains(e.relatedTarget) && !this.popup.contains(e.relatedTarget)) {
- this.hidePopup();
- }
- });
-
- document.addEventListener('click', (e) => {
- if (this.popup.style.display === 'block' && !this.toggleButton.contains(e.target) && !this.popup.contains(e.target)) {
- this.hidePopup();
- }
- });
- document.addEventListener('keydown', (e) => {
- if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
- if (!this.popup.contains(e.target)) { return; }
- switch (e.key) {
- case 'Escape':
- e.preventDefault();
- this.hidePopup();
- break;
- case 'ArrowUp':
- e.preventDefault();
- var li = document.activeElement.parentElement;
- if (li && li.previousElementSibling) {
- li.previousElementSibling.querySelector('button').focus();
- }
- break;
- case 'ArrowDown':
- e.preventDefault();
- var li = document.activeElement.parentElement;
- if (li && li.nextElementSibling) {
- li.nextElementSibling.querySelector('button').focus();
- }
- break;
- case 'Home':
- e.preventDefault();
- this.popup.querySelector('li:first-child button').focus();
- break;
- case 'End':
- e.preventDefault();
- this.popup.querySelector('li:last-child button').focus();
- break;
- }
- });
- }
- }
- new Menu('theme', default_theme, function(theme, store = true) {
- var html = document.querySelector('html');
- var themeColorMetaTag = document.querySelector('meta[name="theme-color"]');
- var stylesheets = {
- ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"),
- tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"),
- highlight: document.querySelector("[href$='highlight.css']"),
- };
- let ace_theme;
- if (theme == 'coal' || theme == 'navy') {
- stylesheets.ayuHighlight.disabled = true;
- stylesheets.tomorrowNight.disabled = false;
- stylesheets.highlight.disabled = true;
- ace_theme = "ace/theme/tomorrow_night";
- } else if (theme == 'ayu') {
- stylesheets.ayuHighlight.disabled = false;
- stylesheets.tomorrowNight.disabled = true;
- stylesheets.highlight.disabled = true;
- ace_theme = "ace/theme/tomorrow_night";
- } else {
- stylesheets.ayuHighlight.disabled = true;
- stylesheets.tomorrowNight.disabled = true;
- stylesheets.highlight.disabled = false;
- ace_theme = "ace/theme/dawn";
- }
- setTimeout(function () {
- themeColorMetaTag.content = getComputedStyle(document.body).backgroundColor;
- }, 1);
- if (window.ace && window.editors) {
- window.editors.forEach(function (editor) {
- editor.setTheme(ace_theme);
- });
- }
- var previousTheme = this.value;
- if (store) {
- try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
- }
- html.classList.remove(previousTheme);
- html.classList.add(theme);
- });
- new Menu('font-family', 'Open Sans', function(fontFamily, store = true) {
- document.querySelector('html').style.fontFamily = fontFamily;
- if (store) {
- try { localStorage.setItem('mdbook-font-family', fontFamily); } catch (e) { }
- }
- });
- new Menu('font-size', '12pt', function(fontSize, store = true) {
- document.querySelector('body').style.fontSize = fontSize;
- if (store) {
- try { localStorage.setItem('mdbook-font-size', fontSize); } catch (e) { }
- }
- });
- })();
- (function sidebar() {
- var html = document.querySelector("html");
- var sidebar = document.getElementById("sidebar");
- var sidebarLinks = document.querySelectorAll('#sidebar a');
- var sidebarToggleButton = document.getElementById("sidebar-toggle");
- var sidebarResizeHandle = document.getElementById("sidebar-resize-handle");
- var firstContact = null;
- function showSidebar() {
- 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);
- try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
- }
- var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle');
- function toggleSection(ev) {
- ev.currentTarget.parentElement.classList.toggle('expanded');
- }
- Array.from(sidebarAnchorToggles).forEach(function (el) {
- el.addEventListener('click', toggleSection);
- });
- function hideSidebar() {
- 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);
- try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
- }
-
- sidebarToggleButton.addEventListener('click', function sidebarToggle() {
- if (html.classList.contains("sidebar-hidden")) {
- var current_width = parseInt(
- document.documentElement.style.getPropertyValue('--sidebar-width'), 10);
- if (current_width < 150) {
- document.documentElement.style.setProperty('--sidebar-width', '150px');
- }
- showSidebar();
- } else if (html.classList.contains("sidebar-visible")) {
- hideSidebar();
- } else {
- if (getComputedStyle(sidebar)['transform'] === 'none') {
- hideSidebar();
- } else {
- showSidebar();
- }
- }
- });
- sidebarResizeHandle.addEventListener('mousedown', initResize, false);
- function initResize(e) {
- window.addEventListener('mousemove', resize, false);
- window.addEventListener('mouseup', stopResize, false);
- html.classList.add('sidebar-resizing');
- }
- function resize(e) {
- var pos = (e.clientX - sidebar.offsetLeft);
- if (pos < 20) {
- hideSidebar();
- } else {
- if (html.classList.contains("sidebar-hidden")) {
- showSidebar();
- }
- pos = Math.min(pos, window.innerWidth - 100);
- document.documentElement.style.setProperty('--sidebar-width', pos + 'px');
- }
- }
-
- function stopResize(e) {
- html.classList.remove('sidebar-resizing');
- window.removeEventListener('mousemove', resize, false);
- window.removeEventListener('mouseup', stopResize, false);
- }
- document.addEventListener('touchstart', function (e) {
- firstContact = {
- x: e.touches[0].clientX,
- time: Date.now()
- };
- }, { passive: true });
- document.addEventListener('touchmove', function (e) {
- if (!firstContact)
- return;
- 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;
- }
- }, { passive: true });
-
- var activeSection = document.getElementById("sidebar").querySelector(".active");
- if (activeSection) {
-
- activeSection.scrollIntoView({ block: 'center' });
- }
- })();
- (function chapterNavigation() {
- document.addEventListener('keydown', function (e) {
- if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
- if (window.search && window.search.hasFocus()) { return; }
- switch (e.key) {
- case 'ArrowRight':
- e.preventDefault();
- var nextButton = document.querySelector('.nav-chapters.next');
- if (nextButton) {
- window.location.href = nextButton.href;
- }
- break;
- case 'ArrowLeft':
- e.preventDefault();
- var previousButton = document.querySelector('.nav-chapters.previous');
- if (previousButton) {
- window.location.href = previousButton.href;
- }
- break;
- }
- });
- })();
- (function clipboard() {
- var clipButtons = document.querySelectorAll('.clip-button');
- function hideTooltip(elem) {
- elem.firstChild.innerText = "";
- elem.className = 'fa fa-copy clip-button';
- }
- function showTooltip(elem, msg) {
- elem.firstChild.innerText = msg;
- elem.className = 'fa fa-copy tooltipped';
- }
- var clipboardSnippets = new ClipboardJS('.clip-button', {
- text: function (trigger) {
- hideTooltip(trigger);
- let playground = trigger.closest("pre");
- return playground_text(playground);
- }
- });
- Array.from(clipButtons).forEach(function (clipButton) {
- clipButton.addEventListener('mouseout', function (e) {
- hideTooltip(e.currentTarget);
- });
- });
- clipboardSnippets.on('success', function (e) {
- e.clearSelection();
- showTooltip(e.trigger, "Copied!");
- });
- clipboardSnippets.on('error', function (e) {
- showTooltip(e.trigger, "Clipboard error!");
- });
- })();
- (function scrollToTop () {
- var menuTitle = document.querySelector('.menu-title');
- menuTitle.addEventListener('click', function () {
- document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' });
- });
- })();
- (function flipPages() {
- function scrollPercent() {
- const documentElm = document.documentElement;
- const totalHeight = documentElm.scrollHeight - window.innerHeight;
- if (totalHeight === 0) return 100;
- return Math.ceil(documentElm.scrollTop / totalHeight * 100);
- }
- (function pagePosition() {
- document.querySelector('html').classList.add('hide-scrollbar');
- const outerPositionBar = document.createElement('div');
- const innerPositionBar = document.createElement('div');
- outerPositionBar.classList.add('page-position');
- outerPositionBar.appendChild(innerPositionBar);
- document.body.appendChild(outerPositionBar);
- innerPositionBar.style.width = scrollPercent();
- window.addEventListener('scroll', () => {
- innerPositionBar.style.width = scrollPercent() + '%';
- });
- })();
- function flipPage(direction = 'right') {
- const operator = direction === 'left' ? -1 : 1;
- const viewHeight = document.documentElement.clientHeight;
- const menuBarHeight = document.getElementById('menu-bar').offsetHeight;
- const prevLinesPx = 50;
- window.scrollBy(0, (viewHeight - menuBarHeight - prevLinesPx) * operator);
- }
- document.querySelector('.nav-page.previous').addEventListener('click', () => {
- if (document.documentElement.scrollTop === 0) {
- const prevA = document.querySelector('.mobile-nav-chapters.previous');
- if (prevA) window.location.href = prevA.href;
- } else {
- flipPage('left');
- }
- });
- document.querySelector('.nav-page.next').addEventListener('click', () => {
- if (scrollPercent() >= 100) {
- const nextA = document.querySelector('.mobile-nav-chapters.next');
- if (nextA) window.location.href = nextA.href;
- } else {
- flipPage('right');
- }
- });
- })();
|