scripts.html 31 KB

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