bulma/docs/_javascript/main.js

300 lines
7.5 KiB
JavaScript
Raw Normal View History

2022-01-16 00:00:43 +00:00
document.addEventListener("DOMContentLoaded", () => {
// Search
2017-07-01 17:30:39 +00:00
const setSearchToggle = () => {
2022-01-16 00:00:43 +00:00
const icon = document.getElementById("searchIcon");
const search = document.getElementById("search");
const input = document.getElementById("algoliaSearch");
2018-03-26 14:33:31 +00:00
if (!icon) {
return;
}
2018-04-09 19:19:29 +00:00
2022-01-16 00:00:43 +00:00
icon.addEventListener("click", (event) => {
search.classList.toggle("bd-is-visible");
2018-04-09 19:19:29 +00:00
2022-01-16 00:00:43 +00:00
if (search.classList.contains("bd-is-visible")) {
algoliaSearch.focus();
2018-04-09 19:19:29 +00:00
}
});
2022-01-16 00:00:43 +00:00
window.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
return search.classList.remove("bd-is-visible");
2018-04-10 22:52:51 +00:00
}
});
};
2018-04-10 22:52:51 +00:00
setSearchToggle();
2018-04-10 22:52:51 +00:00
// Navbar
2018-04-10 22:52:51 +00:00
const setNavbarVisibility = () => {
2022-01-16 00:00:43 +00:00
const navbar = document.getElementById("navbar");
2018-04-10 22:52:51 +00:00
if (!navbar) {
return;
2018-04-10 22:52:51 +00:00
}
const cs = getComputedStyle(navbar);
const paddingX = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);
2022-01-16 00:00:43 +00:00
const brand = navbar.querySelector(".navbar-brand");
const end = navbar.querySelector(".navbar-end");
const search = navbar.querySelector(".bd-search");
const original = document.getElementById("navbarStartOriginal");
const clone = document.getElementById("navbarStartClone");
const rest =
navbar.clientWidth -
paddingX -
brand.clientWidth -
end.clientWidth -
search.clientWidth;
const space = original.clientWidth;
const all = document.querySelectorAll(
2022-01-16 00:00:43 +00:00
"#navbarStartClone > .bd-navbar-item"
);
const base = document.querySelectorAll(
2022-01-16 00:00:43 +00:00
"#navbarStartClone > .bd-navbar-item-base"
);
const more = document.querySelectorAll(
2022-01-16 00:00:43 +00:00
"#navbarStartOriginal > .bd-navbar-item-more"
);
const dropdown = document.querySelectorAll(
2022-01-16 00:00:43 +00:00
"#navbarStartOriginal .bd-navbar-dropdown > .navbar-item"
);
let count = 0;
let totalWidth = 0;
const showMore = () => {};
const hideMore = () => {};
for (const item of all) {
totalWidth += item.offsetWidth;
if (totalWidth > rest) {
break;
}
2018-04-10 22:52:51 +00:00
count++;
}
2018-04-10 22:52:51 +00:00
const howManyMore = Math.max(0, count - base.length);
if (howManyMore > 0) {
for (let i = 0; i < howManyMore; i++) {
2022-01-16 00:00:43 +00:00
more[i].classList.add("bd-is-visible");
dropdown[i].classList.add("bd-is-hidden");
2018-04-10 22:52:51 +00:00
}
}
2018-04-10 22:52:51 +00:00
for (let j = howManyMore; j < more.length; j++) {
2022-01-16 00:00:43 +00:00
more[j].classList.remove("bd-is-visible");
}
2017-07-20 19:01:55 +00:00
for (let k = howManyMore; k < dropdown.length; k++) {
2022-01-16 00:00:43 +00:00
dropdown[k].classList.remove("bd-is-hidden");
}
};
2017-07-29 12:02:00 +00:00
setNavbarVisibility();
// Cookies
2022-01-16 00:00:43 +00:00
const cookieBookModalName = "bulma_closed_book_modal";
const cookieBookModal = Cookies.getJSON(cookieBookModalName) || false;
2017-07-29 12:02:00 +00:00
// Dropdowns
2022-01-16 00:00:43 +00:00
const $dropdowns = getAll(".dropdown:not(.is-hoverable)");
2017-07-20 19:01:55 +00:00
if ($dropdowns.length > 0) {
$dropdowns.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.addEventListener("click", (event) => {
2017-07-20 19:01:55 +00:00
event.stopPropagation();
2022-01-16 00:00:43 +00:00
$el.classList.toggle("is-active");
2017-07-20 19:01:55 +00:00
});
});
2022-01-16 00:00:43 +00:00
document.addEventListener("click", (event) => {
2017-07-21 07:30:56 +00:00
closeDropdowns();
});
}
function closeDropdowns() {
$dropdowns.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.classList.remove("is-active");
2017-07-20 19:01:55 +00:00
});
}
2017-07-02 16:34:44 +00:00
// Toggles
2017-07-01 17:30:39 +00:00
2022-01-16 00:00:43 +00:00
const $burgers = getAll(".navbar-burger");
2017-07-01 17:30:39 +00:00
2017-07-02 16:34:44 +00:00
if ($burgers.length > 0) {
$burgers.forEach(($el) => {
2020-12-09 15:44:40 +00:00
if (!$el.dataset.target) {
return;
}
2022-01-16 00:00:43 +00:00
$el.addEventListener("click", () => {
2017-07-02 16:34:44 +00:00
const target = $el.dataset.target;
const $target = document.getElementById(target);
2022-01-16 00:00:43 +00:00
$el.classList.toggle("is-active");
$target.classList.toggle("is-active");
2017-07-02 16:34:44 +00:00
});
2017-07-01 17:30:39 +00:00
});
}
// Modals
2017-10-26 18:47:48 +00:00
const rootEl = document.documentElement;
2022-01-16 00:00:43 +00:00
const $modals = getAll(".modal");
const $modalButtons = getAll(".modal-button");
const $modalCloses = getAll(
2022-01-16 00:00:43 +00:00
".modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button"
);
2017-07-01 17:30:39 +00:00
if ($modalButtons.length > 0) {
$modalButtons.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.addEventListener("click", () => {
2017-07-01 17:30:39 +00:00
const target = $el.dataset.target;
2018-03-26 14:33:31 +00:00
openModal(target);
2017-07-01 17:30:39 +00:00
});
});
}
if ($modalCloses.length > 0) {
$modalCloses.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.addEventListener("click", () => {
2017-07-01 17:30:39 +00:00
closeModals();
});
});
}
2018-03-26 14:33:31 +00:00
function openModal(target) {
const $target = document.getElementById(target);
2022-01-16 00:00:43 +00:00
rootEl.classList.add("is-clipped");
$target.classList.add("is-active");
2018-03-26 14:33:31 +00:00
}
2017-07-01 17:30:39 +00:00
function closeModals() {
2022-01-16 00:00:43 +00:00
rootEl.classList.remove("is-clipped");
$modals.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.classList.remove("is-active");
2017-07-01 17:30:39 +00:00
});
}
2022-01-16 00:00:43 +00:00
document.addEventListener("keydown", (event) => {
2018-04-08 18:54:36 +00:00
const e = event || window.event;
2018-04-08 18:54:36 +00:00
if (e.keyCode === 27) {
closeModals();
closeDropdowns();
}
});
2017-07-02 15:52:20 +00:00
// Clipboard
2022-01-16 00:00:43 +00:00
const $highlights = getAll(".highlight");
2017-07-02 15:52:20 +00:00
let itemsProcessed = 0;
if ($highlights.length > 0) {
$highlights.forEach(($el) => {
const copyEl = '<button class="button bd-copy">Copy</button>';
const expandEl =
'<button class="button is-small bd-expand">Expand</button>';
2022-01-16 00:00:43 +00:00
$el.insertAdjacentHTML("beforeend", copyEl);
2017-07-02 15:52:20 +00:00
2017-10-09 11:27:08 +00:00
const $parent = $el.parentNode;
2022-01-16 00:00:43 +00:00
if ($parent && $parent.classList.contains("bd-is-more")) {
const showEl =
'<button class="button is-small bd-show"><span class="icon"><i class="fas fa-code"></i></span><strong>Show code</strong></button>';
2022-01-16 00:00:43 +00:00
$parent.insertAdjacentHTML("afterbegin", showEl);
} else if (
$el.firstElementChild.scrollHeight > 480 &&
$el.firstElementChild.clientHeight <= 480
) {
2022-01-16 00:00:43 +00:00
$el.insertAdjacentHTML("beforeend", expandEl);
2017-07-02 15:52:20 +00:00
}
itemsProcessed++;
2017-07-02 15:52:20 +00:00
if (itemsProcessed === $highlights.length) {
addHighlightControls();
}
});
}
function addHighlightControls() {
const $highlightButtons = getAll(
2022-01-16 00:00:43 +00:00
".highlight .bd-copy, .highlight .bd-expand"
);
2017-07-02 15:52:20 +00:00
$highlightButtons.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.addEventListener("mouseenter", () => {
$el.parentNode.classList.add("bd-is-hovering");
$el.parentNode.parentNode.classList.add("bd-is-hovering");
2017-07-02 15:52:20 +00:00
});
2022-01-16 00:00:43 +00:00
$el.addEventListener("mouseleave", () => {
$el.parentNode.classList.remove("bd-is-hovering");
$el.parentNode.parentNode.classList.remove("bd-is-hovering");
2017-07-02 15:52:20 +00:00
});
});
2022-01-16 00:00:43 +00:00
const $highlightExpands = getAll(".bd-snippet .bd-expand");
2017-07-02 15:52:20 +00:00
$highlightExpands.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.addEventListener("click", () => {
$el.parentNode.firstElementChild.style.maxHeight = "none";
2017-07-02 15:52:20 +00:00
});
});
2017-10-09 11:27:08 +00:00
2022-01-16 00:00:43 +00:00
const $highlightShows = getAll(".bd-snippet .bd-show");
2017-10-09 11:27:08 +00:00
$highlightShows.forEach(($el) => {
2022-01-16 00:00:43 +00:00
$el.addEventListener("click", () => {
const text = $el.querySelector("strong").textContent;
const newText = text === "Show code" ? "Hide code" : "Show code";
$el.querySelector("strong").textContent = newText;
$el.parentNode.classList.toggle("bd-is-more-clipped");
2017-10-09 11:27:08 +00:00
});
});
2017-07-02 15:52:20 +00:00
}
2018-02-13 11:15:12 +00:00
setTimeout(() => {
2022-01-16 00:00:43 +00:00
new Clipboard(".bd-copy", {
target: (trigger) => {
2018-02-13 11:15:12 +00:00
return trigger.previousElementSibling.firstElementChild;
},
2018-02-13 11:15:12 +00:00
});
2022-01-16 00:00:43 +00:00
new Clipboard(".bd-clipboard");
2018-02-13 11:15:12 +00:00
}, 100);
2017-07-02 15:52:20 +00:00
// Events
2020-05-10 23:25:29 +00:00
let resizeTimer;
2020-05-11 00:03:16 +00:00
function handleResize() {
window.clearTimeout(resizeTimer);
2020-05-10 23:25:29 +00:00
resizeTimer = window.setTimeout(function () {
setNavbarVisibility();
}, 10);
2020-05-10 23:25:29 +00:00
}
2022-01-16 00:00:43 +00:00
window.addEventListener("resize", handleResize);
2017-07-29 13:59:10 +00:00
2018-04-11 00:14:29 +00:00
// Utils
function getAll(selector, parent = document) {
return Array.prototype.slice.call(parent.querySelectorAll(selector), 0);
2018-04-11 00:14:29 +00:00
}
2017-07-01 17:30:39 +00:00
});