-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
3,029 additions
and
1,419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,83 +18,19 @@ import {BaseElement} from '../src/base-element'; | |
import {assert} from '../src/asserts'; | ||
import {getIntersectionChangeEntry} from '../src/intersection-observer'; | ||
import {isLayoutSizeDefined} from '../src/layout'; | ||
import {setStyles} from '../src/style'; | ||
import {loadPromise} from '../src/event-helper'; | ||
import {registerElement} from '../src/custom-element'; | ||
import {getIframe, listenOnce, postMessage, prefetchBootstrap} from | ||
'../src/3p-frame'; | ||
import {adPrefetch, adPreconnect} from '../ads/_prefetch'; | ||
import {timer} from '../src/timer'; | ||
import {vsyncFor} from '../src/vsync'; | ||
|
||
|
||
/** | ||
* Preview phase only default backfill for ads. If the ad | ||
* cannot fill the slot one of these will be displayed instead. | ||
* @private @const | ||
*/ | ||
const BACKFILL_IMGS_ = { | ||
'300x200': [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
'320x50': [ | ||
'[email protected]', | ||
'[email protected]', | ||
], | ||
}; | ||
|
||
/** @private @const */ | ||
const BACKFILL_DIMENSIONS_ = [ | ||
[300, 200], | ||
[320, 50], | ||
]; | ||
|
||
/** @private @const These tags are allowed to have fixed positioning */ | ||
const POSITION_FIXED_TAG_WHITELIST = { | ||
'AMP-LIGHTBOX': true | ||
}; | ||
|
||
/** | ||
* Preview phase helper to score images through their dimensions. | ||
* @param {!Array<!Array<number>>} dims | ||
* @param {number} maxWidth | ||
* @param {number} maxHeight | ||
* visibleForTesting | ||
*/ | ||
export function scoreDimensions_(dims, maxWidth, maxHeight) { | ||
return dims.map(function(dim) { | ||
const width = dim[0]; | ||
const height = dim[1]; | ||
const widthScore = Math.abs(width - maxWidth); | ||
// if the width is over the max then we need to penalize it | ||
const widthPenalty = Math.abs((maxWidth - width) * 3); | ||
// we add a multiplier to height as we prioritize it more than width | ||
const heightScore = Math.abs(height - maxHeight) * 2; | ||
// if the height is over the max then we need to penalize it | ||
const heightPenalty = Math.abs((maxHeight - height) * 2.5); | ||
|
||
return (widthScore - widthPenalty) + (heightScore - heightPenalty); | ||
}); | ||
} | ||
|
||
/** | ||
* Preview phase helper to update a @1x.png string to @2x.png. | ||
* @param {!Object<string, !Array<string>>} images | ||
* visibleForTesting | ||
*/ | ||
export function upgradeImages_(images) { | ||
Object.keys(images).forEach(key => { | ||
const curDimImgs = images[key]; | ||
curDimImgs.forEach((item, index) => { | ||
curDimImgs[index] = item.replace(/@1x\.png$/, '@2x.png'); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* @param {!Window} win Destination window for the new element. | ||
|
@@ -155,9 +91,6 @@ export function installAd(win) { | |
/** @private {?Element} */ | ||
this.fallback_ = this.getFallback(); | ||
|
||
/** @private {boolean} */ | ||
this.isDefaultFallback_ = false; | ||
|
||
/** @private {boolean} */ | ||
this.isInFixedContainer_ = false; | ||
|
||
|
@@ -177,14 +110,6 @@ export function installAd(win) { | |
|
||
/** @private {boolean} */ | ||
this.shouldSendIntersectionChanges_ = false; | ||
|
||
if (!this.fallback_) { | ||
this.isDefaultFallback_ = true; | ||
|
||
if (this.getDpr() >= 0.5) { | ||
upgradeImages_(BACKFILL_IMGS_); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -351,56 +276,10 @@ export function installAd(win) { | |
* @private | ||
*/ | ||
noContentHandler_() { | ||
if (this.isDefaultFallback_) { | ||
this.setDefaultFallback_(); | ||
this.element.appendChild(this.fallback_); | ||
} | ||
this.element.removeChild(this.iframe_); | ||
this.toggleFallback(true); | ||
} | ||
|
||
/** | ||
* This is a preview-phase only thing where if the ad says that it | ||
* cannot fill the slot we select from a small set of default | ||
* banners. | ||
* @private | ||
* visibleForTesting | ||
*/ | ||
setDefaultFallback_() { | ||
const a = document.createElement('a'); | ||
a.href = 'https://www.ampproject.org'; | ||
a.target = '_blank'; | ||
a.setAttribute('fallback', ''); | ||
const img = new Image(); | ||
setStyles(img, { | ||
width: 'auto', | ||
height: '100%', | ||
margin: 'auto', | ||
}); | ||
|
||
const winner = this.getFallbackImage_(); | ||
img.src = `https://ampproject.org/backfill/${winner}`; | ||
this.fallback_ = a; | ||
a.appendChild(img); | ||
} | ||
|
||
/** | ||
* Picks a random backfill image for the case that no real ad can be | ||
* shown. | ||
* @private | ||
* @return {string} The image URL. | ||
*/ | ||
getFallbackImage_() { | ||
const scores = scoreDimensions_(BACKFILL_DIMENSIONS_, | ||
this.element./*REVIEW*/clientWidth, | ||
this.element./*REVIEW*/clientHeight); | ||
const dims = BACKFILL_DIMENSIONS_[ | ||
scores.indexOf(Math.max.apply(Math, scores))]; | ||
const images = BACKFILL_IMGS_[dims.join('x')]; | ||
// do we need a more sophisticated randomizer? | ||
return images[Math.floor(Math.random() * images.length)]; | ||
} | ||
}; | ||
} | ||
|
||
registerElement(win, 'amp-ad', AmpAd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.