scripts.html 31 KB

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