Skip to content

Commit

Permalink
Refactor the overflow menu to use components design.
Browse files Browse the repository at this point in the history
Closes #1673.

Change-Id: I030745def928796a6abc813a91fb163cb2d76175
  • Loading branch information
ismena committed Feb 21, 2019
1 parent d2069dc commit 4d41b7b
Show file tree
Hide file tree
Showing 13 changed files with 1,453 additions and 1,176 deletions.
6 changes: 6 additions & 0 deletions build/types/ui
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
# UI library.

+../../third_party/language-mapping-list/language-mapping-list.js
+../../ui/audio_language_selection.js
+../../ui/externs/ui.js
+../../ui/cast_button.js
+../../ui/controls.js
+../../ui/constants.js
+../../ui/enums.js
+../../ui/element.js
+../../ui/fast_forward_button.js
+../../ui/fullscreen_button.js
+../../ui/language_utils.js
+../../ui/localization.js
+../../ui/locales.js
+../../ui/mute_button.js
+../../ui/overflow_menu.js
+../../ui/pip_button.js
+../../ui/presentation_time.js
+../../ui/resolution_selection.js
+../../ui/rewind_button.js
+../../ui/spacer.js
+../../ui/text_displayer.js
+../../ui/text_selection.js
+../../ui/ui.js
+../../ui/ui_utils.js
+../../ui/volume_bar.js
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module.exports = function(config) {
{pattern: 'ui/**/*.css', included: false},
{pattern: 'ui/**/*.less', included: false},
{pattern: 'third_party/closure/goog/**/*.js', included: false},
{pattern: 'third_party/language-mapping-list/**/*.js', included: false},
{pattern: 'test/test/assets/*', included: false},
{pattern: 'dist/shaka-player.ui.js', included: false},
{pattern: 'node_modules/**/*.js', included: false},
Expand Down
12 changes: 9 additions & 3 deletions shaka-player.uncompiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,22 @@ goog.require('shaka.text.SimpleTextDisplayer');
goog.require('shaka.text.TextEngine');
goog.require('shaka.text.TtmlTextParser');
goog.require('shaka.text.VttTextParser');
goog.require('shaka.ui.Overlay');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.Controls');
goog.require('shaka.ui.OverflowMenu');
goog.require('shaka.ui.AudioLanguageSelection');
goog.require('shaka.ui.CastButton');
goog.require('shaka.ui.Element');
goog.require('shaka.ui.FastForwardButton');
goog.require('shaka.ui.FullscreenButton');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.MuteButton');
goog.require('shaka.ui.OverflowMenu');
goog.require('shaka.ui.Overlay');
goog.require('shaka.ui.PipButton');
goog.require('shaka.ui.PresentationTimeTracker');
goog.require('shaka.ui.ResolutionSelection');
goog.require('shaka.ui.RewindButton');
goog.require('shaka.ui.Spacer');
goog.require('shaka.ui.TextSelection');
goog.require('shaka.ui.VolumeBar');
goog.require('shaka.util.Error');
goog.require('shaka.util.Iterables');
209 changes: 209 additions & 0 deletions ui/audio_language_selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


goog.provide('shaka.ui.AudioLanguageSelection');

goog.require('shaka.ui.Element');
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');


/**
* @extends {shaka.ui.Element}
* @final
* @export
*/
shaka.ui.AudioLanguageSelection = class extends shaka.ui.Element {
/**
* @param {!HTMLElement} parent
* @param {!shaka.ui.Controls} controls
*/
constructor(parent, controls) {
super(parent, controls);

this.addLanguagesButton_();

this.addAudioLangMenu_();

this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
this.updateLocalizedStrings_();
// TODO: is there a more efficient way of updating just the strings
// we need instead of running the whole language update?
this.updateAudioLanguages_();
});

this.eventManager.listen(
this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
this.updateLocalizedStrings_();
// TODO: is there a more efficient way of updating just the strings
// we need instead of running the whole language update?
this.updateAudioLanguages_();
});


this.eventManager.listen(this.player, 'trackschanged', () => {
this.updateAudioLanguages_();
});

this.eventManager.listen(this.player, 'variantchanged', () => {
this.updateAudioLanguages_();
});

this.eventManager.listen(this.languagesButton_, 'click', () => {
this.onLanguagesClick_();
});

// Set up all the strings in the user's preferred language.
this.updateLocalizedStrings_();
}


