cupper-hugo-theme/themes/infusion/static/js/dom-scripts.js

111 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-06-09 15:27:09 +02:00
/* expandable sections */
(function () {
function toggle (button, target) {
2017-07-01 12:48:01 +02:00
var expanded = button.getAttribute('aria-expanded') === 'true'
2017-06-09 15:27:09 +02:00
button.setAttribute('aria-expanded', !expanded)
target.hidden = !target.hidden
}
var expanders = document.querySelectorAll('[data-expands]')
Array.prototype.forEach.call(expanders, function (expander) {
var target = document.getElementById(expander.getAttribute('data-expands'))
expander.addEventListener('click', function () {
toggle(expander, target)
})
})
2017-07-01 12:48:01 +02:00
}());
2017-06-09 15:27:09 +02:00
2017-07-01 12:48:01 +02:00
/* menu button */
(function () {
var button = document.getElementById('menu-button')
2017-07-30 17:47:07 +02:00
if (button) {
var menu = document.getElementById('patterns-list')
button.addEventListener('click', function() {
var expanded = this.getAttribute('aria-expanded') === 'true'
this.setAttribute('aria-expanded', !expanded)
})
}
2017-07-01 12:48:01 +02:00
}());
2017-06-30 18:14:15 +02:00
/* persist navigation scroll point */
2017-07-01 12:48:01 +02:00
(function () {
2017-07-31 22:07:38 +02:00
window.onunload = function () {
var patternsNav = document.getElementById('patterns-nav')
if (patternsNav) {
var scrollPoint = patternsNav.scrollTop
2017-07-30 17:47:07 +02:00
localStorage.setItem('scrollPoint', scrollPoint)
}
2017-07-21 09:53:42 +02:00
}
2017-06-30 18:14:15 +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) {
document.getElementById('patterns-nav').scrollTop = parseInt(localStorage.getItem('scrollPoint'))
} else {
document.getElementById('patterns-nav').scrollTop = 0
}
}
2017-07-01 12:48:01 +02:00
})
}());
2017-07-27 13:00:25 +02:00
/* Add "link here" links to <h2> headings */
(function () {
var headings = document.querySelectorAll('main > h2')
Array.prototype.forEach.call(headings, function (heading) {
var id = heading.getAttribute('id')
if (id) {
var newHeading = heading.cloneNode(true)
var container = document.createElement('div')
container.setAttribute('class', 'h2-container')
container.appendChild(newHeading)
heading.parentNode.insertBefore(container, heading)
var link = document.createElement('a')
link.setAttribute('href', '#' + id)
link.setAttribute('aria-label', 'link for this section')
2017-08-03 13:16:24 +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
container.appendChild(link)
heading.parentNode.removeChild(heading);
}
})
}());
2017-07-28 15:42:01 +02:00
// SVG scaling fix for IE10-11
(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-07-28 15:42:01 +02:00
var svgs = document.querySelectorAll('a svg, button svg, h1 svg')
// ... iterate over SVGs
Array.prototype.forEach.call(svgs, function(svg) {
// Set preserveAspectRatio to 'XMidYMin slice'
svg.setAttribute('preserveAspectRatio', 'xMidYMin slice')
// Turn the viewBox values into an array
var viewBox = svg.getAttribute('viewBox').split(' ')
// Calculate padding value needed (width/height x 100)
var padding = (viewBox[2] / viewBox[3]) * 100
// Set inline styles
svg.setAttribute('style', 'width: 100%; padding-bottom: ' + padding + '%; height: 1px; overflow: visible')
// Create span wrapper
var span = document.createElement('span')
span.setAttribute('class', 'svg-outer')
span.style.width = '0.75em'
svg.parentNode.insertBefore(span, svg)
span.appendChild(svg)
})
}
}());