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. var xhr = new XMLHttpRequest();
  269. xhr.open('GET', permalink + "index.json");
  270. xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  271. xhr.onload = function () {
  272. if (xhr.status === 200) {
  273. fuse = new Fuse(JSON.parse(xhr.response.toString('utf-8')), {
  274. keys: ['title', 'description', 'content'],
  275. includeMatches: enableSearchHighlight,
  276. shouldSort: true,
  277. threshold: 0.4,
  278. location: 0,
  279. distance: 100,
  280. maxPatternLength: 32,
  281. minMatchCharLength: 1,
  282. });
  283. }
  284. else {
  285. console.error(`[${xhr.status}]Error:`, xhr.statusText);
  286. }
  287. };
  288. xhr.send();
  289. }
  290. function renderSearchResults(results) { // [{}, {}, ...] or [{item: {}, matches: []}, ...]
  291. searchResults = document.getElementById('search-results');
  292. searchMenu = document.getElementById('search-menu');
  293. searchResults.setAttribute('class', 'dd is-active');
  294. var content = document.createElement('div');
  295. content.setAttribute('class', 'dd-content search-content');
  296. if (results.length > 0) {
  297. results.forEach(function (result) {
  298. var item = document.createElement('a');
  299. item.setAttribute('href', result.uri);
  300. item.setAttribute('class', 'dd-item');
  301. 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>`;
  302. content.appendChild(item);
  303. });
  304. } else {
  305. var item = document.createElement('span');
  306. item.setAttribute('class', 'dd-item');
  307. item.innerText = 'No results found';
  308. content.appendChild(item);
  309. }
  310. while (searchMenu.hasChildNodes()) {
  311. searchMenu.removeChild(
  312. searchMenu.lastChild
  313. );
  314. }
  315. searchMenu.appendChild(content);
  316. }
  317. function renderSearchHighlightResults(results) {
  318. searchResults = document.getElementById('search-results');
  319. searchMenu = document.getElementById('search-menu');
  320. searchResults.setAttribute('class', 'dd is-active');
  321. var content = document.createElement('div');
  322. content.setAttribute('class', 'dd-content search-content');
  323. if (results.length > 0) {
  324. results.forEach(function (result) {
  325. var item = document.createElement('a');
  326. item.setAttribute('href', result.item.uri);
  327. item.setAttribute('class', 'dd-item');
  328. 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>`;
  329. content.appendChild(item);
  330. });
  331. } else {
  332. var item = document.createElement('span');
  333. item.setAttribute('class', 'dd-item');
  334. item.innerText = 'No results found';
  335. content.appendChild(item);
  336. }
  337. while (searchMenu.hasChildNodes()) {
  338. searchMenu.removeChild(
  339. searchMenu.lastChild
  340. );
  341. }
  342. searchMenu.appendChild(content);
  343. }
  344. function renderSearchResultsMobile(results) {
  345. searchResults = document.getElementById('search-mobile-results');
  346. var content = document.createElement('div');
  347. content.setAttribute('class', 'mobile-search__content');
  348. if (results.length > 0) {
  349. results.forEach(function (result) {
  350. var item = document.createElement('a');
  351. item.setAttribute('href', result.uri);
  352. 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>`;
  353. content.appendChild(item);
  354. });
  355. } else {
  356. var item = document.createElement('span');
  357. content.appendChild(item);
  358. }
  359. let wrap = document.getElementById('search-mobile-results');
  360. while (wrap.firstChild) {
  361. wrap.removeChild(wrap.firstChild)
  362. }
  363. searchResults.appendChild(content);
  364. }
  365. function renderSearchHighlightResultsMobile(results) {
  366. searchResults = document.getElementById('search-mobile-results');
  367. var content = document.createElement('div');
  368. content.setAttribute('class', 'mobile-search__content');
  369. if (results.length > 0) {
  370. results.forEach(function (result) {
  371. var item = document.createElement('a');
  372. item.setAttribute('href', result.item.uri);
  373. 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>`;
  374. content.appendChild(item);
  375. });
  376. } else {
  377. var item = document.createElement('span');
  378. content.appendChild(item);
  379. }
  380. let wrap = document.getElementById('search-mobile-results');
  381. while (wrap.firstChild) {
  382. wrap.removeChild(wrap.firstChild)
  383. }
  384. searchResults.appendChild(content);
  385. }
  386. function generateHighlightedText(text, regions) {
  387. if (!regions) {
  388. return text;
  389. }
  390. var content = '', nextUnhighlightedRegionStartingIndex = 0;
  391. regions.forEach(function(region) {
  392. if (region[0] === region[1]) {
  393. return null;
  394. }
  395. content += '' +
  396. text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
  397. '<span class="search__highlight">' +
  398. text.substring(region[0], region[1] + 1) +
  399. '</span>' +
  400. '';
  401. nextUnhighlightedRegionStartingIndex = region[1] + 1;
  402. });
  403. content += text.substring(nextUnhighlightedRegionStartingIndex);
  404. return content;
  405. };
  406. initFuse();
  407. var searchElem = document.getElementById('search');
  408. var searchMobile = document.getElementById('search-mobile');
  409. searchElem.addEventListener('input', function(e) {
  410. if (!e.target.value) {
  411. document.getElementById('search-results').setAttribute('class', 'dd');
  412. return null;
  413. }
  414. if (window.innerWidth < 770) {
  415. return null;
  416. }
  417. searchText = e.target.value;
  418. var results = fuse.search(e.target.value);
  419. if (enableSearchHighlight) {
  420. renderSearchHighlightResults(results);
  421. } else {
  422. renderSearchResults(results);
  423. }
  424. });
  425. searchElem.addEventListener('blur', function() {
  426. if (window.innerWidth < 770) {
  427. return null;
  428. }
  429. setTimeout(function () {
  430. document.getElementById('search-results').setAttribute('class', 'dd');
  431. }, 100);
  432. });
  433. searchElem.addEventListener('click', function(e) {
  434. if (window.innerWidth < 770) {
  435. return null;
  436. }
  437. if (!e.target.value) {
  438. document.getElementById('search-results').setAttribute('class', 'dd');
  439. return null;
  440. }
  441. searchText = e.target.value;
  442. var results = fuse.search(e.target.value);
  443. if (enableSearchHighlight) {
  444. renderSearchHighlightResults(results);
  445. } else {
  446. renderSearchResults(results);
  447. }
  448. });
  449. function indexInParent(node) {
  450. var children = node.parentNode.childNodes;
  451. var num = 0;
  452. for (var i = 0; i < children.length; i++) {
  453. if (children[i] == node) return num;
  454. if (children[i].nodeType == 1) num++;
  455. }
  456. return -1;
  457. }
  458. var searchMenuElem = document.getElementById("search-menu");
  459. var activeItem = document.querySelector('#search-menu .dd-item.is-active');
  460. var activeIndex = null;
  461. var items = null;
  462. var searchContainerMaxHeight = 350;
  463. searchElem.addEventListener('keydown', function(e) {
  464. if (window.innerWidth < 770) {
  465. return null;
  466. }
  467. var items = document.querySelectorAll('#search-menu .dd-item');
  468. if (e.key === 'ArrowDown') {
  469. if (activeIndex === null) {
  470. activeIndex = 0;
  471. items[activeIndex].classList.remove('is-active');
  472. } else {
  473. items[activeIndex].classList.remove('is-active');
  474. activeIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
  475. }
  476. items[activeIndex].classList.add('is-active');
  477. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  478. if (overflowedPixel > 0) {
  479. document.querySelector(".search-content").scrollTop += items[activeIndex].getBoundingClientRect().height;
  480. } else if (activeIndex === 0) {
  481. document.querySelector(".search-content").scrollTop = 0;
  482. }
  483. } else if (e.key === 'ArrowUp') {
  484. if (activeIndex === null) {
  485. activeIndex = items.length - 1;
  486. items[activeIndex].classList.remove('is-active');
  487. } else {
  488. items[activeIndex].classList.remove('is-active');
  489. activeIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
  490. }
  491. items[activeIndex].classList.add('is-active');
  492. let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
  493. if (overflowedPixel < 0) {
  494. document.querySelector(".search-content").scrollTop -= items[activeIndex].getBoundingClientRect().height;
  495. } else {
  496. document.querySelector(".search-content").scrollTop = overflowedPixel + items[activeIndex].getBoundingClientRect().height;
  497. }
  498. } else if (e.key === 'Enter') {
  499. var currentItemLink = items[activeIndex].getAttribute('href');
  500. if (currentItemLink) {
  501. location.href = currentItemLink;
  502. }
  503. } else if (e.key === 'Escape') {
  504. e.target.value = null;
  505. if (searchResults) {
  506. searchResults.classList.remove('is-active');
  507. }
  508. }
  509. });
  510. searchMobile.addEventListener('input', function(e) {
  511. if (!e.target.value) {
  512. let wrap = document.getElementById('search-mobile-results');
  513. while (wrap.firstChild) {
  514. wrap.removeChild(wrap.firstChild);
  515. }
  516. return null;
  517. }
  518. searchText = e.target.value;
  519. var results = fuse.search(e.target.value);
  520. renderSearchResultsMobile(results);
  521. if (enableSearchHighlight) {
  522. renderSearchHighlightResultsMobile(results);
  523. } else {
  524. renderSearchResultsMobile(results);
  525. }
  526. });
  527. // ============================================================
  528. }
  529. </script>