UNPKG

videojs-vast-vpaid

Version:
84 lines (64 loc) 2.98 kB
const utilities = require('../../utils/utilityFunctions'); const xml = require('../../utils/xml'); const logger = require('../../utils/consoleLogger'); const TrackingEvent = require('./TrackingEvent'); function Companion (companionJTree) { if (!(this instanceof Companion)) { return new Companion(companionJTree); } logger.info('<Companion> found companion ad'); logger.debug('<Companion> companionJTree:', companionJTree); // Required Elements this.creativeType = xml.attr(companionJTree.staticResource, 'creativeType'); this.staticResource = xml.keyValue(companionJTree.staticResource); logger.info('<Companion> creativeType: ' + this.creativeType); logger.info('<Companion> staticResource: ' + this.staticResource); // Weird bug when the JXON tree is built it doesn't handle casing properly in this situation... let htmlResource = null; if (xml.keyValue(companionJTree.HTMLResource)) { htmlResource = xml.keyValue(companionJTree.HTMLResource); } else if (xml.keyValue(companionJTree.hTMLResource)) { htmlResource = xml.keyValue(companionJTree.hTMLResource); } if (htmlResource !== null) { logger.info('<Companion> found html resource', htmlResource); } this.htmlResource = htmlResource; let iframeResource = null; if (xml.keyValue(companionJTree.IFrameResource)) { iframeResource = xml.keyValue(companionJTree.IFrameResource); } else if (xml.keyValue(companionJTree.iFrameresource)) { iframeResource = xml.keyValue(companionJTree.iFrameresource); } if (iframeResource !== null) { logger.info('<Companion> found iframe resource', iframeResource); } this.iframeResource = iframeResource; // Optional fields this.id = xml.attr(companionJTree, 'id'); this.width = xml.attr(companionJTree, 'width'); this.height = xml.attr(companionJTree, 'height'); this.expandedWidth = xml.attr(companionJTree, 'expandedWidth'); this.expandedHeight = xml.attr(companionJTree, 'expandedHeight'); this.scalable = xml.attr(companionJTree, 'scalable'); this.maintainAspectRatio = xml.attr(companionJTree, 'maintainAspectRatio'); this.minSuggestedDuration = xml.attr(companionJTree, 'minSuggestedDuration'); this.apiFramework = xml.attr(companionJTree, 'apiFramework'); this.companionClickThrough = xml.keyValue(companionJTree.companionClickThrough); this.trackingEvents = parseTrackingEvents(companionJTree.trackingEvents && companionJTree.trackingEvents.tracking); logger.info('<Companion> companionClickThrough: ' + this.companionClickThrough); /** * Local functions ***/ function parseTrackingEvents (trackingEvents) { const trackings = []; if (utilities.isDefined(trackingEvents)) { trackingEvents = utilities.isArray(trackingEvents) ? trackingEvents : [trackingEvents]; trackingEvents.forEach((trackingData) => { trackings.push(new TrackingEvent(trackingData)); }); } return trackings; } } module.exports = Companion;