bulma/docs/_javascript/main.js

298 lines
7.5 KiB
JavaScript
Raw Normal View History

2017-07-01 17:30:39 +00:00
document.addEventListener('DOMContentLoaded', () => {
// Search
2017-07-01 17:30:39 +00:00
const setSearchToggle = () => {
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
icon.addEventListener('click', (event) => {
search.classList.toggle('bd-is-visible');
2018-04-09 19:19:29 +00:00
if (search.classList.contains('bd-is-visible')) {
algoliaSearch.focus();
2018-04-09 19:19:29 +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 = () => {
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);
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(
'#navbarStartClone > .bd-navbar-item'
);
const base = document.querySelectorAll(
'#navbarStartClone > .bd-navbar-item-base'
);
const more = document.querySelectorAll(
'#navbarStartOriginal > .bd-navbar-item-more'
);
const dropdown = document.querySelectorAll(
'#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++) {
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++) {
more[j].classList.remove('bd-is-visible');
}
2017-07-20 19:01:55 +00:00
for (let k = howManyMore; k < dropdown.length; k++) {
dropdown[k].classList.remove('bd-is-hidden');
}
};
2017-07-29 12:02:00 +00:00
setNavbarVisibility();
// Cookies
const cookieBookModalName = 'bulma_closed_book_modal';
const cookieBookModal = Cookies.getJSON(cookieBookModalName) || false;
2017-07-29 12:02:00 +00:00
// Dropdowns
2017-07-24 10:34: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) => {
$el.addEventListener('click', (event) => {
2017-07-20 19:01:55 +00:00
event.stopPropagation();
$el.classList.toggle('is-active');
});
});
document.addEventListener('click', (event) => {
2017-07-21 07:30:56 +00:00
closeDropdowns();
});
}
function closeDropdowns() {
$dropdowns.forEach(($el) => {
2017-07-21 07:30:56 +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
2020-12-09 15:44:40 +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;
}
2017-07-02 16:34:44 +00:00
$el.addEventListener('click', () => {
const target = $el.dataset.target;
const $target = document.getElementById(target);
$el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
2017-07-01 17:30:39 +00:00
});
}
// Modals
2017-10-26 18:47:48 +00:00
const rootEl = document.documentElement;
2017-07-02 15:52:20 +00:00
const $modals = getAll('.modal');
const $modalButtons = getAll('.modal-button');
const $modalCloses = getAll(
'.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) => {
2017-07-01 17:30:39 +00:00
$el.addEventListener('click', () => {
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) => {
2017-07-01 17:30:39 +00:00
$el.addEventListener('click', () => {
closeModals();
});
});
}
2018-03-26 14:33:31 +00:00
function openModal(target) {
const $target = document.getElementById(target);
rootEl.classList.add('is-clipped');
$target.classList.add('is-active');
}
2017-07-01 17:30:39 +00:00
function closeModals() {
2017-10-26 18:47:48 +00:00
rootEl.classList.remove('is-clipped');
$modals.forEach(($el) => {
2017-07-01 17:30:39 +00:00
$el.classList.remove('is-active');
});
}
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
const $highlights = getAll('.highlight');
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>';
2017-10-09 11:27:08 +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;
2017-10-09 14:38:12 +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>';
$parent.insertAdjacentHTML('afterbegin', showEl);
} else if (
$el.firstElementChild.scrollHeight > 480 &&
$el.firstElementChild.clientHeight <= 480
) {
2017-10-09 14:38:12 +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(
'.highlight .bd-copy, .highlight .bd-expand'
);
2017-07-02 15:52:20 +00:00
$highlightButtons.forEach(($el) => {
2017-07-02 15:52:20 +00:00
$el.addEventListener('mouseenter', () => {
2017-08-14 17:25:14 +00:00
$el.parentNode.classList.add('bd-is-hovering');
$el.parentNode.parentNode.classList.add('bd-is-hovering');
2017-07-02 15:52:20 +00:00
});
$el.addEventListener('mouseleave', () => {
2017-08-14 17:25:14 +00:00
$el.parentNode.classList.remove('bd-is-hovering');
$el.parentNode.parentNode.classList.remove('bd-is-hovering');
2017-07-02 15:52:20 +00:00
});
});
const $highlightExpands = getAll('.bd-snippet .bd-expand');
2017-07-02 15:52:20 +00:00
$highlightExpands.forEach(($el) => {
2017-07-02 15:52:20 +00:00
$el.addEventListener('click', () => {
$el.parentNode.firstElementChild.style.maxHeight = 'none';
});
});
2017-10-09 11:27:08 +00:00
const $highlightShows = getAll('.bd-snippet .bd-show');
2017-10-09 11:27:08 +00:00
$highlightShows.forEach(($el) => {
2017-10-09 11:27:08 +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(() => {
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
});
}, 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
}
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
});