/**
 * Consrtructor for the strategy. This is what gets passed to the SDK via: 
 * window['_cbv_strategies'].push(DMStrategy);
 * @param {Object} player A pointer to the player being tracked. The player 
 * is where all the video data is derived from
 */
function DMStrategy(player) {
  this._player = player;

  this._videoId = undefined;
  this._videoTitle = undefined;
  this._videoUrl = undefined;
  this._videoDuration = undefined;
  this._thumbnailUrl = undefined;
  this._currentTime = undefined;
  this._currentTimeInterval = undefined;
  this._autostart = undefined

  this._apiReady = false;

  this._contentType = DMStrategy.ContentType.CONTENT;
  this._videoState = DMStrategy.VideoState.UNPLAYED;

  this._viewStartTime = new Date().getTime();
  this._AdPosition = undefined;
  
  this._subscribeEvents();
}

/**
 * Subscribes to message bus events.
 * @private
 */
DMStrategy.prototype._subscribeEvents = function() {
  // api is already ready when state is available for NPE
  this._player.getState().then(function(state){
      this._fetchDataVideo(state);
      this._autostart = this._player.getSettings().autostart === "off" ? false : true;
      this._apiReady = true;
  }.bind(this));
  /**
   * Ad events
   */
  this._player.on(dailymotion.events.AD_START, (function(){
    this._contentType = DMStrategy.ContentType.AD;
  }).bind(this));

  this._player.on(dailymotion.events.AD_PLAY, (function(){
    this._contentType = DMStrategy.ContentType.AD;
    this._videoState = DMStrategy.VideoState.PLAYED;
  }).bind(this));

  this._player.on(dailymotion.events.AD_TIMECHANGE, (function(){
    this._contentType = DMStrategy.ContentType.AD;
    this._videoState = state.playerIsPlaying ? DMStrategy.VideoState.PLAYED : DMStrategy.VideoState.STOPPED;
  }).bind(this));

  this._player.on(dailymotion.events.AD_PAUSE, (function(){
    this._contentType = DMStrategy.ContentType.AD;
    this._videoState = DMStrategy.VideoState.STOPPED;
  }).bind(this));

  this._player.on(dailymotion.events.AD_END, (function(){
    this._contentType = DMStrategy.ContentType.AD;
    this._videoState = DMStrategy.VideoState.COMPLETED;
  }).bind(this));
  /**
   * Video events
   */
  this._player.on(dailymotion.events.VIDEO_START, (function(){
    this._contentType = DMStrategy.ContentType.CONTENT;
  }).bind(this));

  this._player.on(dailymotion.events.VIDEO_PLAY, (function(){
    this._contentType = DMStrategy.ContentType.CONTENT;
    this._videoState = DMStrategy.VideoState.PLAYED;
  }).bind(this));

  this._player.on(dailymotion.events.VIDEO_PAUSE, (function(){
    this._contentType = DMStrategy.ContentType.CONTENT;
    this._videoState = DMStrategy.VideoState.STOPPED;
  }).bind(this));

  this._player.on(dailymotion.events.VIDEO_END, (function(){
    this._contentType = DMStrategy.ContentType.CONTENT;
    this._videoState = DMStrategy.VideoState.COMPLETED;
  }).bind(this));
  /**
   * Player events
   */
  this._player.on(dailymotion.events.PLAYER_VIDEOCHANGE, (function(state){
    this._videoState = DMStrategy.VideoState.UNPLAYED;
    this._fetchDataVideo(state);
  }).bind(this));
  
};


/**
 * fill Metadata of the video
 * @param {Object} state An Object containing video information
 * @private
 */
 DMStrategy.prototype._fetchDataVideo = function(state) {
  this._videoId = state.videoId;
  
  this._saveFirstVideoId(state.id);
  
  // if this the first video so checking if private video id present
  if(this._firstVideoMap & this._firstVideoMap[this._videoId]){
    this._videoId = this._firstVideoMap[this._videoId];
  }
  
  this._videoTitle = state.videoTitle;
  this._videoDuration = state.videoDuration;
  this._thumbnailUrl = 'https://www.dailymotion.com/thumbnail/video/' + this._videoId;
  this._videoUrl = 'https://www.dailymotion.com/video/' + this._videoId;

  clearInterval(this._currentTimeInterval);
  this._currentTimeInterval = setInterval(function(){
      this._player.getState().then(function(state){
        this._currentTime = state.videoTime || 0;
      }.bind(this));
  }.bind(this),500);
};

/**
 * Save first time video id
 * in order to capture kid if first time video is private one
 * @param {string} player_dom_id html id 
 * @private
 */
DMStrategy.prototype._saveFirstVideoId = function(player_dom_id) {
  // only first time excute
  if(!this._callThiis){
    let videoId = 
      (
        new URL(
          document.getElementById(player_dom_id).querySelector("iframe").src
        )
      ).searchParams.get("video");
    if( videoId ){
      this._firstVideoMap = {
        [this._videoId] : videoId
      }
    }
    
  }

  this._callThiis = true;
}


