| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069 |
- {{ if .Site.Params.custom_js }}
- {{ range .Site.Params.custom_js }}
- {{ $custom_template := resources.Get . }}
- {{ if $custom_template }}
- {{ $custom_js := $custom_template | resources.Minify | resources.Fingerprint }}
- <script defer src="{{ $custom_js.RelPermalink }}"></script>
- {{ end }}
- {{ end }}
- {{ end }}
- {{ if $.Param "enableSearch" }}
- {{ $fuse := resources.Get "js/fuse.min.js" | resources.Fingerprint }}
- <script defer src="{{ $fuse.RelPermalink }}"></script>
- {{ end }}
- {{ $enquire := resources.Get "js/enquire.min.js" | resources.Fingerprint }}
- <script src="{{ $enquire.RelPermalink }}"></script>
- {{ $lazysizes := resources.Get "js/lazysizes.min.js" | resources.Fingerprint }}
- <script defer src="{{ $lazysizes.RelPermalink }}"></script>
- {{ $getParents := resources.Get "js/helper/getParents.js" | resources.Minify | resources.Fingerprint }}
- <script defer src="{{ $getParents.RelPermalink }}"></script>
- {{ $fadeinout := resources.Get "js/helper/fadeinout.js" | resources.Minify | resources.Fingerprint }}
- <script defer src="{{ $fadeinout.RelPermalink }}"></script>
- <script>
- "use strict";
- window.onload = function() {
- {{ $languagedir := ($.Param "languagedir" | default "ltr") }}
- var languagedir = JSON.parse({{ $languagedir | jsonify }});
- {{ $baseurl := $.Site.BaseURL }}
- var baseurl = JSON.parse({{ $baseurl | jsonify }});
- {{ $permalink := .Permalink }}
- var permalink = JSON.parse({{ $permalink | jsonify }});
- // ==================== toc visibility ========================
- var toggleSidebarElem = document.getElementById("toggle-sidebar");
- var toggleMenuElem = document.getElementById("toggle-menu");
- var tocBodyElem = document.querySelector('.toc__body');
- var tocLabelElem = document.querySelector('.toc__label');
- var listMainElem = document.getElementById('list-main');
- var listSideElem = document.getElementById('list-side');
- var singleMenuElem = document.getElementById('single-menu');
- var sliderIcons = document.querySelectorAll('.slider__icon');
- toggleSidebarElem ?
- toggleSidebarElem.addEventListener('change', function (e) {
- if (e.target.checked) {
- if (tocBodyElem) {
- fadeIn(tocBodyElem, 200);
- }
- if (tocLabelElem) {
- fadeIn(tocLabelElem, 200);
- }
- if (listMainElem && listSideElem) {
- listMainElem.className = 'm';
- listSideElem.className = 'r';
- }
- sliderIcons && sliderIcons.forEach(function (elem) {
- if (elem.classList.contains('hide')) {
- elem.classList.remove('hide');
- } else {
- elem.classList.add('hide');
- }
- });
- } else {
- if (tocBodyElem) {
- fadeOut(tocBodyElem, 200);
- }
- if (tocLabelElem) {
- fadeOut(tocLabelElem, 200);
- }
- if (listMainElem && listSideElem) {
- listMainElem.className = 'mr';
- listSideElem.className = 'hide';
- }
- sliderIcons && sliderIcons.forEach(function (elem) {
- if (elem.classList.contains('hide')) {
- elem.classList.remove('hide');
- } else {
- elem.classList.add('hide');
- }
- });
- }
- }) : null;
- toggleMenuElem ?
- toggleMenuElem.addEventListener('change', function (e) {
- if (e.target.checked) {
- if (listMainElem && singleMenuElem) {
- listMainElem.className = 'm';
- singleMenuElem.className = 'l';
- }
- sliderIcons && sliderIcons.forEach(function (elem) {
- if (elem.classList.contains('hide')) {
- elem.classList.remove('hide');
- } else {
- elem.classList.add('hide');
- }
- });
- } else {
- if (listMainElem && singleMenuElem) {
- listMainElem.className = 'lm';
- singleMenuElem.className = 'hide';
- }
- sliderIcons && sliderIcons.forEach(function (elem) {
- if (elem.classList.contains('hide')) {
- elem.classList.remove('hide');
- } else {
- elem.classList.add('hide');
- }
- });
- }
- }) : null;
- // ============================================================
- // ===================== navbar collapse ======================
- var navCollapseBtn = document.getElementById('navCollapseBtn');
- navCollapseBtn ? navCollapseBtn.addEventListener('click', function(e) {
- var navCollapse = document.querySelector('.navbar__collapse');
-
- if (navCollapse) {
- var dataOpen = navCollapse.getAttribute('data-open');
- if (dataOpen === 'true') {
- navCollapse.setAttribute('data-open', 'false');
- navCollapse.style.maxHeight = 0;
- } else {
- navCollapse.setAttribute('data-open', 'true');
- navCollapse.style.maxHeight = navCollapse.scrollHeight + "px";
- }
- }
- }) : null;
- // ============================================================
- // ========================== expand ==========================
- var expandBtn = document.querySelectorAll('.expand__button');
- for (let i = 0; i < expandBtn.length; i++) {
- expandBtn[i].addEventListener("click", function () {
- var content = this.nextElementSibling;
- if (content.style.maxHeight) {
- content.style.maxHeight = null;
- this.querySelector('svg').classList.add('expand-icon__right');
- this.querySelector('svg').classList.remove('expand-icon__down');
- } else {
- content.style.maxHeight = content.scrollHeight + "px";
- this.querySelector('svg').classList.remove('expand-icon__right');
- this.querySelector('svg').classList.add('expand-icon__down');
- }
- });
- }
- // ============================================================
- // ============================ tab ============================
- document.querySelectorAll('.tab') ?
- document.querySelectorAll('.tab').forEach(function(elem, idx) {
- var containerId = elem.getAttribute('id');
- var containerElem = elem;
- var tabLinks = elem.querySelectorAll('.tab__link');
- var tabContents = elem.querySelectorAll('.tab__content');
- var ids = [];
- tabLinks && tabLinks.length > 0 ?
- tabLinks.forEach(function(link, index, self) {
- link.onclick = function(e) {
- for (var i = 0; i < self.length; i++) {
- if (index === parseInt(i, 10)) {
- if (!self[i].classList.contains('active')) {
- self[i].classList.add('active');
- tabContents[i].style.display = 'block';
- }
- } else {
- self[i].classList.remove('active');
- tabContents[i].style.display = 'none';
- }
- }
- }
- }) : null;
- }) : null;
- // =============================================================
- // ========================== codetab ==========================
- document.querySelectorAll('.codetab') ?
- document.querySelectorAll('.codetab').forEach(function(elem, idx) {
- var containerId = elem.getAttribute('id');
- var containerElem = elem;
- var codetabLinks = elem.querySelectorAll('.codetab__link');
- var codetabContents = elem.querySelectorAll('.codetab__content');
- var ids = [];
- codetabLinks && codetabLinks.length > 0 ?
- codetabLinks.forEach(function(link, index, self) {
- link.onclick = function(e) {
- for (var i = 0; i < self.length; i++) {
- if (index === parseInt(i, 10)) {
- if (!self[i].classList.contains('active')) {
- self[i].classList.add('active');
- codetabContents[i].style.display = 'block';
- }
- } else {
- self[i].classList.remove('active');
- codetabContents[i].style.display = 'none';
- }
- }
- }
- }) : null;
- }) : null;
- // =============================================================
-
- // ======================= toggle theme =======================
- {{ $enableDarkMode := ($.Param "enableDarkMode") }}
- var enableDarkMode = JSON.parse({{ $enableDarkMode | jsonify }});
- var root = document.getElementById('root');
- var toggleToLightBtn = document.getElementById('toggleToLight');
- var toggleToDarkBtn = document.getElementById('toggleToDark');
- if (!enableDarkMode) {
- root.className = 'theme__light';
- localStorage.setItem('theme', 'light');
- }
-
- if (toggleToDarkBtn) {
- toggleToDarkBtn.onclick = function (e) {
- root.className = 'theme__dark';
- localStorage.setItem('theme', 'dark');
- toggleToLightBtn.className = 'navbar__icons--icon';
- toggleToDarkBtn.className = 'hide';
- }
- }
- if (toggleToLightBtn) {
- toggleToLightBtn.onclick = function (e) {
- root.className = 'theme__light';
- localStorage.setItem('theme', 'light');
- toggleToLightBtn.className = 'hide';
- toggleToDarkBtn.className = 'navbar__icons--icon';
- }
- }
- // =================== section menu collapse ==================
- document.querySelectorAll('.menu__list').forEach(function(elem) {
- if (elem.classList.contains('active')) {
- elem.style.maxHeight = elem.scrollHeight + "px";
- }
- });
-
- document.querySelectorAll('.menu__title--collapse').forEach(function(elem) {
- elem.addEventListener('click', function (e) {
- var content = this.nextElementSibling;
- var menuTitleIcon = this.querySelector('.menu__title--icon');
- if (!content) {
- return null;
- }
- var parent = elem.parentNode;
- while (parent.classList.contains('menu__list') && parent.classList.contains('active')) {
- parent.style.maxHeight = 100 * parent.children.length + "px";
- parent = parent.parentNode;
- }
- if (content.style.maxHeight) {
- content.style.maxHeight = null;
- content.classList.remove('active');
- menuTitleIcon.classList.add('right');
-
- if (languagedir === 'rtl') {
- menuTitleIcon.classList.remove('downrtl');
- } else {
- menuTitleIcon.classList.remove('down');
- }
- } else {
- content.style.maxHeight = content.scrollHeight + "px";
- content.classList.add('active');
- menuTitleIcon.classList.remove('right');
-
- if (languagedir === 'rtl') {
- menuTitleIcon.classList.add('downrtl');
- } else {
- menuTitleIcon.classList.add('down');
- }
- }
- });
- });
- // ============================================================
- // ========================== drawer ==========================
- var mobileLogo = document.getElementById('mobileLogo');
- var modal = document.getElementById("myModal");
- var drawer = document.getElementById('myDrawer');
- var drawerCloseBtn = document.querySelector('.drawer__close');
- var openDrawer = function() {
- modal.style.opacity = 1;
- if (languagedir === 'rtl') {
- modal.style.right = 0;
- drawer.style.right = 0;
- } else {
- modal.style.left = 0;
- drawer.style.left = 0;
- }
- }
- var closeDrawer = function() {
- modal.style.opacity = 0;
- if (languagedir === 'rtl') {
- drawer.style.right = '-100%';
- } else {
- drawer.style.left = '-100%';
- }
-
- setTimeout(function () {
- if (languagedir === 'rtl') {
- modal.style.right = '-100%';
- } else {
- modal.style.left = '-100%';
- }
- }, 250);
- }
- mobileLogo.onclick = function () {
- openDrawer();
- localStorage.setItem('isDrawerOpen', 'true');
- }
- modal.onclick = function () {
- closeDrawer();
- localStorage.setItem('isDrawerOpen', 'false');
- }
- drawerCloseBtn.onclick = function () {
- closeDrawer();
- localStorage.setItem('isDrawerOpen', 'false');
- }
- // ==============================================================
- // =========================== scroll ===========================
- var lastScrollTop = window.pageYOffset || document.documentElement.scrollTop;
- var tocElem = document.querySelector('.toc');
- var tableOfContentsElem = tocElem ? tocElem.querySelector('#TableOfContents') : null;
- var singleContentsElem = document.querySelector('.single__contents');
- var dataBGImgs = document.querySelectorAll('div[data-bgimg]');
- {{ $tocLevels := ($.Param "tocLevels") }}
- var tocLevels = JSON.parse({{ $tocLevels | jsonify }});
- if (tocLevels) {
- tocLevels = tocLevels.toString();
- } else {
- tocLevels = "h1, h2, h3, h4, h5, h6";
- }
- {{ $isLandingBgImg := $.Params.landing.backgroundImage }}
- var isLandingBgImg = JSON.parse({{ $isLandingBgImg | jsonify }});
- {{ $isHome := .IsHome }}
- var isHome = JSON.parse({{ $isHome | jsonify }});
- function setNavbarBG(scrollTop) {
- if (isHome && isLandingBgImg && Object.keys(isLandingBgImg).length) {
- if (isLandingBgImg.height <= scrollTop) {
- dataBGImgs.forEach(function(elem) {
- elem.setAttribute('data-bgimg', 'false');
- });
- } else {
- dataBGImgs.forEach(function (elem) {
- elem.setAttribute('data-bgimg', 'true');
- });
- }
- }
- }
- setNavbarBG(lastScrollTop);
- window.onscroll = function () {
- var st = window.pageYOffset || document.documentElement.scrollTop;
- if (st > lastScrollTop) { // scroll down
- singleContentsElem ?
- singleContentsElem.querySelectorAll(tocLevels.toString()).forEach(function(elem) {
- if (document.documentElement.scrollTop >= elem.offsetTop) {
- if (tableOfContentsElem) {
- var id = elem.getAttribute('id');
- tocElem.querySelectorAll('a').forEach(function (elem) {
- elem.classList.remove('active');
- });
- tocElem.querySelector('a[href="#' + id + '"]') ?
- tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
- }
- }
- }) : null;
- setNavbarBG(st);
- } else { // scroll up
- singleContentsElem ?
- singleContentsElem.querySelectorAll(tocLevels.toString()).forEach(function(elem) {
- if (document.documentElement.scrollTop >= elem.offsetTop) {
- if (tableOfContentsElem) {
- var id = elem.getAttribute('id');
- tocElem.querySelectorAll('a').forEach(function (elem) {
- elem.classList.remove('active');
- });
- tocElem.querySelector('a[href="#' + id + '"]') ?
- tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
- }
- }
- }) : null;
- setNavbarBG(st);
- }
- lastScrollTop = st <= 0 ? 0 : st;
- };
- // ============================================================
- // ====================== mobile search =======================
- var mobileSearchInputElem = document.querySelector('#search-mobile');
- var mobileSearchClassElem = document.querySelector('.mobile-search');
- var mobileSearchBtnElem = document.querySelector('#mobileSearchBtn');
- var mobileSearchCloseBtnElem = document.querySelector('#search-mobile-close');
- var mobileSearchContainer = document.querySelector('#search-mobile-container');
- var mobileSearchResultsElem = document.querySelector('#search-mobile-results');
- var htmlElem = document.querySelector('html');
- if (mobileSearchClassElem) {
- mobileSearchClassElem.style.display = 'none';
- }
- mobileSearchBtnElem ?
- mobileSearchBtnElem.addEventListener('click', function () {
- if (mobileSearchContainer) {
- mobileSearchContainer.style.display = 'block';
- }
- if (mobileSearchInputElem) {
- mobileSearchInputElem.focus();
- }
- if (htmlElem) {
- htmlElem.style.overflowY = 'hidden';
- }
- }) : null;
- mobileSearchCloseBtnElem ?
- mobileSearchCloseBtnElem.addEventListener('click', function() {
- if (mobileSearchContainer) {
- mobileSearchContainer.style.display = 'none';
- }
- if (mobileSearchInputElem) {
- mobileSearchInputElem.value = '';
- }
-
- if (mobileSearchResultsElem) {
- while (mobileSearchResultsElem.firstChild) {
- mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
- }
- }
- if (htmlElem) {
- htmlElem.style.overflowY = 'visible';
- }
- }) : null;
- mobileSearchInputElem ?
- mobileSearchInputElem.addEventListener('keydown', function(e) {
- if (e.key === 'Escape') {
- if (mobileSearchContainer) {
- mobileSearchContainer.style.display = 'none';
- }
-
- if (mobileSearchInputElem) {
- mobileSearchInputElem.value = '';
- }
- if (mobileSearchResultsElem) {
- while (mobileSearchResultsElem.firstChild) {
- mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
- }
- }
- if (htmlElem) {
- htmlElem.style.overflowY = 'visible';
- }
- }
- }) : null;
- // ============================================================
- // ======================= theme change =======================
- var localTheme = localStorage.getItem('theme');
- var rootEleme = document.getElementById('root');
- var selectThemeElem = document.querySelectorAll('.select-theme');
- var selectThemeItemElem = document.querySelectorAll('.select-theme__item');
-
- if (localTheme) {
- selectThemeItemElem ?
- selectThemeItemElem.forEach(function (elem) {
- if (elem.text.trim() === localTheme) {
- elem.classList.add('is-active');
- } else {
- elem.classList.remove('is-active');
- }
- }) : null;
- }
- selectThemeItemElem ?
- selectThemeItemElem.forEach(function (v, i) {
- v.addEventListener('click', function (e) {
- var selectedThemeVariant = e.target.text.trim();
- localStorage.setItem('theme', selectedThemeVariant);
- rootEleme.removeAttribute('class');
- rootEleme.classList.add(`theme__${selectedThemeVariant}`);
- selectThemeElem.forEach(function(rootElem) {
- rootElem.querySelectorAll('a').forEach(function (elem) {
- if (elem.classList) {
- if (elem.text.trim() === selectedThemeVariant) {
- if (!elem.classList.contains('is-active')) {
- elem.classList.add('is-active');
- }
- } else {
- if (elem.classList.contains('is-active')) {
- elem.classList.remove('is-active');
- }
- }
- }
- });
- });
- if (window.mermaid) {
- if (selectedThemeVariant === "dark" || selectedThemeVariant === "hacker") {
- mermaid.initialize({ theme: 'dark' });
- location.reload();
- } else {
- mermaid.initialize({ theme: 'default' });
- location.reload();
- }
- }
- var utterances = document.querySelector('iframe');
- if (utterances) {
- utterances.contentWindow.postMessage({
- type: 'set-theme',
- theme: selectedThemeVariant === "dark" || selectedThemeVariant === "hacker" ? 'photon-dark' : selectedThemeVariant === 'kimbie' ? 'github-dark-orange' : 'github-light',
- }, 'https://utteranc.es');
- }
- });
- }) : null;
- // ============================================================
- // ========================== search ==========================
- {{ $langprefix := $.Site.LanguagePrefix }}
- var langprefix = JSON.parse({{ $langprefix | jsonify }});
- var searchResults = null;
- var searchMenu = null;
- var searchText = null;
-
- {{ $enableSearchHighlight := ($.Param "enableSearchHighlight") }}
- var enableSearchHighlight = JSON.parse({{ $enableSearchHighlight | jsonify }});
- {{ $enableSearch := ($.Param "enableSearch") }}
- var enableSearch = JSON.parse({{ $enableSearch | jsonify }});
- var fuse = null;
- if (enableSearch) {
- (function initFuse() {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', baseurl + langprefix + "/index.json");
- xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
- xhr.onload = function () {
- if (xhr.status === 200) {
- fuse = new Fuse(JSON.parse(xhr.response.toString('utf-8')), {
- keys: ['title', 'description', 'content'],
- includeMatches: enableSearchHighlight,
- shouldSort: true,
- threshold: 0.4,
- location: 0,
- distance: 100,
- maxPatternLength: 32,
- minMatchCharLength: 1,
- });
- }
- else {
- console.error(`[${xhr.status}]Error:`, xhr.statusText);
- }
- };
- xhr.send();
- })();
- }
-
- function makeLi(ulElem, obj) {
- var li = document.createElement('li');
- li.className = 'search-result__item';
-
- var a = document.createElement('a');
- a.innerHTML = obj.title;
- a.setAttribute('class', 'search-result__item--title');
- a.setAttribute('href', obj.permalink);
- var descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- if (obj.description) {
- descDiv.innerHTML = obj.description;
- } else if (obj.content) {
- descDiv.innerHTML = obj.content.substring(0, 80);
- }
-
- li.appendChild(a);
- li.appendChild(descDiv);
- ulElem.appendChild(li);
- }
- function makeHighlightLi(ulElem, obj) {
- var li = document.createElement('li');
- li.className = 'search-result__item';
- var descDiv = null;
- var a = document.createElement('a');
- a.innerHTML = obj.item.title;
- a.setAttribute('class', 'search-result__item--title');
- a.setAttribute('href', obj.item.uri);
- if (obj.matches && obj.matches.length) {
- for (var i = 0; i < obj.matches.length; i++) {
- if ('title' === obj.matches[i].key) {
- a = document.createElement('a');
- a.innerHTML = generateHighlightedText(obj.matches[i].value, obj.matches[i].indices);
- a.setAttribute('class', 'search-result__item--title');
- a.setAttribute('href', obj.item.uri);
- }
-
- if ('description' === obj.matches[i].key) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = generateHighlightedText(obj.item.description, obj.matches[i].indices);
- } else if ('content' === obj.matches[i].key) {
- if (!descDiv) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = generateHighlightedText(obj.item.content.substring(0, 80), obj.matches[i].indices);
- }
- } else {
- if (obj.item.description) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = obj.item.description;
- } else {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = obj.item.content.substring(0, 80);
- }
- }
- }
- li.appendChild(a);
- if (descDiv) {
- li.appendChild(descDiv);
- }
- if (li) {
- ulElem.appendChild(li);
- }
- }
- }
- function renderSearchResults(searchText, results) {
- searchResults = document.getElementById('search-results');
- searchMenu = document.getElementById('search-menu');
- searchResults.setAttribute('class', 'dd is-active');
-
- var ul = document.createElement('ul');
- ul.setAttribute('class', 'dd-content search-content');
- if (results.length) {
- results.forEach(function (result) {
- var li = document.createElement('li');
- var a = document.createElement('a');
- a.setAttribute('href', result.uri);
- a.setAttribute('class', 'dd-item');
- a.appendChild(li);
- var titleDiv = document.createElement('div');
- titleDiv.innerHTML = result.title;
- titleDiv.setAttribute('class', 'search-result__item--title');
- var descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- if (result.description) {
- descDiv.innerHTML = result.description;
- } else if (result.content) {
- descDiv.innerHTML = result.content.substring(0, 80);
- }
- li.appendChild(titleDiv);
- li.appendChild(descDiv);
- ul.appendChild(a);
- });
- } else {
- var li = document.createElement('li');
- li.setAttribute('class', 'dd-item');
- li.innerText = 'No results found';
- ul.appendChild(li);
- }
- while (searchMenu.hasChildNodes()) {
- searchMenu.removeChild(
- searchMenu.lastChild
- );
- }
-
- searchMenu.appendChild(ul);
- }
- function renderSearchHighlightResults(searchText, results) {
- searchResults = document.getElementById('search-results');
- searchMenu = document.getElementById('search-menu');
- searchResults.setAttribute('class', 'dd is-active');
- var ul = document.createElement('ul');
- ul.setAttribute('class', 'dd-content search-content');
- if (results.length) {
- results.forEach(function (result) {
- var li = document.createElement('li');
- var a = document.createElement('a');
- var descDiv = null;
- a.setAttribute('href', result.item.uri);
- a.setAttribute('class', 'dd-item');
- a.appendChild(li);
- var titleDiv = document.createElement('div');
- titleDiv.innerHTML = result.item.title;
- titleDiv.setAttribute('class', 'search-result__item--title');
-
- if (result.matches && result.matches.length) {
- for (var i = 0; i < result.matches.length; i++) {
- if ('title' === result.matches[i].key) {
- titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
- }
- if ('description' === result.matches[i].key) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
- } else if ('content' === result.matches[i].key) {
- if (!descDiv) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 80), result.matches[i].indices);
- }
- } else {
- if (result.item.description) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = result.item.description;
- } else {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'search-result__item--desc');
- descDiv.innerHTML = result.item.content.substring(0, 80);
- }
- }
- }
-
- li.appendChild(titleDiv);
- if (descDiv) {
- li.appendChild(descDiv);
- }
- ul.appendChild(a);
- }
- });
- } else {
- var li = document.createElement('li');
- li.setAttribute('class', 'dd-item');
- li.innerText = 'No results found';
- ul.appendChild(li);
- }
- while (searchMenu.hasChildNodes()) {
- searchMenu.removeChild(
- searchMenu.lastChild
- );
- }
- searchMenu.appendChild(ul);
- }
- function renderSearchResultsMobile(searchText, results) {
- searchResults = document.getElementById('search-mobile-results');
- var content = document.createElement('div');
- content.setAttribute('class', 'mobile-search__content');
- if (results.length > 0) {
- results.forEach(function (result) {
- var item = document.createElement('a');
- item.setAttribute('href', result.uri);
- 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>';
- content.appendChild(item);
- });
- } else {
- var item = document.createElement('span');
- content.appendChild(item);
- }
- let wrap = document.getElementById('search-mobile-results');
- while (wrap.firstChild) {
- wrap.removeChild(wrap.firstChild)
- }
- searchResults.appendChild(content);
- }
- function renderSearchHighlightResultsMobile(searchText, results) {
- searchResults = document.getElementById('search-mobile-results');
- var ul = document.createElement('div');
- ul.setAttribute('class', 'mobile-search__content');
- if (results.length) {
- results.forEach(function (result) {
- var li = document.createElement('li');
- var a = document.createElement('a');
- var descDiv = null;
- a.setAttribute('href', result.item.uri);
- a.appendChild(li);
- li.setAttribute('class', 'mobile-search__item');
- var titleDiv = document.createElement('div');
- titleDiv.innerHTML = result.item.title;
- titleDiv.setAttribute('class', 'mobile-search__item--title');
-
- if (result.matches && result.matches.length) {
- for (var i = 0; i < result.matches.length; i++) {
- if ('title' === result.matches[i].key) {
- titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
- }
- if ('description' === result.matches[i].key) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'mobile-search__item--desc');
- descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
- } else if ('content' === result.matches[i].key) {
- if (!descDiv) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'mobile-search__item--desc');
- descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 150), result.matches[i].indices);
- }
- } else {
- if (result.item.description) {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'mobile-search__item--desc');
- descDiv.innerHTML = result.item.description;
- } else {
- descDiv = document.createElement('div');
- descDiv.setAttribute('class', 'mobile-search__item--desc');
- descDiv.innerHTML = result.item.content.substring(0, 150);
- }
- }
- }
-
- li.appendChild(titleDiv);
- if (descDiv) {
- li.appendChild(descDiv);
- }
- ul.appendChild(a);
- }
- });
- } else {
- var item = document.createElement('span');
- ul.appendChild(item);
- }
- let wrap = document.getElementById('search-mobile-results');
- while (wrap.firstChild) {
- wrap.removeChild(wrap.firstChild)
- }
- searchResults.appendChild(ul);
- }
- function generateHighlightedText(text, regions) {
- if (!regions) {
- return text;
- }
- var content = '', nextUnhighlightedRegionStartingIndex = 0;
- regions.forEach(function(region) {
- if (region[0] === region[1]) {
- return null;
- }
-
- content += '' +
- text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
- '<span class="search__highlight">' +
- text.substring(region[0], region[1] + 1) +
- '</span>' +
- '';
- nextUnhighlightedRegionStartingIndex = region[1] + 1;
- });
- content += text.substring(nextUnhighlightedRegionStartingIndex);
- return content;
- };
- var searchElem = document.getElementById('search');
- var searchMobile = document.getElementById('search-mobile');
- var searchResultsContainer = document.getElementById('search-results');
- searchElem ?
- searchElem.addEventListener('input', function(e) {
- if (!e.target.value | window.innerWidth < 770) {
- searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
- return null;
- }
- searchText = e.target.value;
- var results = fuse.search(e.target.value);
- if (enableSearchHighlight) {
- renderSearchHighlightResults(searchText, results);
- } else {
- renderSearchResults(searchText, results);
- }
- var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
- dropdownItems ? dropdownItems.forEach(function (item) {
- item.addEventListener('mousedown', function (e) {
- e.target.click();
- });
- }) : null;
- }) : null;
- searchElem ?
- searchElem.addEventListener('blur', function() {
- if (window.innerWidth < 770) {
- return null;
- }
- searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
- }) : null;
- searchElem ?
- searchElem.addEventListener('click', function(e) {
- if (window.innerWidth < 770) {
- return null;
- }
- if (!e.target.value) {
- searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
- return null;
- }
- searchText = e.target.value;
- var results = fuse.search(e.target.value);
- if (enableSearchHighlight) {
- renderSearchHighlightResults(searchText, results);
- } else {
- renderSearchResults(searchText, results);
- }
- var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
- dropdownItems ? dropdownItems.forEach(function (item) {
- item.addEventListener('mousedown', function (e) {
- e.target.click();
- });
- }) : null;
- }) : null;
- var searchMenuElem = document.getElementById("search-menu");
- var activeItem = document.querySelector('#search-menu .dd-item.is-active');
- var activeIndex = null;
- var items = null;
- var searchContainerMaxHeight = 350;
- searchElem ?
- searchElem.addEventListener('keydown', function(e) {
- if (window.innerWidth < 770) {
- return null;
- }
- var items = document.querySelectorAll('#search-menu .dd-item');
-
- if (e.key === 'ArrowDown') {
- if (activeIndex === null) {
- activeIndex = 0;
- items[activeIndex].classList.remove('is-active');
- } else {
- items[activeIndex].classList.remove('is-active');
- activeIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
- }
- items[activeIndex].classList.add('is-active');
- let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
- if (overflowedPixel > 0) {
- document.querySelector(".search-content").scrollTop += items[activeIndex].getBoundingClientRect().height;
- } else if (activeIndex === 0) {
- document.querySelector(".search-content").scrollTop = 0;
- }
- } else if (e.key === 'ArrowUp') {
- if (activeIndex === null) {
- activeIndex = items.length - 1;
- items[activeIndex].classList.remove('is-active');
- } else {
- items[activeIndex].classList.remove('is-active');
- activeIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
- }
- items[activeIndex].classList.add('is-active');
-
- let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
- if (overflowedPixel < 0) {
- document.querySelector(".search-content").scrollTop -= items[activeIndex].getBoundingClientRect().height;
- } else {
- document.querySelector(".search-content").scrollTop = overflowedPixel + items[activeIndex].getBoundingClientRect().height;
- }
- } else if (e.key === 'Enter') {
- var currentItemLink = items[activeIndex].getAttribute('href');
- if (currentItemLink) {
- location.href = currentItemLink;
- }
- } else if (e.key === 'Escape') {
- e.target.value = null;
- if (searchResults) {
- searchResults.classList.remove('is-active');
- }
- }
- }) : null;
- searchMobile ?
- searchMobile.addEventListener('input', function(e) {
- if (!e.target.value) {
- let wrap = document.getElementById('search-mobile-results');
- while (wrap.firstChild) {
- wrap.removeChild(wrap.firstChild);
- }
- return null;
- }
- searchText = e.target.value;
- var results = fuse.search(e.target.value);
-
- if (enableSearchHighlight) {
- renderSearchHighlightResultsMobile(searchText, results);
- } else {
- renderSearchResultsMobile(searchText, results);
- }
- var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
- dropdownItems ? dropdownItems.forEach(function (item) {
- item.addEventListener('mousedown', function (e) {
- e.target.click();
- });
- }) : null;
- }) : null;
- // ============================================================
- }
- </script>
|