UNPKG

vast-player-react

Version:

A React Component That Plays an Inline VAST 4.0 Ad

221 lines (189 loc) 8.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var _source = require('./source'); var _source2 = _interopRequireDefault(_source); var _duration = require('../helpers/duration'); var _tracking = require('../helpers/tracking'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var VALID_MIME_TYPES = ['video/mp4', 'video/ogg', 'video/webm']; var VastVideo = function (_React$Component) { _inherits(VastVideo, _React$Component); function VastVideo(props) { _classCallCheck(this, VastVideo); var _this = _possibleConstructorReturn(this, (VastVideo.__proto__ || Object.getPrototypeOf(VastVideo)).call(this, props)); var durationMillis = (0, _duration.getVastDurationInMillis)(_this.props.duration); var quartile = durationMillis / 4; var validMediaFiles = _this.props.mediaFiles.filter(function (value) { return _lodash2.default.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: (0, _tracking.getTracking)('creativeView', _this.props.tracking), starts: (0, _tracking.getTracking)('start', _this.props.tracking), midpoints: (0, _tracking.getTracking)('midpoint', _this.props.tracking), firstQuartiles: (0, _tracking.getTracking)('firstQuartile', _this.props.tracking), thirdQuartiles: (0, _tracking.getTracking)('thirdQuartile', _this.props.tracking), completes: (0, _tracking.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 } }; return _this; } _createClass(VastVideo, [{ key: 'onClick', value: function onClick() { var videoClicks = this.props.videoClicks; if (videoClicks) { _lodash2.default.each(videoClicks.clickTracking, function (href) { _axios2.default.get(href.getValue()); }); window.open(videoClicks.clickThrough.getValue(), '_blank'); } } }, { key: 'onEnded', value: function onEnded(e) { this.state.tracking.completes.forEach(function (src) { _axios2.default.get(src); }); this.props.onVideoEnded(e); } }, { key: 'startVideo', value: function startVideo() { var _this2 = this; this.videoRef.currentTime = 0; this.videoRef.play(); this.adTimeout = setTimeout(function () { _this2.onEnded(); }, this.state.duration.millis); } }, { key: 'timeUpdate', value: function timeUpdate(evt) { // Ad Timeout based on Ad length clearTimeout(this.adTimeout); var currentTime = evt.currentTarget.currentTime * 1000; var completed = this.state.completed; var tracking = this.state.tracking; var duration = this.state.duration; if (!completed.creativeView) { tracking.creativeViews.forEach(function (src) { _axios2.default.get(src); }); completed.creativeView = true; } if (!completed.start) { tracking.starts.forEach(function (src) { _axios2.default.get(src); completed.start = true; }); completed.start = true; } if (!completed.firstQuartile && currentTime >= duration.firstQuartile) { tracking.firstQuartiles.forEach(function (src) { _axios2.default.get(src); }); completed.firstQuartile = true; } if (!completed.midpoint && currentTime >= duration.midpoint) { tracking.midpoints.forEach(function (src) { _axios2.default.get(src); }); completed.midpoint = true; } if (!completed.thirdQuartile && currentTime >= duration.thirdQuartile) { tracking.thirdQuartiles.forEach(function (src) { _axios2.default.get(src); }); completed.thirdQuartile = true; } } }, { key: 'render', value: function render() { var _this3 = this; var height = 0; var width = 0; var currentHeightDiff = Infinity; var currentWidthDiff = Infinity; var source = this.state.mediaFiles.map(function (media, idx) { var mediaHeight = parseInt(media.getAttr('height'), 10); var mediaWidth = parseInt(media.getAttr('width'), 10); var heightDiff = Math.abs(mediaHeight - _this3.props.height); var widthDiff = Math.abs(mediaWidth - _this3.props.width); if (heightDiff + widthDiff < currentHeightDiff + currentWidthDiff) { currentHeightDiff = heightDiff; currentWidthDiff = widthDiff; height = mediaHeight; width = mediaWidth; } return _react2.default.createElement(_source2.default, { key: idx, media: media }); }); return _react2.default.createElement( 'video', { height: height + 'px', width: width + 'px', ref: function ref(_ref) { return _this3.videoRef = _ref; }, preload: 'auto', onTimeUpdate: function onTimeUpdate(e) { _this3.timeUpdate(e); }, onClick: function onClick(e) { _this3.onClick(e); }, onEnded: function onEnded(e) { _this3.onEnded(e); }, controls: !this.props.disableControls }, source ); } }]); return VastVideo; }(_react2.default.Component); VastVideo.propTypes = { height: _react2.default.PropTypes.number.isRequired, width: _react2.default.PropTypes.number.isRequired, disableControls: _react2.default.PropTypes.bool, duration: _react2.default.PropTypes.string.isRequired, tracking: _react2.default.PropTypes.array.isRequired, videoClicks: _react2.default.PropTypes.object, mediaFiles: _react2.default.PropTypes.array.isRequired, onVideoEnded: _react2.default.PropTypes.func.isRequired }; exports.default = VastVideo; //# sourceMappingURL=index.js.map