mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-14 15:56:38 +03:00
f1c0468f70
### ARIA attributes - Add `aria-hidden="true"` to all decorative SVG icons (`icon.js`) - Implement missing `aria-label` for ad controls (`skip_ad_button.js`, `ad_info.js`) - Add `aria-pressed` to toggle buttons (mute, fullscreen, pip, loop, statistics, remote, stereoscopic) - Remove incorrect `aria-pressed` from one-shot action button (`recenter_vr.js`) - Add `role="menu"`, `aria-haspopup`, `aria-expanded` to menus (`overflow_menu.js`, `settings_menu.js`, `context_menu.js`) - Add `role="menuitemradio"` and `aria-checked` to selection menus (resolution, language, text, playback rate, etc.) - Add `role="toolbar"` to control panel (`controls.js`) - Add `role="heading"` to content title (`content_title.js`) ### Focus management - Restore focus to trigger button when menus close - Maintain focus on fullscreen button after toggle ### CSS - Add `forced-colors` media query for Windows high contrast mode ### Other - Add `alt=""` to seek bar thumbnail image - Add `aria-hidden="true"` to watermark canvas - Add accessibility unit tests for ARIA attributes - Remove redundant `aria-hidden` from checkmark icon (`ui_utils.js`) - Add ARIA terms to project spell-check dictionary Issue https://github.com/shaka-project/shaka-player/issues/3146
246 lines
6.6 KiB
JavaScript
246 lines
6.6 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
|
|
goog.provide('shaka.ui.TextSelection');
|
|
|
|
goog.require('shaka.ui.Controls');
|
|
goog.require('shaka.ui.Enums');
|
|
goog.require('shaka.ui.LanguageUtils');
|
|
goog.require('shaka.ui.Locales');
|
|
goog.require('shaka.ui.Localization');
|
|
goog.require('shaka.ui.OverflowMenu');
|
|
goog.require('shaka.ui.SettingsMenu');
|
|
goog.require('shaka.ui.Utils');
|
|
goog.require('shaka.util.Dom');
|
|
goog.require('shaka.util.FakeEvent');
|
|
goog.requireType('shaka.ui.Controls');
|
|
|
|
|
|
/**
|
|
* @extends {shaka.ui.SettingsMenu}
|
|
* @final
|
|
* @export
|
|
*/
|
|
shaka.ui.TextSelection = class extends shaka.ui.SettingsMenu {
|
|
/**
|
|
* @param {!HTMLElement} parent
|
|
* @param {!shaka.ui.Controls} controls
|
|
*/
|
|
constructor(parent, controls) {
|
|
super(parent,
|
|
controls, shaka.ui.Enums.MaterialDesignSVGIcons['CLOSED_CAPTIONS']);
|
|
|
|
this.button.classList.add('shaka-caption-button');
|
|
this.button.classList.add('shaka-tooltip-status');
|
|
this.menu.classList.add('shaka-text-languages');
|
|
|
|
let hasTrack = false;
|
|
if (this.player) {
|
|
const tracks = this.player.getTextTracks() || [];
|
|
hasTrack = tracks.some((track) => track.active);
|
|
}
|
|
|
|
if (hasTrack) {
|
|
this.button.ariaPressed = 'true';
|
|
} else {
|
|
this.button.ariaPressed = 'false';
|
|
}
|
|
|
|
this.addOffOption_();
|
|
|
|
this.eventManager.listenMulti(
|
|
this.localization,
|
|
[
|
|
shaka.ui.Localization.LOCALE_UPDATED,
|
|
shaka.ui.Localization.LOCALE_CHANGED,
|
|
], () => {
|
|
this.updateLocalizedStrings_();
|
|
// If captions/subtitles are off, this string needs localization.
|
|
// TODO: is there a more efficient way of updating just the strings
|
|
// we need instead of running the whole language update?
|
|
this.updateTextLanguages_();
|
|
});
|
|
|
|
this.eventManager.listenMulti(
|
|
this.player,
|
|
[
|
|
'loading',
|
|
'loaded',
|
|
'unloading',
|
|
'textchanged',
|
|
], () => {
|
|
this.onCaptionStateChange_();
|
|
this.updateTextLanguages_();
|
|
});
|
|
|
|
this.eventManager.listen(this.player, 'trackschanged', () => {
|
|
this.updateTextLanguages_();
|
|
});
|
|
|
|
if (this.isSubMenu) {
|
|
this.eventManager.listenMulti(
|
|
this.controls,
|
|
[
|
|
'submenuopen',
|
|
'submenuclose',
|
|
], () => {
|
|
this.updateTextLanguages_();
|
|
});
|
|
}
|
|
|
|
// Initialize caption state with a fake event.
|
|
this.onCaptionStateChange_();
|
|
|
|
// Set up all the strings in the user's preferred language.
|
|
this.updateLocalizedStrings_();
|
|
|
|
this.updateTextLanguages_();
|
|
}
|
|
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
addOffOption_() {
|
|
const off = shaka.util.Dom.createButton();
|
|
// ARIA: single-select menu item
|
|
off.setAttribute('role', 'menuitemradio');
|
|
off.setAttribute('aria-checked', 'true');
|
|
this.menu.appendChild(off);
|
|
|
|
off.appendChild(shaka.ui.Utils.checkmarkIcon());
|
|
|
|
/** @private {!HTMLElement} */
|
|
this.captionsOffSpan_ = shaka.util.Dom.createHTMLElement('span');
|
|
|
|
off.appendChild(this.captionsOffSpan_);
|
|
}
|
|
|
|
|
|
/** @private */
|
|
onCaptionStateChange_() {
|
|
const tracks = this.player.getTextTracks() || [];
|
|
const hasTrack = tracks.some((track) => track.active);
|
|
|
|
if (hasTrack) {
|
|
this.icon.use(shaka.ui.Enums.MaterialDesignSVGIcons['CLOSED_CAPTIONS']);
|
|
this.button.ariaPressed = 'true';
|
|
} else {
|
|
this.icon.use(
|
|
shaka.ui.Enums.MaterialDesignSVGIcons['CLOSED_CAPTIONS_OFF']);
|
|
this.button.ariaPressed = 'false';
|
|
}
|
|
|
|
this.controls.dispatchEvent(
|
|
new shaka.util.FakeEvent('captionselectionupdated'));
|
|
}
|
|
|
|
/** @private */
|
|
updateTextLanguages_() {
|
|
const tracks = this.player.getTextTracks() || [];
|
|
const hasTrack = tracks.some((track) => track.active);
|
|
|
|
shaka.ui.LanguageUtils.updateTextTracks(tracks, this.menu,
|
|
(track) => this.onTextTrackSelected_(track),
|
|
|
|
// Don't mark current text language as chosen unless captions are
|
|
// enabled
|
|
hasTrack,
|
|
this.currentSelection,
|
|
this.localization,
|
|
this.controls.getConfig());
|
|
|
|
// Add the Off button
|
|
const offButton = shaka.util.Dom.createButton();
|
|
offButton.classList.add('shaka-turn-captions-off-button');
|
|
this.eventManager.listen(offButton, 'click', () => {
|
|
this.player.selectTextTrack();
|
|
});
|
|
|
|
offButton.appendChild(this.captionsOffSpan_);
|
|
|
|
this.menu.appendChild(offButton);
|
|
|
|
if (!hasTrack) {
|
|
// ARIA: single-select menu item
|
|
offButton.setAttribute('role', 'menuitemradio');
|
|
offButton.setAttribute('aria-checked', 'true');
|
|
offButton.appendChild(shaka.ui.Utils.checkmarkIcon());
|
|
this.captionsOffSpan_.classList.add('shaka-chosen-item');
|
|
this.currentSelection.textContent =
|
|
this.localization.resolve(shaka.ui.Locales.Ids.OFF);
|
|
} else {
|
|
this.captionsOffSpan_.classList.remove('shaka-chosen-item');
|
|
}
|
|
|
|
this.button.setAttribute('shaka-status', this.currentSelection.textContent);
|
|
|
|
shaka.ui.Utils.focusOnTheChosenItem(this.menu);
|
|
|
|
this.controls.dispatchEvent(
|
|
new shaka.util.FakeEvent('captionselectionupdated'));
|
|
|
|
shaka.ui.Utils.setDisplay(
|
|
this.button, tracks.length > 0 && !this.isSubMenuOpened);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param {!shaka.extern.TextTrack} track
|
|
* @private
|
|
*/
|
|
onTextTrackSelected_(track) {
|
|
this.player.selectTextTrack(track);
|
|
|
|
// Set text preference for when reloading the stream (e.g. casting), keep
|
|
// this selection.
|
|
this.player.configure({
|
|
preferredText: [{
|
|
language: track.language,
|
|
role: '',
|
|
format: '',
|
|
forced: track.forced || false,
|
|
}],
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
updateLocalizedStrings_() {
|
|
const LocIds = shaka.ui.Locales.Ids;
|
|
|
|
this.button.ariaLabel = this.localization.resolve(LocIds.CAPTIONS);
|
|
this.backButton.ariaLabel = this.localization.resolve(LocIds.BACK);
|
|
this.nameSpan.textContent =
|
|
this.localization.resolve(LocIds.CAPTIONS);
|
|
this.backSpan.textContent =
|
|
this.localization.resolve(LocIds.CAPTIONS);
|
|
this.captionsOffSpan_.textContent =
|
|
this.localization.resolve(LocIds.OFF);
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* @implements {shaka.extern.IUIElement.Factory}
|
|
* @final
|
|
*/
|
|
shaka.ui.TextSelection.Factory = class {
|
|
/** @override */
|
|
create(rootElement, controls) {
|
|
return new shaka.ui.TextSelection(rootElement, controls);
|
|
}
|
|
};
|
|
|
|
shaka.ui.OverflowMenu.registerElement(
|
|
'captions', new shaka.ui.TextSelection.Factory());
|
|
|
|
shaka.ui.Controls.registerElement(
|
|
'captions', new shaka.ui.TextSelection.Factory());
|