big-play-button.js

/**
 * @file big-play-button.js
 */
import Button from './button.js';
import Component from './component.js';
import * as browser from './utils/browser.js';
import {isPromise, silencePromise} from './utils/promise';

/**
 * The initial play button that shows before the video has played. The hiding of the
 * `BigPlayButton` get done via CSS and `Player` states.
 *
 * @extends Button
 */
class BigPlayButton extends Button {
  constructor(player, options) {
    super(player, options);

    this.mouseused_ = false;

    this.setIcon('play');

    this.on('mousedown', (e) => this.handleMouseDown(e));
  }

  /**
   * Builds the default DOM `className`.
   *
   * @return {string}
   *         The DOM `className` for this object. Always returns 'vjs-big-play-button'.
   */
  buildCSSClass() {
    return 'vjs-big-play-button';
  }

  /**
   * This gets called when a `BigPlayButton` "clicked". See {@link ClickableComponent}
   * for more detailed information on what a click can be.
   *
   * @param {KeyboardEvent|MouseEvent|TouchEvent} event
   *        The `keydown`, `tap`, or `click` event that caused this function to be
   *        called.
   *
   * @listens tap
   * @listens click
   */
  handleClick(event) {
    const playPromise = this.player_.play();
    const cb = this.player_.getChild('controlBar');
    const playToggle = cb && cb.getChild('playToggle');

    // exit early if tapped or clicked via the mouse
    if (event.type === 'tap' || this.mouseused_ && 'clientX' in event && 'clientY' in event) {
      silencePromise(playPromise);

      // On Microsoft Edge, moving focus to the <video> (tech) element as playback
      // starts prevents a protected (DRM/EME) video surface from being presented:
      // audio plays but the frame stays black until a later repaint or focus change.
      // Focus the control-bar play toggle instead of the tech on Edge, mirroring the
      // keyboard branch below. This is the pointer-path counterpart to the fix for
      // https://github.com/videojs/video.js/issues/6270 (#6318/#6508 redirected only
      // the keyboard path; the mouse/tap tech.focus() survived and regressed on
      // Chromium Edge with hardware-accelerated DRM).
      if (browser.IS_EDGE) {
        (playToggle || this.player_).focus();
      } else if (this.player_.tech(true)) {
        this.player_.tech(true).focus();
      }

      return;
    }

    if (!playToggle) {
      this.player_.tech(true).focus();
      return;
    }

    const playFocus = () => playToggle.focus();

    if (isPromise(playPromise)) {
      playPromise.then(playFocus, () => {});
    } else {
      this.setTimeout(playFocus, 1);
    }
  }

  /**
   * Event handler that is called when a `BigPlayButton` receives a
   * `keydown` event.
   *
   * @param {KeyboardEvent} event
   *        The `keydown` event that caused this function to be called.
   *
   * @listens keydown
   */
  handleKeyDown(event) {
    this.mouseused_ = false;

    super.handleKeyDown(event);
  }

  /**
   * Handle `mousedown` events on the `BigPlayButton`.
   *
   * @param {MouseEvent} event
   *        `mousedown` or `touchstart` event that triggered this function
   *
   * @listens mousedown
   */
  handleMouseDown(event) {
    this.mouseused_ = true;
  }
}

/**
 * The text that should display over the `BigPlayButton`s controls. Added to for localization.
 *
 * @type {string}
 * @protected
 */
BigPlayButton.prototype.controlText_ = 'Play Video';

Component.registerComponent('BigPlayButton', BigPlayButton);
export default BigPlayButton;