|
1 | | -(function() { |
2 | | - var jQuery = window.jQuery || function() {}; |
3 | | - |
4 | | - jQuery(function($) { |
5 | | - $('.http-example.container').each(function() { |
6 | | - var $container = $(this), |
7 | | - $blocks = $(this).children(), |
8 | | - $captions = $(this).find('.caption'); |
9 | | - $captions.each(function() { |
10 | | - var $block = $(this).parent(); |
11 | | - $(this).on('click', function() { |
12 | | - $captions.removeClass('selected'); |
13 | | - $(this).addClass('selected'); |
14 | | - $blocks.hide(); |
15 | | - $block.show(); |
| 1 | +document.addEventListener("DOMContentLoaded", function() { |
| 2 | + // Select all elements with the class 'http-example container' |
| 3 | + var containers = document.querySelectorAll('.http-example.container'); |
| 4 | + containers.forEach(function(container) { |
| 5 | + |
| 6 | + // Get all child elements |
| 7 | + var blocks = Array.from(container.children); |
| 8 | + |
| 9 | + // Find caption elements or derive them from elements with the 'caption-text' class |
| 10 | + var captions = container.querySelectorAll('.caption'); |
| 11 | + if (captions.length === 0) { |
| 12 | + captions = container.querySelectorAll('.caption-text'); |
| 13 | + captions.forEach(function(caption) { |
| 14 | + caption.classList.add('caption'); |
| 15 | + }); |
| 16 | + } else { |
| 17 | + captions = Array.from(captions); |
| 18 | + } |
| 19 | + |
| 20 | + // Process each caption element |
| 21 | + captions.forEach(function(caption, index) { |
| 22 | + var orphan, block = !!caption.parentElement.id ? caption.parentElement : caption.parentElement.parentElement; |
| 23 | + block.setAttribute('role', 'tabpanel'); |
| 24 | + block.setAttribute('tabindex', '0'); |
| 25 | + block.setAttribute('aria-labelledby', block.id + '-label'); |
| 26 | + |
| 27 | + caption.id = block.id + '-label'; |
| 28 | + caption.setAttribute('role', 'tab'); |
| 29 | + caption.setAttribute('tabindex', '0'); |
| 30 | + caption.setAttribute('aria-label', caption.textContent.trim()); |
| 31 | + caption.setAttribute('aria-controls', block.id); |
| 32 | + |
| 33 | + // Click event for captions |
| 34 | + caption.addEventListener('click', function() { |
| 35 | + captions.forEach(function(otherCaption) { |
| 36 | + if (caption === otherCaption) { |
| 37 | + // Select the clicked caption |
| 38 | + caption.setAttribute('aria-selected', 'true'); |
| 39 | + caption.classList.add('selected'); |
| 40 | + otherCaption.setAttribute('tabindex', '0'); |
| 41 | + } else { |
| 42 | + // Deselect other captions |
| 43 | + otherCaption.setAttribute('aria-selected', 'false'); |
| 44 | + otherCaption.setAttribute('tabindex', '-1'); |
| 45 | + otherCaption.classList.remove('selected'); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + // Hide other blocks and show the selected one |
| 50 | + blocks.forEach(function(otherBlock) { |
| 51 | + if (otherBlock !== block) { |
| 52 | + otherBlock.style.display = 'none'; |
| 53 | + otherBlock.setAttribute('hidden', 'hidden'); |
| 54 | + } |
16 | 55 | }); |
17 | | - $container.append($(this)); |
| 56 | + block.style.display = ''; |
| 57 | + block.removeAttribute('hidden'); |
18 | 58 | }); |
19 | | - $container.append($blocks); |
20 | | - $captions.first().click(); |
| 59 | + |
| 60 | + // Keydown event for accessibility |
| 61 | + caption.addEventListener('keydown', function(event) { |
| 62 | + if (event.code === 'Space' || event.code === 'Enter') { |
| 63 | + caption.click(); |
| 64 | + } else if (event.code === 'ArrowRight') { |
| 65 | + // Move focus to the next tab |
| 66 | + var nextIndex = (index + 1) % captions.length; |
| 67 | + captions[nextIndex].focus(); |
| 68 | + captions[nextIndex].click(); |
| 69 | + } else if (event.code === 'ArrowLeft') { |
| 70 | + // Move focus to the previous tab |
| 71 | + var prevIndex = (index - 1 + captions.length) % captions.length; |
| 72 | + captions[prevIndex].focus(); |
| 73 | + captions[prevIndex].click(); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + if (caption.classList.contains("caption-text")) { |
| 78 | + orphan = caption.parentElement; |
| 79 | + container.appendChild(caption); |
| 80 | + orphan.remove(); |
| 81 | + } else { |
| 82 | + container.appendChild(caption); |
| 83 | + } |
21 | 84 | }); |
22 | | - }); |
23 | 85 |
|
24 | | -})(); |
| 86 | + // Set ARIA role for the container |
| 87 | + container.setAttribute('role', 'tablist'); |
25 | 88 |
|
| 89 | + // Append blocks back to the container |
| 90 | + blocks.forEach(function(block) { |
| 91 | + container.appendChild(block); |
| 92 | + }); |
| 93 | + |
| 94 | + // Automatically click the first caption to set the initial state |
| 95 | + if (captions.length > 0) { |
| 96 | + captions[0].click(); |
| 97 | + } |
| 98 | + }); |
| 99 | +}); |
0 commit comments