template-dom-scripts.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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('h2, h3, h4, h5, h6');
  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 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 darkTheme = document.getElementById('darkTheme');
  89. darkTheme.disabled = false;
  90. }
  91. function clearDarkTheme() {
  92. var darkTheme = document.getElementById('darkTheme');
  93. darkTheme.disabled = true;
  94. }
  95. function defaultDarkTheme() {
  96. {{- with .Site.Params.defaultDarkTheme }}
  97. if (localStorage.getItem('darkTheme') == null) {
  98. persistTheme('true');
  99. checkbox.checked = true;
  100. }
  101. {{- else }}
  102. if (localStorage.getItem('darkTheme') == null) {
  103. persistTheme('false');
  104. checkbox.checked = false;
  105. }
  106. {{ end }}
  107. }
  108. checkbox.addEventListener('change', function () {
  109. defaultDarkTheme();
  110. if (this.checked) {
  111. applyDarkTheme();
  112. persistTheme('true');
  113. } else {
  114. clearDarkTheme();
  115. persistTheme('false');
  116. }
  117. });
  118. function showTheme() {
  119. if (localStorage.getItem('darkTheme') === 'true') {
  120. applyDarkTheme();
  121. checkbox.checked = true;
  122. } else {
  123. clearDarkTheme();
  124. checkbox.checked = false;
  125. }
  126. }
  127. function showContent() {
  128. document.body.style.visibility = 'visible';
  129. document.body.style.opacity = 1;
  130. }
  131. window.addEventListener('DOMContentLoaded', function () {
  132. defaultDarkTheme();
  133. showTheme();
  134. showContent();
  135. });
  136. }());