book.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 edition = "2015";
  97. if(classes.contains("edition2018")) {
  98. edition = "2018";
  99. } else if(classes.contains("edition2021")) {
  100. edition = "2021";
  101. }
  102. var params = {
  103. version: "stable",
  104. optimize: "0",
  105. code: text,
  106. edition: edition
  107. };
  108. if (text.indexOf("#![feature") !== -1) {
  109. params.version = "nightly";
  110. }
  111. result_block.innerText = "Running...";
  112. fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
  113. headers: {
  114. 'Content-Type': "application/json",
  115. },
  116. method: 'POST',
  117. mode: 'cors',
  118. body: JSON.stringify(params)
  119. })
  120. .then(response => response.json())
  121. .then(response => {
  122. if (response.result.trim() === '') {
  123. result_block.innerText = "No output";
  124. result_block.classList.add("result-no-output");
  125. } else {
  126. result_block.innerText = response.result;
  127. result_block.classList.remove("result-no-output");
  128. }
  129. })
  130. .catch(error => result_block.innerText = "Playground Communication: " + error.message);
  131. }
  132. // Syntax highlighting Configuration
  133. hljs.configure({
  134. tabReplace: ' ', // 4 spaces
  135. languages: [], // Languages used for auto-detection
  136. });
  137. let code_nodes = Array
  138. .from(document.querySelectorAll('code'))
  139. // Don't highlight `inline code` blocks in headers.
  140. .filter(function (node) {return !node.parentElement.classList.contains("header"); });
  141. if (window.ace) {
  142. // language-rust class needs to be removed for editable
  143. // blocks or highlightjs will capture events
  144. code_nodes
  145. .filter(function (node) {return node.classList.contains("editable"); })
  146. .forEach(function (block) { block.classList.remove('language-rust'); });
  147. Array
  148. code_nodes
  149. .filter(function (node) {return !node.classList.contains("editable"); })
  150. .forEach(function (block) { hljs.highlightBlock(block); });
  151. } else {
  152. code_nodes.forEach(function (block) { hljs.highlightBlock(block); });
  153. }
  154. // Adding the hljs class gives code blocks the color css
  155. // even if highlighting doesn't apply
  156. code_nodes.forEach(function (block) { block.classList.add('hljs'); });
  157. Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
  158. var lines = Array.from(block.querySelectorAll('.boring'));
  159. // If no lines were hidden, return
  160. if (!lines.length) { return; }
  161. block.classList.add("hide-boring");
  162. var buttons = document.createElement('div');
  163. buttons.className = 'buttons';
  164. buttons.innerHTML = "<button class=\"fa fa-eye\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
  165. // add expand button
  166. var pre_block = block.parentNode;
  167. pre_block.insertBefore(buttons, pre_block.firstChild);
  168. pre_block.querySelector('.buttons').addEventListener('click', function (e) {
  169. if (e.target.classList.contains('fa-eye')) {
  170. e.target.classList.remove('fa-eye');
  171. e.target.classList.add('fa-eye-slash');
  172. e.target.title = 'Hide lines';
  173. e.target.setAttribute('aria-label', e.target.title);
  174. block.classList.remove('hide-boring');
  175. } else if (e.target.classList.contains('fa-eye-slash')) {
  176. e.target.classList.remove('fa-eye-slash');
  177. e.target.classList.add('fa-eye');
  178. e.target.title = 'Show hidden lines';
  179. e.target.setAttribute('aria-label', e.target.title);
  180. block.classList.add('hide-boring');
  181. }
  182. });
  183. });
  184. if (window.playground_copyable) {
  185. Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
  186. var pre_block = block.parentNode;
  187. if (!pre_block.classList.contains('playground')) {
  188. var buttons = pre_block.querySelector(".buttons");
  189. if (!buttons) {
  190. buttons = document.createElement('div');
  191. buttons.className = 'buttons';
  192. pre_block.insertBefore(buttons, pre_block.firstChild);
  193. }
  194. var clipButton = document.createElement('button');
  195. clipButton.className = 'fa fa-copy clip-button';
  196. clipButton.title = 'Copy to clipboard';
  197. clipButton.setAttribute('aria-label', clipButton.title);
  198. clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
  199. buttons.insertBefore(clipButton, buttons.firstChild);
  200. }
  201. });
  202. }
  203. // Process playground code blocks
  204. Array.from(document.querySelectorAll(".playground")).forEach(function (pre_block) {
  205. // Add play button
  206. var buttons = pre_block.querySelector(".buttons");
  207. if (!buttons) {
  208. buttons = document.createElement('div');
  209. buttons.className = 'buttons';
  210. pre_block.insertBefore(buttons, pre_block.firstChild);
  211. }
  212. var runCodeButton = document.createElement('button');
  213. runCodeButton.className = 'fa fa-play play-button';
  214. runCodeButton.hidden = true;
  215. runCodeButton.title = 'Run this code';
  216. runCodeButton.setAttribute('aria-label', runCodeButton.title);
  217. buttons.insertBefore(runCodeButton, buttons.firstChild);
  218. runCodeButton.addEventListener('click', function (e) {
  219. run_rust_code(pre_block);
  220. });
  221. if (window.playground_copyable) {
  222. var copyCodeClipboardButton = document.createElement('button');
  223. copyCodeClipboardButton.className = 'fa fa-copy clip-button';
  224. copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
  225. copyCodeClipboardButton.title = 'Copy to clipboard';
  226. copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
  227. buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
  228. }
  229. let code_block = pre_block.querySelector("code");
  230. if (window.ace && code_block.classList.contains("editable")) {
  231. var undoChangesButton = document.createElement('button');
  232. undoChangesButton.className = 'fa fa-history reset-button';
  233. undoChangesButton.title = 'Undo changes';
  234. undoChangesButton.setAttribute('aria-label', undoChangesButton.title);
  235. buttons.insertBefore(undoChangesButton, buttons.firstChild);
  236. undoChangesButton.addEventListener('click', function () {
  237. let editor = window.ace.edit(code_block);
  238. editor.setValue(editor.originalCode);
  239. editor.clearSelection();
  240. });
  241. }
  242. });
  243. })();
  244. (function themes() {
  245. var html = document.querySelector('html');
  246. var themeToggleButton = document.getElementById('theme-toggle');
  247. var themePopup = document.getElementById('theme-list');
  248. var themeColorMetaTag = document.querySelector('meta[name="theme-color"]');
  249. var stylesheets = {
  250. ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"),
  251. tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"),
  252. highlight: document.querySelector("[href$='highlight.css']"),
  253. };
  254. function showThemes() {
  255. themePopup.style.display = 'block';
  256. themeToggleButton.setAttribute('aria-expanded', true);
  257. themePopup.querySelector("button#" + get_theme()).focus();
  258. }
  259. function hideThemes() {
  260. themePopup.style.display = 'none';
  261. themeToggleButton.setAttribute('aria-expanded', false);
  262. themeToggleButton.focus();
  263. }
  264. function get_theme() {
  265. var theme;
  266. try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
  267. if (theme === null || theme === undefined) {
  268. return default_theme;
  269. } else {
  270. return theme;
  271. }
  272. }
  273. function set_theme(theme, store = true) {
  274. let ace_theme;
  275. if (theme == 'coal' || theme == 'navy') {
  276. stylesheets.ayuHighlight.disabled = true;
  277. stylesheets.tomorrowNight.disabled = false;
  278. stylesheets.highlight.disabled = true;
  279. ace_theme = "ace/theme/tomorrow_night";
  280. } else if (theme == 'ayu') {
  281. stylesheets.ayuHighlight.disabled = false;
  282. stylesheets.tomorrowNight.disabled = true;
  283. stylesheets.highlight.disabled = true;
  284. ace_theme = "ace/theme/tomorrow_night";
  285. } else {
  286. stylesheets.ayuHighlight.disabled = true;
  287. stylesheets.tomorrowNight.disabled = true;
  288. stylesheets.highlight.disabled = false;
  289. ace_theme = "ace/theme/dawn";
  290. }
  291. setTimeout(function () {
  292. themeColorMetaTag.content = getComputedStyle(document.body).backgroundColor;
  293. }, 1);
  294. if (window.ace && window.editors) {
  295. window.editors.forEach(function (editor) {
  296. editor.setTheme(ace_theme);
  297. });
  298. }
  299. var previousTheme = get_theme();
  300. if (store) {
  301. try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
  302. }
  303. html.classList.remove(previousTheme);
  304. html.classList.add(theme);
  305. }
  306. // Set theme
  307. var theme = get_theme();
  308. set_theme(theme, false);
  309. themeToggleButton.addEventListener('click', function () {
  310. if (themePopup.style.display === 'block') {
  311. hideThemes();
  312. } else {
  313. showThemes();
  314. }
  315. });
  316. themePopup.addEventListener('click', function (e) {
  317. var theme;
  318. if (e.target.className === "theme") {
  319. theme = e.target.id;
  320. } else if (e.target.parentElement.className === "theme") {
  321. theme = e.target.parentElement.id;
  322. } else {
  323. return;
  324. }
  325. set_theme(theme);
  326. });
  327. themePopup.addEventListener('focusout', function(e) {
  328. // e.relatedTarget is null in Safari and Firefox on macOS (see workaround below)
  329. if (!!e.relatedTarget && !themeToggleButton.contains(e.relatedTarget) && !themePopup.contains(e.relatedTarget)) {
  330. hideThemes();
  331. }
  332. });
  333. // Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628
  334. document.addEventListener('click', function(e) {
  335. if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) {
  336. hideThemes();
  337. }
  338. });
  339. document.addEventListener('keydown', function (e) {
  340. if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
  341. if (!themePopup.contains(e.target)) { return; }
  342. switch (e.key) {
  343. case 'Escape':
  344. e.preventDefault();
  345. hideThemes();
  346. break;
  347. case 'ArrowUp':
  348. e.preventDefault();
  349. var li = document.activeElement.parentElement;
  350. if (li && li.previousElementSibling) {
  351. li.previousElementSibling.querySelector('button').focus();
  352. }
  353. break;
  354. case 'ArrowDown':
  355. e.preventDefault();
  356. var li = document.activeElement.parentElement;
  357. if (li && li.nextElementSibling) {
  358. li.nextElementSibling.querySelector('button').focus();
  359. }
  360. break;
  361. case 'Home':
  362. e.preventDefault();
  363. themePopup.querySelector('li:first-child button').focus();
  364. break;
  365. case 'End':
  366. e.preventDefault();
  367. themePopup.querySelector('li:last-child button').focus();
  368. break;
  369. }
  370. });
  371. })();
  372. (function sidebar() {
  373. var html = document.querySelector("html");
  374. var sidebar = document.getElementById("sidebar");
  375. var sidebarLinks = document.querySelectorAll('#sidebar a');
  376. var sidebarToggleButton = document.getElementById("sidebar-toggle");
  377. var sidebarResizeHandle = document.getElementById("sidebar-resize-handle");
  378. var firstContact = null;
  379. function showSidebar() {
  380. html.classList.remove('sidebar-hidden')
  381. html.classList.add('sidebar-visible');
  382. Array.from(sidebarLinks).forEach(function (link) {
  383. link.setAttribute('tabIndex', 0);
  384. });
  385. sidebarToggleButton.setAttribute('aria-expanded', true);
  386. sidebar.setAttribute('aria-hidden', false);
  387. try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
  388. }
  389. var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle');
  390. function toggleSection(ev) {
  391. ev.currentTarget.parentElement.classList.toggle('expanded');
  392. }
  393. Array.from(sidebarAnchorToggles).forEach(function (el) {
  394. el.addEventListener('click', toggleSection);
  395. });
  396. function hideSidebar() {
  397. html.classList.remove('sidebar-visible')
  398. html.classList.add('sidebar-hidden');
  399. Array.from(sidebarLinks).forEach(function (link) {
  400. link.setAttribute('tabIndex', -1);
  401. });
  402. sidebarToggleButton.setAttribute('aria-expanded', false);
  403. sidebar.setAttribute('aria-hidden', true);
  404. try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
  405. }
  406. // Toggle sidebar
  407. sidebarToggleButton.addEventListener('click', function sidebarToggle() {
  408. if (html.classList.contains("sidebar-hidden")) {
  409. var current_width = parseInt(
  410. document.documentElement.style.getPropertyValue('--sidebar-width'), 10);
  411. if (current_width < 150) {
  412. document.documentElement.style.setProperty('--sidebar-width', '150px');
  413. }
  414. showSidebar();
  415. } else if (html.classList.contains("sidebar-visible")) {
  416. hideSidebar();
  417. } else {
  418. if (getComputedStyle(sidebar)['transform'] === 'none') {
  419. hideSidebar();
  420. } else {
  421. showSidebar();
  422. }
  423. }
  424. });
  425. sidebarResizeHandle.addEventListener('mousedown', initResize, false);
  426. function initResize(e) {
  427. window.addEventListener('mousemove', resize, false);
  428. window.addEventListener('mouseup', stopResize, false);
  429. html.classList.add('sidebar-resizing');
  430. }
  431. function resize(e) {
  432. var pos = (e.clientX - sidebar.offsetLeft);
  433. if (pos < 20) {
  434. hideSidebar();
  435. } else {
  436. if (html.classList.contains("sidebar-hidden")) {
  437. showSidebar();
  438. }
  439. pos = Math.min(pos, window.innerWidth - 100);
  440. document.documentElement.style.setProperty('--sidebar-width', pos + 'px');
  441. }
  442. }
  443. //on mouseup remove windows functions mousemove & mouseup
  444. function stopResize(e) {
  445. html.classList.remove('sidebar-resizing');
  446. window.removeEventListener('mousemove', resize, false);
  447. window.removeEventListener('mouseup', stopResize, false);
  448. }
  449. document.addEventListener('touchstart', function (e) {
  450. firstContact = {
  451. x: e.touches[0].clientX,
  452. time: Date.now()
  453. };
  454. }, { passive: true });
  455. document.addEventListener('touchmove', function (e) {
  456. if (!firstContact)
  457. return;
  458. var curX = e.touches[0].clientX;
  459. var xDiff = curX - firstContact.x,
  460. tDiff = Date.now() - firstContact.time;
  461. if (tDiff < 250 && Math.abs(xDiff) >= 150) {
  462. if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300))
  463. showSidebar();
  464. else if (xDiff < 0 && curX < 300)
  465. hideSidebar();
  466. firstContact = null;
  467. }
  468. }, { passive: true });
  469. // Scroll sidebar to current active section
  470. var activeSection = document.getElementById("sidebar").querySelector(".active");
  471. if (activeSection) {
  472. // https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
  473. activeSection.scrollIntoView({ block: 'center' });
  474. }
  475. })();
  476. (function chapterNavigation() {
  477. document.addEventListener('keydown', function (e) {
  478. if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
  479. if (window.search && window.search.hasFocus()) { return; }
  480. switch (e.key) {
  481. case 'ArrowRight':
  482. e.preventDefault();
  483. var nextButton = document.querySelector('.nav-chapters.next');
  484. if (nextButton) {
  485. window.location.href = nextButton.href;
  486. }
  487. break;
  488. case 'ArrowLeft':
  489. e.preventDefault();
  490. var previousButton = document.querySelector('.nav-chapters.previous');
  491. if (previousButton) {
  492. window.location.href = previousButton.href;
  493. }
  494. break;
  495. }
  496. });
  497. })();
  498. (function clipboard() {
  499. var clipButtons = document.querySelectorAll('.clip-button');
  500. function hideTooltip(elem) {
  501. elem.firstChild.innerText = "";
  502. elem.className = 'fa fa-copy clip-button';
  503. }
  504. function showTooltip(elem, msg) {
  505. elem.firstChild.innerText = msg;
  506. elem.className = 'fa fa-copy tooltipped';
  507. }
  508. var clipboardSnippets = new ClipboardJS('.clip-button', {
  509. text: function (trigger) {
  510. hideTooltip(trigger);
  511. let playground = trigger.closest("pre");
  512. return playground_text(playground);
  513. }
  514. });
  515. Array.from(clipButtons).forEach(function (clipButton) {
  516. clipButton.addEventListener('mouseout', function (e) {
  517. hideTooltip(e.currentTarget);
  518. });
  519. });
  520. clipboardSnippets.on('success', function (e) {
  521. e.clearSelection();
  522. showTooltip(e.trigger, "Copied!");
  523. });
  524. clipboardSnippets.on('error', function (e) {
  525. showTooltip(e.trigger, "Clipboard error!");
  526. });
  527. })();
  528. (function scrollToTop () {
  529. var menuTitle = document.querySelector('.menu-title');
  530. menuTitle.addEventListener('click', function () {
  531. document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' });
  532. });
  533. })();
  534. (function controllMenu() {
  535. var menu = document.getElementById('menu-bar');
  536. (function controllPosition() {
  537. var scrollTop = document.scrollingElement.scrollTop;
  538. var prevScrollTop = scrollTop;
  539. var minMenuY = -menu.clientHeight - 50;
  540. // When the script loads, the page can be at any scroll (e.g. if you reforesh it).
  541. menu.style.top = scrollTop + 'px';
  542. // Same as parseInt(menu.style.top.slice(0, -2), but faster
  543. var topCache = menu.style.top.slice(0, -2);
  544. menu.classList.remove('sticky');
  545. var stickyCache = false; // Same as menu.classList.contains('sticky'), but faster
  546. document.addEventListener('scroll', function () {
  547. scrollTop = Math.max(document.scrollingElement.scrollTop, 0);
  548. // `null` means that it doesn't need to be updated
  549. var nextSticky = null;
  550. var nextTop = null;
  551. var scrollDown = scrollTop > prevScrollTop;
  552. var menuPosAbsoluteY = topCache - scrollTop;
  553. if (scrollDown) {
  554. nextSticky = false;
  555. if (menuPosAbsoluteY > 0) {
  556. nextTop = prevScrollTop;
  557. }
  558. } else {
  559. if (menuPosAbsoluteY > 0) {
  560. nextSticky = true;
  561. } else if (menuPosAbsoluteY < minMenuY) {
  562. nextTop = prevScrollTop + minMenuY;
  563. }
  564. }
  565. if (nextSticky === true && stickyCache === false) {
  566. menu.classList.add('sticky');
  567. stickyCache = true;
  568. } else if (nextSticky === false && stickyCache === true) {
  569. menu.classList.remove('sticky');
  570. stickyCache = false;
  571. }
  572. if (nextTop !== null) {
  573. menu.style.top = nextTop + 'px';
  574. topCache = nextTop;
  575. }
  576. prevScrollTop = scrollTop;
  577. }, { passive: true });
  578. })();
  579. (function controllBorder() {
  580. menu.classList.remove('bordered');
  581. document.addEventListener('scroll', function () {
  582. if (menu.offsetTop === 0) {
  583. menu.classList.remove('bordered');
  584. } else {
  585. menu.classList.add('bordered');
  586. }
  587. }, { passive: true });
  588. })();
  589. })();