| 1 |
"use strict"; |
| 2 |
|
| 3 |
// Fix back button cache problem |
| 4 |
window.onunload = function () { }; |
| 5 |
|
| 6 |
// Global variable, shared between modules |
| 7 |
function playground_text(playground, hidden = true) { |
| 8 |
let code_block = playground.querySelector("code"); |
| 9 |
|
| 10 |
if (window.ace && code_block.classList.contains("editable")) { |
| 11 |
let editor = window.ace.edit(code_block); |
| 12 |
return editor.getValue(); |
| 13 |
} else if (hidden) { |
| 14 |
return code_block.textContent; |
| 15 |
} else { |
| 16 |
return code_block.innerText; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
(function codeSnippets() { |
| 21 |
function fetch_with_timeout(url, options, timeout = 6000) { |
| 22 |
return Promise.race([ |
| 23 |
fetch(url, options), |
| 24 |
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout)) |
| 25 |
]); |
| 26 |
} |
| 27 |
|
| 28 |
var playgrounds = Array.from(document.querySelectorAll(".playground")); |
| 29 |
if (playgrounds.length > 0) { |
| 30 |
fetch_with_timeout("https://play.rust-lang.org/meta/crates", { |
| 31 |
headers: { |
| 32 |
'Content-Type': "application/json", |
| 33 |
}, |
| 34 |
method: 'POST', |
| 35 |
mode: 'cors', |
| 36 |
}) |
| 37 |
.then(response => response.json()) |
| 38 |
.then(response => { |
| 39 |
// get list of crates available in the rust playground |
| 40 |
let playground_crates = response.crates.map(item => item["id"]); |
| 41 |
playgrounds.forEach(block => handle_crate_list_update(block, playground_crates)); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
function handle_crate_list_update(playground_block, playground_crates) { |
| 46 |
// update the play buttons after receiving the response |
| 47 |
update_play_button(playground_block, playground_crates); |
| 48 |
|
| 49 |
// and install on change listener to dynamically update ACE editors |
| 50 |
if (window.ace) { |
| 51 |
let code_block = playground_block.querySelector("code"); |
| 52 |
if (code_block.classList.contains("editable")) { |
| 53 |
let editor = window.ace.edit(code_block); |
| 54 |
editor.addEventListener("change", function (e) { |
| 55 |
update_play_button(playground_block, playground_crates); |
| 56 |
}); |
| 57 |
// add Ctrl-Enter command to execute rust code |
| 58 |
editor.commands.addCommand({ |
| 59 |
name: "run", |
| 60 |
bindKey: { |
| 61 |
win: "Ctrl-Enter", |
| 62 |
mac: "Ctrl-Enter" |
| 63 |
}, |
| 64 |
exec: _editor => run_rust_code(playground_block) |
| 65 |
}); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// updates the visibility of play button based on `no_run` class and |
| 71 |
// used crates vs ones available on https://play.rust-lang.org |
| 72 |
function update_play_button(pre_block, playground_crates) { |
| 73 |
var play_button = pre_block.querySelector(".play-button"); |
| 74 |
|
| 75 |
// skip if code is `no_run` |
| 76 |
if (pre_block.querySelector('code').classList.contains("no_run")) { |
| 77 |
play_button.classList.add("hidden"); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// get list of `extern crate`'s from snippet |
| 82 |
var txt = playground_text(pre_block); |
| 83 |
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g; |
| 84 |
var snippet_crates = []; |
| 85 |
var item; |
| 86 |
while (item = re.exec(txt)) { |
| 87 |
snippet_crates.push(item[1]); |
| 88 |
} |
| 89 |
|
| 90 |
// check if all used crates are available on play.rust-lang.org |
| 91 |
var all_available = snippet_crates.every(function (elem) { |
| 92 |
return playground_crates.indexOf(elem) > -1; |
| 93 |
}); |
| 94 |
|
| 95 |
if (all_available) { |
| 96 |
play_button.classList.remove("hidden"); |
| 97 |
} else { |
| 98 |
play_button.classList.add("hidden"); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
function run_rust_code(code_block) { |
| 103 |
var result_block = code_block.querySelector(".result"); |
| 104 |
if (!result_block) { |
| 105 |
result_block = document.createElement('code'); |
| 106 |
result_block.className = 'result hljs language-bash'; |
| 107 |
|
| 108 |
code_block.append(result_block); |
| 109 |
} |
| 110 |
|
| 111 |
let text = playground_text(code_block); |
| 112 |
let classes = code_block.querySelector('code').classList; |
| 113 |
let edition = "2015"; |
| 114 |
if(classes.contains("edition2018")) { |
| 115 |
edition = "2018"; |
| 116 |
} else if(classes.contains("edition2021")) { |
| 117 |
edition = "2021"; |
| 118 |
} |
| 119 |
var params = { |
| 120 |
version: "stable", |
| 121 |
optimize: "0", |
| 122 |
code: text, |
| 123 |
edition: edition |
| 124 |
}; |
| 125 |
|
| 126 |
if (text.indexOf("#![feature") !== -1) { |
| 127 |
params.version = "nightly"; |
| 128 |
} |
| 129 |
|
| 130 |
result_block.innerText = "Running..."; |
| 131 |
|
| 132 |
fetch_with_timeout("https://play.rust-lang.org/evaluate.json", { |
| 133 |
headers: { |
| 134 |
'Content-Type': "application/json", |
| 135 |
}, |
| 136 |
method: 'POST', |
| 137 |
mode: 'cors', |
| 138 |
body: JSON.stringify(params) |
| 139 |
}) |
| 140 |
.then(response => response.json()) |
| 141 |
.then(response => { |
| 142 |
if (response.result.trim() === '') { |
| 143 |
result_block.innerText = "No output"; |
| 144 |
result_block.classList.add("result-no-output"); |
| 145 |
} else { |
| 146 |
result_block.innerText = response.result; |
| 147 |
result_block.classList.remove("result-no-output"); |
| 148 |
} |
| 149 |
}) |
| 150 |
.catch(error => result_block.innerText = "Playground Communication: " + error.message); |
| 151 |
} |
| 152 |
|
| 153 |
// Syntax highlighting Configuration |
| 154 |
hljs.configure({ |
| 155 |
tabReplace: ' ', // 4 spaces |
| 156 |
languages: [], // Languages used for auto-detection |
| 157 |
}); |
| 158 |
|
| 159 |
let code_nodes = Array |
| 160 |
.from(document.querySelectorAll('code')) |
| 161 |
// Don't highlight `inline code` blocks in headers. |
| 162 |
.filter(function (node) {return !node.parentElement.classList.contains("header"); }); |
| 163 |
|
| 164 |
if (window.ace) { |
| 165 |
// language-rust class needs to be removed for editable |
| 166 |
// blocks or highlightjs will capture events |
| 167 |
code_nodes |
| 168 |
.filter(function (node) {return node.classList.contains("editable"); }) |
| 169 |
.forEach(function (block) { block.classList.remove('language-rust'); }); |
| 170 |
|
| 171 |
code_nodes |
| 172 |
.filter(function (node) {return !node.classList.contains("editable"); }) |
| 173 |
.forEach(function (block) { hljs.highlightBlock(block); }); |
| 174 |
} else { |
| 175 |
code_nodes.forEach(function (block) { hljs.highlightBlock(block); }); |
| 176 |
} |
| 177 |
|
| 178 |
// Adding the hljs class gives code blocks the color css |
| 179 |
// even if highlighting doesn't apply |
| 180 |
code_nodes.forEach(function (block) { block.classList.add('hljs'); }); |
| 181 |
|
| 182 |
Array.from(document.querySelectorAll("code.hljs")).forEach(function (block) { |
| 183 |
|
| 184 |
var lines = Array.from(block.querySelectorAll('.boring')); |
| 185 |
// If no lines were hidden, return |
| 186 |
if (!lines.length) { return; } |
| 187 |
block.classList.add("hide-boring"); |
| 188 |
|
| 189 |
var buttons = document.createElement('div'); |
| 190 |
buttons.className = 'buttons'; |
| 191 |
buttons.innerHTML = "<button class=\"fa fa-eye\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>"; |
| 192 |
|
| 193 |
// add expand button |
| 194 |
var pre_block = block.parentNode; |
| 195 |
pre_block.insertBefore(buttons, pre_block.firstChild); |
| 196 |
|
| 197 |
pre_block.querySelector('.buttons').addEventListener('click', function (e) { |
| 198 |
if (e.target.classList.contains('fa-eye')) { |
| 199 |
e.target.classList.remove('fa-eye'); |
| 200 |
e.target.classList.add('fa-eye-slash'); |
| 201 |
e.target.title = 'Hide lines'; |
| 202 |
e.target.setAttribute('aria-label', e.target.title); |
| 203 |
|
| 204 |
block.classList.remove('hide-boring'); |
| 205 |
} else if (e.target.classList.contains('fa-eye-slash')) { |
| 206 |
e.target.classList.remove('fa-eye-slash'); |
| 207 |
e.target.classList.add('fa-eye'); |
| 208 |
e.target.title = 'Show hidden lines'; |
| 209 |
e.target.setAttribute('aria-label', e.target.title); |
| 210 |
|
| 211 |
block.classList.add('hide-boring'); |
| 212 |
} |
| 213 |
}); |
| 214 |
}); |
| 215 |
|
| 216 |
if (window.playground_copyable) { |
| 217 |
Array.from(document.querySelectorAll('pre code')).forEach(function (block) { |
| 218 |
var pre_block = block.parentNode; |
| 219 |
if (!pre_block.classList.contains('playground')) { |
| 220 |
var buttons = pre_block.querySelector(".buttons"); |
| 221 |
if (!buttons) { |
| 222 |
buttons = document.createElement('div'); |
| 223 |
buttons.className = 'buttons'; |
| 224 |
pre_block.insertBefore(buttons, pre_block.firstChild); |
| 225 |
} |
| 226 |
|
| 227 |
var clipButton = document.createElement('button'); |
| 228 |
clipButton.className = 'clip-button'; |
| 229 |
clipButton.title = 'Copy to clipboard'; |
| 230 |
clipButton.setAttribute('aria-label', clipButton.title); |
| 231 |
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>'; |
| 232 |
|
| 233 |
buttons.insertBefore(clipButton, buttons.firstChild); |
| 234 |
} |
| 235 |
}); |
| 236 |
} |
| 237 |
|
| 238 |
// Process playground code blocks |
| 239 |
Array.from(document.querySelectorAll(".playground")).forEach(function (pre_block) { |
| 240 |
// Add play button |
| 241 |
var buttons = pre_block.querySelector(".buttons"); |
| 242 |
if (!buttons) { |
| 243 |
buttons = document.createElement('div'); |
| 244 |
buttons.className = 'buttons'; |
| 245 |
pre_block.insertBefore(buttons, pre_block.firstChild); |
| 246 |
} |
| 247 |
|
| 248 |
var runCodeButton = document.createElement('button'); |
| 249 |
runCodeButton.className = 'fa fa-play play-button'; |
| 250 |
runCodeButton.hidden = true; |
| 251 |
runCodeButton.title = 'Run this code'; |
| 252 |
runCodeButton.setAttribute('aria-label', runCodeButton.title); |
| 253 |
|
| 254 |
buttons.insertBefore(runCodeButton, buttons.firstChild); |
| 255 |
runCodeButton.addEventListener('click', function (e) { |
| 256 |
run_rust_code(pre_block); |
| 257 |
}); |
| 258 |
|
| 259 |
if (window.playground_copyable) { |
| 260 |
var copyCodeClipboardButton = document.createElement('button'); |
| 261 |
copyCodeClipboardButton.className = 'clip-button'; |
| 262 |
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>'; |
| 263 |
copyCodeClipboardButton.title = 'Copy to clipboard'; |
| 264 |
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title); |
| 265 |
|
| 266 |
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild); |
| 267 |
} |
| 268 |
|
| 269 |
let code_block = pre_block.querySelector("code"); |
| 270 |
if (window.ace && code_block.classList.contains("editable")) { |
| 271 |
var undoChangesButton = document.createElement('button'); |
| 272 |
undoChangesButton.className = 'fa fa-history reset-button'; |
| 273 |
undoChangesButton.title = 'Undo changes'; |
| 274 |
undoChangesButton.setAttribute('aria-label', undoChangesButton.title); |
| 275 |
|
| 276 |
buttons.insertBefore(undoChangesButton, buttons.firstChild); |
| 277 |
|
| 278 |
undoChangesButton.addEventListener('click', function () { |
| 279 |
let editor = window.ace.edit(code_block); |
| 280 |
editor.setValue(editor.originalCode); |
| 281 |
editor.clearSelection(); |
| 282 |
}); |
| 283 |
} |
| 284 |
}); |
| 285 |
})(); |
| 286 |
|
| 287 |
(function themes() { |
| 288 |
var html = document.querySelector('html'); |
| 289 |
var themeToggleButton = document.getElementById('theme-toggle'); |
| 290 |
var themePopup = document.getElementById('theme-list'); |
| 291 |
var themeColorMetaTag = document.querySelector('meta[name="theme-color"]'); |
| 292 |
var themeIds = []; |
| 293 |
themePopup.querySelectorAll('button.theme').forEach(function (el) { |
| 294 |
themeIds.push(el.id); |
| 295 |
}); |
| 296 |
var stylesheets = { |
| 297 |
ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"), |
| 298 |
tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"), |
| 299 |
highlight: document.querySelector("[href$='highlight.css']"), |
| 300 |
}; |
| 301 |
|
| 302 |
function showThemes() { |
| 303 |
themePopup.style.display = 'block'; |
| 304 |
themeToggleButton.setAttribute('aria-expanded', true); |
| 305 |
themePopup.querySelector("button#" + get_theme()).focus(); |
| 306 |
} |
| 307 |
|
| 308 |
function updateThemeSelected() { |
| 309 |
themePopup.querySelectorAll('.theme-selected').forEach(function (el) { |
| 310 |
el.classList.remove('theme-selected'); |
| 311 |
}); |
| 312 |
themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected'); |
| 313 |
} |
| 314 |
|
| 315 |
function hideThemes() { |
| 316 |
themePopup.style.display = 'none'; |
| 317 |
themeToggleButton.setAttribute('aria-expanded', false); |
| 318 |
themeToggleButton.focus(); |
| 319 |
} |
| 320 |
|
| 321 |
function get_theme() { |
| 322 |
var theme; |
| 323 |
try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { } |
| 324 |
if (theme === null || theme === undefined || !themeIds.includes(theme)) { |
| 325 |
return default_theme; |
| 326 |
} else { |
| 327 |
return theme; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
function set_theme(theme, store = true) { |
| 332 |
let ace_theme; |
| 333 |
|
| 334 |
if (theme == 'coal' || theme == 'navy' || theme == 'muon') { |
| 335 |
stylesheets.ayuHighlight.disabled = true; |
| 336 |
stylesheets.tomorrowNight.disabled = false; |
| 337 |
stylesheets.highlight.disabled = true; |
| 338 |
|
| 339 |
ace_theme = "ace/theme/tomorrow_night"; |
| 340 |
} else if (theme == 'ayu') { |
| 341 |
stylesheets.ayuHighlight.disabled = false; |
| 342 |
stylesheets.tomorrowNight.disabled = true; |
| 343 |
stylesheets.highlight.disabled = true; |
| 344 |
ace_theme = "ace/theme/tomorrow_night"; |
| 345 |
} else { |
| 346 |
stylesheets.ayuHighlight.disabled = true; |
| 347 |
stylesheets.tomorrowNight.disabled = true; |
| 348 |
stylesheets.highlight.disabled = false; |
| 349 |
ace_theme = "ace/theme/dawn"; |
| 350 |
} |
| 351 |
|
| 352 |
setTimeout(function () { |
| 353 |
themeColorMetaTag.content = getComputedStyle(document.documentElement).backgroundColor; |
| 354 |
}, 1); |
| 355 |
|
| 356 |
if (window.ace && window.editors) { |
| 357 |
window.editors.forEach(function (editor) { |
| 358 |
editor.setTheme(ace_theme); |
| 359 |
}); |
| 360 |
} |
| 361 |
|
| 362 |
var previousTheme = get_theme(); |
| 363 |
|
| 364 |
if (store) { |
| 365 |
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { } |
| 366 |
} |
| 367 |
|
| 368 |
html.classList.remove(previousTheme); |
| 369 |
html.classList.add(theme); |
| 370 |
updateThemeSelected(); |
| 371 |
} |
| 372 |
|
| 373 |
// Set theme |
| 374 |
var theme = get_theme(); |
| 375 |
|
| 376 |
set_theme(theme, false); |
| 377 |
|
| 378 |
themeToggleButton.addEventListener('click', function () { |
| 379 |
if (themePopup.style.display === 'block') { |
| 380 |
hideThemes(); |
| 381 |
} else { |
| 382 |
showThemes(); |
| 383 |
} |
| 384 |
}); |
| 385 |
|
| 386 |
themePopup.addEventListener('click', function (e) { |
| 387 |
var theme; |
| 388 |
if (e.target.className === "theme") { |
| 389 |
theme = e.target.id; |
| 390 |
} else if (e.target.parentElement.className === "theme") { |
| 391 |
theme = e.target.parentElement.id; |
| 392 |
} else { |
| 393 |
return; |
| 394 |
} |
| 395 |
set_theme(theme); |
| 396 |
}); |
| 397 |
|
| 398 |
themePopup.addEventListener('focusout', function(e) { |
| 399 |
// e.relatedTarget is null in Safari and Firefox on macOS (see workaround below) |
| 400 |
if (!!e.relatedTarget && !themeToggleButton.contains(e.relatedTarget) && !themePopup.contains(e.relatedTarget)) { |
| 401 |
hideThemes(); |
| 402 |
} |
| 403 |
}); |
| 404 |
|
| 405 |
// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628 |
| 406 |
document.addEventListener('click', function(e) { |
| 407 |
if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) { |
| 408 |
hideThemes(); |
| 409 |
} |
| 410 |
}); |
| 411 |
|
| 412 |
document.addEventListener('keydown', function (e) { |
| 413 |
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; } |
| 414 |
if (!themePopup.contains(e.target)) { return; } |
| 415 |
|
| 416 |
switch (e.key) { |
| 417 |
case 'Escape': |
| 418 |
e.preventDefault(); |
| 419 |
hideThemes(); |
| 420 |
break; |
| 421 |
case 'ArrowUp': |
| 422 |
e.preventDefault(); |
| 423 |
var li = document.activeElement.parentElement; |
| 424 |
if (li && li.previousElementSibling) { |
| 425 |
li.previousElementSibling.querySelector('button').focus(); |
| 426 |
} |
| 427 |
break; |
| 428 |
case 'ArrowDown': |
| 429 |
e.preventDefault(); |
| 430 |
var li = document.activeElement.parentElement; |
| 431 |
if (li && li.nextElementSibling) { |
| 432 |
li.nextElementSibling.querySelector('button').focus(); |
| 433 |
} |
| 434 |
break; |
| 435 |
case 'Home': |
| 436 |
e.preventDefault(); |
| 437 |
themePopup.querySelector('li:first-child button').focus(); |
| 438 |
break; |
| 439 |
case 'End': |
| 440 |
e.preventDefault(); |
| 441 |
themePopup.querySelector('li:last-child button').focus(); |
| 442 |
break; |
| 443 |
} |
| 444 |
}); |
| 445 |
})(); |
| 446 |
|
| 447 |
(function sidebar() { |
| 448 |
var body = document.querySelector("body"); |
| 449 |
var sidebar = document.getElementById("sidebar"); |
| 450 |
var sidebarLinks = document.querySelectorAll('#sidebar a'); |
| 451 |
var sidebarToggleButton = document.getElementById("sidebar-toggle"); |
| 452 |
var sidebarToggleAnchor = document.getElementById("sidebar-toggle-anchor"); |
| 453 |
var sidebarResizeHandle = document.getElementById("sidebar-resize-handle"); |
| 454 |
var firstContact = null; |
| 455 |
|
| 456 |
function showSidebar() { |
| 457 |
body.classList.remove('sidebar-hidden') |
| 458 |
body.classList.add('sidebar-visible'); |
| 459 |
Array.from(sidebarLinks).forEach(function (link) { |
| 460 |
link.setAttribute('tabIndex', 0); |
| 461 |
}); |
| 462 |
sidebarToggleButton.setAttribute('aria-expanded', true); |
| 463 |
sidebar.setAttribute('aria-hidden', false); |
| 464 |
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { } |
| 465 |
} |
| 466 |
|
| 467 |
function hideSidebar() { |
| 468 |
body.classList.remove('sidebar-visible') |
| 469 |
body.classList.add('sidebar-hidden'); |
| 470 |
Array.from(sidebarLinks).forEach(function (link) { |
| 471 |
link.setAttribute('tabIndex', -1); |
| 472 |
}); |
| 473 |
sidebarToggleButton.setAttribute('aria-expanded', false); |
| 474 |
sidebar.setAttribute('aria-hidden', true); |
| 475 |
try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { } |
| 476 |
} |
| 477 |
|
| 478 |
// Toggle sidebar |
| 479 |
sidebarToggleAnchor.addEventListener('change', function sidebarToggle() { |
| 480 |
if (sidebarToggleAnchor.checked) { |
| 481 |
var current_width = parseInt( |
| 482 |
document.documentElement.style.getPropertyValue('--sidebar-width'), 10); |
| 483 |
if (current_width < 150) { |
| 484 |
document.documentElement.style.setProperty('--sidebar-width', '150px'); |
| 485 |
} |
| 486 |
showSidebar(); |
| 487 |
} else { |
| 488 |
hideSidebar(); |
| 489 |
} |
| 490 |
}); |
| 491 |
|
| 492 |
sidebarResizeHandle.addEventListener('mousedown', initResize, false); |
| 493 |
|
| 494 |
function initResize(e) { |
| 495 |
window.addEventListener('mousemove', resize, false); |
| 496 |
window.addEventListener('mouseup', stopResize, false); |
| 497 |
body.classList.add('sidebar-resizing'); |
| 498 |
} |
| 499 |
function resize(e) { |
| 500 |
var pos = (e.clientX - sidebar.offsetLeft); |
| 501 |
if (pos < 20) { |
| 502 |
hideSidebar(); |
| 503 |
} else { |
| 504 |
if (body.classList.contains("sidebar-hidden")) { |
| 505 |
showSidebar(); |
| 506 |
} |
| 507 |
pos = Math.min(pos, window.innerWidth - 100); |
| 508 |
document.documentElement.style.setProperty('--sidebar-width', pos + 'px'); |
| 509 |
} |
| 510 |
} |
| 511 |
//on mouseup remove windows functions mousemove & mouseup |
| 512 |
function stopResize(e) { |
| 513 |
body.classList.remove('sidebar-resizing'); |
| 514 |
window.removeEventListener('mousemove', resize, false); |
| 515 |
window.removeEventListener('mouseup', stopResize, false); |
| 516 |
} |
| 517 |
|
| 518 |
document.addEventListener('touchstart', function (e) { |
| 519 |
firstContact = { |
| 520 |
x: e.touches[0].clientX, |
| 521 |
time: Date.now() |
| 522 |
}; |
| 523 |
}, { passive: true }); |
| 524 |
|
| 525 |
document.addEventListener('touchmove', function (e) { |
| 526 |
if (!firstContact) |
| 527 |
return; |
| 528 |
|
| 529 |
var curX = e.touches[0].clientX; |
| 530 |
var xDiff = curX - firstContact.x, |
| 531 |
tDiff = Date.now() - firstContact.time; |
| 532 |
|
| 533 |
if (tDiff < 250 && Math.abs(xDiff) >= 150) { |
| 534 |
if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300)) |
| 535 |
showSidebar(); |
| 536 |
else if (xDiff < 0 && curX < 300) |
| 537 |
hideSidebar(); |
| 538 |
|
| 539 |
firstContact = null; |
| 540 |
} |
| 541 |
}, { passive: true }); |
| 542 |
})(); |
| 543 |
|
| 544 |
(function chapterNavigation() { |
| 545 |
document.addEventListener('keydown', function (e) { |
| 546 |
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; } |
| 547 |
if (window.search && window.search.hasFocus()) { return; } |
| 548 |
var html = document.querySelector('html'); |
| 549 |
|
| 550 |
function next() { |
| 551 |
var nextButton = document.querySelector('.nav-chapters.next'); |
| 552 |
if (nextButton) { |
| 553 |
window.location.href = nextButton.href; |
| 554 |
} |
| 555 |
} |
| 556 |
function prev() { |
| 557 |
var previousButton = document.querySelector('.nav-chapters.previous'); |
| 558 |
if (previousButton) { |
| 559 |
window.location.href = previousButton.href; |
| 560 |
} |
| 561 |
} |
| 562 |
switch (e.key) { |
| 563 |
case 'ArrowRight': |
| 564 |
e.preventDefault(); |
| 565 |
if (html.dir == 'rtl') { |
| 566 |
prev(); |
| 567 |
} else { |
| 568 |
next(); |
| 569 |
} |
| 570 |
break; |
| 571 |
case 'ArrowLeft': |
| 572 |
e.preventDefault(); |
| 573 |
if (html.dir == 'rtl') { |
| 574 |
next(); |
| 575 |
} else { |
| 576 |
prev(); |
| 577 |
} |
| 578 |
break; |
| 579 |
} |
| 580 |
}); |
| 581 |
})(); |
| 582 |
|
| 583 |
(function clipboard() { |
| 584 |
var clipButtons = document.querySelectorAll('.clip-button'); |
| 585 |
|
| 586 |
function hideTooltip(elem) { |
| 587 |
elem.firstChild.innerText = ""; |
| 588 |
elem.className = 'clip-button'; |
| 589 |
} |
| 590 |
|
| 591 |
function showTooltip(elem, msg) { |
| 592 |
elem.firstChild.innerText = msg; |
| 593 |
elem.className = 'clip-button tooltipped'; |
| 594 |
} |
| 595 |
|
| 596 |
var clipboardSnippets = new ClipboardJS('.clip-button', { |
| 597 |
text: function (trigger) { |
| 598 |
hideTooltip(trigger); |
| 599 |
let playground = trigger.closest("pre"); |
| 600 |
return playground_text(playground, false); |
| 601 |
} |
| 602 |
}); |
| 603 |
|
| 604 |
Array.from(clipButtons).forEach(function (clipButton) { |
| 605 |
clipButton.addEventListener('mouseout', function (e) { |
| 606 |
hideTooltip(e.currentTarget); |
| 607 |
}); |
| 608 |
}); |
| 609 |
|
| 610 |
clipboardSnippets.on('success', function (e) { |
| 611 |
e.clearSelection(); |
| 612 |
showTooltip(e.trigger, "Copied!"); |
| 613 |
}); |
| 614 |
|
| 615 |
clipboardSnippets.on('error', function (e) { |
| 616 |
showTooltip(e.trigger, "Clipboard error!"); |
| 617 |
}); |
| 618 |
})(); |
| 619 |
|
| 620 |
(function scrollToTop () { |
| 621 |
var menuTitle = document.querySelector('.menu-title'); |
| 622 |
|
| 623 |
menuTitle.addEventListener('click', function () { |
| 624 |
document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' }); |
| 625 |
}); |
| 626 |
})(); |
| 627 |
|
| 628 |
(function controllMenu() { |
| 629 |
var menu = document.getElementById('menu-bar'); |
| 630 |
|
| 631 |
(function controllPosition() { |
| 632 |
var scrollTop = document.scrollingElement.scrollTop; |
| 633 |
var prevScrollTop = scrollTop; |
| 634 |
var minMenuY = -menu.clientHeight - 50; |
| 635 |
// When the script loads, the page can be at any scroll (e.g. if you reforesh it). |
| 636 |
menu.style.top = scrollTop + 'px'; |
| 637 |
// Same as parseInt(menu.style.top.slice(0, -2), but faster |
| 638 |
var topCache = menu.style.top.slice(0, -2); |
| 639 |
menu.classList.remove('sticky'); |
| 640 |
var stickyCache = false; // Same as menu.classList.contains('sticky'), but faster |
| 641 |
document.addEventListener('scroll', function () { |
| 642 |
scrollTop = Math.max(document.scrollingElement.scrollTop, 0); |
| 643 |
// `null` means that it doesn't need to be updated |
| 644 |
var nextSticky = null; |
| 645 |
var nextTop = null; |
| 646 |
var scrollDown = scrollTop > prevScrollTop; |
| 647 |
var menuPosAbsoluteY = topCache - scrollTop; |
| 648 |
if (scrollDown) { |
| 649 |
nextSticky = false; |
| 650 |
if (menuPosAbsoluteY > 0) { |
| 651 |
nextTop = prevScrollTop; |
| 652 |
} |
| 653 |
} else { |
| 654 |
if (menuPosAbsoluteY > 0) { |
| 655 |
nextSticky = true; |
| 656 |
} else if (menuPosAbsoluteY < minMenuY) { |
| 657 |
nextTop = prevScrollTop + minMenuY; |
| 658 |
} |
| 659 |
} |
| 660 |
if (nextSticky === true && stickyCache === false) { |
| 661 |
menu.classList.add('sticky'); |
| 662 |
stickyCache = true; |
| 663 |
} else if (nextSticky === false && stickyCache === true) { |
| 664 |
menu.classList.remove('sticky'); |
| 665 |
stickyCache = false; |
| 666 |
} |
| 667 |
if (nextTop !== null) { |
| 668 |
menu.style.top = nextTop + 'px'; |
| 669 |
topCache = nextTop; |
| 670 |
} |
| 671 |
prevScrollTop = scrollTop; |
| 672 |
}, { passive: true }); |
| 673 |
})(); |
| 674 |
(function controllBorder() { |
| 675 |
function updateBorder() { |
| 676 |
if (menu.offsetTop === 0) { |
| 677 |
menu.classList.remove('bordered'); |
| 678 |
} else { |
| 679 |
menu.classList.add('bordered'); |
| 680 |
} |
| 681 |
} |
| 682 |
updateBorder(); |
| 683 |
document.addEventListener('scroll', updateBorder, { passive: true }); |
| 684 |
})(); |
| 685 |
})(); |