scripts.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. {{ $enquire := resources.Get "js/enquire.min.js" | resources.Fingerprint }}
  2. <script src="{{ $enquire.RelPermalink }}"></script>
  3. {{ $fuse := resources.Get "js/fuse.min.js" | resources.Fingerprint }}
  4. <script defer src="{{ $fuse.RelPermalink }}"></script>
  5. {{ $lazysizes := resources.Get "js/lazysizes.min.js" | resources.Fingerprint }}
  6. <script defer src="{{ $lazysizes.RelPermalink }}"></script>
  7. {{ $getParents := resources.Get "js/helper/getParents.js" | resources.Minify | resources.Fingerprint }}
  8. <script defer src="{{ $getParents.RelPermalink }}"></script>
  9. {{ $fadeinout := resources.Get "js/helper/fadeinout.js" | resources.Minify | resources.Fingerprint }}
  10. <script defer src="{{ $fadeinout.RelPermalink }}"></script>
  11. <script>
  12. "use strict";
  13. window.onload = function() {
  14. // ========================== expand ==========================
  15. var expandBtn = document.querySelectorAll('.expand__button');
  16. for (let i = 0; i < expandBtn.length; i++) {
  17. expandBtn[i].addEventListener("click", function () {
  18. var content = this.nextElementSibling;
  19. if (content.style.maxHeight) {
  20. content.style.maxHeight = null;
  21. this.querySelector('svg').classList.add('expand-icon__right');
  22. this.querySelector('svg').classList.remove('expand-icon__down');
  23. } else {
  24. content.style.maxHeight = content.scrollHeight + "px";
  25. this.querySelector('svg').classList.remove('expand-icon__right');
  26. this.querySelector('svg').classList.add('expand-icon__down');
  27. }
  28. });
  29. }
  30. // ============================================================
  31. // ======================= toggle theme =======================
  32. var root = document.getElementById('root');
  33. var toggleToLightBtn = document.getElementById('toggleToLight');
  34. var toggleToDarkBtn = document.getElementById('toggleToDark');
  35. toggleToDark.onclick = function(e) {
  36. root.className = 'theme__dark';
  37. localStorage.setItem('theme', 'dark');
  38. toggleToLightBtn.className = 'navbar__icons--icon';
  39. toggleToDarkBtn.className = 'hide';
  40. }
  41. toggleToLight.onclick = function (e) {
  42. root.className = 'theme__light';
  43. localStorage.setItem('theme', 'light');
  44. toggleToLightBtn.className = 'hide';
  45. toggleToDarkBtn.className = 'navbar__icons--icon';
  46. }
  47. // =================== section menu collapse ==================
  48. document.querySelectorAll('.menu__list').forEach(function(elem) {
  49. if (elem.classList.contains('active')) {
  50. elem.style.maxHeight = elem.scrollHeight + "px";
  51. }
  52. });
  53. document.querySelectorAll('.menu__title--collapse').forEach(function(elem) {
  54. elem.addEventListener('click', function (e) {
  55. var content = this.nextElementSibling;
  56. var menuTitleIcon = this.querySelector('.menu__title--icon');
  57. if (!content) {
  58. return null;
  59. }
  60. if (content.style.maxHeight) {
  61. content.style.maxHeight = null;
  62. content.classList.remove('active');
  63. menuTitleIcon.classList.add('right');
  64. menuTitleIcon.classList.remove('down');
  65. } else {
  66. content.style.maxHeight = content.scrollHeight + "px";
  67. content.classList.add('active');
  68. menuTitleIcon.classList.remove('right');
  69. menuTitleIcon.classList.add('down');
  70. }
  71. });
  72. });
  73. // ============================================================
  74. // ========================== drawer ==========================
  75. var mobileLogo = document.getElementById('mobileLogo');
  76. var modal = document.getElementById("myModal");
  77. var drawer = document.getElementById('myDrawer');
  78. var drawerCloseBtn = document.querySelector('.drawer__close');
  79. var openDrawer = function() {
  80. modal.style.left = 0;
  81. modal.style.opacity = 1;
  82. drawer.style.left = 0;
  83. drawer.style.transition = "left 0.3s ease-out";
  84. drawer.style.webkitTransition = "left 0.3s ease-out";
  85. }
  86. var closeDrawer = function() {
  87. modal.style.opacity = 0;
  88. drawer.style.left = '-100%';
  89. setTimeout(function () {
  90. modal.style.left = '-100%';
  91. }, 250);
  92. }
  93. mobileLogo.onclick = function () {
  94. openDrawer();
  95. localStorage.setItem('isDrawerOpen', 'true');
  96. }
  97. modal.onclick = function () {
  98. closeDrawer();
  99. localStorage.setItem('isDrawerOpen', 'false');
  100. }
  101. drawerCloseBtn.onclick = function () {
  102. closeDrawer();
  103. localStorage.setItem('isDrawerOpen', 'false');
  104. }
  105. // ==============================================================
  106. // =========================== scroll ===========================
  107. var lastScrollTop = window.pageYOffset || document.documentElement.scrollTop;
  108. var tocElem = document.querySelector('.toc');
  109. var tableOfContentsElem = tocElem ? tocElem.querySelector('#TableOfContents') : null;
  110. var singleContentsElem = document.querySelector('.single__contents');
  111. window.onscroll = function () {
  112. var st = window.pageYOffset || document.documentElement.scrollTop;
  113. if (st > lastScrollTop) { // scroll down
  114. singleContentsElem ?
  115. singleContentsElem.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach(function(elem) {
  116. if (document.documentElement.scrollTop >= elem.offsetTop) {
  117. if (tableOfContentsElem) {
  118. var id = elem.getAttribute('id');
  119. tocElem.querySelectorAll('a').forEach(function (elem) {
  120. elem.classList.remove('active');
  121. });
  122. tocElem.querySelector('a[href="#' + id + '"]') ?
  123. tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
  124. }
  125. }
  126. }) : null;
  127. } else { // scroll up
  128. singleContentsElem ?
  129. singleContentsElem.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach(function(elem) {
  130. if (document.documentElement.scrollTop >= elem.offsetTop) {
  131. if (tableOfContentsElem) {
  132. var id = elem.getAttribute('id');
  133. tocElem.querySelectorAll('a').forEach(function (elem) {
  134. elem.classList.remove('active');
  135. });
  136. tocElem.querySelector('a[href="#' + id + '"]') ?
  137. tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
  138. }
  139. }
  140. }) : null;
  141. }
  142. lastScrollTop = st <= 0 ? 0 : st;
  143. };
  144. // ============================================================
  145. // ====================== mobile search =======================
  146. var mobileSearchInputElem = document.querySelector('#search-mobile');
  147. var mobileSearchClassElem = document.querySelector('.mobile-search');
  148. var mobileSearchBtnElem = document.querySelector('#mobileSearchBtn');
  149. var mobileSearchCloseBtnElem = document.querySelector('#search-mobile-close');
  150. var mobileSearchContainer = document.querySelector('#search-mobile-container');
  151. var mobileSearchResultsElem = document.querySelector('#search-mobile-results');
  152. var htmlElem = document.querySelector('html');
  153. if (mobileSearchClassElem) {
  154. mobileSearchClassElem.style.display = 'none';
  155. }
  156. mobileSearchBtnElem ?
  157. mobileSearchBtnElem.addEventListener('click', function () {
  158. if (mobileSearchContainer) {
  159. mobileSearchContainer.style.display = 'block';
  160. }
  161. if (mobileSearchInputElem) {
  162. mobileSearchInputElem.focus();
  163. }
  164. if (htmlElem) {
  165. htmlElem.style.overflowY = 'hidden';
  166. }
  167. }) : null;
  168. mobileSearchCloseBtnElem ?
  169. mobileSearchCloseBtnElem.addEventListener('click', function() {
  170. if (mobileSearchContainer) {
  171. mobileSearchContainer.style.display = 'none';
  172. }
  173. if (mobileSearchInputElem) {
  174. mobileSearchInputElem.value = '';
  175. }
  176. if (mobileSearchResultsElem) {
  177. while (mobileSearchResultsElem.firstChild) {
  178. mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
  179. }
  180. }
  181. if (htmlElem) {
  182. htmlElem.style.overflowY = 'visible';
  183. }
  184. }) : null;
  185. mobileSearchInputElem ?
  186. mobileSearchInputElem.addEventListener('keydown', function(e) {
  187. if (e.key === 'Escape') {
  188. if (mobileSearchContainer) {
  189. mobileSearchContainer.style.display = 'none';
  190. }
  191. if (mobileSearchInputElem) {
  192. mobileSearchInputElem.value = '';
  193. }
  194. if (mobileSearchResultsElem) {
  195. while (mobileSearchResultsElem.firstChild) {
  196. mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
  197. }
  198. }
  199. if (htmlElem) {
  200. htmlElem.style.overflowY = 'visible';
  201. }
  202. }
  203. }) : null;
  204. // ============================================================
  205. // ======================= theme change =======================
  206. var localTheme = localStorage.getItem('theme');
  207. var rootEleme = document.getElementById('root');
  208. var selectThemeElem = document.querySelectorAll('.select-theme');
  209. var selectThemeItemElem = document.querySelectorAll('.select-theme__item');
  210. if (localTheme) {
  211. selectThemeItemElem ?
  212. selectThemeItemElem.forEach(function (elem) {
  213. if (elem.text.trim() === localTheme) {
  214. elem.classList.add('is-active');
  215. } else {
  216. elem.classList.remove('is-active');
  217. }
  218. }) : null;
  219. }
  220. selectThemeItemElem ?
  221. selectThemeItemElem.forEach(function (v, i) {
  222. v.addEventListener('click', function (e) {
  223. var selectedThemeVariant = e.target.text.trim();
  224. localStorage.setItem('theme', selectedThemeVariant);
  225. rootEleme.removeAttribute('class');
  226. rootEleme.classList.add(`theme__${selectedThemeVariant}`);
  227. selectThemeElem.forEach(function(rootElem) {
  228. rootElem.querySelectorAll('a').forEach(function (elem) {
  229. if (elem.classList) {
  230. if (elem.text.trim() === selectedThemeVariant) {
  231. if (!elem.classList.contains('is-active')) {
  232. elem.classList.add('is-active');
  233. }
  234. } else {
  235. if (elem.classList.contains('is-active')) {
  236. elem.classList.remove('is-active');
  237. }
  238. }
  239. }
  240. });
  241. });
  242. if (window.mermaid) {
  243. if (selectedThemeVariant === "dark" || selectedThemeVariant === "hacker") {
  244. mermaid.initialize({ theme: 'dark' });
  245. location.reload();
  246. } else {
  247. mermaid.initialize({ theme: 'default' });
  248. location.reload();
  249. }
  250. }
  251. var utterances = document.querySelector('iframe');
  252. if (utterances) {
  253. utterances.contentWindow.postMessage({
  254. type: 'set-theme',
  255. theme: selectedThemeVariant === "dark" || selectedThemeVariant === "hacker" ? 'photon-dark' : selectedThemeVariant === 'kimbie' ? 'github-dark-orange' : 'github-light',
  256. }, 'https://utteranc.es');
  257. }
  258. });
  259. }) : null;
  260. // ============================================================
  261. // ========================== search ==========================
  262. var baseurl = null;
  263. {{ $siteBaseURL:= .Site.BaseURL }}
  264. var siteBaseURL = JSON.parse({{ $siteBaseURL | jsonify }});
  265. var siteBaseChecker = /\/\w+\//i;
  266. var isSlug = siteBaseChecker.test(siteBaseURL);
  267. var isThemeSite = location.origin.includes('themes.gohugo.io');
  268. {{ if .Site.IsMultiLingual }}
  269. if (isThemeSite) {
  270. baseurl = "{{.Site.BaseURL}}{{.Site.LanguagePrefix}}";
  271. } else {
  272. var hasLangPostfix = location.pathname.includes("/{{.Site.Language.Lang}}");
  273. if (hasLangPostfix) {
  274. if (isSlug) {
  275. baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0] + "{{.Site.Language.Lang}}";
  276. } else {
  277. baseurl = location.origin + "/{{.Site.Language.Lang}}";
  278. }
  279. } else {
  280. if (isSlug) {
  281. baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0];
  282. } else {
  283. baseurl = location.origin;
  284. }
  285. }
  286. }
  287. {{ else }}
  288. if (isThemeSite) {
  289. baseurl = "{{.Site.BaseURL}}";
  290. } else {
  291. if (isSlug) {
  292. baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0];
  293. } else {
  294. baseurl = location.origin;
  295. }
  296. }
  297. {{ end }}
  298. var searchResults = null;
  299. var searchMenu = null;
  300. var searchText = null;
  301. {{ $enableSearchHighlight := ($.Param "enableSearchHighlight") }}
  302. var enableSearchHighlight = JSON.parse({{ $enableSearchHighlight | jsonify }});
  303. var fuse = null;
  304. function endsWith(str, suffix) {
  305. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  306. }
  307. function initFuse() {
  308. if (!endsWith(baseurl, "/")) {
  309. baseurl = baseurl + '/';
  310. };
  311. var xhr = new XMLHttpRequest();
  312. xhr.open('GET', baseurl + "index.json");
  313. xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  314. xhr.onload = function () {
  315. if (xhr.status === 200) {
  316. fuse = new Fuse(JSON.parse(xhr.response.toString('utf-8')), {
  317. keys: ['title', 'description', 'content'],
  318. includeMatches: enableSearchHighlight,
  319. shouldSort: true,
  320. threshold: 0.4,
  321. location: 0,
  322. distance: 100,
  323. maxPatternLength: 32,
  324. minMatchCharLength: 1,
  325. });
  326. }
  327. else {
  328. console.error(`[${xhr.status}]Error:`, xhr.statusText);
  329. }
  330. };
  331. xhr.send();
  332. }
  333. function renderSearchResults(results) { // [{}, {}, ...] or [{item: {}, matches: []}, ...]
  334. searchResults = document.getElementById('search-results');
  335. searchMenu = document.getElementById('search-menu');
  336. searchResults.setAttribute('class', 'dd is-active');
  337. var content = document.createElement('div');
  338. content.setAttribute('class', 'dd-content search-content');
  339. if (results.length > 0) {
  340. results.forEach(function (result) {
  341. var item = document.createElement('a');
  342. item.setAttribute('href', result.uri);
  343. item.setAttribute('class', 'dd-item');
  344. item.innerHTML = `<div class="menu-item"><div class="menu-item__title">📄 ${result.title}</div><div class="menu-item__desc">${result.description ? result.description : result.content}</div></div>`;
  345. content.appendChild(item);
  346. });
  347. } else {
  348. var item = document.createElement('span');
  349. item.setAttribute('class', 'dd-item');
  350. item.innerText = 'No results found';
  351. content.appendChild(item);
  352. }
  353. while (searchMenu.hasChildNodes()) {
  354. searchMenu.removeChild(
  355. searchMenu.lastChild
  356. );
  357. }
  358. searchMenu.appendChild(content);
  359. }
  360. function renderSearchHighlightResults(results) {
  361. searchResults = document.getElementById('search-results');
  362. searchMenu = document.getElementById('search-menu');
  363. searchResults.setAttribute('class', 'dd is-active');
  364. var content = document.createElement('div');
  365. content.setAttribute('class', 'dd-content search-content');
  366. if (results.length > 0) {
  367. results.forEach(function (result) {
  368. var item = document.createElement('a');
  369. item.setAttribute('href', result.item.uri);
  370. item.setAttribute('class', 'dd-item');
  371. item.innerHTML = `<div class="menu-item"><div class="menu-item__title">📄 ${generateHighlightedText(result.item.title, result.matches[0].indices)}</div><div class="menu-item__desc">${result.matches[1] ? generateHighlightedText(result.item.description, result.matches[1].indices) : result.matches[2] ? generateHighlightedText(result.item.content, result.matches[2].indices) : ''}</div></div>`;
  372. content.appendChild(item);
  373. });
  374. } else {
  375. var item = document.createElement('span');
  376. item.setAttribute('class', 'dd-item');
  377. item.innerText = 'No results found';
  378. content.appendChild(item);
  379. }
  380. while (searchMenu.hasChildNodes()) {
  381. searchMenu.removeChild(
  382. searchMenu.lastChild
  383. );
  384. }
  385. searchMenu.appendChild(content);
  386. }
  387. function renderSearchResultsMobile(results) {
  388. searchResults = document.getElementById('search-mobile-results');
  389. var content = document.createElement('div');
  390. content.setAttribute('class', 'mobile-search__content');
  391. if (results.length > 0) {
  392. results.forEach(function (result) {
  393. var item = document.createElement('a');
  394. item.setAttribute('href', result.uri);
  395. item.innerHTML = `<div class="mobile-search__item"><div class="mobile-search__item--title">📄 ${result.title}</div><div class="mobile-search__item--desc">${result.description ? result.description : result.content}</div></div>`;
  396. content.appendChild(item);
  397. });
  398. } else {
  399. var item = document.createElement('span');
  400. content.appendChild(item);
  401. }
  402. let wrap = document.getElementById('search-mobile-results');
  403. while (wrap.firstChild) {
  404. wrap.removeChild(wrap.firstChild)
  405. }
  406. searchResults.appendChild(content);
  407. }
  408. function renderSearchHighlightResultsMobile(results) {
  409. searchResults = document.getElementById('search-mobile-results');
  410. var content = document.createElement('div');
  411. content.setAttribute('class', 'mobile-search__content');
  412. if (results.length > 0) {
  413. results.forEach(function (result) {
  414. var item = document.createElement('a');
  415. item.setAttribute('href', result.item.uri);
  416. item.innerHTML = `<div class="mobile-search__item"><div class="mobile-search__item--title">📄 ${generateHighlightedText(result.item.title, result.matches[0].indices)}</div><div class="mobile-search__item--desc">${result.matches[1] ? generateHighlightedText(result.item.description, result.matches[1].indices) : result.matches[2] ? generateHighlightedText(result.item.content, result.matches[2].indices) : ''}</div></div>`;
  417. content.appendChild(item);
  418. });
  419. } else {
  420. var item = document.createElement('span');
  421. content.appendChild(item);
  422. }
  423. let wrap = document.getElementById('search-mobile-results');
  424. while (wrap.firstChild) {
  425. wrap.removeChild(wrap.firstChild)
  426. }
  427. searchResults.appendChild(content);
  428. }
  429. function generateHighlightedText(text, regions) {
  430. if (!regions) {
  431. return text;
  432. }
  433. var content = '', nextUnhighlightedRegionStartingIndex = 0;
  434. regions.forEach(function(region) {
  435. if (region[0] === region[1]) {
  436. return null;
  437. }
  438. content += '' +
  439. text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
  440. '<span class="search__highlight">' +
  441. text.substring(region[0], region[1] + 1) +
  442. '</span>' +
  443. '';
  444. nextUnhighlightedRegionStartingIndex = region[1] + 1;
  445. });
  446. content += text.substring(nextUnhighlightedRegionStartingIndex);
  447. return content;
  448. };
  449. initFuse();
  450. var searchElem = document.getElementById('search');
  451. var searchMobile = document.getElementById('search-mobile');
  452. searchElem.addEventListener('input', function(e) {
  453. if (!e.target.value) {
  454. document.getElementById('search-results').setAttribute('class', 'dd');
  455. return null;
  456. }
  457. if (window.innerWidth < 770) {
  458. return null;
  459. }
  460. searchText = e.target.value;
  461. var results = fuse.search(e.target.value);
  462. if (enableSearchHighlight) {
  463. renderSearchHighlightResults(results);
  464. } else {
  465. renderSearchResults(results);
  466. }
  467. });
  468. searchElem.addEventListener('blur', function() {
  469. if (window.innerWidth < 770) {
  470. return null;
  471. }
  472. setTimeout(function () {
  473. document.getElementById('search-results').setAttribute('class', 'dd');
  474. }, 100);
  475. });
  476. searchElem.addEventListener('click', function(e) {
  477. if (window.innerWidth < 770) {
  478. return null;
  479. }
  480. if (!e.target.value) {
  481. document.getElementById('search-results').setAttribute('class', 'dd');
  482. return null;
  483. }
  484. searchText = e.target.value;
  485. var results = fuse.search(e.target.value);
  486. if (enableSearchHighlight) {
  487. renderSearchHighlightResults(results);
  488. } else {
  489. renderSearchResults(results);
  490. }
  491. });
  492. function indexInParent(node) {
  493. var children = node.parentNode.childNodes;
  494. var num = 0;
  495. for (var i = 0; i < children.length; i++) {
  496. if (children[i] == node) return num;
  497. if (children[i].nodeType == 1) num++;
  498. }
  499. return -1;
  500. }
  501. var searchMenuElem = document.getElementById("search-menu");
  502. var activeItem = document.querySelector('#search-menu .dd-item.is-active');
  503. var activeIndex = null;
  504. var items = null;
  505. var searchContainerMaxHeight = 350;
  506. searchElem.addEventListener('keydown', function(e) {
  507. if (window.innerWidth < 770) {
  508. return null;
  509. }
  510. var items = document.querySelectorAll('#search-menu .dd-item');
  511. if (e.key === 'ArrowDown') {
  512. if (activeIndex === null) {
  513. activeIndex = 0;
  514. items[activeIndex].classList.remove('is-active');
  515. } else {
  516. items[activeIndex].classList.remove('is-active');
  517. activeIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
  518. }
  519. items[activeIndex].classList.add('is-active');
  520. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  521. if (overflowedPixel > 0) {
  522. document.querySelector(".search-content").scrollTop += items[activeIndex].getBoundingClientRect().height;
  523. } else if (activeIndex === 0) {
  524. document.querySelector(".search-content").scrollTop = 0;
  525. }
  526. } else if (e.key === 'ArrowUp') {
  527. if (activeIndex === null) {
  528. activeIndex = items.length - 1;
  529. items[activeIndex].classList.remove('is-active');
  530. } else {
  531. items[activeIndex].classList.remove('is-active');
  532. activeIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
  533. }
  534. items[activeIndex].classList.add('is-active');
  535. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  536. if (overflowedPixel < 0) {
  537. document.querySelector(".search-content").scrollTop -= items[activeIndex].getBoundingClientRect().height;
  538. } else {
  539. document.querySelector(".search-content").scrollTop = overflowedPixel + items[activeIndex].getBoundingClientRect().height;
  540. }
  541. } else if (e.key === 'Enter') {
  542. var currentItemLink = items[activeIndex].getAttribute('href');
  543. if (currentItemLink) {
  544. location.href = currentItemLink;
  545. }
  546. } else if (e.key === 'Escape') {
  547. e.target.value = null;
  548. if (searchResults) {
  549. searchResults.classList.remove('is-active');
  550. }
  551. }
  552. });
  553. searchMobile.addEventListener('input', function(e) {
  554. if (!e.target.value) {
  555. let wrap = document.getElementById('search-mobile-results');
  556. while (wrap.firstChild) {
  557. wrap.removeChild(wrap.firstChild);
  558. }
  559. return null;
  560. }
  561. searchText = e.target.value;
  562. var results = fuse.search(e.target.value);
  563. renderSearchResultsMobile(results);
  564. if (enableSearchHighlight) {
  565. renderSearchHighlightResultsMobile(results);
  566. } else {
  567. renderSearchResultsMobile(results);
  568. }
  569. });
  570. // ============================================================
  571. }
  572. </script>