Skip to content

Commit

Permalink
add logic to resize AST divs and iframes (prebid#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 and Pedro López Jiménez committed Mar 18, 2019
1 parent bad6bd3 commit ae63f34
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 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 @@ -84,21 +84,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 ae63f34

Please sign in to comment.