all files / linear/ index.jsx

14.29% Statements 11/77
0% Branches 0/24
0% Functions 0/20
14.29% Lines 11/77
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                                                                                                                                                                                                                                                                                                                               
'use strict';
import axios from 'axios';
import React from 'react';
import _ from 'lodash';
import VideoSource from './source';
import { getVastDurationInMillis } from '../helpers/duration';
import { getTracking } from '../helpers/tracking';
 
const VALID_MIME_TYPES = ['video/mp4', 'video/ogg', 'video/webm'];
 
class VastVideo extends React.Component {
 
  constructor(props) {
    super(props);
    const durationMillis = getVastDurationInMillis(this.props.duration);
    const quartile = (durationMillis / 4);
    const validMediaFiles = this.props.mediaFiles
      .filter((value) => (
        _.includes(VALID_MIME_TYPES, value.getAttr('type'))
      ));
    if (validMediaFiles.length === 0) {
      throw new Error('No Supported Video MIME Types');
    }
 
    this.state = {
      mediaFiles: validMediaFiles,
      tracking: {
        creativeViews: getTracking('creativeView', this.props.tracking),
        starts: getTracking('start', this.props.tracking),
        midpoints: getTracking('midpoint', this.props.tracking),
        firstQuartiles: getTracking('firstQuartile', this.props.tracking),
        thirdQuartiles: getTracking('thirdQuartile', this.props.tracking),
        completes: getTracking('complete', this.props.tracking),
      },
      duration: {
        millis: durationMillis,
        firstQuartile: Math.floor(quartile),
        midpoint: Math.floor(quartile * 2),
        thirdQuartile: Math.floor(quartile * 3),
      },
      completed: {
        creativeView: false,
        start: false,
        midpoint: false,
        firstQuartile: false,
        thirdQuartile: false,
        complete: false,
      },
    };
  }
 
  onClick() {
    const videoClicks = this.props.videoClicks;
    if (videoClicks) {
      _.each(videoClicks.clickTracking, (href) => {
        axios.get(href.getValue());
      });
      window.open(videoClicks.clickThrough.getValue(), '_blank');
    }
  }
 
  onEnded(e) {
    this.state.tracking.completes.forEach((src) => {
      axios.get(src);
    });
    this.props.onVideoEnded(e);
  }
 
  startVideo() {
    this.videoRef.currentTime = 0;
    this.videoRef.play();
    this.adTimeout = setTimeout(() => {
      this.onEnded();
    }, this.state.duration.millis);
  }
 
  timeUpdate(evt) {
    // Ad Timeout based on Ad length
    clearTimeout(this.adTimeout);
    const currentTime = (evt.currentTarget.currentTime * 1000);
    const completed = this.state.completed;
    const tracking = this.state.tracking;
    const duration = this.state.duration;
 
    if (!completed.creativeView) {
      completed.creativeViews.forEach((src) => {
        axios.get(src);
      });
      completed.creativeView = true;
    }
 
    if (!completed.start) {
      tracking.starts.forEach((src) => {
        axios.get(src);
        completed.start = true;
      });
      completed.start = true;
    }
 
    if (!completed.firstQuartile &&
      currentTime >= duration.firstQuartile) {
      tracking.firstQuartiles.forEach((src) => {
        axios.get(src);
      });
      completed.firstQuartile = true;
    }
 
    if (!completed.midpoint &&
      currentTime >= duration.midpoint) {
      tracking.midpoints.forEach((src) => {
        axios.get(src);
      });
      completed.midpoint = true;
    }
 
    if (!completed.thirdQuartile &&
      currentTime >= duration.thirdQuartile) {
      tracking.thirdQuartiles.forEach((src) => {
        axios.get(src);
      });
      completed.thirdQuartile = true;
    }
  }
 
  render() {
    let height = 0;
    let width = 0;
    let currentHeightDiff = Infinity;
    let currentWidthDiff = Infinity;
    const source = this.state.mediaFiles.map((media, idx) => {
      const mediaHeight = parseInt(media.getAttr('height'), 10);
      const mediaWidth = parseInt(media.getAttr('width'), 10);
      const heightDiff = Math.abs(mediaHeight - this.props.height);
      const widthDiff = Math.abs(mediaWidth - this.props.width);
      if ((heightDiff + widthDiff) < (currentHeightDiff + currentWidthDiff)) {
        currentHeightDiff = heightDiff;
        currentWidthDiff = widthDiff;
        height = mediaHeight;
        width = mediaWidth;
      }
      return (<VideoSource key={idx} media={media} />);
    });
 
    return (<video
      height={`${height}px`}
      width={`${width}px`}
      ref={(ref) => (this.videoRef = ref)}
      preload="auto"
      onTimeUpdate={(e) => { this.timeUpdate(e); }}
      onClick={(e) => { this.onClick(e); }}
      onEnded={(e) => { this.onEnded(e); }}
      controls={!this.props.disableControls}
    >
      {source}
    </video>);
  }
}
 
VastVideo.propTypes = {
  height: React.PropTypes.number.isRequired,
  width: React.PropTypes.number.isRequired,
  disableControls: React.PropTypes.bool,
  duration: React.PropTypes.string.isRequired,
  tracking: React.PropTypes.array.isRequired,
  videoClicks: React.PropTypes.object,
  mediaFiles: React.PropTypes.array.isRequired,
  onVideoEnded: React.PropTypes.func.isRequired,
};
 
export default VastVideo;