src/scripts/ads/vast/VASTResponse.js
'use strict';
var Ad = require('./Ad');
var VideoClicks = require('./VideoClicks');
var Linear = require('./Linear');
var InLine = require('./InLine');
var Wrapper = require('./Wrapper');
var utilities = require('../../utils/utilityFunctions');
var xml = require('../../utils/xml');
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) {
var 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) {
var errorURL = error instanceof xml.JXONTree ? xml.keyValue(error) : error;
if (errorURL) {
this.errorURLMacros.push(errorURL);
}
};
VASTResponse.prototype._addImpressions = function (impressions) {
utilities.isArray(impressions) && appendToArray(this.impressions, impressions);
};
VASTResponse.prototype._addClickThrough = function (clickThrough) {
if (utilities.isNotEmptyString(clickThrough)) {
this.clickThrough = clickThrough;
}
};
VASTResponse.prototype._addClickTrackings = function (clickTrackings) {
utilities.isArray(clickTrackings) && appendToArray(this.clickTrackings, clickTrackings);
};
VASTResponse.prototype._addCustomClicks = function (customClicks) {
utilities.isArray(customClicks) && appendToArray(this.customClicks, customClicks);
};
VASTResponse.prototype._addTrackingEvents = function (trackingEvents) {
var eventsMap = this.trackingEvents;
if (trackingEvents) {
trackingEvents = utilities.isArray(trackingEvents) ? trackingEvents : [trackingEvents];
trackingEvents.forEach(function (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) {
utilities.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) {
var that = this;
if (inLine instanceof InLine) {
this._addTitle(inLine.adTitle);
this._addErrorTrackUrl(inLine.error);
this._addImpressions(inLine.impressions);
inLine.creatives.forEach(function (creative) {
if (creative.linear) {
that._addLinear(creative.linear);
}
});
}
};
VASTResponse.prototype._addWrapper = function (wrapper) {
var that = this;
if (wrapper instanceof Wrapper) {
this._addErrorTrackUrl(wrapper.error);
this._addImpressions(wrapper.impressions);
wrapper.creatives.forEach(function (creative) {
var 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(function (item) {
array.push(item);
});
}
module.exports = VASTResponse;