scripts.html 23 KB

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