X-Git-Url: https://git.arvados.org/rnaseq-cwl-training.git/blobdiff_plain/0cc762dafead8f398e8d0b67bd29774a8f988ae0..a2027125b36c8ffe0fef473c0e0fd99696e30206:/assets/js/tabs.js diff --git a/assets/js/tabs.js b/assets/js/tabs.js index 0148f32..0e5b795 100644 --- a/assets/js/tabs.js +++ b/assets/js/tabs.js @@ -1,43 +1,77 @@ -const removeActiveClasses = function (ulElement) { - const lis = ulElement.querySelectorAll('li'); - Array.prototype.forEach.call(lis, function(li) { - li.classList.remove('active'); - }); - } +window.addEventListener('load', function() { + // Get relevant elements and collections + const tabbed = document.querySelector('.tabbed'); + const tablist = tabbed.querySelector('ul'); + const tabs = tablist.querySelectorAll('a'); + const panels = tabbed.querySelectorAll('[id^="section"]'); - const getChildPosition = function (element) { - var parent = element.parentNode; - var i = 0; - for (var i = 0; i < parent.children.length; i++) { - if (parent.children[i] === element) { - return i; - } - } + // The tab switching function + const switchTab = (oldTab, newTab) => { + newTab.focus(); + // Make the active tab focusable by the user (Tab key) + newTab.removeAttribute('tabindex'); + // Set the selected state + newTab.setAttribute('aria-selected', 'true'); + newTab.classList.add('active'); + oldTab.removeAttribute('aria-selected'); + oldTab.setAttribute('tabindex', '-1'); + oldTab.classList.remove('active'); + // Get the indices of the new and old tabs to find the correct + // tab panels to show and hide + let index = Array.prototype.indexOf.call(tabs, newTab); + let oldIndex = Array.prototype.indexOf.call(tabs, oldTab); + panels[oldIndex].hidden = true; + panels[index].hidden = false; + } - throw new Error('No parent found'); - } + // Add the tablist role to the first