123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- '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 rootEl = 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);
- rootEl.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() {
- rootEl.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);
- 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);
- }
- 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';
- });
- });
- var $highlightShows = getAll('.highlight .bd-show');
- $highlightShows.forEach(function ($el) {
- $el.addEventListener('click', function () {
- $el.parentNode.parentNode.classList.remove('bd-is-more-clipped');
- });
- });
- }
- new Clipboard('.bd-copy', {
- target: function target(trigger) {
- return trigger.previousSibling;
- }
- });
- // Functions
- function getAll(selector) {
- return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
- }
- // Scrolling
- var navbarEl = document.getElementById('navbar');
- var navbarBurger = document.getElementById('navbarBurger');
- var specialShadow = document.getElementById('specialShadow');
- var NAVBAR_HEIGHT = 52;
- var THRESHOLD = 160;
- var navbarOpen = false;
- var horizon = NAVBAR_HEIGHT;
- var whereYouStoppedScrolling = 0;
- var scrollFactor = 0;
- var currentTranslate = 0;
- navbarBurger.addEventListener('click', function (el) {
- navbarOpen = !navbarOpen;
- if (navbarOpen) {
- rootEl.classList.add('bd-is-clipped-touch');
- } else {
- rootEl.classList.remove('bd-is-clipped-touch');
- }
- });
- function upOrDown(lastY, currentY) {
- if (currentY >= lastY) {
- return goingDown(currentY);
- }
- return goingUp(currentY);
- }
- function goingDown(currentY) {
- var trigger = NAVBAR_HEIGHT;
- whereYouStoppedScrolling = currentY;
- if (currentY > horizon) {
- horizon = currentY;
- }
- translateHeader(currentY, false);
- }
- function goingUp(currentY) {
- var trigger = 0;
- if (currentY < whereYouStoppedScrolling - NAVBAR_HEIGHT) {
- horizon = currentY + NAVBAR_HEIGHT;
- }
- translateHeader(currentY, true);
- }
- function constrainDelta(delta) {
- return Math.max(0, Math.min(delta, NAVBAR_HEIGHT));
- }
- function translateHeader(currentY, upwards) {
- // let topTranslateValue;
- var translateValue = void 0;
- if (upwards && currentTranslate == 0) {
- translateValue = 0;
- } else if (currentY <= NAVBAR_HEIGHT) {
- translateValue = currentY * -1;
- } else {
- var delta = constrainDelta(Math.abs(currentY - horizon));
- translateValue = delta - NAVBAR_HEIGHT;
- }
- if (translateValue != currentTranslate) {
- var navbarStyle = '\n transform: translateY(' + translateValue + 'px);\n ';
- currentTranslate = translateValue;
- navbarEl.setAttribute('style', navbarStyle);
- }
- if (currentY > THRESHOLD * 2) {
- scrollFactor = 1;
- } else if (currentY > THRESHOLD) {
- scrollFactor = (currentY - THRESHOLD) / THRESHOLD;
- } else {
- scrollFactor = 0;
- }
- var translateFactor = 1 + translateValue / NAVBAR_HEIGHT;
- specialShadow.style.opacity = scrollFactor;
- specialShadow.style.transform = 'scaleY(' + translateFactor + ')';
- }
- translateHeader(window.scrollY, false);
- var ticking = false;
- var lastY = 0;
- window.addEventListener('scroll', function () {
- var currentY = window.scrollY;
- if (!ticking) {
- window.requestAnimationFrame(function () {
- upOrDown(lastY, currentY);
- ticking = false;
- lastY = currentY;
- });
- }
- ticking = true;
- });
- });
|