/**
 * This represents an abbreviated name for your custom player strategy.
 */
DMStrategy.prototype.getStrategyName = function() {
  return 'DMS';
};


/**
 * Enum for the content type. One of these values should be returned from 
 * the "getContentType" function.
 * @enum {string}
 */
DMStrategy.ContentType = {
  AD: 'ad',
  CONTENT: 'ct'
};


/**
 * Enum for the ad position. One of these values should be returned from 
 * the "getAdPosition" function.
 * @enum {string}
 */
DMStrategy.AdPosition = {
  PREROLL: 'a1',
  MIDROLL: 'a2',
  POSTROLL: 'a3',
  OVERLAY: 'a4',
  SPECIAL: 'a5'
};


/**
 * Enum for the video state. One of these values should be returned from 
 * the "getState" function.
 * @enum {string}
 */
DMStrategy.VideoState = {
  UNPLAYED: 's1',
  PLAYED: 's2',
  STOPPED: 's3',
  COMPLETED: 's4'
};


/**
 * Enum for the video play type. One of these values should be returned from 
 * the "getAutoplayType" function.
 * @enum {string}
 */
DMStrategy.AutoplayType = {
  UNKNOWN: 'unkn',
  MANUAL: 'man',
  AUTOPLAY: 'auto',
  CONTINUOUS: 'cont'
};


/**
 * Indicates if the video strategy is ready for pinging.
 * Typically this is called when all path and title metadata
 * is available
 * Note: Pings should only be sent after this reads true.
 * @return {boolean} The ready state of the strategy.
 */
DMStrategy.prototype.isReady = function() {
  return this._apiReady;
};


/**
 * Gets the human readable video title.
 * This is returned in the i key of the ping.
 * @return {string} The video title.
 */
DMStrategy.prototype.getTitle = function() {
  return this._videoTitle || '';
};


/**
 * Gets the video path.
 * This is returned in the p key of the ping.
 * Note: this should be the playable video path if available.
 * @return {string} The video path.
 */
DMStrategy.prototype.getVideoPath = function() {
  return this._videoUrl || '';
};


/**
 * Gets the total duration of the video.
 * This is returned in the _vd key of the ping.
 * @return {number} The total duration time in milliseconds.
 */
DMStrategy.prototype.getTotalDuration = function() {
  return parseInt(this._videoDuration * 1000) || 0;
};


/**
 * Gets the current state of the video. Returns value from the VideoState enum.
 * This is returned in the _vs key of the ping.
 * @return {string} The current video state. {@link DMStrategy.VideoState}
 */
DMStrategy.prototype.getState = function() {
  return this._videoState;
};


/**
 * Gets the current play time of the video (where the playhead is).
 * This is returned in the _vpt key of the ping.
 * @return {number} The current play time in milliseconds.
 */
DMStrategy.prototype.getCurrentPlayTime = function() {
  return parseInt(this._currentTime * 1000) || 0;
};


/**
 * Gets the thumbnail of the video.
 * This is returned in the _vtn key of the ping.
 * @return {string} The [absolute] path to the thumbnail.
 */
DMStrategy.prototype.getThumbnailPath = function() {
  return this._thumbnailUrl || '';
};


/**
 * Gets the Autoplay type of the video.
 *
 * @return {DMStrategy.AutoplayType} The type of 
 */
DMStrategy.prototype.getAutoplayType = function() {
  return this._autostart ? 
    DMStrategy.AutoplayType.AUTOPLAY : 
    DMStrategy.AutoplayType.MANUAL;
};


/**
 * Gets the time since start of viewing.
 * This is returned in the _vvs key of the ping.
 * @return {number} The time since viewing started in milliseconds.
 */
DMStrategy.prototype.getViewStartTime = function() {
  return (new Date().getTime() - this._viewStartTime) || 0;
};


/**
 * Gets the type of video playing. Returns value from the ContentType enum.
 * This is returned in the _vt key of the ping.
 * @return {DMStrategy.ContentType} The type of content (ad or ct).
 */
DMStrategy.prototype.getContentType = function() {
  return this._contentType;
};


/**
 * Gets the ad position.
 * @return {StrategyInterface.AdPosition|string} The ad position
 * from a1 (pre-roll), a2 (mid-roll), a3 (post-roll),
 * a4 (overlay), or a5 (special).
 */
DMStrategy.prototype.getAdPosition = function() {
  return '';
};


/**
 * Verifies that the given player belongs to this strategy. Used for a
 * greedy search of the matching strategy for a given element or object.
 * @param {Object} player A pointer to the player being tracked.
 * @return {boolean} If the strategy can handle this type of object.
 */
DMStrategy.verify = function(player) {
  return typeof player['play'] == 'function';
};

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.