Source: ui/rewind_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.RewindButton');
  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.RewindButton = 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-rewind-button');
  29. this.button_.classList.add('shaka-tooltip-status');
  30. this.button_.setAttribute('shaka-status',
  31. this.localization.resolve(shaka.ui.Locales.Ids.OFF));
  32. this.button_.textContent =
  33. shaka.ui.Enums.MaterialDesignIcons.REWIND;
  34. this.parent.appendChild(this.button_);
  35. this.updateAriaLabel_();
  36. /** @private {!Array<number>} */
  37. this.rewindRates_ = this.controls.getConfig().rewindRates;
  38. this.eventManager.listen(
  39. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  40. this.updateAriaLabel_();
  41. });
  42. this.eventManager.listen(
  43. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  44. this.updateAriaLabel_();
  45. });
  46. this.eventManager.listen(this.button_, 'click', () => {
  47. if (!this.controls.isOpaque()) {
  48. return;
  49. }
  50. this.rewind_();
  51. });
  52. }
  53. /**
  54. * @private
  55. */
  56. updateAriaLabel_() {
  57. this.button_.ariaLabel =
  58. this.localization.resolve(shaka.ui.Locales.Ids.REWIND);
  59. }
  60. /**
  61. * Cycles trick play rate between the selected rewind rates.
  62. * @private
  63. */
  64. rewind_() {
  65. if (!this.video.duration) {
  66. return;
  67. }
  68. const trickPlayRate = this.player.getPlaybackRate();
  69. const newRateIndex = this.rewindRates_.indexOf(trickPlayRate) + 1;
  70. // When the button is clicked, the next rate in this.rewindRates_ is
  71. // selected. If no more rates are available, the first one is set.
  72. const newRate = (newRateIndex != this.rewindRates_.length) ?
  73. this.rewindRates_[newRateIndex] : this.rewindRates_[0];
  74. if (this.video.paused) {
  75. // Our fast forward is implemented with playbackRate and needs the video
  76. // to be playing (to not be paused) to take immediate effect.
  77. // If the video is paused, "unpause" it.
  78. this.video.play();
  79. }
  80. this.player.trickPlay(newRate);
  81. this.button_.setAttribute('shaka-status', newRate + 'x');
  82. }
  83. };
  84. /**
  85. * @implements {shaka.extern.IUIElement.Factory}
  86. * @final
  87. */
  88. shaka.ui.RewindButton.Factory = class {
  89. /** @override */
  90. create(rootElement, controls) {
  91. return new shaka.ui.RewindButton(rootElement, controls);
  92. }
  93. };
  94. shaka.ui.Controls.registerElement(
  95. 'rewind', new shaka.ui.RewindButton.Factory());