@@ -18,83 +18,19 @@ import {BaseElement} from '../src/base-element';
1818import { assert } from '../src/asserts' ;
1919import { getIntersectionChangeEntry } from '../src/intersection-observer' ;
2020import { isLayoutSizeDefined } from '../src/layout' ;
21- import { setStyles } from '../src/style' ;
2221import { loadPromise } from '../src/event-helper' ;
2322import { registerElement } from '../src/custom-element' ;
2423import { getIframe , listenOnce , postMessage , prefetchBootstrap } from
2524 '../src/3p-frame' ;
2625import { adPrefetch , adPreconnect } from '../ads/_prefetch' ;
2726import { timer } from '../src/timer' ;
28- import { vsyncFor } from '../src/vsync' ;
2927
3028
31- /**
32- * Preview phase only default backfill for ads. If the ad
33- * cannot fill the slot one of these will be displayed instead.
34- * @private @const
35- */
36- const BACKFILL_IMGS_ = {
37- '300x200' : [
38- 39- 40- 41- 42- 43- ] ,
44- '320x50' : [
45- 46- 47- ] ,
48- } ;
49-
50- /** @private @const */
51- const BACKFILL_DIMENSIONS_ = [
52- [ 300 , 200 ] ,
53- [ 320 , 50 ] ,
54- ] ;
55-
5629/** @private @const These tags are allowed to have fixed positioning */
5730const POSITION_FIXED_TAG_WHITELIST = {
5831 'AMP-LIGHTBOX' : true
5932} ;
6033
61- /**
62- * Preview phase helper to score images through their dimensions.
63- * @param {!Array<!Array<number>> } dims
64- * @param {number } maxWidth
65- * @param {number } maxHeight
66- * visibleForTesting
67- */
68- export function scoreDimensions_ ( dims , maxWidth , maxHeight ) {
69- return dims . map ( function ( dim ) {
70- const width = dim [ 0 ] ;
71- const height = dim [ 1 ] ;
72- const widthScore = Math . abs ( width - maxWidth ) ;
73- // if the width is over the max then we need to penalize it
74- const widthPenalty = Math . abs ( ( maxWidth - width ) * 3 ) ;
75- // we add a multiplier to height as we prioritize it more than width
76- const heightScore = Math . abs ( height - maxHeight ) * 2 ;
77- // if the height is over the max then we need to penalize it
78- const heightPenalty = Math . abs ( ( maxHeight - height ) * 2.5 ) ;
79-
80- return ( widthScore - widthPenalty ) + ( heightScore - heightPenalty ) ;
81- } ) ;
82- }
83-
84- /**
85- * Preview phase helper to update a @1x.png string to @2x.png.
86- * @param {!Object<string, !Array<string>> } images
87- * visibleForTesting
88- */
89- export function upgradeImages_ ( images ) {
90- Object . keys ( images ) . forEach ( key => {
91- const curDimImgs = images [ key ] ;
92- curDimImgs . forEach ( ( item , index ) => {
93- curDimImgs [ index ] = item . replace ( / @ 1 x \. p n g $ / , '@2x.png' ) ;
94- } ) ;
95- } ) ;
96- }
97-
9834
9935/**
10036 * @param {!Window } win Destination window for the new element.
@@ -155,9 +91,6 @@ export function installAd(win) {
15591 /** @private {?Element} */
15692 this . fallback_ = this . getFallback ( ) ;
15793
158- /** @private {boolean} */
159- this . isDefaultFallback_ = false ;
160-
16194 /** @private {boolean} */
16295 this . isInFixedContainer_ = false ;
16396
@@ -177,14 +110,6 @@ export function installAd(win) {
177110
178111 /** @private {boolean} */
179112 this . shouldSendIntersectionChanges_ = false ;
180-
181- if ( ! this . fallback_ ) {
182- this . isDefaultFallback_ = true ;
183-
184- if ( this . getDpr ( ) >= 0.5 ) {
185- upgradeImages_ ( BACKFILL_IMGS_ ) ;
186- }
187- }
188113 }
189114
190115 /**
@@ -351,56 +276,10 @@ export function installAd(win) {
351276 * @private
352277 */
353278 noContentHandler_ ( ) {
354- if ( this . isDefaultFallback_ ) {
355- this . setDefaultFallback_ ( ) ;
356- this . element . appendChild ( this . fallback_ ) ;
357- }
358279 this . element . removeChild ( this . iframe_ ) ;
359280 this . toggleFallback ( true ) ;
360281 }
361-
362- /**
363- * This is a preview-phase only thing where if the ad says that it
364- * cannot fill the slot we select from a small set of default
365- * banners.
366- * @private
367- * visibleForTesting
368- */
369- setDefaultFallback_ ( ) {
370- const a = document . createElement ( 'a' ) ;
371- a . href = 'https://www.ampproject.org' ;
372- a . target = '_blank' ;
373- a . setAttribute ( 'fallback' , '' ) ;
374- const img = new Image ( ) ;
375- setStyles ( img , {
376- width : 'auto' ,
377- height : '100%' ,
378- margin : 'auto' ,
379- } ) ;
380-
381- const winner = this . getFallbackImage_ ( ) ;
382- img . src = `https://ampproject.org/backfill/${ winner } ` ;
383- this . fallback_ = a ;
384- a . appendChild ( img ) ;
385- }
386-
387- /**
388- * Picks a random backfill image for the case that no real ad can be
389- * shown.
390- * @private
391- * @return {string } The image URL.
392- */
393- getFallbackImage_ ( ) {
394- const scores = scoreDimensions_ ( BACKFILL_DIMENSIONS_ ,
395- this . element . /*REVIEW*/ clientWidth ,
396- this . element . /*REVIEW*/ clientHeight ) ;
397- const dims = BACKFILL_DIMENSIONS_ [
398- scores . indexOf ( Math . max . apply ( Math , scores ) ) ] ;
399- const images = BACKFILL_IMGS_ [ dims . join ( 'x' ) ] ;
400- // do we need a more sophisticated randomizer?
401- return images [ Math . floor ( Math . random ( ) * images . length ) ] ;
402- }
403- } ;
282+ }
404283
405284 registerElement ( win , 'amp-ad' , AmpAd ) ;
406285}
0 commit comments