dom-scripts.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.onbeforeunload = 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', '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, .tags svg, th 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%; height: 1px; 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. }());
  91. /* Enable scrolling by keyboard of code samples */
  92. (function () {
  93. var codeBlocks = document.querySelectorAll('pre');
  94. Array.prototype.forEach.call(codeBlocks, function (block) {
  95. if (block.querySelector('code')) {
  96. block.setAttribute('role', 'region');
  97. block.setAttribute('aria-label', 'code sample');
  98. if (block.scrollWidth > block.clientWidth) {
  99. block.setAttribute('tabindex', '0');
  100. }
  101. }
  102. });
  103. }());
  104. /* Switch and persist theme */
  105. (function () {
  106. var checkbox = document.getElementById('themer');
  107. var inverter = document.getElementById('inverter');
  108. function darkTheme(media) {
  109. if (!('filter' in document.body.style)) {
  110. checkbox.parentNode.hidden = true;
  111. return;
  112. }
  113. inverter.setAttribute('media', media);
  114. inverter.textContent = inverter.textContent.trim();
  115. localStorage.setItem('darkTheme', media);
  116. }
  117. checkbox.addEventListener('change', function () {
  118. darkTheme(this.checked ? 'screen' : 'none');
  119. });
  120. window.addEventListener('DOMContentLoaded', function () {
  121. if ('filter' in document.body.style) {
  122. if (localStorage.getItem('darkTheme') === 'screen') {
  123. checkbox.click();
  124. }
  125. }
  126. });
  127. }());