scripts.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. {{ $permalink := .Permalink }}
  14. var permalink = JSON.parse({{ $permalink | jsonify }});
  15. window.onload = function() {
  16. // ========================== expand ==========================
  17. var expandBtn = document.querySelectorAll('.expand__button');
  18. for (let i = 0; i < expandBtn.length; i++) {
  19. expandBtn[i].addEventListener("click", function () {
  20. var content = this.nextElementSibling;
  21. if (content.style.maxHeight) {
  22. content.style.maxHeight = null;
  23. this.querySelector('svg').classList.add('expand-icon__right');
  24. this.querySelector('svg').classList.remove('expand-icon__down');
  25. } else {
  26. content.style.maxHeight = content.scrollHeight + "px";
  27. this.querySelector('svg').classList.remove('expand-icon__right');
  28. this.querySelector('svg').classList.add('expand-icon__down');
  29. }
  30. });
  31. }
  32. // ============================================================
  33. // ======================= toggle theme =======================
  34. var root = document.getElementById('root');
  35. var toggleToLightBtn = document.getElementById('toggleToLight');
  36. var toggleToDarkBtn = document.getElementById('toggleToDark');
  37. toggleToDark.onclick = function(e) {
  38. root.className = 'theme__dark';
  39. localStorage.setItem('theme', 'dark');
  40. toggleToLightBtn.className = 'navbar__icons--icon';
  41. toggleToDarkBtn.className = 'hide';
  42. }
  43. toggleToLight.onclick = function (e) {
  44. root.className = 'theme__light';
  45. localStorage.setItem('theme', 'light');
  46. toggleToLightBtn.className = 'hide';
  47. toggleToDarkBtn.className = 'navbar__icons--icon';
  48. }
  49. // =================== section menu collapse ==================
  50. document.querySelectorAll('.menu__list').forEach(function(elem) {
  51. if (elem.classList.contains('active')) {
  52. elem.style.maxHeight = elem.scrollHeight + "px";
  53. }
  54. });
  55. document.querySelectorAll('.menu__title--collapse').forEach(function(elem) {
  56. elem.addEventListener('click', function (e) {
  57. var content = this.nextElementSibling;
  58. var menuTitleIcon = this.querySelector('.menu__title--icon');
  59. if (!content) {
  60. return null;
  61. }
  62. if (content.style.maxHeight) {
  63. content.style.maxHeight = null;
  64. content.classList.remove('active');
  65. menuTitleIcon.classList.add('right');
  66. menuTitleIcon.classList.remove('down');
  67. } else {
  68. content.style.maxHeight = content.scrollHeight + "px";
  69. content.classList.add('active');
  70. menuTitleIcon.classList.remove('right');
  71. menuTitleIcon.classList.add('down');
  72. }
  73. });
  74. });
  75. // ============================================================
  76. // ========================== drawer ==========================
  77. var mobileLogo = document.getElementById('mobileLogo');
  78. var modal = document.getElementById("myModal");
  79. var drawer = document.getElementById('myDrawer');
  80. var drawerCloseBtn = document.querySelector('.drawer__close');
  81. var openDrawer = function() {
  82. modal.style.left = 0;
  83. modal.style.opacity = 1;
  84. drawer.style.left = 0;
  85. }
  86. var closeDrawer = function() {
  87. modal.style.opacity = 0;
  88. drawer.style.left = '-100%';
  89. setTimeout(function () {
  90. modal.style.left = '-100%';
  91. }, 250);
  92. }
  93. mobileLogo.onclick = function () {
  94. openDrawer();
  95. localStorage.setItem('isDrawerOpen', 'true');
  96. }
  97. modal.onclick = function () {
  98. closeDrawer();
  99. localStorage.setItem('isDrawerOpen', 'false');
  100. }
  101. drawerCloseBtn.onclick = function () {
  102. closeDrawer();
  103. localStorage.setItem('isDrawerOpen', 'false');
  104. }
  105. // ==============================================================
  106. // =========================== scroll ===========================
  107. var lastScrollTop = window.pageYOffset || document.documentElement.scrollTop;
  108. var tocElem = document.querySelector('.toc');
  109. var tableOfContentsElem = tocElem ? tocElem.querySelector('#TableOfContents') : null;
  110. var singleContentsElem = document.querySelector('.single__contents');
  111. window.onscroll = function () {
  112. var st = window.pageYOffset || document.documentElement.scrollTop;
  113. if (st > lastScrollTop) { // scroll down
  114. singleContentsElem ?
  115. singleContentsElem.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach(function(elem) {
  116. if (document.documentElement.scrollTop >= elem.offsetTop) {
  117. if (tableOfContentsElem) {
  118. var id = elem.getAttribute('id');
  119. tocElem.querySelectorAll('a').forEach(function (elem) {
  120. elem.classList.remove('active');
  121. });
  122. tocElem.querySelector('a[href="#' + id + '"]') ?
  123. tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
  124. }
  125. }
  126. }) : null;
  127. } else { // scroll up
  128. singleContentsElem ?
  129. singleContentsElem.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach(function(elem) {
  130. if (document.documentElement.scrollTop >= elem.offsetTop) {
  131. if (tableOfContentsElem) {
  132. var id = elem.getAttribute('id');
  133. tocElem.querySelectorAll('a').forEach(function (elem) {
  134. elem.classList.remove('active');
  135. });
  136. tocElem.querySelector('a[href="#' + id + '"]') ?
  137. tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
  138. }
  139. }
  140. }) : null;
  141. }
  142. lastScrollTop = st <= 0 ? 0 : st;
  143. };
  144. // ============================================================
  145. // ====================== mobile search =======================
  146. var mobileSearchInputElem = document.querySelector('#search-mobile');
  147. var mobileSearchClassElem = document.querySelector('.mobile-search');
  148. var mobileSearchBtnElem = document.querySelector('#mobileSearchBtn');
  149. var mobileSearchCloseBtnElem = document.querySelector('#search-mobile-close');
  150. var mobileSearchContainer = document.querySelector('#search-mobile-container');
  151. var mobileSearchResultsElem = document.querySelector('#search-mobile-results');
  152. var htmlElem = document.querySelector('html');
  153. if (mobileSearchClassElem) {
  154. mobileSearchClassElem.style.display = 'none';
  155. }
  156. mobileSearchBtnElem ?
  157. mobileSearchBtnElem.addEventListener('click', function () {
  158. if (mobileSearchContainer) {
  159. mobileSearchContainer.style.display = 'block';
  160. }
  161. if (mobileSearchInputElem) {
  162. mobileSearchInputElem.focus();
  163. }
  164. if (htmlElem) {
  165. htmlElem.style.overflowY = 'hidden';
  166. }
  167. }) : null;
  168. mobileSearchCloseBtnElem ?
  169. mobileSearchCloseBtnElem.addEventListener('click', function() {
  170. if (mobileSearchContainer) {
  171. mobileSearchContainer.style.display = 'none';
  172. }
  173. if (mobileSearchInputElem) {
  174. mobileSearchInputElem.value = '';
  175. }
  176. if (mobileSearchResultsElem) {
  177. while (mobileSearchResultsElem.firstChild) {
  178. mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
  179. }
  180. }
  181. if (htmlElem) {
  182. htmlElem.style.overflowY = 'visible';
  183. }
  184. }) : null;
  185. mobileSearchInputElem ?
  186. mobileSearchInputElem.addEventListener('keydown', function(e) {
  187. if (e.key === 'Escape') {
  188. if (mobileSearchContainer) {
  189. mobileSearchContainer.style.display = 'none';
  190. }
  191. if (mobileSearchInputElem) {
  192. mobileSearchInputElem.value = '';
  193. }
  194. if (mobileSearchResultsElem) {
  195. while (mobileSearchResultsElem.firstChild) {
  196. mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
  197. }
  198. }
  199. if (htmlElem) {
  200. htmlElem.style.overflowY = 'visible';
  201. }
  202. }
  203. }) : null;
  204. // ============================================================
  205. // ======================= theme change =======================
  206. var localTheme = localStorage.getItem('theme');
  207. var rootEleme = document.getElementById('root');
  208. var selectThemeElem = document.querySelectorAll('.select-theme');
  209. var selectThemeItemElem = document.querySelectorAll('.select-theme__item');
  210. if (localTheme) {
  211. selectThemeItemElem ?
  212. selectThemeItemElem.forEach(function (elem) {
  213. if (elem.text.trim() === localTheme) {
  214. elem.classList.add('is-active');
  215. } else {
  216. elem.classList.remove('is-active');
  217. }
  218. }) : null;
  219. }
  220. selectThemeItemElem ?
  221. selectThemeItemElem.forEach(function (v, i) {
  222. v.addEventListener('click', function (e) {
  223. var selectedThemeVariant = e.target.text.trim();
  224. localStorage.setItem('theme', selectedThemeVariant);
  225. rootEleme.removeAttribute('class');
  226. rootEleme.classList.add(`theme__${selectedThemeVariant}`);
  227. selectThemeElem.forEach(function(rootElem) {
  228. rootElem.querySelectorAll('a').forEach(function (elem) {
  229. if (elem.classList) {
  230. if (elem.text.trim() === selectedThemeVariant) {
  231. if (!elem.classList.contains('is-active')) {
  232. elem.classList.add('is-active');
  233. }
  234. } else {
  235. if (elem.classList.contains('is-active')) {
  236. elem.classList.remove('is-active');
  237. }
  238. }
  239. }
  240. });
  241. });
  242. if (window.mermaid) {
  243. if (selectedThemeVariant === "dark" || selectedThemeVariant === "hacker") {
  244. mermaid.initialize({ theme: 'dark' });
  245. location.reload();
  246. } else {
  247. mermaid.initialize({ theme: 'default' });
  248. location.reload();
  249. }
  250. }
  251. var utterances = document.querySelector('iframe');
  252. if (utterances) {
  253. utterances.contentWindow.postMessage({
  254. type: 'set-theme',
  255. theme: selectedThemeVariant === "dark" || selectedThemeVariant === "hacker" ? 'photon-dark' : selectedThemeVariant === 'kimbie' ? 'github-dark-orange' : 'github-light',
  256. }, 'https://utteranc.es');
  257. }
  258. });
  259. }) : null;
  260. // ============================================================
  261. // ========================== search ==========================
  262. var searchResults = null;
  263. var searchMenu = null;
  264. var searchText = null;
  265. {{ $enableSearchHighlight := ($.Param "enableSearchHighlight") }}
  266. var enableSearchHighlight = JSON.parse({{ $enableSearchHighlight | jsonify }});
  267. var fuse = null;
  268. function initFuse() {
  269. var xhr = new XMLHttpRequest();
  270. xhr.open('GET', permalink + "index.json");
  271. xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  272. xhr.onload = function () {
  273. if (xhr.status === 200) {
  274. fuse = new Fuse(JSON.parse(xhr.response.toString('utf-8')), {
  275. keys: ['title', 'description', 'content'],
  276. includeMatches: enableSearchHighlight,
  277. shouldSort: true,
  278. threshold: 0.4,
  279. location: 0,
  280. distance: 100,
  281. maxPatternLength: 32,
  282. minMatchCharLength: 1,
  283. });
  284. }
  285. else {
  286. console.error(`[${xhr.status}]Error:`, xhr.statusText);
  287. }
  288. };
  289. xhr.send();
  290. }
  291. function renderSearchResults(results) { // [{}, {}, ...] or [{item: {}, matches: []}, ...]
  292. searchResults = document.getElementById('search-results');
  293. searchMenu = document.getElementById('search-menu');
  294. searchResults.setAttribute('class', 'dd is-active');
  295. var content = document.createElement('div');
  296. content.setAttribute('class', 'dd-content search-content');
  297. if (results.length > 0) {
  298. results.forEach(function (result) {
  299. var item = document.createElement('a');
  300. item.setAttribute('href', result.uri);
  301. item.setAttribute('class', 'dd-item');
  302. item.innerHTML = `<div class="menu-item"><div class="menu-item__title">📄 ${result.title}</div><div class="menu-item__desc">${result.description ? result.description : result.content}</div></div>`;
  303. content.appendChild(item);
  304. });
  305. } else {
  306. var item = document.createElement('span');
  307. item.setAttribute('class', 'dd-item');
  308. item.innerText = 'No results found';
  309. content.appendChild(item);
  310. }
  311. while (searchMenu.hasChildNodes()) {
  312. searchMenu.removeChild(
  313. searchMenu.lastChild
  314. );
  315. }
  316. searchMenu.appendChild(content);
  317. }
  318. function renderSearchHighlightResults(results) {
  319. searchResults = document.getElementById('search-results');
  320. searchMenu = document.getElementById('search-menu');
  321. searchResults.setAttribute('class', 'dd is-active');
  322. var content = document.createElement('div');
  323. content.setAttribute('class', 'dd-content search-content');
  324. if (results.length > 0) {
  325. results.forEach(function (result) {
  326. var item = document.createElement('a');
  327. item.setAttribute('href', result.item.uri);
  328. item.setAttribute('class', 'dd-item');
  329. item.innerHTML = `<div class="menu-item"><div class="menu-item__title">📄 ${generateHighlightedText(result.item.title, result.matches[0].indices)}</div><div class="menu-item__desc">${result.matches[1] ? generateHighlightedText(result.item.description, result.matches[1].indices) : result.matches[2] ? generateHighlightedText(result.item.content, result.matches[2].indices) : ''}</div></div>`;
  330. content.appendChild(item);
  331. });
  332. } else {
  333. var item = document.createElement('span');
  334. item.setAttribute('class', 'dd-item');
  335. item.innerText = 'No results found';
  336. content.appendChild(item);
  337. }
  338. while (searchMenu.hasChildNodes()) {
  339. searchMenu.removeChild(
  340. searchMenu.lastChild
  341. );
  342. }
  343. searchMenu.appendChild(content);
  344. }
  345. function renderSearchResultsMobile(results) {
  346. searchResults = document.getElementById('search-mobile-results');
  347. var content = document.createElement('div');
  348. content.setAttribute('class', 'mobile-search__content');
  349. if (results.length > 0) {
  350. results.forEach(function (result) {
  351. var item = document.createElement('a');
  352. item.setAttribute('href', result.uri);
  353. 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>`;
  354. content.appendChild(item);
  355. });
  356. } else {
  357. var item = document.createElement('span');
  358. content.appendChild(item);
  359. }
  360. let wrap = document.getElementById('search-mobile-results');
  361. while (wrap.firstChild) {
  362. wrap.removeChild(wrap.firstChild)
  363. }
  364. searchResults.appendChild(content);
  365. }
  366. function renderSearchHighlightResultsMobile(results) {
  367. searchResults = document.getElementById('search-mobile-results');
  368. var content = document.createElement('div');
  369. content.setAttribute('class', 'mobile-search__content');
  370. if (results.length > 0) {
  371. results.forEach(function (result) {
  372. var item = document.createElement('a');
  373. item.setAttribute('href', result.item.uri);
  374. item.innerHTML = `<div class="mobile-search__item"><div class="mobile-search__item--title">📄 ${generateHighlightedText(result.item.title, result.matches[0].indices)}</div><div class="mobile-search__item--desc">${result.matches[1] ? generateHighlightedText(result.item.description, result.matches[1].indices) : result.matches[2] ? generateHighlightedText(result.item.content, result.matches[2].indices) : ''}</div></div>`;
  375. content.appendChild(item);
  376. });
  377. } else {
  378. var item = document.createElement('span');
  379. content.appendChild(item);
  380. }
  381. let wrap = document.getElementById('search-mobile-results');
  382. while (wrap.firstChild) {
  383. wrap.removeChild(wrap.firstChild)
  384. }
  385. searchResults.appendChild(content);
  386. }
  387. function generateHighlightedText(text, regions) {
  388. if (!regions) {
  389. return text;
  390. }
  391. var content = '', nextUnhighlightedRegionStartingIndex = 0;
  392. regions.forEach(function(region) {
  393. if (region[0] === region[1]) {
  394. return null;
  395. }
  396. content += '' +
  397. text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
  398. '<span class="search__highlight">' +
  399. text.substring(region[0], region[1] + 1) +
  400. '</span>' +
  401. '';
  402. nextUnhighlightedRegionStartingIndex = region[1] + 1;
  403. });
  404. content += text.substring(nextUnhighlightedRegionStartingIndex);
  405. return content;
  406. };
  407. initFuse();
  408. var searchElem = document.getElementById('search');
  409. var searchMobile = document.getElementById('search-mobile');
  410. searchElem.addEventListener('input', function(e) {
  411. if (!e.target.value) {
  412. document.getElementById('search-results').setAttribute('class', 'dd');
  413. return null;
  414. }
  415. if (window.innerWidth < 770) {
  416. return null;
  417. }
  418. searchText = e.target.value;
  419. var results = fuse.search(e.target.value);
  420. if (enableSearchHighlight) {
  421. renderSearchHighlightResults(results);
  422. } else {
  423. renderSearchResults(results);
  424. }
  425. });
  426. searchElem.addEventListener('blur', function() {
  427. if (window.innerWidth < 770) {
  428. return null;
  429. }
  430. setTimeout(function () {
  431. document.getElementById('search-results').setAttribute('class', 'dd');
  432. }, 100);
  433. });
  434. searchElem.addEventListener('click', function(e) {
  435. if (window.innerWidth < 770) {
  436. return null;
  437. }
  438. if (!e.target.value) {
  439. document.getElementById('search-results').setAttribute('class', 'dd');
  440. return null;
  441. }
  442. searchText = e.target.value;
  443. var results = fuse.search(e.target.value);
  444. if (enableSearchHighlight) {
  445. renderSearchHighlightResults(results);
  446. } else {
  447. renderSearchResults(results);
  448. }
  449. });
  450. function indexInParent(node) {
  451. var children = node.parentNode.childNodes;
  452. var num = 0;
  453. for (var i = 0; i < children.length; i++) {
  454. if (children[i] == node) return num;
  455. if (children[i].nodeType == 1) num++;
  456. }
  457. return -1;
  458. }
  459. var searchMenuElem = document.getElementById("search-menu");
  460. var activeItem = document.querySelector('#search-menu .dd-item.is-active');
  461. var activeIndex = null;
  462. var items = null;
  463. var searchContainerMaxHeight = 350;
  464. searchElem.addEventListener('keydown', function(e) {
  465. if (window.innerWidth < 770) {
  466. return null;
  467. }
  468. var items = document.querySelectorAll('#search-menu .dd-item');
  469. if (e.key === 'ArrowDown') {
  470. if (activeIndex === null) {
  471. activeIndex = 0;
  472. items[activeIndex].classList.remove('is-active');
  473. } else {
  474. items[activeIndex].classList.remove('is-active');
  475. activeIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
  476. }
  477. items[activeIndex].classList.add('is-active');
  478. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  479. if (overflowedPixel > 0) {
  480. document.querySelector(".search-content").scrollTop += items[activeIndex].getBoundingClientRect().height;
  481. } else if (activeIndex === 0) {
  482. document.querySelector(".search-content").scrollTop = 0;
  483. }
  484. } else if (e.key === 'ArrowUp') {
  485. if (activeIndex === null) {
  486. activeIndex = items.length - 1;
  487. items[activeIndex].classList.remove('is-active');
  488. } else {
  489. items[activeIndex].classList.remove('is-active');
  490. activeIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
  491. }
  492. items[activeIndex].classList.add('is-active');
  493. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  494. if (overflowedPixel < 0) {
  495. document.querySelector(".search-content").scrollTop -= items[activeIndex].getBoundingClientRect().height;
  496. } else {
  497. document.querySelector(".search-content").scrollTop = overflowedPixel + items[activeIndex].getBoundingClientRect().height;
  498. }
  499. } else if (e.key === 'Enter') {
  500. var currentItemLink = items[activeIndex].getAttribute('href');
  501. if (currentItemLink) {
  502. location.href = currentItemLink;
  503. }
  504. } else if (e.key === 'Escape') {
  505. e.target.value = null;
  506. if (searchResults) {
  507. searchResults.classList.remove('is-active');
  508. }
  509. }
  510. });
  511. searchMobile.addEventListener('input', function(e) {
  512. if (!e.target.value) {
  513. let wrap = document.getElementById('search-mobile-results');
  514. while (wrap.firstChild) {
  515. wrap.removeChild(wrap.firstChild);
  516. }
  517. return null;
  518. }
  519. searchText = e.target.value;
  520. var results = fuse.search(e.target.value);
  521. renderSearchResultsMobile(results);
  522. if (enableSearchHighlight) {
  523. renderSearchHighlightResultsMobile(results);
  524. } else {
  525. renderSearchResultsMobile(results);
  526. }
  527. });
  528. // ============================================================
  529. }
  530. </script>