{{- /*
|
Site scripts. Each block is progressive enhancement: its buttons are hidden
|
in the markup and only revealed here, so a site with JavaScript disabled
|
never shows an inert button.
|
|
1. Copy-to-clipboard behaviour for code blocks rendered by
|
layouts/_markup/render-codeblock.html. A single delegated listener
|
handles every code block on the page. Disable the copy buttons entirely
|
with `[ananke] copy_code = false`.
|
2. Header navigation rendered by layouts/_partials/site-navigation.html:
|
the menu button that collapses the menu below the Tachyons `-l`
|
breakpoint, and the submenu toggles. Only included if the site has a
|
`main` menu.
|
|
Override this partial in your own project to add or replace site scripts.
|
*/ -}}
|
{{- if ne false site.Params.ananke.copy_code -}}
|
<script>
|
(function () {
|
"use strict";
|
|
// Reveal the copy buttons once the DOM is ready. This script is included in
|
// the <head>, so the code blocks may not exist yet when it first runs.
|
function revealButtons() {
|
var buttons = document.querySelectorAll(".code-block .code-copy[hidden]");
|
for (var i = 0; i < buttons.length; i++) {
|
buttons[i].hidden = false;
|
}
|
}
|
if (document.readyState === "loading") {
|
document.addEventListener("DOMContentLoaded", revealButtons);
|
} else {
|
revealButtons();
|
}
|
|
function flash(button) {
|
var label = button.querySelector(".code-copy-label");
|
var previous = label ? label.textContent : null;
|
button.classList.add("is-copied");
|
if (label) label.textContent = "Copied";
|
window.setTimeout(function () {
|
button.classList.remove("is-copied");
|
if (label && previous !== null) label.textContent = previous;
|
}, 2000);
|
}
|
|
function legacyCopy(text) {
|
var area = document.createElement("textarea");
|
area.value = text;
|
area.setAttribute("readonly", "");
|
area.style.position = "absolute";
|
area.style.left = "-9999px";
|
document.body.appendChild(area);
|
area.select();
|
var ok = false;
|
try {
|
ok = document.execCommand("copy");
|
} catch (e) {
|
ok = false;
|
}
|
document.body.removeChild(area);
|
return ok;
|
}
|
|
function copyFrom(button) {
|
var container = button.closest(".code-block");
|
if (!container) return;
|
var pre = container.querySelector("pre");
|
if (!pre) return;
|
var source = pre.querySelector("code") || pre;
|
var text = source.innerText;
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
navigator.clipboard.writeText(text).then(
|
function () { flash(button); },
|
function () { if (legacyCopy(text)) flash(button); }
|
);
|
} else if (legacyCopy(text)) {
|
flash(button);
|
}
|
}
|
|
document.addEventListener("click", function (event) {
|
var button = event.target.closest(".code-copy");
|
if (button) copyFrom(button);
|
});
|
})();
|
</script>
|
{{- end -}}
|
|
{{- if site.Menus.main -}}
|
<script>
|
(function () {
|
"use strict";
|
|
// The toggle button of the open submenu. Only one submenu is open at a time.
|
var openButton = null;
|
|
function controlled(button) {
|
return document.getElementById(button.getAttribute("aria-controls"));
|
}
|
|
function setSubmenu(button, expanded) {
|
var submenu = controlled(button);
|
if (!submenu) return;
|
button.setAttribute("aria-expanded", expanded ? "true" : "false");
|
submenu.hidden = !expanded;
|
submenu.classList.remove("ananke-submenu-flip");
|
if (expanded) {
|
// Align the dropdown to the other edge of its parent if it would
|
// leave the viewport.
|
var rect = submenu.getBoundingClientRect();
|
if (rect.right > document.documentElement.clientWidth || rect.left < 0) {
|
submenu.classList.add("ananke-submenu-flip");
|
}
|
}
|
openButton = expanded ? button : null;
|
}
|
|
function closeSubmenu(returnFocus) {
|
var button = openButton;
|
if (!button) return;
|
setSubmenu(button, false);
|
if (returnFocus) button.focus();
|
}
|
|
function setMenu(button, expanded) {
|
var nav = button.closest(".ananke-nav");
|
if (!nav) return;
|
button.setAttribute("aria-expanded", expanded ? "true" : "false");
|
nav.classList.toggle("ananke-nav-collapsed", !expanded);
|
}
|
|
// Reveal the buttons and collapse the menu and submenus once the DOM is
|
// ready. Without JavaScript everything stays visible.
|
function enhance() {
|
var menuButtons = document.querySelectorAll(".ananke-nav-toggle[hidden]");
|
for (var i = 0; i < menuButtons.length; i++) {
|
if (!controlled(menuButtons[i])) continue;
|
setMenu(menuButtons[i], false);
|
menuButtons[i].hidden = false;
|
}
|
var buttons = document.querySelectorAll(".ananke-submenu-toggle[hidden]");
|
for (var j = 0; j < buttons.length; j++) {
|
var submenu = controlled(buttons[j]);
|
if (!submenu) continue;
|
submenu.hidden = true;
|
buttons[j].hidden = false;
|
var label = buttons[j].parentNode.querySelector(".ananke-submenu-label");
|
if (label) label.hidden = true;
|
}
|
}
|
if (document.readyState === "loading") {
|
document.addEventListener("DOMContentLoaded", enhance);
|
} else {
|
enhance();
|
}
|
|
document.addEventListener("click", function (event) {
|
var menuButton = event.target.closest(".ananke-nav-toggle");
|
if (menuButton) {
|
setMenu(menuButton, menuButton.getAttribute("aria-expanded") !== "true");
|
return;
|
}
|
var button = event.target.closest(".ananke-submenu-toggle");
|
if (button) {
|
var expand = button.getAttribute("aria-expanded") !== "true";
|
if (openButton && openButton !== button) closeSubmenu(false);
|
setSubmenu(button, expand);
|
} else if (openButton && !openButton.parentNode.contains(event.target)) {
|
closeSubmenu(false);
|
}
|
});
|
|
document.addEventListener("keydown", function (event) {
|
if (event.key !== "Escape") return;
|
if (openButton) {
|
// Return focus to the button only if focus was inside the menu item.
|
closeSubmenu(openButton.parentNode.contains(document.activeElement));
|
return;
|
}
|
var menuButton = document.querySelector('.ananke-nav-toggle[aria-expanded="true"]');
|
if (!menuButton) return;
|
var panel = controlled(menuButton);
|
var inside = panel && panel.contains(document.activeElement);
|
setMenu(menuButton, false);
|
if (inside) menuButton.focus();
|
});
|
|
// Close a submenu when keyboard focus leaves its menu item. Pointer users
|
// get the outside-click handler instead: on small screens submenus open
|
// inline, and closing one on pointer focus would move the target of the
|
// click that is still in progress.
|
var keyboardFocus = false;
|
document.addEventListener("keydown", function () {
|
keyboardFocus = true;
|
}, true);
|
document.addEventListener("pointerdown", function () {
|
keyboardFocus = false;
|
}, true);
|
document.addEventListener("focusout", function (event) {
|
if (!keyboardFocus || !openButton || !event.relatedTarget) return;
|
var item = openButton.parentNode;
|
if (item.contains(event.target) && !item.contains(event.relatedTarget)) {
|
closeSubmenu(false);
|
}
|
});
|
})();
|
</script>
|
{{- end -}}
|