scripts.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. // ===================== navbar collapse ======================
  15. var navCollapseBtn = document.getElementById('navCollapseBtn');
  16. navCollapseBtn ? navCollapseBtn.addEventListener('click', function(e) {
  17. var navCollapse = document.querySelector('.navbar__collapse');
  18. if (navCollapse) {
  19. var dataOpen = navCollapse.getAttribute('data-open');
  20. if (dataOpen === 'true') {
  21. navCollapse.setAttribute('data-open', 'false');
  22. navCollapse.style.maxHeight = 0;
  23. } else {
  24. navCollapse.setAttribute('data-open', 'true');
  25. navCollapse.style.maxHeight = navCollapse.scrollHeight + "px";
  26. }
  27. }
  28. }) : null;
  29. // ============================================================
  30. // ========================== expand ==========================
  31. var expandBtn = document.querySelectorAll('.expand__button');
  32. for (let i = 0; i < expandBtn.length; i++) {
  33. expandBtn[i].addEventListener("click", function () {
  34. var content = this.nextElementSibling;
  35. if (content.style.maxHeight) {
  36. content.style.maxHeight = null;
  37. this.querySelector('svg').classList.add('expand-icon__right');
  38. this.querySelector('svg').classList.remove('expand-icon__down');
  39. } else {
  40. content.style.maxHeight = content.scrollHeight + "px";
  41. this.querySelector('svg').classList.remove('expand-icon__right');
  42. this.querySelector('svg').classList.add('expand-icon__down');
  43. }
  44. });
  45. }
  46. // ============================================================
  47. // ======================= toggle theme =======================
  48. var root = document.getElementById('root');
  49. var toggleToLightBtn = document.getElementById('toggleToLight');
  50. var toggleToDarkBtn = document.getElementById('toggleToDark');
  51. toggleToDark.onclick = function(e) {
  52. root.className = 'theme__dark';
  53. localStorage.setItem('theme', 'dark');
  54. toggleToLightBtn.className = 'navbar__icons--icon';
  55. toggleToDarkBtn.className = 'hide';
  56. }
  57. toggleToLight.onclick = function (e) {
  58. root.className = 'theme__light';
  59. localStorage.setItem('theme', 'light');
  60. toggleToLightBtn.className = 'hide';
  61. toggleToDarkBtn.className = 'navbar__icons--icon';
  62. }
  63. // =================== section menu collapse ==================
  64. document.querySelectorAll('.menu__list').forEach(function(elem) {
  65. if (elem.classList.contains('active')) {
  66. elem.style.maxHeight = elem.scrollHeight + "px";
  67. }
  68. });
  69. document.querySelectorAll('.menu__title--collapse').forEach(function(elem) {
  70. elem.addEventListener('click', function (e) {
  71. var content = this.nextElementSibling;
  72. var menuTitleIcon = this.querySelector('.menu__title--icon');
  73. if (!content) {
  74. return null;
  75. }
  76. if (content.style.maxHeight) {
  77. content.style.maxHeight = null;
  78. content.classList.remove('active');
  79. menuTitleIcon.classList.add('right');
  80. menuTitleIcon.classList.remove('down');
  81. } else {
  82. content.style.maxHeight = content.scrollHeight + "px";
  83. content.classList.add('active');
  84. menuTitleIcon.classList.remove('right');
  85. menuTitleIcon.classList.add('down');
  86. }
  87. });
  88. });
  89. // ============================================================
  90. // ========================== drawer ==========================
  91. var mobileLogo = document.getElementById('mobileLogo');
  92. var modal = document.getElementById("myModal");
  93. var drawer = document.getElementById('myDrawer');
  94. var drawerCloseBtn = document.querySelector('.drawer__close');
  95. var openDrawer = function() {
  96. modal.style.left = 0;
  97. modal.style.opacity = 1;
  98. drawer.style.left = 0;
  99. }
  100. var closeDrawer = function() {
  101. modal.style.opacity = 0;
  102. drawer.style.left = '-100%';
  103. setTimeout(function () {
  104. modal.style.left = '-100%';
  105. }, 250);
  106. }
  107. mobileLogo.onclick = function () {
  108. openDrawer();
  109. localStorage.setItem('isDrawerOpen', 'true');
  110. }
  111. modal.onclick = function () {
  112. closeDrawer();
  113. localStorage.setItem('isDrawerOpen', 'false');
  114. }
  115. drawerCloseBtn.onclick = function () {
  116. closeDrawer();
  117. localStorage.setItem('isDrawerOpen', 'false');
  118. }
  119. // ==============================================================
  120. // =========================== scroll ===========================
  121. var lastScrollTop = window.pageYOffset || document.documentElement.scrollTop;
  122. var tocElem = document.querySelector('.toc');
  123. var tableOfContentsElem = tocElem ? tocElem.querySelector('#TableOfContents') : null;
  124. var singleContentsElem = document.querySelector('.single__contents');
  125. window.onscroll = function () {
  126. var st = window.pageYOffset || document.documentElement.scrollTop;
  127. if (st > lastScrollTop) { // scroll down
  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. } else { // scroll up
  142. singleContentsElem ?
  143. singleContentsElem.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach(function(elem) {
  144. if (document.documentElement.scrollTop >= elem.offsetTop) {
  145. if (tableOfContentsElem) {
  146. var id = elem.getAttribute('id');
  147. tocElem.querySelectorAll('a').forEach(function (elem) {
  148. elem.classList.remove('active');
  149. });
  150. tocElem.querySelector('a[href="#' + id + '"]') ?
  151. tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
  152. }
  153. }
  154. }) : null;
  155. }
  156. lastScrollTop = st <= 0 ? 0 : st;
  157. };
  158. // ============================================================
  159. // ====================== mobile search =======================
  160. var mobileSearchInputElem = document.querySelector('#search-mobile');
  161. var mobileSearchClassElem = document.querySelector('.mobile-search');
  162. var mobileSearchBtnElem = document.querySelector('#mobileSearchBtn');
  163. var mobileSearchCloseBtnElem = document.querySelector('#search-mobile-close');
  164. var mobileSearchContainer = document.querySelector('#search-mobile-container');
  165. var mobileSearchResultsElem = document.querySelector('#search-mobile-results');
  166. var htmlElem = document.querySelector('html');
  167. if (mobileSearchClassElem) {
  168. mobileSearchClassElem.style.display = 'none';
  169. }
  170. mobileSearchBtnElem ?
  171. mobileSearchBtnElem.addEventListener('click', function () {
  172. if (mobileSearchContainer) {
  173. mobileSearchContainer.style.display = 'block';
  174. }
  175. if (mobileSearchInputElem) {
  176. mobileSearchInputElem.focus();
  177. }
  178. if (htmlElem) {
  179. htmlElem.style.overflowY = 'hidden';
  180. }
  181. }) : null;
  182. mobileSearchCloseBtnElem ?
  183. mobileSearchCloseBtnElem.addEventListener('click', function() {
  184. if (mobileSearchContainer) {
  185. mobileSearchContainer.style.display = 'none';
  186. }
  187. if (mobileSearchInputElem) {
  188. mobileSearchInputElem.value = '';
  189. }
  190. if (mobileSearchResultsElem) {
  191. while (mobileSearchResultsElem.firstChild) {
  192. mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
  193. }
  194. }
  195. if (htmlElem) {
  196. htmlElem.style.overflowY = 'visible';
  197. }
  198. }) : null;
  199. mobileSearchInputElem ?
  200. mobileSearchInputElem.addEventListener('keydown', function(e) {
  201. if (e.key === 'Escape') {
  202. if (mobileSearchContainer) {
  203. mobileSearchContainer.style.display = 'none';
  204. }
  205. if (mobileSearchInputElem) {
  206. mobileSearchInputElem.value = '';
  207. }
  208. if (mobileSearchResultsElem) {
  209. while (mobileSearchResultsElem.firstChild) {
  210. mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
  211. }
  212. }
  213. if (htmlElem) {
  214. htmlElem.style.overflowY = 'visible';
  215. }
  216. }
  217. }) : null;
  218. // ============================================================
  219. // ======================= theme change =======================
  220. var localTheme = localStorage.getItem('theme');
  221. var rootEleme = document.getElementById('root');
  222. var selectThemeElem = document.querySelectorAll('.select-theme');
  223. var selectThemeItemElem = document.querySelectorAll('.select-theme__item');
  224. if (localTheme) {
  225. selectThemeItemElem ?
  226. selectThemeItemElem.forEach(function (elem) {
  227. if (elem.text.trim() === localTheme) {
  228. elem.classList.add('is-active');
  229. } else {
  230. elem.classList.remove('is-active');
  231. }
  232. }) : null;
  233. }
  234. selectThemeItemElem ?
  235. selectThemeItemElem.forEach(function (v, i) {
  236. v.addEventListener('click', function (e) {
  237. var selectedThemeVariant = e.target.text.trim();
  238. localStorage.setItem('theme', selectedThemeVariant);
  239. rootEleme.removeAttribute('class');
  240. rootEleme.classList.add(`theme__${selectedThemeVariant}`);
  241. selectThemeElem.forEach(function(rootElem) {
  242. rootElem.querySelectorAll('a').forEach(function (elem) {
  243. if (elem.classList) {
  244. if (elem.text.trim() === selectedThemeVariant) {
  245. if (!elem.classList.contains('is-active')) {
  246. elem.classList.add('is-active');
  247. }
  248. } else {
  249. if (elem.classList.contains('is-active')) {
  250. elem.classList.remove('is-active');
  251. }
  252. }
  253. }
  254. });
  255. });
  256. if (window.mermaid) {
  257. if (selectedThemeVariant === "dark" || selectedThemeVariant === "hacker") {
  258. mermaid.initialize({ theme: 'dark' });
  259. location.reload();
  260. } else {
  261. mermaid.initialize({ theme: 'default' });
  262. location.reload();
  263. }
  264. }
  265. var utterances = document.querySelector('iframe');
  266. if (utterances) {
  267. utterances.contentWindow.postMessage({
  268. type: 'set-theme',
  269. theme: selectedThemeVariant === "dark" || selectedThemeVariant === "hacker" ? 'photon-dark' : selectedThemeVariant === 'kimbie' ? 'github-dark-orange' : 'github-light',
  270. }, 'https://utteranc.es');
  271. }
  272. });
  273. }) : null;
  274. // ============================================================
  275. // ========================== search ==========================
  276. {{ $permalink:= .Permalink }}
  277. var permalink = JSON.parse({{ $permalink | jsonify }});
  278. var searchResults = null;
  279. var searchMenu = null;
  280. var searchText = null;
  281. {{ $enableSearchHighlight := ($.Param "enableSearchHighlight") }}
  282. var enableSearchHighlight = JSON.parse({{ $enableSearchHighlight | jsonify }});
  283. var fuse = null;
  284. (function initFuse() {
  285. var xhr = new XMLHttpRequest();
  286. xhr.open('GET', permalink + "index.json");
  287. xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  288. xhr.onload = function () {
  289. if (xhr.status === 200) {
  290. fuse = new Fuse(JSON.parse(xhr.response.toString('utf-8')), {
  291. keys: ['title', 'description', 'content'],
  292. includeMatches: enableSearchHighlight,
  293. shouldSort: true,
  294. threshold: 0.4,
  295. location: 0,
  296. distance: 100,
  297. maxPatternLength: 32,
  298. minMatchCharLength: 1,
  299. });
  300. }
  301. else {
  302. console.error(`[${xhr.status}]Error:`, xhr.statusText);
  303. }
  304. };
  305. xhr.send();
  306. })();
  307. function makeLi(ulElem, obj) {
  308. var li = document.createElement('li');
  309. li.className = 'search-result__item';
  310. var a = document.createElement('a');
  311. a.innerHTML = obj.title;
  312. a.setAttribute('class', 'search-result__item--title');
  313. a.setAttribute('href', obj.permalink);
  314. var descDiv = document.createElement('div');
  315. descDiv.setAttribute('class', 'search-result__item--desc');
  316. if (obj.description) {
  317. descDiv.innerHTML = obj.description;
  318. } else if (obj.content) {
  319. descDiv.innerHTML = obj.content.substring(0, 80);
  320. }
  321. li.appendChild(a);
  322. li.appendChild(descDiv);
  323. ulElem.appendChild(li);
  324. }
  325. function makeHighlightLi(ulElem, obj) {
  326. var li = document.createElement('li');
  327. li.className = 'search-result__item';
  328. var descDiv = null;
  329. var a = document.createElement('a');
  330. a.innerHTML = obj.item.title;
  331. a.setAttribute('class', 'search-result__item--title');
  332. a.setAttribute('href', obj.item.uri);
  333. if (obj.matches && obj.matches.length) {
  334. for (var i = 0; i < obj.matches.length; i++) {
  335. if ('title' === obj.matches[i].key) {
  336. a = document.createElement('a');
  337. a.innerHTML = generateHighlightedText(obj.matches[i].value, obj.matches[i].indices);
  338. a.setAttribute('class', 'search-result__item--title');
  339. a.setAttribute('href', obj.item.uri);
  340. }
  341. if ('description' === obj.matches[i].key) {
  342. descDiv = document.createElement('div');
  343. descDiv.setAttribute('class', 'search-result__item--desc');
  344. descDiv.innerHTML = generateHighlightedText(obj.item.description, obj.matches[i].indices);
  345. } else if ('content' === obj.matches[i].key) {
  346. if (!descDiv) {
  347. descDiv = document.createElement('div');
  348. descDiv.setAttribute('class', 'search-result__item--desc');
  349. descDiv.innerHTML = generateHighlightedText(obj.item.content.substring(0, 80), obj.matches[i].indices);
  350. }
  351. } else {
  352. if (obj.item.description) {
  353. descDiv = document.createElement('div');
  354. descDiv.setAttribute('class', 'search-result__item--desc');
  355. descDiv.innerHTML = obj.item.description;
  356. } else {
  357. descDiv = document.createElement('div');
  358. descDiv.setAttribute('class', 'search-result__item--desc');
  359. descDiv.innerHTML = obj.item.content.substring(0, 80);
  360. }
  361. }
  362. }
  363. li.appendChild(a);
  364. if (descDiv) {
  365. li.appendChild(descDiv);
  366. }
  367. if (li) {
  368. ulElem.appendChild(li);
  369. }
  370. }
  371. }
  372. function renderSearchResults(searchText, results) {
  373. searchResults = document.getElementById('search-results');
  374. searchMenu = document.getElementById('search-menu');
  375. searchResults.setAttribute('class', 'dd is-active');
  376. var ul = document.createElement('ul');
  377. ul.setAttribute('class', 'dd-content search-content');
  378. if (results.length) {
  379. results.forEach(function (result) {
  380. var li = document.createElement('li');
  381. var a = document.createElement('a');
  382. a.setAttribute('href', result.uri);
  383. a.setAttribute('class', 'dd-item');
  384. a.appendChild(li);
  385. var titleDiv = document.createElement('div');
  386. titleDiv.innerHTML = result.title;
  387. titleDiv.setAttribute('class', 'search-result__item--title');
  388. var descDiv = document.createElement('div');
  389. descDiv.setAttribute('class', 'search-result__item--desc');
  390. if (result.description) {
  391. descDiv.innerHTML = result.description;
  392. } else if (result.content) {
  393. descDiv.innerHTML = result.content.substring(0, 80);
  394. }
  395. li.appendChild(titleDiv);
  396. li.appendChild(descDiv);
  397. ul.appendChild(a);
  398. });
  399. } else {
  400. var li = document.createElement('li');
  401. li.setAttribute('class', 'dd-item');
  402. li.innerText = 'No results found';
  403. ul.appendChild(li);
  404. }
  405. while (searchMenu.hasChildNodes()) {
  406. searchMenu.removeChild(
  407. searchMenu.lastChild
  408. );
  409. }
  410. searchMenu.appendChild(ul);
  411. }
  412. function renderSearchHighlightResults(searchText, results) {
  413. searchResults = document.getElementById('search-results');
  414. searchMenu = document.getElementById('search-menu');
  415. searchResults.setAttribute('class', 'dd is-active');
  416. var ul = document.createElement('ul');
  417. ul.setAttribute('class', 'dd-content search-content');
  418. if (results.length) {
  419. results.forEach(function (result) {
  420. var li = document.createElement('li');
  421. var a = document.createElement('a');
  422. var descDiv = null;
  423. a.setAttribute('href', result.item.uri);
  424. a.setAttribute('class', 'dd-item');
  425. a.appendChild(li);
  426. var titleDiv = document.createElement('div');
  427. titleDiv.innerHTML = result.item.title;
  428. titleDiv.setAttribute('class', 'search-result__item--title');
  429. if (result.matches && result.matches.length) {
  430. for (var i = 0; i < result.matches.length; i++) {
  431. if ('title' === result.matches[i].key) {
  432. titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
  433. }
  434. if ('description' === result.matches[i].key) {
  435. descDiv = document.createElement('div');
  436. descDiv.setAttribute('class', 'search-result__item--desc');
  437. descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
  438. } else if ('content' === result.matches[i].key) {
  439. if (!descDiv) {
  440. descDiv = document.createElement('div');
  441. descDiv.setAttribute('class', 'search-result__item--desc');
  442. descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 80), result.matches[i].indices);
  443. }
  444. } else {
  445. if (result.item.description) {
  446. descDiv = document.createElement('div');
  447. descDiv.setAttribute('class', 'search-result__item--desc');
  448. descDiv.innerHTML = result.item.description;
  449. } else {
  450. descDiv = document.createElement('div');
  451. descDiv.setAttribute('class', 'search-result__item--desc');
  452. descDiv.innerHTML = result.item.content.substring(0, 80);
  453. }
  454. }
  455. }
  456. li.appendChild(titleDiv);
  457. if (descDiv) {
  458. li.appendChild(descDiv);
  459. }
  460. ul.appendChild(a);
  461. }
  462. });
  463. } else {
  464. var li = document.createElement('li');
  465. li.setAttribute('class', 'dd-item');
  466. li.innerText = 'No results found';
  467. ul.appendChild(li);
  468. }
  469. while (searchMenu.hasChildNodes()) {
  470. searchMenu.removeChild(
  471. searchMenu.lastChild
  472. );
  473. }
  474. searchMenu.appendChild(ul);
  475. }
  476. function renderSearchResultsMobile(searchText, results) {
  477. searchResults = document.getElementById('search-mobile-results');
  478. var content = document.createElement('div');
  479. content.setAttribute('class', 'mobile-search__content');
  480. if (results.length > 0) {
  481. results.forEach(function (result) {
  482. var item = document.createElement('a');
  483. item.setAttribute('href', result.uri);
  484. 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>';
  485. content.appendChild(item);
  486. });
  487. } else {
  488. var item = document.createElement('span');
  489. content.appendChild(item);
  490. }
  491. let wrap = document.getElementById('search-mobile-results');
  492. while (wrap.firstChild) {
  493. wrap.removeChild(wrap.firstChild)
  494. }
  495. searchResults.appendChild(content);
  496. }
  497. function renderSearchHighlightResultsMobile(searchText, results) {
  498. searchResults = document.getElementById('search-mobile-results');
  499. var ul = document.createElement('div');
  500. ul.setAttribute('class', 'mobile-search__content');
  501. if (results.length) {
  502. results.forEach(function (result) {
  503. var li = document.createElement('li');
  504. var a = document.createElement('a');
  505. var descDiv = null;
  506. a.setAttribute('href', result.item.uri);
  507. a.appendChild(li);
  508. li.setAttribute('class', 'mobile-search__item');
  509. var titleDiv = document.createElement('div');
  510. titleDiv.innerHTML = result.item.title;
  511. titleDiv.setAttribute('class', 'mobile-search__item--title');
  512. if (result.matches && result.matches.length) {
  513. for (var i = 0; i < result.matches.length; i++) {
  514. if ('title' === result.matches[i].key) {
  515. titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
  516. }
  517. if ('description' === result.matches[i].key) {
  518. descDiv = document.createElement('div');
  519. descDiv.setAttribute('class', 'mobile-search__item--desc');
  520. descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
  521. } else if ('content' === result.matches[i].key) {
  522. if (!descDiv) {
  523. descDiv = document.createElement('div');
  524. descDiv.setAttribute('class', 'mobile-search__item--desc');
  525. descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 150), result.matches[i].indices);
  526. }
  527. } else {
  528. if (result.item.description) {
  529. descDiv = document.createElement('div');
  530. descDiv.setAttribute('class', 'mobile-search__item--desc');
  531. descDiv.innerHTML = result.item.description;
  532. } else {
  533. descDiv = document.createElement('div');
  534. descDiv.setAttribute('class', 'mobile-search__item--desc');
  535. descDiv.innerHTML = result.item.content.substring(0, 150);
  536. }
  537. }
  538. }
  539. li.appendChild(titleDiv);
  540. if (descDiv) {
  541. li.appendChild(descDiv);
  542. }
  543. ul.appendChild(a);
  544. }
  545. });
  546. } else {
  547. var item = document.createElement('span');
  548. ul.appendChild(item);
  549. }
  550. let wrap = document.getElementById('search-mobile-results');
  551. while (wrap.firstChild) {
  552. wrap.removeChild(wrap.firstChild)
  553. }
  554. searchResults.appendChild(ul);
  555. }
  556. function generateHighlightedText(text, regions) {
  557. if (!regions) {
  558. return text;
  559. }
  560. var content = '', nextUnhighlightedRegionStartingIndex = 0;
  561. regions.forEach(function(region) {
  562. if (region[0] === region[1]) {
  563. return null;
  564. }
  565. content += '' +
  566. text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
  567. '<span class="search__highlight">' +
  568. text.substring(region[0], region[1] + 1) +
  569. '</span>' +
  570. '';
  571. nextUnhighlightedRegionStartingIndex = region[1] + 1;
  572. });
  573. content += text.substring(nextUnhighlightedRegionStartingIndex);
  574. return content;
  575. };
  576. var searchElem = document.getElementById('search');
  577. var searchMobile = document.getElementById('search-mobile');
  578. var searchResultsContainer = document.getElementById('search-results');
  579. searchElem ?
  580. searchElem.addEventListener('input', function(e) {
  581. if (!e.target.value | window.innerWidth < 770) {
  582. searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
  583. return null;
  584. }
  585. searchText = e.target.value;
  586. var results = fuse.search(e.target.value);
  587. if (enableSearchHighlight) {
  588. renderSearchHighlightResults(searchText, results);
  589. } else {
  590. renderSearchResults(searchText, results);
  591. }
  592. var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
  593. dropdownItems ? dropdownItems.forEach(function (item) {
  594. item.addEventListener('mousedown', function (e) {
  595. console.log('mousedown');
  596. e.target.click();
  597. });
  598. }) : null;
  599. }) : null;
  600. searchElem ?
  601. searchElem.addEventListener('blur', function() {
  602. if (window.innerWidth < 770) {
  603. return null;
  604. }
  605. searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
  606. }) : null;
  607. searchElem ?
  608. searchElem.addEventListener('click', function(e) {
  609. if (window.innerWidth < 770) {
  610. return null;
  611. }
  612. if (!e.target.value) {
  613. searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
  614. return null;
  615. }
  616. searchText = e.target.value;
  617. var results = fuse.search(e.target.value);
  618. if (enableSearchHighlight) {
  619. renderSearchHighlightResults(searchText, results);
  620. } else {
  621. renderSearchResults(searchText, results);
  622. }
  623. var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
  624. dropdownItems ? dropdownItems.forEach(function (item) {
  625. item.addEventListener('mousedown', function (e) {
  626. console.log('mousedown');
  627. e.target.click();
  628. });
  629. }) : null;
  630. }) : null;
  631. var searchMenuElem = document.getElementById("search-menu");
  632. var activeItem = document.querySelector('#search-menu .dd-item.is-active');
  633. var activeIndex = null;
  634. var items = null;
  635. var searchContainerMaxHeight = 350;
  636. searchElem.addEventListener('keydown', function(e) {
  637. if (window.innerWidth < 770) {
  638. return null;
  639. }
  640. var items = document.querySelectorAll('#search-menu .dd-item');
  641. if (e.key === 'ArrowDown') {
  642. if (activeIndex === null) {
  643. activeIndex = 0;
  644. items[activeIndex].classList.remove('is-active');
  645. } else {
  646. items[activeIndex].classList.remove('is-active');
  647. activeIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
  648. }
  649. items[activeIndex].classList.add('is-active');
  650. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  651. if (overflowedPixel > 0) {
  652. document.querySelector(".search-content").scrollTop += items[activeIndex].getBoundingClientRect().height;
  653. } else if (activeIndex === 0) {
  654. document.querySelector(".search-content").scrollTop = 0;
  655. }
  656. } else if (e.key === 'ArrowUp') {
  657. if (activeIndex === null) {
  658. activeIndex = items.length - 1;
  659. items[activeIndex].classList.remove('is-active');
  660. } else {
  661. items[activeIndex].classList.remove('is-active');
  662. activeIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
  663. }
  664. items[activeIndex].classList.add('is-active');
  665. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  666. if (overflowedPixel < 0) {
  667. document.querySelector(".search-content").scrollTop -= items[activeIndex].getBoundingClientRect().height;
  668. } else {
  669. document.querySelector(".search-content").scrollTop = overflowedPixel + items[activeIndex].getBoundingClientRect().height;
  670. }
  671. } else if (e.key === 'Enter') {
  672. var currentItemLink = items[activeIndex].getAttribute('href');
  673. if (currentItemLink) {
  674. location.href = currentItemLink;
  675. }
  676. } else if (e.key === 'Escape') {
  677. e.target.value = null;
  678. if (searchResults) {
  679. searchResults.classList.remove('is-active');
  680. }
  681. }
  682. });
  683. searchMobile ?
  684. searchMobile.addEventListener('input', function(e) {
  685. if (!e.target.value) {
  686. let wrap = document.getElementById('search-mobile-results');
  687. while (wrap.firstChild) {
  688. wrap.removeChild(wrap.firstChild);
  689. }
  690. return null;
  691. }
  692. searchText = e.target.value;
  693. var results = fuse.search(e.target.value);
  694. if (enableSearchHighlight) {
  695. renderSearchHighlightResultsMobile(searchText, results);
  696. } else {
  697. renderSearchResultsMobile(searchText, results);
  698. }
  699. var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
  700. dropdownItems ? dropdownItems.forEach(function (item) {
  701. item.addEventListener('mousedown', function (e) {
  702. console.log('mousedown');
  703. e.target.click();
  704. });
  705. }) : null;
  706. }) : null;
  707. // ============================================================
  708. }
  709. </script>