"use strict";
angular.module("com.2fdevs.videogular.plugins.imaads", [])
.directive(
"vgImaAds",
["$window", "VG_STATES", function ($window, VG_STATES) {
return {
restrict: "E",
require: "^videogular",
scope: {
vgNetwork: "=?",
vgUnitPath: "=?",
vgCompanion: "=?",
vgCompanionSize: "=?",
vgAdTagUrl: "=?",
vgPreroll: "=?",
vgMidroll: "=?",
vgPostroll: "=?",
vgSkipButton: "=?"
},
link: function (scope, elem, attr, API) {
var adsManager = null;
var adsLoaded = false;
var w;
var h;
var startplay = false;
var onContentEnded = function () {
adsLoader.contentComplete();
};
var currentAd = 0;
var currentIsLinear = true;
var skipButton = angular.element(scope.vgSkipButton);
var videoFinished = false;
var adContainerDiv = elem[0];
//Lang ads
google.ima.settings.setLocale('ar');
var adDisplayContainer = new google.ima.AdDisplayContainer(adContainerDiv);
var adsLoader = new google.ima.AdsLoader(adDisplayContainer);
adsLoader.getSettings().setVpaidMode( google.ima.ImaSdkSettings.VpaidMode.ENABLED);
scope.onAdsManagerLoaded = function onAdsManagerLoaded(adsManagerLoadedEvent) {
console.log('onAdsManagerLoaded');
scope.show();
adsManager = adsManagerLoadedEvent.getAdsManager(API.mediaElement[0]);
scope.processAdsManager(adsManager);
};
scope.onAdError = function onAdError(err) {
console.log('Ad Error');
console.log(err);
ga('send', 'event', 'video', 'AD Error: '+err.b.h.toString(), "ChoufTV");
// $('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('width', '0%');
$('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('height', '100px');
if (!videoFinished) API.play();
if (currentAd==1) {
console.log(adsManager);
if (adsManager){
adsManager.destroy();
// adsLoader.contentComplete();
}
if(scope.vgMidroll.active){
console.log('midroll');
ga('send', 'event', 'video', 'Midroll AD Request', "ChoufTV");
API.play();
scope.requestAds(scope.vgMidroll.url);
currentAd++;
}
}
if(currentAd==2){
if (adsManager){
adsManager.destroy();
// adsLoader.contentComplete()
}
scope.hide();
}
};
adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
scope.onAdsManagerLoaded,
false);
adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
scope.onAdError,
false);
scope.API = API;
scope.LunchPostroll = function(){
ga('send', 'event', 'video', 'Video Finished', "ChoufTV");
videoFinished = true;
if(adsManager){
adsLoader.contentComplete();
adsManager.destroy();
}
if(scope.vgPostroll.active){
console.log('postroll');
ga('send', 'event', 'video', 'Postroll AD Request', "ChoufTV");
scope.requestAds(scope.vgPostroll.url);
}
}
API.onComplete = scope.LunchPostroll;
scope.onUpdateState = function onUpdateState(newState) {
switch (newState) {
if (currentAd==0 && (scope.vgPreroll.active || scope.vgMidroll.active)) {
API.pause();
currentAd++;
if(scope.vgPreroll.active) {
console.log('Preroll ad');
console.log('vgPreroll.active');
ga('send', 'event', 'video', 'Preroll AD Request', "ChoufTV");
scope.requestAds(scope.vgPreroll.url);
}
else if(scope.vgMidroll.active) {
console.log('vgMidroll.active');
ga('send', 'event', 'video', 'Midroll AD Request', "ChoufTV");
scope.requestAds(scope.vgMidroll.url);
}
}
break;
case VG_STATES.STOP:
// adsManager.destroy();
adsLoader.contentComplete();
break;
}
};
scope.requestAds = function requestAds(adTagUrl) {
// Show only to get computed style in pixels
scope.show();
console.log('requestAds');
scope.resetIMA_();
adDisplayContainer.initialize();
var adsRequest = new google.ima.AdsRequest();
var computedStyle = $window.getComputedStyle(elem[0]);
adsRequest.adTagUrl = adTagUrl;
adsRequest.linearAdSlotWidth = parseInt(computedStyle.width, 10);
adsRequest.linearAdSlotHeight = parseInt(computedStyle.height, 10);
adsRequest.nonLinearAdSlotWidth = parseInt(computedStyle.width, 10);
adsRequest.nonLinearAdSlotHeight = parseInt(computedStyle.height, 10);
adsRequest.forceNonLinearFullSlot = true;
adsRequest.supportsYouTubeHosted = true;
console.log(adsRequest);
// console.log(adsLoader);
adsLoader.requestAds(adsRequest);
};
scope.processAdsManager = function processAdsManager(adsManager) {
w = API.videogularElement[0].offsetWidth;
h = API.videogularElement[0].offsetHeight;
// Attach the pause/resume events.
adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED, scope.onContentPauseRequested, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, scope.onContentResumeRequested, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED, scope.onSkippableStateChanged, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, scope.onAllAdsComplete, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, scope.onAdComplete, false, this);
adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, scope.onAdError, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, scope.onAdLOADED, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY, scope.onAd_BREAK_READY, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED, scope.onAdSKIPPED, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, scope.onAdCONTENT_RESUME_REQUESTED, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.CLICK, scope.onAdCLICK, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, scope.onAdSTARTED, false, this);
adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE, scope.onAdUSER_CLOSE, false, this);
try {
adsManager.init(w, h, google.ima.ViewMode.NORMAL);
}
catch(adError){
console.log('----------AdError----------');
ga('send', 'event', 'video', 'AD Error', "ChoufTV");
console.log(adError);
}
adsManager.start();
};
scope.onAd_BREAK_READY = function(){
console.log('onAd_BREAK_READY');
}
scope.onAdSKIPPED = function(){
console.log('onAdSKIPPED');
ga('send', 'event', 'video', 'AD Skipped', "ChoufTV");
}
scope.onAdCLICK = function(){
console.log('onAdCLICK');
ga('send', 'event', 'video', 'AD Clicked', "ChoufTV");
}
scope.onAdSTARTED = function(){
console.log('onAdSTARTED');
ga('send', 'event', 'video', 'AD Started', "ChoufTV");
}
scope.onAdUSER_CLOSE = function(){
console.log('USER_CLOSE');
ga('send', 'event', 'video', 'AD User Close', "ChoufTV");
}
scope.onAdCONTENT_RESUME_REQUESTED = function(){
console.log('onAdCONTENT_RESUME_REQUESTED');
// $('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('width', '100%');
// $('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('height', '100%');
$('vg-ima-ads').css("bottom", "46px");
if (currentAd==1) {
if(adsManager){
adsManager.destroy();
adsLoader.contentComplete();
}
if(scope.vgMidroll.active){
console.log('midroll');
ga('send', 'event', 'video', 'Midroll AD Request', "ChoufTV");
API.play();
scope.requestAds(scope.vgMidroll.url);
currentAd++;
}
}
}
scope.onAdLOADED = function(){
console.log('onAdLOADED');
currentIsLinear = adsManager.getCurrentAd().b.linear;
console.log(adsManager.getCurrentAd().b.width);
console.log(adsManager.getCurrentAd().b.height);
if(currentIsLinear){
console.log('linear');
ga('send', 'event', 'video', 'Linear AD Loaded', "ChoufTV");
$('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('width', '100%');
$('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('height', '100%');
$('vg-ima-ads iframe').css("position", "absolute");
$('vg-ima-ads iframe').css("top", "0px");
$('vg-ima-ads').css("bottom", "0px");
}
else{
console.log('non linear');
ga('send', 'event', 'video', 'NonLinear AD Loaded', "ChoufTV");
$('vg-ima-ads').css("bottom", "46px");
setTimeout(function() {
var ad_width = adsManager.getCurrentAd().b.width + 10;
if(ad_width==10) ad_width = 450;
var ad_height = adsManager.getCurrentAd().b.height + 10;
$('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('height', ad_height+'px');
$('vg-ima-ads, vg-ima-ads div, vg-ima-ads iframe').css('width', ad_width+'px');
}, 200);
if (!videoFinished) API.play();
}
}
scope.onSkippableStateChanged = function onSkippableStateChanged() {
var isSkippable = adsManager.getAdSkippableState();
if (isSkippable) {
skipButton.css("display", "block");
}
else {
skipButton.css("display", "none");
}
};
scope.onClickSkip = function onClickSkip() {
adsManager.skip();
};
scope.onContentPauseRequested = function onContentPauseRequested() {
scope.show();
API.mediaElement[0].removeEventListener('ended', onContentEnded);
API.pause();
};
scope.onContentResumeRequested = function onContentResumeRequested() {
API.mediaElement[0].addEventListener('ended', onContentEnded);
if (!videoFinished) API.play();
scope.hide();
};
scope.onAllAdsComplete = function onAllAdsComplete() {
scope.hide();
console.log('onAllAdsComplete');
// The last ad was a post-roll
if (adsManager.getCuePoints().join().indexOf("-1") >= 0) {
API.stop();
}
};
scope.onAdComplete = function onAdComplete() {
// TODO: Update view with current ad count
console.log('onAdComplete');
};
scope.resetIMA_ = function() {
if (adsManager) {
adsManager.destroy();
adsManager = null;
}
if (adsLoader) {
adsLoader.contentComplete();
}
};
scope.show = function show() {
elem.css("display", "block");
};
scope.hide = function hide() {
elem.css("display", "none");
};
skipButton.bind("click", scope.onClickSkip);
elem.prepend(skipButton);
angular.element($window).bind("resize", function () {
w = API.videogularElement[0].offsetWidth;
h = API.videogularElement[0].offsetHeight;
if (adsManager) {
if (API.isFullScreen) {
adsManager.resize(w, h, google.ima.ViewMode.FULLSCREEN);
}
else {
adsManager.resize(w, h, google.ima.ViewMode.NORMAL);
}
}
});
if (API.isConfig) {
scope.$watch("API.config",
function () {
if (scope.API.config) {
scope.vgNetwork = scope.API.config.plugins["ima-ads"].network;
scope.vgUnitPath = scope.API.config.plugins["ima-ads"].unitPath;
scope.vgCompanion = scope.API.config.plugins["ima-ads"].companion;
scope.vgCompanionSize = scope.API.config.plugins["ima-ads"].companionSize;
scope.vgAdTagUrl = scope.API.config.plugins["ima-ads"].adTagUrl;
scope.vgSkipButton = scope.API.config.plugins["ima-ads"].skipButton;
}
}
);
}
scope.$watch(
function () {
return API.currentState;
},
function (newVal, oldVal) {
if (newVal != oldVal) {
scope.onUpdateState(newVal);
}
}
);
}
}
}]
);