all files / lib/ vast-player.jsx

96.77% Statements 60/62
85.29% Branches 29/34
100% Functions 15/15
96.72% Lines 59/61
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                                                                                                                                                                                                                     
'use strict';
 
import React from 'react';
import Inline from './in-line/index';
import vastXml from 'vast-xml-4';
import axios from 'axios';
import _ from 'lodash';
import { vastBaseStyle } from './helpers/styles';
 
const vastBase = JSON.parse(JSON.stringify(vastBaseStyle));
 
class VastPlayer extends React.Component {
 
  constructor(props) {
    super(props);
    const vast = this.props.vastJson;
    let ads = null;
    let adCount = 0;
 
    // References to all Ads
    this.adRefs = [];
 
    if (vast) {
      ads = this.validateJson(this.props.vastJson);
      adCount = ads.length;
    } else if (!this.props.vastXml) {
      throw new Error('Pass either VAST XML or VAST JSON(from vast-xml-4 lib)');
    }
 
    this.state = {
      heightStr: `${this.props.height}px`,
      widthStr: `${this.props.width}px`,
      vast,
      adCount,
      index: 0,
      ads,
      isTrackingFired: false,
    };
  }
 
  componentDidMount() {
    if (!this.state.vast && this.props.vastXml) {
      vastXml.parse(this.props.vastXml).then((json) => {
        const ads = this.validateJson(json.vast);
        this.setState({
          vast: json.vast,
          ads,
          adCount: ads.length,
        });
      });
    }
    this.fireTracking();
  }
 
  componentDidUpdate() {
    this.fireTracking();
    if (Ethis.adRefs[this.state.index]) {
      this.adRefs[this.state.index].startVideo();
    }
  }
 
  onEnded() {
    if ((Ithis.state.index + 1) < this.state.adCount) {
      this.setState({
        index: (this.state.index + 1),
      });
    } else {
      if (Ethis.props.onEnded) {
        this.props.onEnded();
      }
    }
  }
 
  fireTracking() {
    if (!this.isTrackingFired && this.state.vast) {
      this.isTrackingFired = true;
      this.state.ads.forEach((ad) => {
        if (ad.inLine.impression) {
          ad.inLine.impression.forEach((pixel) => {
            axios.get(pixel.getValue());
          });
        }
 
        if (ad.inLine.viewableImpression && ad.inLine.viewableImpression.viewUndetermined) {
          ad.inLine.viewableImpression.viewUndetermined.forEach((impression) => {
            axios.get(impression.getValue());
          });
        }
      });
    }
  }
 
  validateJson(vastJson) {
    const ads = vastJson.ad.filter((a) => (!!a.inLine));
    if (vastJson.getAttr('version') !== '4.0') {
      throw new Error('React Vast Player only support VAST 4.0');
    }
 
    if (ads.length === 0) {
      throw new Error('No InLine Ads Found');
    }
 
    return ads;
  }
 
  render() {
    let renderable = null;
    const vastPlayerStyle = {
      backgroundColor: '#000000',
      height: this.state.heightStr,
      width: this.state.widthStr,
      ...vastBase,
    };
    if (this.state.vast) {
      renderable = this.state.ads.map((ad, idx) => {
        const videoOptions = _.cloneDeep(this.props.videoOptions);
        const style = {
          ...vastBase,
        };
        if (Iidx !== this.state.index) {
          style.display = 'none';
        }
 
        return (
          <div
            key={`inline-div-${idx}`}
            style={style}
          >
            <Inline
              ref={(ref) => (this.adRefs.push(ref))}
              onEnded={(e) => { this.onEnded(e); }}
              key={`inline-${idx}`}
              height={this.props.height}
              width={this.props.width}
              defaultDuration={this.props.defaultDuration}
              inLine={ad.inLine}
              videoOptions={videoOptions}
            />
          </div>
        );
      });
    }
 
    return (
      <div
        style={vastPlayerStyle}
      >
        {renderable}
      </div>
    );
  }
 
}
 
VastPlayer.propTypes = {
  vastXml: React.PropTypes.string,
  vastJson: React.PropTypes.object,
  defaultDuration: React.PropTypes.string,
  height: React.PropTypes.number.isRequired,
  width: React.PropTypes.number.isRequired,
  onEnded: React.PropTypes.func,
  videoOptions: React.PropTypes.object,
};
 
export default VastPlayer;