/**
* @private
*/
addAudioLangMenu_() {
/** @private {!HTMLElement} */
this.audioLangMenu_ = shaka.ui.Utils.createHTMLElement('div');
this.audioLangMenu_.classList.add('shaka-audio-languages');
this.audioLangMenu_.classList.add('shaka-no-propagation');
this.audioLangMenu_.classList.add('shaka-show-controls-on-mouse-over');
this.audioLangMenu_.classList.add('shaka-settings-menu');

/** @private {!HTMLElement} */
this.backFromLanguageButton_ = shaka.ui.Utils.createHTMLElement('button');
this.backFromLanguageButton_.classList.add('shaka-back-to-overflow-button');
this.audioLangMenu_.appendChild(this.backFromLanguageButton_);

const backIcon = shaka.ui.Utils.createHTMLElement('i');
backIcon.classList.add('material-icons');
backIcon.textContent = shaka.ui.Enums.MaterialDesignIcons.BACK;
this.backFromLanguageButton_.appendChild(backIcon);

/** @private {!HTMLElement} */
this.backFromLanguageSpan_ = shaka.ui.Utils.createHTMLElement('span');
this.backFromLanguageButton_.appendChild(this.backFromLanguageSpan_);

const controlsContainer = this.controls.getControlsContainer();
controlsContainer.appendChild(this.audioLangMenu_);
}


/**
* @private
*/
addLanguagesButton_() {
/** @private {!HTMLElement} */
this.languagesButton_ = shaka.ui.Utils.createHTMLElement('button');
this.languagesButton_.classList.add('shaka-language-button');

const icon = shaka.ui.Utils.createHTMLElement('i');
icon.classList.add('material-icons');
icon.textContent = shaka.ui.Enums.MaterialDesignIcons.LANGUAGE;
this.languagesButton_.appendChild(icon);

const label = shaka.ui.Utils.createHTMLElement('label');
label.classList.add('shaka-overflow-button-label');

/** @private {!HTMLElement} */
this.languageNameSpan_ = shaka.ui.Utils.createHTMLElement('span');
this.languageNameSpan_.classList.add('languageSpan');
label.appendChild(this.languageNameSpan_);

/** @private {!HTMLElement} */
this.currentAudioLanguage_ = shaka.ui.Utils.createHTMLElement('span');
this.currentAudioLanguage_.classList.add('shaka-current-selection-span');
const language = this.player.getConfiguration().preferredAudioLanguage;
this.currentAudioLanguage_.textContent =
shaka.ui.LanguageUtils.getLanguageName(language, this.localization);
label.appendChild(this.currentAudioLanguage_);

this.languagesButton_.appendChild(label);

this.parent.appendChild(this.languagesButton_);
}

/** @private */
onLanguagesClick_() {
this.controls.dispatchEvent(new shaka.util.FakeEvent('submenuopen'));
shaka.ui.Utils.setDisplay(this.audioLangMenu_, true);
// Focus on the currently selected language button.
shaka.ui.Utils.focusOnTheChosenItem(this.audioLangMenu_);
}

/** @private */
updateAudioLanguages_() {
const tracks = this.player.getVariantTracks();

const languagesAndRoles = this.player.getAudioLanguagesAndRoles();
const languages = languagesAndRoles.map((langAndRole) => {
return langAndRole.language;
});

shaka.ui.LanguageUtils.updateLanguages(tracks, this.audioLangMenu_,
languages,
this.onAudioLanguageSelected_.bind(this), /* updateChosen */ true,
this.currentAudioLanguage_,
this.localization);
shaka.ui.Utils.focusOnTheChosenItem(this.audioLangMenu_);
}


/**
* @param {string} language
* @private
*/
onAudioLanguageSelected_(language) {
this.player.selectAudioLanguage(language);
}


/**
* @private
*/
updateLocalizedStrings_() {
const LocIds = shaka.ui.Locales.Ids;

this.backFromLanguageButton_.setAttribute(shaka.ui.Constants.ARIA_LABEL,
this.localization.resolve(LocIds.ARIA_LABEL_BACK));
this.languagesButton_.setAttribute(shaka.ui.Constants.ARIA_LABEL,
this.localization.resolve(LocIds.ARIA_LABEL_LANGUAGE));
this.languageNameSpan_.textContent =
this.localization.resolve(LocIds.LABEL_LANGUAGE);
this.backFromLanguageSpan_.textContent =
this.localization.resolve(LocIds.LABEL_LANGUAGE);
}
};


/**
* @implements {shaka.extern.IUIElement.Factory}
* @final
*/
shaka.ui.AudioLanguageSelection.Factory = class {
/** @override */
create(rootElement, controls) {
return new shaka.ui.AudioLanguageSelection(rootElement, controls);
}
};

shaka.ui.OverflowMenu.registerElement(
'language', new shaka.ui.AudioLanguageSelection.Factory());
Loading

0 comments on commit 4d41b7b

Please sign in to comment.