scripts.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. {{ $permalink:= .Permalink }}
  261. var permalink = JSON.parse({{ $permalink | jsonify }});
  262. var searchResults = null;
  263. var searchMenu = null;
  264. var searchText = null;
  265. {{ $enableSearchHighlight := ($.Param "enableSearchHighlight") }}
  266. var enableSearchHighlight = JSON.parse({{ $enableSearchHighlight | jsonify }});
  267. var fuse = null;
  268. (function initFuse() {
  269. var xhr = new XMLHttpRequest();
  270. xhr.open('GET', permalink + "index.json");
  271. xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  272. xhr.onload = function () {
  273. if (xhr.status === 200) {
  274. fuse = new Fuse(JSON.parse(xhr.response.toString('utf-8')), {
  275. keys: ['title', 'description', 'content'],
  276. includeMatches: enableSearchHighlight,
  277. shouldSort: true,
  278. threshold: 0.4,
  279. location: 0,
  280. distance: 100,
  281. maxPatternLength: 32,
  282. minMatchCharLength: 1,
  283. });
  284. }
  285. else {
  286. console.error(`[${xhr.status}]Error:`, xhr.statusText);
  287. }
  288. };
  289. xhr.send();
  290. })();
  291. function makeLi(ulElem, obj) {
  292. var li = document.createElement('li');
  293. li.className = 'search-result__item';
  294. var a = document.createElement('a');
  295. a.innerHTML = obj.title;
  296. a.setAttribute('class', 'search-result__item--title');
  297. a.setAttribute('href', obj.permalink);
  298. var descDiv = document.createElement('div');
  299. descDiv.setAttribute('class', 'search-result__item--desc');
  300. if (obj.description) {
  301. descDiv.innerHTML = obj.description;
  302. } else if (obj.content) {
  303. descDiv.innerHTML = obj.content.substring(0, 80);
  304. }
  305. li.appendChild(a);
  306. li.appendChild(descDiv);
  307. ulElem.appendChild(li);
  308. }
  309. function makeHighlightLi(ulElem, obj) {
  310. var li = document.createElement('li');
  311. li.className = 'search-result__item';
  312. var descDiv = null;
  313. var a = document.createElement('a');
  314. a.innerHTML = obj.item.title;
  315. a.setAttribute('class', 'search-result__item--title');
  316. a.setAttribute('href', obj.item.uri);
  317. if (obj.matches && obj.matches.length) {
  318. for (var i = 0; i < obj.matches.length; i++) {
  319. if ('title' === obj.matches[i].key) {
  320. a = document.createElement('a');
  321. a.innerHTML = generateHighlightedText(obj.matches[i].value, obj.matches[i].indices);
  322. a.setAttribute('class', 'search-result__item--title');
  323. a.setAttribute('href', obj.item.uri);
  324. }
  325. if ('description' === obj.matches[i].key) {
  326. descDiv = document.createElement('div');
  327. descDiv.setAttribute('class', 'search-result__item--desc');
  328. descDiv.innerHTML = generateHighlightedText(obj.item.description, obj.matches[i].indices);
  329. } else if ('content' === obj.matches[i].key) {
  330. if (!descDiv) {
  331. descDiv = document.createElement('div');
  332. descDiv.setAttribute('class', 'search-result__item--desc');
  333. descDiv.innerHTML = generateHighlightedText(obj.item.content.substring(0, 80), obj.matches[i].indices);
  334. }
  335. } else {
  336. if (obj.item.description) {
  337. descDiv = document.createElement('div');
  338. descDiv.setAttribute('class', 'search-result__item--desc');
  339. descDiv.innerHTML = obj.item.description;
  340. } else {
  341. descDiv = document.createElement('div');
  342. descDiv.setAttribute('class', 'search-result__item--desc');
  343. descDiv.innerHTML = obj.item.content.substring(0, 80);
  344. }
  345. }
  346. }
  347. li.appendChild(a);
  348. if (descDiv) {
  349. li.appendChild(descDiv);
  350. }
  351. if (li) {
  352. ulElem.appendChild(li);
  353. }
  354. }
  355. }
  356. function renderSearchResults(searchText, results) {
  357. searchResults = document.getElementById('search-results');
  358. searchMenu = document.getElementById('search-menu');
  359. searchResults.setAttribute('class', 'dd is-active');
  360. var ul = document.createElement('ul');
  361. ul.setAttribute('class', 'dd-content search-content');
  362. if (results.length) {
  363. results.forEach(function (result) {
  364. var li = document.createElement('li');
  365. var a = document.createElement('a');
  366. a.setAttribute('href', result.uri);
  367. a.setAttribute('class', 'dd-item');
  368. a.appendChild(li);
  369. var titleDiv = document.createElement('div');
  370. titleDiv.innerHTML = result.title;
  371. titleDiv.setAttribute('class', 'search-result__item--title');
  372. var descDiv = document.createElement('div');
  373. descDiv.setAttribute('class', 'search-result__item--desc');
  374. if (result.description) {
  375. descDiv.innerHTML = result.description;
  376. } else if (result.content) {
  377. descDiv.innerHTML = result.content.substring(0, 80);
  378. }
  379. li.appendChild(titleDiv);
  380. li.appendChild(descDiv);
  381. ul.appendChild(a);
  382. });
  383. } else {
  384. var li = document.createElement('li');
  385. li.setAttribute('class', 'dd-item');
  386. li.innerText = 'No results found';
  387. ul.appendChild(li);
  388. }
  389. while (searchMenu.hasChildNodes()) {
  390. searchMenu.removeChild(
  391. searchMenu.lastChild
  392. );
  393. }
  394. searchMenu.appendChild(ul);
  395. }
  396. function renderSearchHighlightResults(searchText, results) {
  397. searchResults = document.getElementById('search-results');
  398. searchMenu = document.getElementById('search-menu');
  399. searchResults.setAttribute('class', 'dd is-active');
  400. var ul = document.createElement('ul');
  401. ul.setAttribute('class', 'dd-content search-content');
  402. if (results.length) {
  403. results.forEach(function (result) {
  404. var li = document.createElement('li');
  405. var a = document.createElement('a');
  406. var descDiv = null;
  407. a.setAttribute('href', result.item.uri);
  408. a.setAttribute('class', 'dd-item');
  409. a.appendChild(li);
  410. var titleDiv = document.createElement('div');
  411. titleDiv.innerHTML = result.item.title;
  412. titleDiv.setAttribute('class', 'search-result__item--title');
  413. if (result.matches && result.matches.length) {
  414. for (var i = 0; i < result.matches.length; i++) {
  415. if ('title' === result.matches[i].key) {
  416. titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
  417. }
  418. if ('description' === result.matches[i].key) {
  419. descDiv = document.createElement('div');
  420. descDiv.setAttribute('class', 'search-result__item--desc');
  421. descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
  422. } else if ('content' === result.matches[i].key) {
  423. if (!descDiv) {
  424. descDiv = document.createElement('div');
  425. descDiv.setAttribute('class', 'search-result__item--desc');
  426. descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 80), result.matches[i].indices);
  427. }
  428. } else {
  429. if (result.item.description) {
  430. descDiv = document.createElement('div');
  431. descDiv.setAttribute('class', 'search-result__item--desc');
  432. descDiv.innerHTML = result.item.description;
  433. } else {
  434. descDiv = document.createElement('div');
  435. descDiv.setAttribute('class', 'search-result__item--desc');
  436. descDiv.innerHTML = result.item.content.substring(0, 80);
  437. }
  438. }
  439. }
  440. li.appendChild(titleDiv);
  441. if (descDiv) {
  442. li.appendChild(descDiv);
  443. }
  444. ul.appendChild(a);
  445. }
  446. });
  447. } else {
  448. var li = document.createElement('li');
  449. li.setAttribute('class', 'dd-item');
  450. li.innerText = 'No results found';
  451. ul.appendChild(li);
  452. }
  453. while (searchMenu.hasChildNodes()) {
  454. searchMenu.removeChild(
  455. searchMenu.lastChild
  456. );
  457. }
  458. searchMenu.appendChild(ul);
  459. }
  460. function renderSearchResultsMobile(searchText, results) {
  461. searchResults = document.getElementById('search-mobile-results');
  462. var content = document.createElement('div');
  463. content.setAttribute('class', 'mobile-search__content');
  464. if (results.length > 0) {
  465. results.forEach(function (result) {
  466. var item = document.createElement('a');
  467. item.setAttribute('href', result.uri);
  468. 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>';
  469. content.appendChild(item);
  470. });
  471. } else {
  472. var item = document.createElement('span');
  473. content.appendChild(item);
  474. }
  475. let wrap = document.getElementById('search-mobile-results');
  476. while (wrap.firstChild) {
  477. wrap.removeChild(wrap.firstChild)
  478. }
  479. searchResults.appendChild(content);
  480. }
  481. function renderSearchHighlightResultsMobile(searchText, results) {
  482. searchResults = document.getElementById('search-mobile-results');
  483. var ul = document.createElement('div');
  484. ul.setAttribute('class', 'mobile-search__content');
  485. if (results.length) {
  486. results.forEach(function (result) {
  487. var li = document.createElement('li');
  488. var a = document.createElement('a');
  489. var descDiv = null;
  490. a.setAttribute('href', result.item.uri);
  491. a.appendChild(li);
  492. li.setAttribute('class', 'mobile-search__item');
  493. var titleDiv = document.createElement('div');
  494. titleDiv.innerHTML = result.item.title;
  495. titleDiv.setAttribute('class', 'mobile-search__item--title');
  496. if (result.matches && result.matches.length) {
  497. for (var i = 0; i < result.matches.length; i++) {
  498. if ('title' === result.matches[i].key) {
  499. titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
  500. }
  501. if ('description' === result.matches[i].key) {
  502. descDiv = document.createElement('div');
  503. descDiv.setAttribute('class', 'mobile-search__item--desc');
  504. descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
  505. } else if ('content' === result.matches[i].key) {
  506. if (!descDiv) {
  507. descDiv = document.createElement('div');
  508. descDiv.setAttribute('class', 'mobile-search__item--desc');
  509. descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 150), result.matches[i].indices);
  510. }
  511. } else {
  512. if (result.item.description) {
  513. descDiv = document.createElement('div');
  514. descDiv.setAttribute('class', 'mobile-search__item--desc');
  515. descDiv.innerHTML = result.item.description;
  516. } else {
  517. descDiv = document.createElement('div');
  518. descDiv.setAttribute('class', 'mobile-search__item--desc');
  519. descDiv.innerHTML = result.item.content.substring(0, 150);
  520. }
  521. }
  522. }
  523. li.appendChild(titleDiv);
  524. if (descDiv) {
  525. li.appendChild(descDiv);
  526. }
  527. ul.appendChild(a);
  528. }
  529. });
  530. } else {
  531. var item = document.createElement('span');
  532. ul.appendChild(item);
  533. }
  534. let wrap = document.getElementById('search-mobile-results');
  535. while (wrap.firstChild) {
  536. wrap.removeChild(wrap.firstChild)
  537. }
  538. searchResults.appendChild(ul);
  539. }
  540. function generateHighlightedText(text, regions) {
  541. if (!regions) {
  542. return text;
  543. }
  544. var content = '', nextUnhighlightedRegionStartingIndex = 0;
  545. regions.forEach(function(region) {
  546. if (region[0] === region[1]) {
  547. return null;
  548. }
  549. content += '' +
  550. text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
  551. '<span class="search__highlight">' +
  552. text.substring(region[0], region[1] + 1) +
  553. '</span>' +
  554. '';
  555. nextUnhighlightedRegionStartingIndex = region[1] + 1;
  556. });
  557. content += text.substring(nextUnhighlightedRegionStartingIndex);
  558. return content;
  559. };
  560. var searchElem = document.getElementById('search');
  561. var searchMobile = document.getElementById('search-mobile');
  562. var searchResultsContainer = document.getElementById('search-results');
  563. searchElem ?
  564. searchElem.addEventListener('input', function(e) {
  565. if (!e.target.value | window.innerWidth < 770) {
  566. searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
  567. return null;
  568. }
  569. searchText = e.target.value;
  570. var results = fuse.search(e.target.value);
  571. if (enableSearchHighlight) {
  572. renderSearchHighlightResults(searchText, results);
  573. } else {
  574. renderSearchResults(searchText, results);
  575. }
  576. var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
  577. dropdownItems ? dropdownItems.forEach(function (item) {
  578. item.addEventListener('mousedown', function (e) {
  579. console.log('mousedown');
  580. e.target.click();
  581. });
  582. }) : null;
  583. }) : null;
  584. searchElem ?
  585. searchElem.addEventListener('blur', function() {
  586. if (window.innerWidth < 770) {
  587. return null;
  588. }
  589. searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
  590. }) : null;
  591. searchElem ?
  592. searchElem.addEventListener('click', function(e) {
  593. if (window.innerWidth < 770) {
  594. return null;
  595. }
  596. if (!e.target.value) {
  597. searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
  598. return null;
  599. }
  600. searchText = e.target.value;
  601. var results = fuse.search(e.target.value);
  602. if (enableSearchHighlight) {
  603. renderSearchHighlightResults(searchText, results);
  604. } else {
  605. renderSearchResults(searchText, results);
  606. }
  607. var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
  608. dropdownItems ? dropdownItems.forEach(function (item) {
  609. item.addEventListener('mousedown', function (e) {
  610. console.log('mousedown');
  611. e.target.click();
  612. });
  613. }) : null;
  614. }) : null;
  615. var searchMenuElem = document.getElementById("search-menu");
  616. var activeItem = document.querySelector('#search-menu .dd-item.is-active');
  617. var activeIndex = null;
  618. var items = null;
  619. var searchContainerMaxHeight = 350;
  620. searchElem.addEventListener('keydown', function(e) {
  621. if (window.innerWidth < 770) {
  622. return null;
  623. }
  624. var items = document.querySelectorAll('#search-menu .dd-item');
  625. if (e.key === 'ArrowDown') {
  626. if (activeIndex === null) {
  627. activeIndex = 0;
  628. items[activeIndex].classList.remove('is-active');
  629. } else {
  630. items[activeIndex].classList.remove('is-active');
  631. activeIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
  632. }
  633. items[activeIndex].classList.add('is-active');
  634. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  635. if (overflowedPixel > 0) {
  636. document.querySelector(".search-content").scrollTop += items[activeIndex].getBoundingClientRect().height;
  637. } else if (activeIndex === 0) {
  638. document.querySelector(".search-content").scrollTop = 0;
  639. }
  640. } else if (e.key === 'ArrowUp') {
  641. if (activeIndex === null) {
  642. activeIndex = items.length - 1;
  643. items[activeIndex].classList.remove('is-active');
  644. } else {
  645. items[activeIndex].classList.remove('is-active');
  646. activeIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
  647. }
  648. items[activeIndex].classList.add('is-active');
  649. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  650. if (overflowedPixel < 0) {
  651. document.querySelector(".search-content").scrollTop -= items[activeIndex].getBoundingClientRect().height;
  652. } else {
  653. document.querySelector(".search-content").scrollTop = overflowedPixel + items[activeIndex].getBoundingClientRect().height;
  654. }
  655. } else if (e.key === 'Enter') {
  656. var currentItemLink = items[activeIndex].getAttribute('href');
  657. if (currentItemLink) {
  658. location.href = currentItemLink;
  659. }
  660. } else if (e.key === 'Escape') {
  661. e.target.value = null;
  662. if (searchResults) {
  663. searchResults.classList.remove('is-active');
  664. }
  665. }
  666. });
  667. searchMobile ?
  668. searchMobile.addEventListener('input', function(e) {
  669. if (!e.target.value) {
  670. let wrap = document.getElementById('search-mobile-results');
  671. while (wrap.firstChild) {
  672. wrap.removeChild(wrap.firstChild);
  673. }
  674. return null;
  675. }
  676. searchText = e.target.value;
  677. var results = fuse.search(e.target.value);
  678. if (enableSearchHighlight) {
  679. renderSearchHighlightResultsMobile(searchText, results);
  680. } else {
  681. renderSearchResultsMobile(searchText, results);
  682. }
  683. var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
  684. dropdownItems ? dropdownItems.forEach(function (item) {
  685. item.addEventListener('mousedown', function (e) {
  686. console.log('mousedown');
  687. e.target.click();
  688. });
  689. }) : null;
  690. }) : null;
  691. // ============================================================
  692. }
  693. </script>