template-dom-scripts.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. {{ if not .Site.Params.hideHeaderLinks }}
  47. /* Add "link here" links to <h2> headings */
  48. (function () {
  49. var headings = document.querySelectorAll('main > h2');
  50. Array.prototype.forEach.call(headings, function (heading) {
  51. var id = heading.getAttribute('id');
  52. if (id) {
  53. var newHeading = heading.cloneNode(true);
  54. newHeading.setAttribute('tabindex', '-1');
  55. var container = document.createElement('div');
  56. container.setAttribute('class', 'h2-container');
  57. container.appendChild(newHeading);
  58. heading.parentNode.insertBefore(container, heading);
  59. var link = document.createElement('a');
  60. link.setAttribute('href', '#' + id);
  61. link.innerHTML = '<svg aria-hidden="true" class="link-icon" 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. {{ end }}
  68. /* Enable scrolling by keyboard of code samples */
  69. (function () {
  70. var codeBlocks = document.querySelectorAll('pre, .code-annotated');
  71. Array.prototype.forEach.call(codeBlocks, function (block) {
  72. if (block.querySelector('code')) {
  73. block.setAttribute('role', 'region');
  74. block.setAttribute('aria-label', 'code sample');
  75. if (block.scrollWidth > block.clientWidth) {
  76. block.setAttribute('tabindex', '0');
  77. }
  78. }
  79. });
  80. }());
  81. /* Switch and persist theme */
  82. (function () {
  83. var checkbox = document.getElementById('themer');
  84. function persistTheme(val) {
  85. localStorage.setItem('darkTheme', val);
  86. }
  87. function applyDarkTheme() {
  88. var rules = [
  89. '.intro-and-nav, .main-and-footer { filter: invert(100%); }',
  90. '* { background-color: inherit; }',
  91. 'img:not([src*=".svg"]), .colors, iframe, .demo-container { filter: invert(100%); }'
  92. ];
  93. rules.forEach(function(rule) {
  94. document.styleSheets[0].insertRule(rule);
  95. })
  96. }
  97. function clearDarkTheme() {
  98. for (let i = 0; i < document.styleSheets[0].cssRules.length; i++) {
  99. document.styleSheets[0].deleteRule(i);
  100. }
  101. }
  102. checkbox.addEventListener('change', function () {
  103. if (this.checked) {
  104. applyDarkTheme();
  105. persistTheme('true');
  106. } else {
  107. clearDarkTheme();
  108. persistTheme('false');
  109. }
  110. });
  111. function handleDarkThemeAsDefault() {
  112. {{ if .Site.Params.darkThemeAsDefault }}
  113. persistTheme('true');
  114. handleDarkThemeAsDefault = function() {};
  115. {{ end }}
  116. }
  117. function showTheme() {
  118. if (localStorage.getItem('darkTheme') === 'true') {
  119. applyDarkTheme();
  120. checkbox.checked = true;
  121. }
  122. }
  123. function showContent() {
  124. document.body.style.visibility = 'visible';
  125. document.body.style.opacity = 1;
  126. }
  127. window.addEventListener('DOMContentLoaded', function () {
  128. handleDarkThemeAsDefault();
  129. showTheme();
  130. showContent();
  131. });
  132. }());