UNPKG

videojs-vast-vpaid

Version:
191 lines (157 loc) 4.82 kB
const utilities = require('../../utils/utilityFunctions'); const xml = require('../../utils/xml'); const VideoClicks = require('./VideoClicks'); const Linear = require('./Linear'); const InLine = require('./InLine'); const Ad = require('./Ad'); const Wrapper = require('./Wrapper'); window.InLine__A = InLine; function VASTResponse () { if (!(this instanceof VASTResponse)) { return new VASTResponse(); } this._linearAdded = false; this.ads = []; this.errorURLMacros = []; this.impressions = []; this.clickTrackings = []; this.customClicks = []; this.trackingEvents = {}; this.mediaFiles = []; this.clickThrough = undefined; this.adTitle = ''; this.duration = undefined; this.skipoffset = undefined; } VASTResponse.prototype.addAd = function (ad) { let inLine, wrapper; if (ad instanceof Ad) { inLine = ad.inLine; wrapper = ad.wrapper; this.ads.push(ad); if (inLine) { this._addInLine(inLine); } if (wrapper) { this._addWrapper(wrapper); } } }; VASTResponse.prototype._addErrorTrackUrl = function (error) { const errorURL = error instanceof xml.JXONTree ? xml.keyValue(error) : error; if (errorURL) { this.errorURLMacros.push(errorURL); } }; VASTResponse.prototype._addImpressions = function (impressions) { if (Array.isArray(impressions)) { appendToArray(this.impressions, impressions); } }; VASTResponse.prototype._addClickThrough = function (clickThrough) { if (utilities.isNotEmptyString(clickThrough)) { this.clickThrough = clickThrough; } }; VASTResponse.prototype._addClickTrackings = function (clickTrackings) { if (Array.isArray(clickTrackings)) { appendToArray(this.clickTrackings, clickTrackings); } }; VASTResponse.prototype._addCustomClicks = function (customClicks) { if (Array.isArray(customClicks)) { appendToArray(this.customClicks, customClicks); } }; VASTResponse.prototype._addTrackingEvents = function (trackingEvents) { const eventsMap = this.trackingEvents; if (trackingEvents) { trackingEvents = utilities.isArray(trackingEvents) ? trackingEvents : [trackingEvents]; trackingEvents.forEach((trackingEvent) => { if (!eventsMap[trackingEvent.name]) { eventsMap[trackingEvent.name] = []; } eventsMap[trackingEvent.name].push(trackingEvent); }); } }; VASTResponse.prototype._addTitle = function (title) { if (utilities.isNotEmptyString(title)) { this.adTitle = title; } }; VASTResponse.prototype._addDuration = function (duration) { if (utilities.isNumber(duration)) { this.duration = duration; } }; VASTResponse.prototype._addVideoClicks = function (videoClicks) { if (videoClicks instanceof VideoClicks) { this._addClickThrough(videoClicks.clickThrough); this._addClickTrackings(videoClicks.clickTrackings); this._addCustomClicks(videoClicks.customClicks); } }; VASTResponse.prototype._addMediaFiles = function (mediaFiles) { if (Array.isArray(mediaFiles)) { appendToArray(this.mediaFiles, mediaFiles); } }; VASTResponse.prototype._addSkipoffset = function (offset) { if (offset) { this.skipoffset = offset; } }; VASTResponse.prototype._addAdParameters = function (adParameters) { if (adParameters) { this.adParameters = adParameters; } }; VASTResponse.prototype._addLinear = function (linear) { if (linear instanceof Linear) { this._addDuration(linear.duration); this._addTrackingEvents(linear.trackingEvents); this._addVideoClicks(linear.videoClicks); this._addMediaFiles(linear.mediaFiles); this._addSkipoffset(linear.skipoffset); this._addAdParameters(linear.adParameters); this._linearAdded = true; } }; VASTResponse.prototype._addInLine = function (inLine) { const that = this; if (inLine instanceof InLine) { this._addTitle(inLine.adTitle); this._addErrorTrackUrl(inLine.error); this._addImpressions(inLine.impressions); inLine.creatives.forEach((creative) => { if (creative.linear) { that._addLinear(creative.linear); } }); } }; VASTResponse.prototype._addWrapper = function (wrapper) { const that = this; if (wrapper instanceof Wrapper) { this._addErrorTrackUrl(wrapper.error); this._addImpressions(wrapper.impressions); wrapper.creatives.forEach((creative) => { const linear = creative.linear; if (linear) { that._addVideoClicks(linear.videoClicks); that.clickThrough = undefined;// We ensure that no clickThrough has been added that._addTrackingEvents(linear.trackingEvents); } }); } }; VASTResponse.prototype.hasLinear = function () { return this._linearAdded; }; function appendToArray (array, items) { items.forEach((item) => { array.push(item); }); } module.exports = VASTResponse;