All files / audio-players archive-audio-jwplayer-wrapper.jsx

16.16% Statements 16/99
1.45% Branches 1/69
20% Functions 3/15
15.31% Lines 15/98

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                  1x   1x     1x                       1x 1x 1x   1x                 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                       1x 1x                   1x   1x                     1x                                    
/* global Play */
/* eslint-disable react/destructuring-assignment */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
/**
 * IA Audio Player
 * Uses global: Play class (IA JWPlayer wrapper), jwplayer
 *
 * It will display photo if given, and will overlay the media player at the base of the photo.
 *
 * Usage notes:
 * - We directly interact with Play8's Play class, we only listen to JWP.
 * - uses 0 index so it will always be off by -1 when relating to `trackNumber`
 */
class ArchiveAudioPlayer extends Component {
  constructor(props) {
    super(props);
 
    this.jwPlayerInstance = React.createRef();
 
    // expecting jwplayer to be globally ready
    this.state = {
      player: null,
      jwplayerInstance: null,
      /** Track Numbers start at 1, 0 = album (unsupported in IA player) */
      trackNumber: null,
      playlistLoadCount: 0,
      playerReady: false,
      startingOnTrack: 0,
      playerEverStarted: false,
      trackStarting: false,
    };
 
    this.onPlaylistItemCB = this.onPlaylistItemCB.bind(this);
    this.updateAndPlayTrack = this.updateAndPlayTrack.bind(this);
    this.setURL = this.setURL.bind(this);
 
    this.maxPlaylistLoadsUntilPlayerIsReady = 2;
  }
 
  /**
   * Register jwplayer/play8 instance as component mounts on client
   */
  componentDidMount() {
    const {
      jwplayerInfo, jwplayerID, backgroundPhoto, onRegistrationComplete, jwplayerStartingPoint
    } = this.props;
    const { jwplayerPlaylist, identifier } = jwplayerInfo;
    Eif (!jwplayerPlaylist.length) return;
 
    const waveformer = backgroundPhoto
      ? {}
      : { waveformer: 'jw-holder' };
    // Load IA Player & tack on JWP listeners
    const baseConfig = {
      so: true,
      audio: true,
      identifier,
      hide_list: true,
      responsive: true,
      onPlaylistItem: this.onPlaylistItemCB,
      onReady: (jwplayerInstance) => {
        /**
         * Capture if player starts on track N+1
         */
        const trackIdx = jwplayerInstance.getPlaylistIndex();
        const trackNumber = trackIdx + 1;
        this.setState({ trackNumber, jwplayerInstance }, () => {
          if (jwplayerStartingPoint) {
            /**
             * tell main app to start here
             * so 3rd party players can start there as well
             */
            this.setState({ startingOnTrack: trackNumber }, () => {
              jwplayerStartingPoint(trackNumber);
            });
          }
 
          /* JWP listeners to track player states */
          this.state.jwplayerInstance.on('complete', () => {
            this.setState({ trackStarting: false, playerEverStarted: true });
          });
 
          this.state.jwplayerInstance.on('paused', () => {
            this.setState({ trackStarting: false, playerEverStarted: true });
          });
 
          this.state.jwplayerInstance.on('play', () => {
            // patron hits Play triangle on JWP
            const thisTrackNumber = jwplayerInstance.getPlaylistIndex();
            this.setState({
              trackStarting: true, playerEverStarted: true, trackNumber: thisTrackNumber + 1
            });
          });
        });
      },
    };
 
    if (window.Play && Play) {
      const compiledConfig = Object.assign({}, baseConfig, waveformer);
      const player = Play(jwplayerID, jwplayerPlaylist, compiledConfig);
      this.setState({ player });
 
      if (onRegistrationComplete) {
        /**
         * Currently, this is where we support external ability to set URL
         * through Internet Archive's JWPlayer Wrapper
         */
        onRegistrationComplete(this.setURL);
      }
    }
  }
 
