Source: ui/fast_forward_button.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.ui.FastForwardButton');
  7. goog.require('shaka.ui.Controls');
  8. goog.require('shaka.ui.Element');
  9. goog.require('shaka.ui.Enums');
  10. goog.require('shaka.ui.Locales');
  11. goog.require('shaka.ui.Localization');
  12. goog.require('shaka.util.Dom');
  13. /**
  14. * @extends {shaka.ui.Element}
  15. * @final
  16. * @export
  17. */
  18. shaka.ui.FastForwardButton = class extends shaka.ui.Element {
  19. /**
  20. * @param {!HTMLElement} parent
  21. * @param {!shaka.ui.Controls} controls
  22. */
  23. constructor(parent, controls) {
  24. super(parent, controls);
  25. /** @private {!HTMLButtonElement} */
  26. this.button_ = shaka.util.Dom.createButton();
  27. this.button_.classList.add('material-icons-round');
  28. this.button_.classList.add('shaka-fast-forward-button');
  29. this.button_.classList.add('shaka-tooltip-status');
  30. this.button_.setAttribute('shaka-status', '1x');
  31. this.button_.textContent =
  32. shaka.ui.Enums.MaterialDesignIcons.FAST_FORWARD;
  33. this.parent.appendChild(this.button_);
  34. this.updateAriaLabel_();
  35. /** @private {!Array.<number>} */
  36. this.fastForwardRates_ = this.controls.getConfig().fastForwardRates;
  37. this.eventManager.listen(
  38. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  39. this.updateAriaLabel_();
  40. });
  41. this.eventManager.listen(
  42. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  43. this.updateAriaLabel_();
  44. });
  45. this.eventManager.listen(this.button_, 'click', () => {
  46. this.fastForward_();
  47. });
  48. }
  49. /**
  50. * @private
  51. */
  52. updateAriaLabel_() {
  53. this.button_.ariaLabel =
  54. this.localization.resolve(shaka.ui.Locales.Ids.FAST_FORWARD);
  55. }
  56. /**
  57. * Cycles trick play rate between the selected fast forward rates.
  58. * @private
  59. */
  60. fastForward_() {
  61. if (!this.video.duration) {
  62. return;
  63. }
  64. const trickPlayRate = this.player.getPlaybackRate();
  65. const newRateIndex = this.fastForwardRates_.indexOf(trickPlayRate) + 1;
  66. // When the button is clicked, the next rate in this.fastForwardRates_ is
  67. // selected. If no more rates are available, the first one is set.
  68. const newRate = (newRateIndex != this.fastForwardRates_.length) ?
  69. this.fastForwardRates_[newRateIndex] : this.fastForwardRates_[0];
  70. this.player.trickPlay(newRate);
  71. this.button_.setAttribute('shaka-status', newRate + 'x');
  72. }
  73. };
  74. /**
  75. * @implements {shaka.extern.IUIElement.Factory}
  76. * @final
  77. */
  78. shaka.ui.FastForwardButton.Factory = class {
  79. /** @override */
  80. create(rootElement, controls) {
  81. return new shaka.ui.FastForwardButton(rootElement, controls);
  82. }
  83. };
  84. shaka.ui.Controls.registerElement(
  85. 'fast_forward', new shaka.ui.FastForwardButton.Factory());