zq-accessible-tabs.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. (function (window, document) {
  2. 'use strict';
  3. var instanceCount = 0;
  4. function toArray(value) {
  5. return Array.prototype.slice.call(value || []);
  6. }
  7. function directChildByClass(parent, className) {
  8. for (var index = 0; index < parent.children.length; index += 1) {
  9. if (parent.children[index].classList.contains(className)) {
  10. return parent.children[index];
  11. }
  12. }
  13. return null;
  14. }
  15. function activate(root, tabs, panels, activeIndex, focusTab) {
  16. if (!tabs.length || !panels.length) {
  17. return;
  18. }
  19. var nextIndex = Math.max(0, Math.min(activeIndex, tabs.length - 1));
  20. tabs.forEach(function (tab, index) {
  21. var active = index === nextIndex;
  22. var control = tab.querySelector('a') || tab;
  23. tab.classList.toggle('on', active);
  24. control.setAttribute('aria-selected', active ? 'true' : 'false');
  25. control.setAttribute('tabindex', active ? '0' : '-1');
  26. if (panels[index]) {
  27. panels[index].hidden = !active;
  28. panels[index].classList.toggle('is-active', active);
  29. panels[index].setAttribute('aria-hidden', active ? 'false' : 'true');
  30. }
  31. });
  32. root.setAttribute('data-active-index', String(nextIndex));
  33. if (focusTab) {
  34. (tabs[nextIndex].querySelector('a') || tabs[nextIndex]).focus();
  35. }
  36. }
  37. function bind(container) {
  38. if (container.getAttribute('data-zq-tabs-ready') === 'true') {
  39. return;
  40. }
  41. var header = directChildByClass(container, 'hd');
  42. var tabList = header && header.querySelector('ul');
  43. var panelContainer = directChildByClass(container, 'bd');
  44. if (!tabList || !panelContainer) {
  45. return;
  46. }
  47. var tabs = toArray(tabList.children).filter(function (item) {
  48. return item.tagName === 'LI';
  49. });
  50. var panels = toArray(panelContainer.children);
  51. if (!tabs.length || !panels.length) {
  52. return;
  53. }
  54. var idPrefix = 'zq-tabs-' + (++instanceCount);
  55. tabList.setAttribute('role', 'tablist');
  56. tabList.classList.add('zq-tabs-list');
  57. panelContainer.classList.add('zq-tabs-panels');
  58. tabs.forEach(function (tab, index) {
  59. var control = tab.querySelector('a') || tab;
  60. var tabId = idPrefix + '-tab-' + index;
  61. var panelId = idPrefix + '-panel-' + index;
  62. tab.setAttribute('role', 'presentation');
  63. control.setAttribute('role', 'tab');
  64. control.setAttribute('aria-controls', panelId);
  65. control.id = tabId;
  66. control.setAttribute('data-tab-index', String(index));
  67. if (index === tabs.length - 1) {
  68. control.setAttribute('tabindex', '0');
  69. }
  70. if (panels[index]) {
  71. panels[index].id = panelId;
  72. panels[index].setAttribute('role', 'tabpanel');
  73. panels[index].setAttribute('aria-labelledby', tabId);
  74. panels[index].classList.add('zq-tabs-panel');
  75. }
  76. function selectAndFocus() {
  77. activate(container, tabs, panels, index, true);
  78. }
  79. tab.addEventListener('mouseenter', function () {
  80. activate(container, tabs, panels, index, false);
  81. });
  82. control.addEventListener('focus', function () {
  83. activate(container, tabs, panels, index, false);
  84. });
  85. control.addEventListener('keydown', function (event) {
  86. var targetIndex = index;
  87. if (event.key === 'ArrowRight' || event.key === 'ArrowDown') {
  88. targetIndex = (index + 1) % tabs.length;
  89. } else if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') {
  90. targetIndex = (index - 1 + tabs.length) % tabs.length;
  91. } else if (event.key === 'Home') {
  92. targetIndex = 0;
  93. } else if (event.key === 'End') {
  94. targetIndex = tabs.length - 1;
  95. } else if (event.key === 'Enter' || event.key === ' ') {
  96. event.preventDefault();
  97. selectAndFocus();
  98. return;
  99. } else {
  100. return;
  101. }
  102. event.preventDefault();
  103. activate(container, tabs, panels, targetIndex, true);
  104. });
  105. control.addEventListener('click', function (event) {
  106. var href = control.getAttribute('href') || '';
  107. if (!href || href === '#' || href.indexOf('javascript:') === 0) {
  108. event.preventDefault();
  109. }
  110. activate(container, tabs, panels, index, false);
  111. });
  112. });
  113. var initialIndex = tabs.findIndex(function (tab) {
  114. return tab.classList.contains('on');
  115. });
  116. activate(container, tabs, panels, initialIndex < 0 ? 0 : initialIndex, false);
  117. container.setAttribute('data-zq-tabs-ready', 'true');
  118. }
  119. function init(root) {
  120. var scope = root || document;
  121. toArray(scope.querySelectorAll('.slideTxtBox')).forEach(bind);
  122. }
  123. window.ZQAccessibleTabs = {init: init};
  124. if (document.readyState === 'loading') {
  125. document.addEventListener('DOMContentLoaded', function () {
  126. init(document);
  127. });
  128. } else {
  129. init(document);
  130. }
  131. })(window, document);