scripts.html 34 KB

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