dom-scripts.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* expandable sections */
  2. (function () {
  3. function toggle (button, target) {
  4. var expanded = button.getAttribute('aria-expanded') === 'true'
  5. button.setAttribute('aria-expanded', !expanded)
  6. target.hidden = !target.hidden
  7. }
  8. var expanders = document.querySelectorAll('[data-expands]')
  9. Array.prototype.forEach.call(expanders, function (expander) {
  10. var target = document.getElementById(expander.getAttribute('data-expands'))
  11. expander.addEventListener('click', function () {
  12. toggle(expander, target)
  13. })
  14. })
  15. }());
  16. /* menu button */
  17. (function () {
  18. var button = document.getElementById('menu-button')
  19. if (button) {
  20. var menu = document.getElementById('patterns-list')
  21. button.addEventListener('click', function() {
  22. var expanded = this.getAttribute('aria-expanded') === 'true'
  23. this.setAttribute('aria-expanded', !expanded)
  24. })
  25. }
  26. }());
  27. /* persist navigation scroll point */
  28. (function () {
  29. window.onunload = function () {
  30. var patternsNav = document.getElementById('patterns-nav')
  31. if (patternsNav) {
  32. var scrollPoint = patternsNav.scrollTop
  33. localStorage.setItem('scrollPoint', scrollPoint)
  34. }
  35. }
  36. window.addEventListener('DOMContentLoaded', function () {
  37. if (document.getElementById('patterns-nav')) {
  38. if (window.location.href.indexOf('patterns/') !== -1) {
  39. document.getElementById('patterns-nav').scrollTop = parseInt(localStorage.getItem('scrollPoint'))
  40. } else {
  41. document.getElementById('patterns-nav').scrollTop = 0
  42. }
  43. }
  44. })
  45. }());
  46. /* Add "link here" links to <h2> headings */
  47. (function () {
  48. var headings = document.querySelectorAll('main > h2')
  49. Array.prototype.forEach.call(headings, function (heading) {
  50. var id = heading.getAttribute('id')
  51. if (id) {
  52. var newHeading = heading.cloneNode(true)
  53. var container = document.createElement('div')
  54. container.setAttribute('class', 'h2-container')
  55. container.appendChild(newHeading)
  56. heading.parentNode.insertBefore(container, heading)
  57. var link = document.createElement('a')
  58. link.setAttribute('href', '#' + id)
  59. var headingText = heading.textContent
  60. link.setAttribute('aria-label', 'To this ' + headingText + ' section')
  61. link.innerHTML = '<svg aria-hidden="true" viewBox="0 0 50 50" focusable="false"> <use xlink:href="#link"></use> </svg>'
  62. container.appendChild(link)
  63. heading.parentNode.removeChild(heading);
  64. }
  65. })
  66. }());
  67. // SVG scaling fix for IE10-11
  68. (function () {
  69. if (window.matchMedia('(-ms-high-contrast: none), (-ms-high-contrast: active)').matches) {
  70. // Get all the SVGs on the page except the symbol defs
  71. var svgs = document.querySelectorAll('a svg, button svg, h1 svg')
  72. // ... iterate over SVGs
  73. Array.prototype.forEach.call(svgs, function(svg) {
  74. // Set preserveAspectRatio to 'XMidYMin slice'
  75. svg.setAttribute('preserveAspectRatio', 'xMidYMin slice')
  76. // Turn the viewBox values into an array
  77. var viewBox = svg.getAttribute('viewBox').split(' ')
  78. // Calculate padding value needed (width/height x 100)
  79. var padding = (viewBox[2] / viewBox[3]) * 100
  80. // Set inline styles
  81. svg.setAttribute('style', 'width: 100%; padding-bottom: ' + padding + '%; overflow: visible')
  82. // Create span wrapper
  83. var span = document.createElement('span')
  84. span.setAttribute('class', 'svg-outer')
  85. span.style.width = '0.75em'
  86. svg.parentNode.insertBefore(span, svg)
  87. span.appendChild(svg)
  88. })
  89. }
  90. }());