| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067 |
- {{ 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";
- {{ $languagedir := ($.Param "languagedir" | default "ltr") }}
- var languagedir = JSON.parse({{ $languagedir | jsonify }});
- window.onload = function() {
- // ==================== 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 =======================
- var root = document.getElementById('root');
- var toggleToLightBtn = document.getElementById('toggleToLight');
- var toggleToDarkBtn = document.getElementById('toggleToDark');
- toggleToDark.onclick = function(e) {
- root.className = 'theme__dark';
- localStorage.setItem('theme', 'dark');
- toggleToLightBtn.className = 'navbar__icons--icon';
- toggleToDarkBtn.className = 'hide';
- }
- toggleToLight.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')) {
- console.log('parent');
- parent.style.maxHeight = 100 * parent.children.length + "px";
- parent = parent.parentNode;
- }
- if (content.style.maxHeight) {
- console.log('null');
- 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 {
- console.log('scrollHeight');
- 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 ==========================
- {{ $baseurl := $.Site.BaseURL }}
- var baseurl = JSON.parse({{ $baseurl | jsonify }});
- {{ $permalink := .Permalink }}
- var permalink = JSON.parse({{ $permalink | jsonify }});
- {{ $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) {
- console.log('mousedown');
- 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) {
- console.log('mousedown');
- 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) {
- console.log('mousedown');
- e.target.click();
- });
- }) : null;
- }) : null;
- // ============================================================
- }
- </script>
|