bulma/docs/_javascript/main.js

505 lines
13 KiB
JavaScript
Raw Normal View History

2017-07-01 17:30:39 +00:00
document.addEventListener('DOMContentLoaded', () => {
2018-03-26 14:33:31 +00:00
// Cookies
2018-03-28 08:10:39 +00:00
const cookieBookModalName = 'bulma_closed_book_modal';
const cookieBookModal = Cookies.getJSON(cookieBookModalName) || false;
2018-03-26 14:33:31 +00:00
2018-04-09 19:19:29 +00:00
// Sidebar links
const $categories = getAll('#categories .bd-category');
if ($categories.length > 0) {
$categories.forEach(el => {
const toggle_el = el.querySelector('.bd-category-toggle');
toggle_el.addEventListener('click', event => {
2018-05-03 00:41:52 +00:00
// closeCategories(el);
2018-04-09 19:19:29 +00:00
el.classList.toggle('is-active');
});
});
}
function closeCategories(current_el) {
$categories.forEach(el => {
if (current_el == el) {
return;
}
el.classList.remove('is-active');
});
}
2018-04-10 22:52:51 +00:00
const anchors_ref_el = document.getElementById('anchorsReference');
const anchors_el = document.getElementById('anchors');
const anchor_links_el = getAll('.bd-anchor-link');
2018-04-11 00:14:29 +00:00
let anchors_by_id = {};
let anchors_order = [];
let anchor_nav_els = [];
2018-04-10 22:52:51 +00:00
if (anchors_el && anchor_links_el.length > 0) {
2018-04-11 01:11:12 +00:00
anchors_el.classList.add('is-active');
2018-04-10 22:52:51 +00:00
const anchors_el_list = anchors_el.querySelector('.bd-anchors-list');
2018-04-11 00:14:29 +00:00
anchor_links_el.forEach((el, index) => {
2018-04-10 22:52:51 +00:00
const link_target = el.getAttribute('href');
const link_text = el.previousElementSibling.innerText;
if (link_text != '') {
const item_el = createAnchorLink(link_text, link_target);
anchors_el_list.appendChild(item_el);
2018-04-11 00:14:29 +00:00
const anchor_key = link_target.substring(1); // #target -> target
anchors_by_id[anchor_key] = {
id: anchor_key,
index,
target: link_target,
text: link_text,
nav_el: item_el,
};
anchors_order.push(anchor_key);
anchor_nav_els.push(item_el);
2018-04-10 22:52:51 +00:00
}
});
const back_to_top_el = createAnchorLink('Back to top', '');
back_to_top_el.onclick = scrollToTop;
anchors_el_list.appendChild(back_to_top_el);
}
function scrollToTop() {
window.scrollTo(0, 0);
}
function createAnchorLink(text, target) {
const item_el = document.createElement('li');
const link_el = document.createElement('a');
const text_node = document.createTextNode(text);
if (target) {
link_el.setAttribute('href', target);
}
link_el.appendChild(text_node);
item_el.appendChild(link_el);
return item_el;
}
function closeCategories(current_el) {
$categories.forEach(el => {
if (current_el == el) {
return;
}
el.classList.remove('is-active');
});
}
2018-03-26 14:33:31 +00:00
// Meta links
2017-07-20 19:01:55 +00:00
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);
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
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 => {
$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 => {
$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');
2017-07-01 17:30:39 +00:00
$modals.forEach($el => {
$el.classList.remove('is-active');
});
}
2018-04-08 18:54:36 +00:00
document.addEventListener('keydown', event => {
const e = event || window.event;
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 => {
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')) {
2018-01-10 16:30:23 +00:00
const showEl = '<button class="bd-show"><div><span class="icon"><i class="fas fa-code"></i></span> <strong>Show code</strong></div></button>';
2017-10-09 11:27:08 +00:00
$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
}
2018-02-13 11:15:12 +00:00
setTimeout(() => {
new Clipboard('.bd-copy', {
target: trigger => {
return trigger.previousElementSibling.firstElementChild;
}
});
}, 100);
2017-07-02 15:52:20 +00:00
// Functions
2020-05-10 23:25:29 +00:00
function getAll(selector, parent = document) {
return Array.prototype.slice.call(parent.querySelectorAll(selector), 0);
2017-07-02 15:52:20 +00:00
}
2017-10-26 18:47:48 +00:00
// Scrolling
2018-04-10 22:52:51 +00:00
const html_el = document.documentElement;
2017-10-26 18:47:48 +00:00
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;
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
2018-04-11 00:14:29 +00:00
// Anchors highlight
let past_anchors = [];
anchor_links_el.reverse();
const trigger_offset = 24 ; // In pixels
const typo_el = document.getElementById('typo');
function whenScrolling() {
2018-04-10 22:52:51 +00:00
if (anchors_ref_el) {
const bounds = anchors_ref_el.getBoundingClientRect();
2018-04-11 00:14:29 +00:00
const anchors_height = anchors_el.clientHeight;
const typo_bounds = typo_el.getBoundingClientRect();
const typo_height = typo_el.clientHeight;
2018-04-10 22:52:51 +00:00
2018-04-11 00:14:29 +00:00
if (bounds.top < 1 && typo_bounds.top - anchors_height + typo_height > 0) {
2018-04-10 22:52:51 +00:00
anchors_el.classList.add('is-pinned');
} else {
anchors_el.classList.remove('is-pinned');
}
2018-04-11 00:14:29 +00:00
anchor_links_el.some(el => {
const bounds = el.getBoundingClientRect();
const href = el.getAttribute('href');
const key = href.substring(1); // #target -> target
if (bounds.top < 1 + trigger_offset && past_anchors.indexOf(key) == -1) {
past_anchors.push(key);
highlightAnchor();
return;
} else if (bounds.top > 0 + trigger_offset && past_anchors.indexOf(key) != -1) {
removeFromArray(past_anchors, key);
highlightAnchor();
return;
}
});
}
}
function highlightAnchor() {
const future_anchors = anchors_order.diff(past_anchors);
let highest_index = -1;
let highest_anchor_key = '';
if (past_anchors.length > 0) {
past_anchors.forEach((key, index) => {
const anchor = anchors_by_id[key];
anchor.nav_el.className = 'is-past';
// Keep track of the bottom most item
if (anchor.index > highest_index) {
highest_index = anchor.index;
highest_anchor_key = key;
}
});
if (highest_anchor_key in anchors_by_id) {
anchors_by_id[highest_anchor_key].nav_el.className = 'is-current';
}
}
if (future_anchors.length > 0) {
future_anchors.forEach((key, index) => {
const anchor = anchors_by_id[key];
anchor.nav_el.className = '';
});
2018-04-10 22:52:51 +00:00
}
}
2020-05-10 23:25:29 +00:00
// Spacing table
2020-05-11 00:03:16 +00:00
const spacingTables = getAll('.bd-spacing-table');
2020-05-10 23:25:29 +00:00
2020-05-11 00:03:16 +00:00
spacingTables.forEach(spacingEl => {
const spacingRows = getAll('tbody tr', spacingEl);
const spacingCells = getAll('tbody td', spacingEl);
const spacingValues = getAll('tfoot th', spacingEl);
spacingEl.addEventListener('mouseleave', () => {
resetTable(spacingCells, spacingValues);
});
2020-05-10 23:25:29 +00:00
2020-05-11 00:03:16 +00:00
spacingCells.forEach(el => {
el.addEventListener('mouseenter', () => {
resetTable(spacingCells, spacingValues);
const row = Array.prototype.indexOf.call(el.parentNode.parentNode.children, el.parentNode);
const column = Array.prototype.indexOf.call(el.parentNode.children, el);
highlightRowAndColumn(row, column, spacingRows, spacingValues);
});
2020-05-10 23:25:29 +00:00
});
});
2020-05-11 00:03:16 +00:00
function resetTable(cells, values) {
cells.forEach(el => el.classList.remove('bd-current-row', 'bd-current-column'));
values.forEach(el => el.classList.remove('bd-current-value'));
2020-05-10 23:25:29 +00:00
}
2020-05-11 00:03:16 +00:00
function highlightRowAndColumn(rowIndex, columnIndex, rows, values) {
const row = rows[rowIndex];
2020-05-10 23:40:11 +00:00
let i = columnIndex;
2020-05-10 23:25:29 +00:00
2020-05-10 23:40:11 +00:00
while (i > -1) {
row.children[i].classList.add('bd-current-row');
i--;
}
2020-05-11 00:03:16 +00:00
const nextRows = rows.slice(rowIndex);
2020-05-10 23:40:11 +00:00
nextRows.forEach(r => {
2020-05-10 23:25:29 +00:00
r.children[columnIndex].classList.add('bd-current-column');
});
if (columnIndex < 2) {
return;
}
2020-05-11 00:03:16 +00:00
values[columnIndex - 1].classList.add('bd-current-value');
2020-05-10 23:25:29 +00:00
}
2018-04-11 00:14:29 +00:00
// Scroll
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;
2018-04-09 00:12:55 +00:00
if (specialShadow) {
specialShadow.style.opacity = scrollFactor;
specialShadow.style.transform = 'scaleY(' + translateFactor + ')';
}
2017-10-26 18:47:48 +00:00
}
2018-04-10 22:52:51 +00:00
let ticking = false;
let lastY = 0;
2017-10-26 18:47:48 +00:00
2018-04-10 22:52:51 +00:00
window.addEventListener('scroll', function() {
const currentY = window.scrollY;
2017-11-07 14:03:21 +00:00
2018-04-10 22:52:51 +00:00
if (!ticking) {
window.requestAnimationFrame(function() {
// upOrDown(lastY, currentY);
2018-04-11 00:14:29 +00:00
whenScrolling();
2018-04-10 22:52:51 +00:00
ticking = false;
lastY = currentY;
});
}
2017-10-26 18:47:48 +00:00
2018-04-10 22:52:51 +00:00
ticking = true;
});
2017-07-29 13:59:10 +00:00
2018-04-11 00:14:29 +00:00
// Utils
function removeFromArray(array, value) {
if (array.includes(value)) {
const value_index = array.indexOf(value);
array.splice(value_index, 1);
}
return array;
}
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
2017-07-01 17:30:39 +00:00
});