|
10 | 10 | shimScriptAccess: 'always', |
11 | 11 | features: ['playpause', 'current', 'progress', 'duration', 'tracks', 'volume', 'a11y', 'fullscreen'], |
12 | 12 | alwaysShowControls: true, |
13 | | - toggleCaptionsButtonWhenOnlyOne: true, |
| 13 | + toggleCaptionsButtonWhenOnlyOne: true, // Note: >2 captions? Refactor accessibleCaptionButton() |
14 | 14 | success: function(mediaElement, originalNode, instance) { |
15 | | - |
| 15 | + |
| 16 | + // See <git>/_includes/_components/mediaelement.html |
| 17 | + var translations = { |
| 18 | + btnEnableCaption: originalNode.dataset.translationEnableCaption, |
| 19 | + btnDisableCaption: originalNode.dataset.translationDisableCaption, |
| 20 | + btnEnableAudioDescription: originalNode.dataset.translationEnableAudioDescription, |
| 21 | + btnDisableAudioDescription: originalNode.dataset.translationDisableAudioDescription |
| 22 | + }; |
| 23 | + |
| 24 | + // Subtitle toggle |
| 25 | + // WCAG 4.1.2: label button correctly |
| 26 | + // WCAG 2.1.4: toggle by space bar without pausing video |
| 27 | + accessibleCaptionButton(); |
| 28 | + instance.controls.querySelector('.mejs__captions-button button') |
| 29 | + .addEventListener('click', function() { setTimeout(accessibleCaptionButton, 50); }); |
| 30 | + instance.controls.querySelector('.mejs__captions-button button') |
| 31 | + .addEventListener('keydown', function($e) { |
| 32 | + if ($e.code === 'Space') { $e.stopPropagation(); } // Automatically generates click |
| 33 | + }); |
| 34 | + |
| 35 | + function accessibleCaptionButton() { |
| 36 | + var btn = instance.controls.querySelector('.mejs__captions-button button'); |
| 37 | + var enabled = btn.parentElement.classList.contains('mejs__captions-enabled'); |
| 38 | + var btnText = enabled ? translations.btnDisableCaption : translations.btnEnableCaption; |
| 39 | + btn.setAttribute('aria-pressed', enabled); |
| 40 | + btn.setAttribute('title', btnText); |
| 41 | + btn.setAttribute('aria-label', btnText); |
| 42 | + btn.textContent = btnText; |
| 43 | + } |
| 44 | + |
| 45 | + // Audio (mute) toggle |
| 46 | + // WCAG 2.1.4: toggle by space bar without pausing video |
| 47 | + instance.controls.querySelector('.mejs__volume-button button') |
| 48 | + .addEventListener('keydown', function($e) { |
| 49 | + if ($e.code === 'Space') { $e.stopPropagation(); } // Automatically generates click |
| 50 | + }); |
| 51 | + |
| 52 | + // Audio description toggle |
| 53 | + // WCAG 4.1.2: label button correctly |
| 54 | + // WCAG 2.1.4: toggle by space bar without pausing video |
| 55 | + accessibleAudioDescriptionButton(); |
| 56 | + instance.controls.querySelector('.mejs__audio-description-button button') |
| 57 | + .addEventListener('click', function() { setTimeout(accessibleAudioDescriptionButton, 50); }); |
| 58 | + instance.controls.querySelector('.mejs__audio-description-button button') |
| 59 | + .addEventListener('keydown', function($e) { |
| 60 | + if ($e.code === 'Space') { $e.stopPropagation(); } // Automatically generates click |
| 61 | + }); |
| 62 | + |
| 63 | + function accessibleAudioDescriptionButton() { |
| 64 | + var btn = instance.controls.querySelector('.mejs__audio-description-button button'); |
| 65 | + var enabled = btn.parentElement.classList.contains('audio-description-on'); |
| 66 | + var btnText = enabled ? translations.btnDisableAudioDescription : translations.btnEnableAudioDescription; |
| 67 | + btn.setAttribute('aria-pressed', enabled); |
| 68 | + btn.setAttribute('title', btnText); |
| 69 | + btn.setAttribute('aria-label', btnText); |
| 70 | + btn.textContent = btnText; |
| 71 | + } |
| 72 | + |
| 73 | + // Audio (mute) toggle |
| 74 | + // WCAG 2.1.4: toggle by space bar without pausing video |
| 75 | + instance.controls.querySelector('.mejs__fullscreen-button button') |
| 76 | + .addEventListener('keydown', function($e) { |
| 77 | + if ($e.code === 'Space') { $e.stopPropagation(); } // Automatically generates click |
| 78 | + }); |
16 | 79 | } |
17 | 80 | }); |
18 | 81 | } |
|
0 commit comments