  /**
   * We track when:
   * - Play8 is loaded
   * - track is playing
   * - autoplay is happening
   * - to change next track
   *
   * @inheritdoc
   */
  componentDidUpdate(prevProps, prevState) {
    const { sourceData: { index: incomingTrackNum = null } } = this.props;
    const {
      playerReady, playlistLoadCount, playerEverStarted, jwplayerInstance, trackNumber, trackStarting
    } = this.state;
    const { sourceData: prevSourceData } = prevProps;
    const { index: prevIndex } = prevSourceData;
    const trackN = parseInt(incomingTrackNum, 10);
    const isNowMainPlayer = !prevSourceData.hasOwnProperty('index') && trackN;
    const unsupportedAlbumView = incomingTrackNum === 0; // 0 = album for 3d party players
    console.log('*** componentDidUpdate1 **', {
      stateTrackNumber: this.state.trackNumber, trackN, isNowMainPlayer, isNan: Number.isNaN(trackN), prevProps
    });
 
    if (!jwplayerInstance || unsupportedAlbumView) {
      return;
    }
 
    if (Number.isNaN(trackN) || (trackN < 0) || isNowMainPlayer) {
      return;
    }
 
    const playerStatus = jwplayerInstance.getState();
    const jwpTrackIndex = jwplayerInstance.getPlaylistIndex();
    const jwpPlayingOnColdLoad = prevIndex === null && incomingTrackNum >= 1;
    const incomingTrackChange = !jwpPlayingOnColdLoad && (incomingTrackNum > prevIndex) || (trackNumber !== incomingTrackNum);
    const autoplaying = incomingTrackChange && (playerStatus === 'idle') && (jwpTrackIndex + 1 === incomingTrackNum);
 
    const iaPlayerisReady = playlistLoadCount === this.maxPlaylistLoadsUntilPlayerIsReady;
 
    console.log('*** componentDidUpdate2 **', {
      jwpTrackIndex,
      iaPlayerisReady,
      jwpPlayingOnColdLoad,
      autoplaying,
      incomingTrackChange,
      incomingTrackNum
    });
 
 
    if (!playerReady && iaPlayerisReady) {
      /** IA player starting, let's tell main app where we are starting */
      this.updateAndPlayTrack({
        playerReady: true,
        trackNumber: incomingTrackNum,
      }, !autoplaying);
      return;
    }
 
    if (!prevState.playerReady && playerReady) {
      /** Play8 w/ JWP is now ready & at rest, we can also rest */
      return;
    }
 
    if (!playerEverStarted) {
      // First song to play
      this.updateAndPlayTrack({
        trackNumber: incomingTrackNum,
        playerEverStarted: true,
        playerReady: true
      });
      return;
    }
 
    const trackStarted = !prevState.trackStarting && trackStarting;
    const trackEnded = prevState.trackStarting && !trackStarting;
 
    if (trackStarted || trackEnded) {
      /** we are inbetween track playing */
      return;
    }
 
    if (autoplaying) {
      /** update track pointer as play continues */
      this.updateAndPlayTrack({ trackNumber: incomingTrackNum }, false);
      return;
    }
 
    const trackChange = incomingTrackNum !== trackNumber;
    if (trackChange) {
      this.updateAndPlayTrack({ trackNumber: incomingTrackNum });
    }
  }
 
  /**
   * Event Handler that fires when JWPlayer starts a new track
   * (eg: controlbar or auto-advance, player starting on track N+1)
   *
   * @param { object } jwplayer - instance
   * @param { event } event - incoming track info
  */
  onPlaylistItemCB(jwplayer, event) {
    const { index: incomingTrackIdx } = event;
    const {
      trackNumber,
      playlistLoadCount,
    } = this.state;
    const { jwplayerStartingPoint } = this.props;
    const callCount = playlistLoadCount + 1;
    const newState = { playlistLoadCount: callCount };
 
    if (callCount < this.maxPlaylistLoadsUntilPlayerIsReady) {
      this.setState(newState);
      return;
    }
 
    if (callCount === this.maxPlaylistLoadsUntilPlayerIsReady) {
      // finally, Play8 class is ready.
      this.setState({ ...newState, startingOnTrack: incomingTrackIdx }, () => {
        if (jwplayerStartingPoint) {
          jwplayerStartingPoint(incomingTrackIdx + 1);
        }
      });
      return;
    }
 
    if (trackNumber !== incomingTrackIdx + 1) {
      const { jwplayerPlaylistChange } = this.props;
      jwplayerPlaylistChange(incomingTrackIdx + 1);
    }
  }
 
  /**
   * Signals to IA's jwplayer handler, Play8,
   * that it's time to change the URL to match given track
   *
   * @param { number } trackNumber
   */
  setURL(trackNumber) {
    const { player } = this.state;
    const playlistIndex = trackNumber - 1 || 0;
    return player.playN(playlistIndex, true);
  }
 
  /**
   * This updates internal state & then tells jwplayer/Play8 to start playing track
   *
   * @param { Object } stateToUpdate
   * @param { number } stateToUpdate.trackNumber - Track index to play. *Required
   * @param { * } stateToUpdate[param] - optional states to update
   * @param { boolean } playTrack -
   */
  updateAndPlayTrack(stateToUpdate, playTrack = true) {
    const { trackNumber } = stateToUpdate;
    const { player } = this.state;
    this.setState({ ...stateToUpdate, trackStarting: true }, () => {
      if (playTrack) {
        const trackIdx = trackNumber - 1 || 0;
        player.playN(trackIdx);
      }
    });
  }
 
  render() {
    const { jwplayerID, style } = this.props;
    return (
      <div className="ia-player-wrapper" style={style}>
        <div className="iaux-player-wrapper">
          <div id={jwplayerID} />
        </div>
      </div>
    );
  }
}
 
ArchiveAudioPlayer.displayName = 'ArchiveAudioPlayer';
 
ArchiveAudioPlayer.defaultProps = {
  backgroundPhoto: '',
  jwplayerID: '',
  jwplayerPlaylistChange: null,
  jwplayerInfo: {},
  sourceData: null,
  onRegistrationComplete: null,
  style: '',
  jwplayerStartingPoint: null
};
 
ArchiveAudioPlayer.propTypes = {
  backgroundPhoto: PropTypes.string,
  jwplayerID: PropTypes.string,
  jwplayerPlaylistChange: PropTypes.func,
  jwplayerStartingPoint: PropTypes.func,
  jwplayerInfo: PropTypes.shape({
    jwplayerPlaylist: PropTypes.array,
    identifier: PropTypes.string
  }),
  sourceData: PropTypes.shape({
    index: PropTypes.number
  }),
  onRegistrationComplete: PropTypes.func,
  // eslint-disable-next-line react/forbid-prop-types
  style: PropTypes.object
};
 
export default ArchiveAudioPlayer;