scripts.html 30 KB

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