Skip to content

Commit

Permalink
add logic to resize AST divs and iframes (#3206)
Browse files Browse the repository at this point in the history
* add logic to resize AST divs and iframes

* fix import package

* replace AST lookup with new API function
  • Loading branch information
jsnellbaker authored Nov 8, 2018
1 parent 8c78cde commit 2206ba4
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/secureCreatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import events from './events';
import { fireNativeTrackers } from './native';
import { EVENTS } from './constants';
import { isSlotMatchingAdUnitCode } from './utils';
import { isSlotMatchingAdUnitCode, logWarn } from './utils';
import { auctionManager } from './auctionManager';
import find from 'core-js/library/fn/array/find';
import { isRendererRequired, executeRenderer } from './Renderer';
Expand Down Expand Up @@ -74,12 +74,38 @@ function sendAdToCreative(adObject, remoteDomain, source) {
function resizeRemoteCreative({ adUnitCode, width, height }) {
// resize both container div + iframe
['div', 'iframe'].forEach(elmType => {
let elementStyle = getElementByAdUnit(elmType).style;
elementStyle.width = width + 'px';
elementStyle.height = height + 'px';
let element = getElementByAdUnit(elmType);
if (element) {
let elementStyle = element.style;
elementStyle.width = width + 'px';
elementStyle.height = height + 'px';
} else {
logWarn(`Unable to locate matching page element for adUnitCode ${adUnitCode}. Can't resize it to ad's dimensions. Please review setup.`);
}
});

function getElementByAdUnit(elmType) {
return document.getElementById(find(window.googletag.pubads().getSlots().filter(isSlotMatchingAdUnitCode(adUnitCode)), slot => slot)
.getSlotElementId()).querySelector(elmType);
let id = getElementIdBasedOnAdServer(adUnitCode);
let parentDivEle = document.getElementById(id);
return parentDivEle && parentDivEle.querySelector(elmType);
}

function getElementIdBasedOnAdServer(adUnitCode) {
if (window.googletag) {
return getDfpElementId(adUnitCode)
} else if (window.apntag) {
return getAstElementId(adUnitCode)
} else {
return adUnitCode;
}
}

function getDfpElementId(adUnitCode) {
return find(window.googletag.pubads().getSlots().filter(isSlotMatchingAdUnitCode(adUnitCode)), slot => slot).getSlotElementId()
}

function getAstElementId(adUnitCode) {
let astTag = window.apntag.getTag(adUnitCode);
return astTag && astTag.targetId;
}
}

0 comments on commit 2206ba4

Please sign in to comment.