bulma/docs/_javascript/main.js

276 lines
7.1 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
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 => {
$el.addEventListener('click', () => {
const target = $el.dataset.target;
const $target = document.getElementById(target);
2017-10-26 18:47:48 +00:00
rootEl.classList.add('is-clipped');
2017-07-01 17:30:39 +00:00
$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-10-26 18:47:48 +00:00
rootEl.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 => {
2017-10-09 11:27:08 +00:00
const copyEl = '<button class="button is-small bd-copy">Copy</button>';
const expandEl = '<button class="button is-small bd-expand">Expand</button>';
$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')) {
2017-10-09 11:27:08 +00:00
const showEl = '<button class="bd-show"><div><span class="icon"><i class="fa fa-code"></i></span> <strong>Show code</strong></div></button>';
$el.insertAdjacentHTML('beforeend', showEl);
2017-10-09 14:38:12 +00:00
} else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
$el.insertAdjacentHTML('beforeend', expandEl);
2017-07-02 15:52:20 +00:00
}
itemsProcessed++;
if (itemsProcessed === $highlights.length) {
addHighlightControls();
}
});
}
function addHighlightControls() {
2017-08-14 11:58:21 +00:00
const $highlightButtons = getAll('.highlight .bd-copy, .highlight .bd-expand');
2017-07-02 15:52:20 +00:00
$highlightButtons.forEach($el => {
$el.addEventListener('mouseenter', () => {
2017-08-14 17:25:14 +00:00
$el.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');
2017-07-02 15:52:20 +00:00
});
});
2017-08-14 11:58:21 +00:00
const $highlightExpands = getAll('.highlight .bd-expand');
2017-07-02 15:52:20 +00:00
$highlightExpands.forEach($el => {
$el.addEventListener('click', () => {
$el.parentNode.firstElementChild.style.maxHeight = 'none';
});
});
2017-10-09 11:27:08 +00:00
2017-10-10 09:22:28 +00:00
const $highlightShows = getAll('.highlight .bd-show');
2017-10-09 11:27:08 +00:00
$highlightShows.forEach($el => {
$el.addEventListener('click', () => {
2017-10-10 09:22:28 +00:00
$el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
2017-10-09 11:27:08 +00:00
});
});
2017-07-02 15:52:20 +00:00
}
2017-08-14 11:58:21 +00:00
new Clipboard('.bd-copy', {
2017-07-02 15:52:20 +00:00
target: function(trigger) {
return trigger.previousSibling;
}
});
// Functions
function getAll(selector) {
return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
}
2017-10-26 18:47:48 +00:00
// Scrolling
const navbarEl = document.getElementById('navbar');
const navbarBurger = document.getElementById('navbarBurger');
const specialShadow = document.getElementById('specialShadow');
2017-11-07 14:03:21 +00:00
const NAVBAR_HEIGHT = 52;
const THRESHOLD = 160;
2017-10-26 18:47:48 +00:00
let navbarOpen = false;
2017-11-07 14:03:21 +00:00
let horizon = NAVBAR_HEIGHT;
2017-10-26 18:47:48 +00:00
let whereYouStoppedScrolling = 0;
let scrollFactor = 0;
2017-11-07 14:03:21 +00:00
let currentTranslate = 0;
2017-10-26 18:47:48 +00:00
navbarBurger.addEventListener('click', el => {
navbarOpen = !navbarOpen;
if (navbarOpen) {
rootEl.classList.add('bd-is-clipped-touch');
} else {
rootEl.classList.remove('bd-is-clipped-touch');
}
});
2017-07-29 13:59:10 +00:00
2017-10-26 18:47:48 +00:00
function upOrDown(lastY, currentY) {
if (currentY >= lastY) {
return goingDown(currentY);
}
return goingUp(currentY);
2017-07-29 13:59:10 +00:00
}
2017-10-26 18:47:48 +00:00
function goingDown(currentY) {
2017-11-07 14:03:21 +00:00
const trigger = NAVBAR_HEIGHT;
2017-10-26 18:47:48 +00:00
whereYouStoppedScrolling = currentY;
if (currentY > horizon) {
horizon = currentY;
}
2017-07-29 13:59:10 +00:00
2017-11-07 14:03:21 +00:00
translateHeader(currentY, false);
2017-07-29 13:59:10 +00:00
}
2017-10-26 18:47:48 +00:00
function goingUp(currentY) {
const trigger = 0;
2017-11-07 14:03:21 +00:00
if (currentY < (whereYouStoppedScrolling - NAVBAR_HEIGHT)) {
horizon = currentY + NAVBAR_HEIGHT;
2017-07-29 13:59:10 +00:00
}
2017-10-26 18:47:48 +00:00
2017-11-07 14:03:21 +00:00
translateHeader(currentY, true);
2017-10-26 18:47:48 +00:00
}
function constrainDelta(delta) {
2017-11-07 14:03:21 +00:00
return Math.max(0, Math.min(delta, NAVBAR_HEIGHT));
2017-07-29 13:59:10 +00:00
}
2017-11-07 14:03:21 +00:00
function translateHeader(currentY, upwards) {
// let topTranslateValue;
let translateValue;
2017-10-26 18:47:48 +00:00
2017-11-07 14:03:21 +00:00
if (upwards && currentTranslate == 0) {
translateValue = 0;
} else if (currentY <= NAVBAR_HEIGHT) {
translateValue = currentY * -1;
} else {
const delta = constrainDelta(Math.abs(currentY - horizon));
translateValue = delta - NAVBAR_HEIGHT;
}
if (translateValue != currentTranslate) {
const navbarStyle = `
transform: translateY(${translateValue}px);
`;
currentTranslate = translateValue;
navbarEl.setAttribute('style', navbarStyle);
}
if (currentY > THRESHOLD * 2) {
2017-10-26 18:47:48 +00:00
scrollFactor = 1;
2017-11-07 14:03:21 +00:00
} else if (currentY > THRESHOLD) {
scrollFactor = (currentY - THRESHOLD) / THRESHOLD;
2017-10-26 18:47:48 +00:00
} else {
2017-10-26 19:00:25 +00:00
scrollFactor = 0;
2017-10-26 18:47:48 +00:00
}
2017-11-07 14:03:21 +00:00
const translateFactor = 1 + translateValue / NAVBAR_HEIGHT;
2017-10-26 18:47:48 +00:00
specialShadow.style.opacity = scrollFactor;
specialShadow.style.transform = 'scaleY(' + translateFactor + ')';
}
2017-11-07 14:03:21 +00:00
translateHeader(window.scrollY, false);
2017-10-26 18:47:48 +00:00
let ticking = false;
let lastY = 0;
2017-11-07 14:03:21 +00:00
2017-10-26 18:47:48 +00:00
window.addEventListener('scroll', function() {
const currentY = window.scrollY;
if (!ticking) {
window.requestAnimationFrame(function() {
upOrDown(lastY, currentY);
ticking = false;
lastY = currentY;
});
}
ticking = true;
});
2017-07-29 13:59:10 +00:00
2017-07-01 17:30:39 +00:00
});