const navButton = document.querySelector('.nav__menu--button');
const closeButton = document.querySelector('.nav__menu--close');
const mobileNavMenu = document.querySelector('.k-nav');
const pageContainer = document.querySelector('.page__container');
navButton.addEventListener('click', () => {
mobileNavMenu.style.display = 'block';
closeButton.style.display = 'block';
navButton.style.display = 'none';
pageContainer.style.position = 'fixed';
});
closeButton.addEventListener('click', () => {
mobileNavMenu.style.display = 'none';
closeButton.style.display = 'none';
navButton.style.display = 'block';
pageContainer.style.position = 'static';
});
function addCopyButtonsToCodeBlocks() {
const allCodeBlocks = document.querySelectorAll('.codehilite:not(.k-default-codeblock .codehilite)');
allCodeBlocks.forEach((block) => {
if (block.querySelector('.code__copy--button')) {
return;
}
const button = document.createElement('button');
button.className = 'code__copy--button';
button.setAttribute('aria-label', 'Copy code to clipboard');
const icon = document.createElement('i');
icon.className = 'icon--copy';
button.appendChild(icon);
const tooltip = document.createElement('div');
tooltip.className = 'code__copy--tooltip';
tooltip.textContent = 'Copied!';
button.appendChild(tooltip);
block.insertBefore(button, block.firstChild);
button.addEventListener('click', () => {
const codeElement = block.querySelector('pre code') || block.querySelector('code') || block.querySelector('pre');
if (!codeElement) return;
let text = codeElement.innerText || codeElement.textContent;
text = cleanCodeText(text);
copyWithFallback(text, button);
});
});
}
function cleanCodeText(text) {
const lines = text.split('\n');
const cleanedLines = lines.map(line => {
line = line.replace(/^\s*>>>\s?/, '');
line = line.replace(/^\s*\.\.\.\s?/, '');
return line;
});
return cleanedLines.join('\n');
}
function copyWithFallback(text, button) {
const inputElement = document.createElement('textarea');
inputElement.value = text;
inputElement.setAttribute('readonly', '');
inputElement.style.position = 'absolute';
inputElement.style.left = '-9999px';
document.body.appendChild(inputElement);
inputElement.select();
try {
document.execCommand('copy');
showCopyTooltip(button);
} catch (err) {
console.error('Failed to copy text:', err);
}
inputElement.remove();
}
function showCopyTooltip(button) {
const tooltip = button.querySelector('.code__copy--tooltip');
if (tooltip) {
tooltip.style.display = 'block';
setTimeout(() => {
tooltip.style.display = 'none';
}, 2000);
}
}
document.addEventListener('DOMContentLoaded', addCopyButtonsToCodeBlocks);
const copyButtons = document.querySelectorAll('.code__copy--button');
copyButtons.forEach((button) => {
button.addEventListener('click', () => {
const parent = button.parentNode;
const text = parent.querySelector('.language-python').innerText;
const inputElement = document.createElement('textarea');
console.log('text', text);
inputElement.value = text;
inputElement.setAttribute('class', 'visually-hidden');
const body = document.body;
body.appendChild(inputElement);
inputElement.select();
document.execCommand('copy');
inputElement.remove();
button.querySelector('.code__copy--tooltip').style.display = 'block';
setTimeout(() => {
button.querySelector('.code__copy--tooltip').style.display = 'none';
}, 2000);
});
});
const searchForms = document.querySelectorAll('.nav__search');
const mobileNavSearchIcon = document.querySelector('.nav__search--mobile');
const mobileNavSearchForm = document.querySelector('.nav__search-form--mobile');
const mobileNavControls = document.querySelector('.nav__controls--mobile');
const desktopSearch = document.querySelector('.nav__menu .nav__search');
mobileNavSearchIcon.addEventListener('click', () => {
mobileNavControls.style.display = 'none';
mobileNavSearchForm.style.display = 'block';
});
searchForms.forEach((search) => {
search.addEventListener('submit', (event) => {
event.preventDefault();
const text = search.querySelector('.nav__search--input').value;
window.location = `/search.html?query=${text}`;
});
});
pageContainer.addEventListener('click', () => {
mobileNavControls.style.display = 'flex';
mobileNavSearchForm.style.display = 'none';
});
mobileNavMenu.addEventListener('click', () => {
mobileNavControls.style.display = 'flex';
mobileNavSearchForm.style.display = 'none';
});
if (window.location.pathname.indexOf('search.html') > -1) {
desktopSearch.style.display = 'none';
mobileNavSearchIcon.style.visibility = 'hidden';
}
const exploreModule = document.querySelector('.explore');
const exploreContent = document.querySelector('.explore__content');
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
window.addEventListener('resize', verticallyCenterExploreContent);
return;
}
window.removeEventListener('resize', verticallyCenterExploreContent);
});
},
{ threshold: 0 }
);
if (exploreModule && window.innerWidth > 1199) {
observer.observe(exploreModule);
verticallyCenterExploreContent();
}
function verticallyCenterExploreContent() {
exploreContent.style.marginTop = `${Math.round(exploreContent.getBoundingClientRect().height / 2)}px`;
}