| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834 |
- {{ $enquire := resources.Get "js/enquire.min.js" | resources.Fingerprint }}
- <script src="{{ $enquire.RelPermalink }}"></script>
- {{ $fuse := resources.Get "js/fuse.min.js" | resources.Fingerprint }}
- <script defer src="{{ $fuse.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() {
- // ===================== 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');
- }
- });
- }
- // ============================================================
-
- // ======================= 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;
- }
- if (content.style.maxHeight) {
- content.style.maxHeight = null;
- content.classList.remove('active');
- menuTitleIcon.classList.add('right');
- menuTitleIcon.classList.remove('down');
- } else {
- content.style.maxHeight = content.scrollHeight + "px";
- content.classList.add('active');
- menuTitleIcon.classList.remove('right');
- 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.left = 0;
- modal.style.opacity = 1;
- drawer.style.left = 0;
- }
- var closeDrawer = function() {
- modal.style.opacity = 0;
- drawer.style.left = '-100%';
- setTimeout(function () {
- 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');
- {{ $tocLevels := ($.Param "tocLevels") }}
- var tocLevels = JSON.parse({{ $tocLevels | jsonify }});
- if (tocLevels) {
- tocLevels = tocLevels.toString();
- } else {
- tocLevels = "h1, h2, h3, h4, h5, h6";
- }
- 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;
- } 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;
- }
- 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 ==========================
- {{ $permalink:= .Permalink }}
- var permalink = JSON.parse({{ $permalink | jsonify }});
- var searchResults = null;
- var searchMenu = null;
- var searchText = null;
-
- {{ $enableSearchHighlight := ($.Param "enableSearchHighlight") }}
- var enableSearchHighlight = JSON.parse({{ $enableSearchHighlight | jsonify }});
- var fuse = null;
- (function initFuse() {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', permalink + "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.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');
- }
- }
- });
- 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>
|