scripts.html 31 KB

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