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_.textContent =
  30. shaka.ui.Enums.MaterialDesignIcons.REWIND;
  31. this.parent.appendChild(this.button_);
  32. this.updateAriaLabel_();
  33. this.eventManager.listen(
  34. this.localization, shaka.ui.Localization.LOCALE_UPDATED, () => {
  35. this.updateAriaLabel_();
  36. });
  37. this.eventManager.listen(
  38. this.localization, shaka.ui.Localization.LOCALE_CHANGED, () => {
  39. this.updateAriaLabel_();
  40. });
  41. this.eventManager.listen(this.button_, 'click', () => {
  42. this.rewind_();
  43. });
  44. }
  45. /**
  46. * @private
  47. */
  48. updateAriaLabel_() {
  49. this.button_.ariaLabel =
  50. this.localization.resolve(shaka.ui.Locales.Ids.REWIND);
  51. }
  52. /**
  53. * Cycles trick play rate between -1, -2, -4, and -8.
  54. * @private
  55. */
  56. rewind_() {
  57. if (!this.video.duration) {
  58. return;
  59. }
  60. const trickPlayRate = this.player.getPlaybackRate();
  61. // Every time the button is clicked, the rate is multiplied by 2,
  62. // unless the rate is at it's slowest (-8), in which case it is
  63. // dropped back to 1.
  64. const newRate = (trickPlayRate > 0 || trickPlayRate < -4) ?
  65. -1 : trickPlayRate * 2;
  66. this.player.trickPlay(newRate);
  67. }
  68. };
  69. /**
  70. * @implements {shaka.extern.IUIElement.Factory}
  71. * @final
  72. */
  73. shaka.ui.RewindButton.Factory = class {
  74. /** @override */
  75. create(rootElement, controls) {
  76. return new shaka.ui.RewindButton(rootElement, controls);
  77. }
  78. };
  79. shaka.ui.Controls.registerElement(
  80. 'rewind', new shaka.ui.RewindButton.Factory());