bulma/docs/versions/0.6.0/lib/main.js
2017-10-10 11:49:56 +01:00

388 lines
9.7 KiB
JavaScript

'use strict';
document.addEventListener('DOMContentLoaded', function () {
// Dropdowns
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);
// window.history.replaceState(null, document.title, `${window.location.origin}${window.location.pathname}${target}`);
return false;
});
});
}
// Dropdowns
var $dropdowns = getAll('.dropdown:not(.is-hoverable)');
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) {
closeDropdowns();
});
}
function closeDropdowns() {
$dropdowns.forEach(function ($el) {
$el.classList.remove('is-active');
});
}
// Toggles
var $burgers = getAll('.burger');
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');
});
});
}
// Modals
var $html = document.documentElement;
var $modals = getAll('.modal');
var $modalButtons = getAll('.modal-button');
var $modalCloses = getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button');
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();
});
});
}
document.addEventListener('keydown', function (event) {
var e = event || window.event;
if (e.keyCode === 27) {
closeModals();
closeDropdowns();
}
});
function closeModals() {
$html.classList.remove('is-clipped');
$modals.forEach(function ($el) {
$el.classList.remove('is-active');
});
}
// Clipboard
var $highlights = getAll('.highlight');
var itemsProcessed = 0;
if ($highlights.length > 0) {
$highlights.forEach(function ($el) {
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);
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
=======
>>>>>>> Improve snippet
=======
>>>>>>> Fix rebase
=======
=======
>>>>>>> Add show code
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
=======
>>>>>>> Fix rebase
=======
>>>>>>> Build assets
var $parent = $el.parentNode;
if ($parent && $parent.classList.contains('bd-is-more')) {
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);
} else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
$el.insertAdjacentHTML('beforeend', expandEl);
<<<<<<< HEAD
<<<<<<< HEAD
=======
if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
$el.insertAdjacentHTML('beforeend', expandEl);
}
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
var $parent = $el.parentNode;
if ($parent && $parent.classList.contains('bd-is-more')) {
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);
<<<<<<< HEAD
>>>>>>> Add show code
=======
} else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
$el.insertAdjacentHTML('beforeend', expandEl);
>>>>>>> Improve snippet
=======
<<<<<<< HEAD
=======
>>>>>>> Improve snippet
=======
=======
>>>>>>> Improve snippet
>>>>>>> Improve snippet
var $parent = $el.parentNode;
if ($parent && $parent.classList.contains('bd-is-more')) {
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);
<<<<<<< HEAD
>>>>>>> Add show code
<<<<<<< HEAD
>>>>>>> Add show code
=======
=======
} else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
$el.insertAdjacentHTML('beforeend', expandEl);
>>>>>>> Improve snippet
>>>>>>> Improve snippet
=======
<<<<<<< HEAD
=======
=======
>>>>>>> Improve snippet
>>>>>>> Improve snippet
var $parent = $el.parentNode;
if ($parent && $parent.classList.contains('bd-is-more')) {
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);
<<<<<<< HEAD
>>>>>>> Add show code
<<<<<<< HEAD
>>>>>>> Add show code
<<<<<<< HEAD
>>>>>>> Add show code
=======
=======
=======
} else if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
$el.insertAdjacentHTML('beforeend', expandEl);
>>>>>>> Improve snippet
>>>>>>> Improve snippet
>>>>>>> Improve snippet
=======
>>>>>>> Fix rebase
>>>>>>> Fix rebase
=======
>>>>>>> Build assets
}
itemsProcessed++;
if (itemsProcessed === $highlights.length) {
addHighlightControls();
}
});
}
function addHighlightControls() {
var $highlightButtons = getAll('.highlight .bd-copy, .highlight .bd-expand');
$highlightButtons.forEach(function ($el) {
$el.addEventListener('mouseenter', function () {
$el.parentNode.classList.add('bd-is-hovering');
});
$el.addEventListener('mouseleave', function () {
$el.parentNode.classList.remove('bd-is-hovering');
});
});
var $highlightExpands = getAll('.highlight .bd-expand');
$highlightExpands.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.firstElementChild.style.maxHeight = 'none';
});
});
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> Improve snippet
=======
>>>>>>> Add show code
=======
=======
>>>>>>> Fix rebase
=======
>>>>>>> Improve snippet
>>>>>>> Improve snippet
=======
>>>>>>> Build assets
var $highlightShows = getAll('.highlight .bd-show');
$highlightShows.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
<<<<<<< HEAD
=======
var $highlightShows = getAll('.bd-highlight-clipped .bd-show');
$highlightShows.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.classList.remove('bd-is-clipped');
>>>>>>> Add show code
=======
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
=======
=======
>>>>>>> Add show code
>>>>>>> Add show code
=======
=======
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
<<<<<<< HEAD
>>>>>>> Improve snippet
=======
=======
>>>>>>> Fix rebase
>>>>>>> Fix rebase
var $highlightShows = getAll('.highlight .bd-show');
$highlightShows.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
>>>>>>> Improve snippet
=======
=======
var $highlightShows = getAll('.bd-highlight-clipped .bd-show');
$highlightShows.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.classList.remove('bd-is-clipped');
>>>>>>> Add show code
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
=======
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
=======
>>>>>>> Fix rebase
>>>>>>> Improve snippet
=======
=======
var $highlightShows = getAll('.bd-highlight-clipped .bd-show');
$highlightShows.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.classList.remove('bd-is-clipped');
>>>>>>> Add show code
>>>>>>> Add show code
=======
>>>>>>> Improve snippet
>>>>>>> Improve snippet
=======
>>>>>>> Fix rebase
=======
>>>>>>> Build assets
});
});
}
new Clipboard('.bd-copy', {
target: function target(trigger) {
return trigger.previousSibling;
}
});
// Functions
function getAll(selector) {
return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
}
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);
});