cupper-hugo-theme/docs/js/dom-scripts.js

125 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-07-03 13:43:14 +02:00
/* expandable sections */
(function () {
function toggle (button, target) {
2017-08-12 10:14:05 +02:00
var expanded = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', !expanded);
target.hidden = !target.hidden;
2017-07-03 13:43:14 +02:00
}
2017-08-12 10:14:05 +02:00
var expanders = document.querySelectorAll('[data-expands]');
2017-07-03 13:43:14 +02:00
Array.prototype.forEach.call(expanders, function (expander) {
2017-08-12 10:14:05 +02:00
var target = document.getElementById(expander.getAttribute('data-expands'));
2017-07-03 13:43:14 +02:00
expander.addEventListener('click', function () {
2017-08-12 10:14:05 +02:00
toggle(expander, target);
2017-07-03 13:43:14 +02:00
})
})
}());
/* menu button */
(function () {
2017-08-12 10:14:05 +02:00
var button = document.getElementById('menu-button');
2017-07-30 17:47:07 +02:00
if (button) {
2017-08-12 10:14:05 +02:00
var menu = document.getElementById('patterns-list');
2017-07-30 17:47:07 +02:00
button.addEventListener('click', function() {
2017-08-12 10:14:05 +02:00
var expanded = this.getAttribute('aria-expanded') === 'true';
this.setAttribute('aria-expanded', !expanded);
2017-07-30 17:47:07 +02:00
})
}
2017-07-03 13:43:14 +02:00
}());
/* persist navigation scroll point */
(function () {
2017-08-12 10:14:05 +02:00
window.onbeforeunload = function () {
var patternsNav = document.getElementById('patterns-nav');
2017-07-31 22:07:38 +02:00
if (patternsNav) {
2017-08-12 10:14:05 +02:00
var scrollPoint = patternsNav.scrollTop;
localStorage.setItem('scrollPoint', scrollPoint);
2017-07-30 17:47:07 +02:00
}
2017-07-21 09:53:42 +02:00
}
2017-07-03 13:43:14 +02:00
2017-07-21 09:53:42 +02:00
window.addEventListener('DOMContentLoaded', function () {
2017-07-31 22:07:38 +02:00
if (document.getElementById('patterns-nav')) {
if (window.location.href.indexOf('patterns/') !== -1) {
2017-08-12 10:14:05 +02:00
document.getElementById('patterns-nav').scrollTop = parseInt(localStorage.getItem('scrollPoint'));
2017-07-31 22:07:38 +02:00
} else {
2017-08-12 10:14:05 +02:00
document.getElementById('patterns-nav').scrollTop = 0;
2017-07-31 22:07:38 +02:00
}
}
2017-07-03 13:43:14 +02:00
})
}());
2017-07-27 13:00:25 +02:00
/* Add "link here" links to <h2> headings */
(function () {
2017-08-12 10:14:05 +02:00
var headings = document.querySelectorAll('main > h2');
2017-07-27 13:00:25 +02:00
Array.prototype.forEach.call(headings, function (heading) {
2017-08-12 10:14:05 +02:00
var id = heading.getAttribute('id');
2017-07-27 13:00:25 +02:00
if (id) {
2017-08-12 10:14:05 +02:00
var newHeading = heading.cloneNode(true);
2017-07-27 13:00:25 +02:00
2017-08-12 10:14:05 +02:00
var container = document.createElement('div');
container.setAttribute('class', 'h2-container');
container.appendChild(newHeading);
2017-07-27 13:00:25 +02:00
2017-08-12 10:14:05 +02:00
heading.parentNode.insertBefore(container, heading);
2017-07-27 13:00:25 +02:00
2017-08-12 10:14:05 +02:00
var link = document.createElement('a');
link.setAttribute('href', '#' + id);
var headingText = heading.textContent;
2017-08-13 16:23:41 +02:00
link.setAttribute('aria-label', 'This ' + headingText + ' section');
2017-08-12 10:14:05 +02:00
link.innerHTML = '<svg aria-hidden="true" viewBox="0 0 50 50" focusable="false"> <use xlink:href="#link"></use> </svg>';
2017-07-27 13:00:25 +02:00
2017-08-12 10:14:05 +02:00
container.appendChild(link);
2017-07-27 13:00:25 +02:00
heading.parentNode.removeChild(heading);
}
})
}());
2017-07-28 15:42:01 +02:00
2017-08-12 10:14:05 +02:00
/* SVG scaling fix for IE10-11 */
2017-07-28 15:42:01 +02:00
(function () {
if (window.matchMedia('(-ms-high-contrast: none), (-ms-high-contrast: active)').matches) {
2017-08-03 14:22:01 +02:00
// Get all the SVGs on the page except the symbol defs
2017-08-12 10:14:05 +02:00
var svgs = document.querySelectorAll('a svg, button svg, h1 svg, .tags svg');
2017-07-28 15:42:01 +02:00
// ... iterate over SVGs
Array.prototype.forEach.call(svgs, function(svg) {
// Set preserveAspectRatio to 'XMidYMin slice'
2017-08-12 10:14:05 +02:00
svg.setAttribute('preserveAspectRatio', 'xMidYMin slice');
2017-07-28 15:42:01 +02:00
// Turn the viewBox values into an array
2017-08-12 10:14:05 +02:00
var viewBox = svg.getAttribute('viewBox').split(' ');
2017-07-28 15:42:01 +02:00
// Calculate padding value needed (width/height x 100)
2017-08-12 10:14:05 +02:00
var padding = (viewBox[2] / viewBox[3]) * 100;
2017-07-28 15:42:01 +02:00
// Set inline styles
2017-08-12 10:14:05 +02:00
svg.setAttribute('style', 'width: 100%; height: 1px; padding-bottom: ' + padding + '%; overflow: visible');
2017-07-28 15:42:01 +02:00
// Create span wrapper
2017-08-12 10:14:05 +02:00
var span = document.createElement('span');
span.setAttribute('class', 'svg-outer');
span.style.width = '0.75em';
2017-07-28 15:42:01 +02:00
2017-08-12 10:14:05 +02:00
svg.parentNode.insertBefore(span, svg);
span.appendChild(svg);
2017-07-28 15:42:01 +02:00
})
}
}());
2017-08-12 10:14:05 +02:00
2017-08-14 14:19:47 +02:00
/* Enable scrolling by keyboard of code samples */
(function () {
var codeBlocks = document.querySelectorAll('pre');
Array.prototype.forEach.call(codeBlocks, function (block) {
if (block.querySelector('code')) {
block.setAttribute('role', 'region');
block.setAttribute('tabindex', '0');
block.setAttribute('aria-label', 'code sample');
}
2017-08-12 10:14:05 +02:00
});
2017-08-14 14:19:47 +02:00
}());