all files / scripts/ads/vpaid/ VPAIDFlashTech.js

95.74% Statements 45/47
90.91% Branches 20/22
100% Functions 7/7
95.74% Lines 45/47
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              22×     22× 18× 18× 18× 18× 18×     22×           19×     16× 16× 16×                   16×     12×                          
'use strict';
 
var MimeTypes = require('../../utils/mimetypes');
 
var VASTError = require('../vast/VASTError');
 
var VPAIDFLASHClient = require('VPAIDFLASHClient/js/VPAIDFLASHClient');
 
var utilities = require('../../utils/utilityFunctions');
var dom = require('../../utils/dom');
 
var logger = require ('../../utils/consoleLogger');
 
function VPAIDFlashTech(mediaFile, settings) {
  Iif (!(this instanceof VPAIDFlashTech)) {
    return new VPAIDFlashTech(mediaFile);
  }
  sanityCheck(mediaFile);
  this.name = 'vpaid-flash';
  this.mediaFile = mediaFile;
  this.containerEl = null;
  this.vpaidFlashClient = null;
  this.settings = settings;
 
  /*** local functions ***/
  function sanityCheck(mediaFile) {
    if (!mediaFile || !utilities.isString(mediaFile.src)) {
      throw new VASTError('on VPAIDFlashTech, invalid MediaFile');
    }
  }
}
 
VPAIDFlashTech.VPAIDFLASHClient = VPAIDFLASHClient;
 
VPAIDFlashTech.supports = function (type) {
  return (MimeTypes.flash.indexOf(type) > -1) && VPAIDFlashTech.VPAIDFLASHClient.isSupported();
};
 
VPAIDFlashTech.prototype.loadAdUnit = function loadFlashCreative(containerEl, objectEl, callback) {
  var that = this;
  var flashClientOpts = this.settings && this.settings.vpaidFlashLoaderPath ? {data: this.settings.vpaidFlashLoaderPath} : undefined;
  sanityCheck(containerEl, callback);
 
  this.containerEl = containerEl;
 
  logger.debug ("<VPAIDFlashTech.loadAdUnit> loading VPAIDFLASHClient with opts:", flashClientOpts);
 
  this.vpaidFlashClient = new VPAIDFlashTech.VPAIDFLASHClient(containerEl, function (error) {
    if (error) {
      return callback(error);
    }
 
    logger.info ("<VPAIDFlashTech.loadAdUnit> calling VPAIDFLASHClient.loadAdUnit(); that.mediaFile:", that.mediaFile);
    that.vpaidFlashClient.loadAdUnit(that.mediaFile.src, callback);
  }, flashClientOpts);
 
  /*** Local Functions ***/
  function sanityCheck(container, cb) {
 
    if (!dom.isDomElement(container)) {
      throw new VASTError('on VPAIDFlashTech.loadAdUnit, invalid dom container element');
    }
 
    if (!utilities.isFunction(cb)) {
      throw new VASTError('on VPAIDFlashTech.loadAdUnit, missing valid callback');
    }
  }
};
 
VPAIDFlashTech.prototype.unloadAdUnit = function () {
  if (this.vpaidFlashClient) {
    try{
      this.vpaidFlashClient.destroy();
    } catch(e){
      logger.error ('VAST ERROR: trying to unload the VPAID adunit');
    }
    this.vpaidFlashClient = null;
  }
 
  if (this.containerEl) {
    dom.remove(this.containerEl);
    this.containerEl = null;
  }
};
 
module.exports = VPAIDFlashTech;