bulma/docs/lib/main.js

190 lines
5.0 KiB
JavaScript
Raw Normal View History

2017-07-01 17:30:39 +00:00
'use strict';
document.addEventListener('DOMContentLoaded', function () {
2017-07-20 19:01:55 +00:00
// Dropdowns
2017-07-29 12:02:00 +00:00
var $metalinks = getAll('#meta a');
if ($metalinks.length > 0) {
$metalinks.forEach(function ($el) {
$el.addEventListener('click', function (event) {
event.preventDefault();
var target = $el.getAttribute('href');
var $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
var $dropdowns = getAll('.dropdown:not(.is-hoverable)');
2017-07-20 19:01:55 +00:00
if ($dropdowns.length > 0) {
$dropdowns.forEach(function ($el) {
$el.addEventListener('click', function (event) {
event.stopPropagation();
$el.classList.toggle('is-active');
});
});
document.addEventListener('click', function (event) {
2017-07-21 07:30:56 +00:00
closeDropdowns();
});
}
function closeDropdowns() {
$dropdowns.forEach(function ($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
var $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(function ($el) {
$el.addEventListener('click', function () {
var target = $el.dataset.target;
var $target = document.getElementById(target);
$el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
2017-07-01 17:30:39 +00:00
});
}
// Modals
var $html = document.documentElement;
2017-07-02 15:52:20 +00:00
var $modals = getAll('.modal');
var $modalButtons = getAll('.modal-button');
var $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(function ($el) {
$el.addEventListener('click', function () {
var target = $el.dataset.target;
var $target = document.getElementById(target);
$html.classList.add('is-clipped');
$target.classList.add('is-active');
});
});
}
if ($modalCloses.length > 0) {
$modalCloses.forEach(function ($el) {
$el.addEventListener('click', function () {
closeModals();
});
});
}
2017-07-21 07:30:56 +00:00
document.addEventListener('keydown', function (event) {
var 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(function ($el) {
$el.classList.remove('is-active');
});
}
2017-07-02 15:52:20 +00:00
// Clipboard
var $highlights = getAll('.highlight');
var itemsProcessed = 0;
if ($highlights.length > 0) {
$highlights.forEach(function ($el) {
2017-10-09 11:27:08 +00:00
var copyEl = '<button class="button is-small bd-copy">Copy</button>';
var 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
var $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
var 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
var $highlightButtons = getAll('.highlight .bd-copy, .highlight .bd-expand');
2017-07-02 15:52:20 +00:00
$highlightButtons.forEach(function ($el) {
$el.addEventListener('mouseenter', function () {
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', function () {
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
var $highlightExpands = getAll('.highlight .bd-expand');
2017-07-02 15:52:20 +00:00
$highlightExpands.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.firstElementChild.style.maxHeight = 'none';
});
});
2017-10-09 11:27:08 +00:00
2017-10-09 14:38:12 +00:00
var $highlightShows = getAll('.highlight .bd-show');
2017-10-09 11:27:08 +00:00
$highlightShows.forEach(function ($el) {
$el.addEventListener('click', function () {
2017-10-09 14:38:12 +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 target(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
var latestKnownScrollY = 0;
var 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
});