book.js 25 KB

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