bulma/docs/_javascript/main.js

179 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-07-01 17:30:39 +00:00
document.addEventListener('DOMContentLoaded', () => {
2017-07-20 19:01:55 +00:00
// Dropdowns
2017-07-29 12:02:00 +00:00
const $metalinks = getAll('#meta a');
if ($metalinks.length > 0) {
$metalinks.forEach($el => {
$el.addEventListener('click', event => {
event.preventDefault();
const target = $el.getAttribute('href');
const $target = document.getElementById(target.substring(1));
$target.scrollIntoView(true);
2017-07-29 13:59:10 +00:00
// window.history.replaceState(null, document.title, `${window.location.origin}${window.location.pathname}${target}`);
2017-07-29 12:02:00 +00:00
return false;
});
});
}
// 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 => {
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 => {
$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
2017-07-02 16:34:44 +00:00
const $burgers = getAll('.burger');
2017-07-01 17:30:39 +00:00
2017-07-02 16:34:44 +00:00
if ($burgers.length > 0) {
$burgers.forEach($el => {
$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
const $html = 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 => {
$el.addEventListener('click', () => {
const target = $el.dataset.target;
const $target = document.getElementById(target);
$html.classList.add('is-clipped');
$target.classList.add('is-active');
});
});
}
if ($modalCloses.length > 0) {
$modalCloses.forEach($el => {
$el.addEventListener('click', () => {
closeModals();
});
});
}
2017-07-21 07:30:56 +00:00
document.addEventListener('keydown', event => {
const e = event || window.event;
2017-07-01 17:30:39 +00:00
if (e.keyCode === 27) {
closeModals();
2017-07-21 07:30:56 +00:00
closeDropdowns();
2017-07-01 17:30:39 +00:00
}
});
function closeModals() {
2017-07-21 07:30:56 +00:00
$html.classList.remove('is-clipped');
2017-07-01 17:30:39 +00:00
$modals.forEach($el => {
$el.classList.remove('is-active');
});
}
2017-07-02 15:52:20 +00:00
// Clipboard
const $highlights = getAll('.highlight');
let itemsProcessed = 0;
if ($highlights.length > 0) {
$highlights.forEach($el => {
const copy = '<button class="copy">Copy</button>';
const expand = '<button class="expand">Expand</button>';
$el.insertAdjacentHTML('beforeend', copy);
2017-07-11 21:19:06 +00:00
if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
2017-07-02 15:52:20 +00:00
$el.insertAdjacentHTML('beforeend', expand);
}
itemsProcessed++;
if (itemsProcessed === $highlights.length) {
addHighlightControls();
}
});
}
function addHighlightControls() {
const $highlightButtons = getAll('.highlight .copy, .highlight .expand');
$highlightButtons.forEach($el => {
$el.addEventListener('mouseenter', () => {
$el.parentNode.style.boxShadow = '0 0 0 1px #ed6c63';
});
$el.addEventListener('mouseleave', () => {
$el.parentNode.style.boxShadow = 'none';
});
});
const $highlightExpands = getAll('.highlight .expand');
$highlightExpands.forEach($el => {
$el.addEventListener('click', () => {
$el.parentNode.firstElementChild.style.maxHeight = 'none';
});
});
}
new Clipboard('.copy', {
target: function(trigger) {
return trigger.previousSibling;
}
});
// Functions
function getAll(selector) {
return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
}
2017-07-29 13:59:10 +00:00
let latestKnownScrollY = 0;
let ticking = false;
function scrollUpdate() {
ticking = false;
// do stuff
}
function onScroll() {
latestKnownScrollY = window.scrollY;
scrollRequestTick();
}
function scrollRequestTick() {
if(!ticking) {
requestAnimationFrame(scrollUpdate);
}
ticking = true;
}
window.addEventListener('scroll', onScroll, false);
2017-07-01 17:30:39 +00:00
});