-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdfp-lite.html
More file actions
261 lines (213 loc) · 8.05 KB
/
dfp-lite.html
File metadata and controls
261 lines (213 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/utils/debounce.html">
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
<script src="../deepmerge/index.js"></script>
<!--
`dfp-lite-ad`
Google Dfp Lite ad element
-->
<dom-module id="dfp-lite-ad">
<template>
<style>
:host {
display: block;
width: 100%;
height: 100%;
}
</style>
</template>
<script>
(function() {
'use strict';
Polymer.DfpLiteManager = Polymer.DfpLiteManager || {
instance: null,
requestAvailability: () => {
if (!Polymer.DfpLiteManager.instance) {
Polymer.DfpLiteManager.instance = document.createElement('dfp-lite-manager');
document.documentElement.appendChild(Polymer.DfpLiteManager.instance);
}
}
};
class DfpLiteManager extends Polymer.Element {
static get is() { return 'dfp-lite-manager'; }
static get properties() {
return {
cookieOptOut: {
type: Boolean
},
targeting: {
type: Object
},
categoryExclusions: {
type: Array
},
tagForChildDirectedTreatment: {
type: Boolean
}
};
}
constructor() {
super();
if (Polymer.DfpLiteManager.instance) return;
Polymer.DfpLiteManager.instance = this;
this.loadScript = new Promise((resolve, reject) => {
let script = document.createElement('script');
script.src = 'https://securepubads.g.doubleclick.net/static/glade.js';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
connectedCallback() {
super.connectedCallback()
this.setAttribute('hidden', true);
}
render(ad) {
// TODO: not 100% certain that passing the ad to render is valid
this.loadScript.then(() => window.glade.run(ad));
}
}
window.customElements.define(DfpLiteManager.is, DfpLiteManager);
class DfpLiteAd extends Polymer.mixinBehaviors([Polymer.IronResizableBehavior], Polymer.Element) {
static get is() { return 'dfp-lite-ad'; }
static get properties() {
return {
adUnitPath: {
type: String
},
clickUrl: {
type: String
},
pageUrl: {
type: String
},
cookieOptOut: {
type: Boolean
},
targeting: {
type: Object
},
categoryExclusions: {
type: Array
},
tagForChildDirectedTreatment: {
type: Boolean
}
};
}
static get observers() {
return [
'_onPropertiesChanged(adUnitPath, clickUrl, pageUrl, cookieOptOut, targeting, categoryExclusions, tagForChildDirectedTreatment)'
]
}
constructor() {
super();
Polymer.DfpLiteManager.requestAvailability();
this._onIronResizeBound = this._onIronResize.bind(this);
this._onAdFetchedBound = this._onAdFetched.bind(this);
this._onAdRenderedBound = this._onAdRendered.bind(this);
this.ad = document.createElement('div');
this.ad.style.position = 'absolute';
this.ad.setAttribute('width', 'fill');
this.ad.setAttribute('height', 'fill');
this.ad.dataset.glade = '';
// this just makes the "has it resized" code simpler
// by removing the need to check for it being undefined
this._prev = { width: 0, height: 0 };
}
connectedCallback() {
super.connectedCallback()
this.ad.addEventListener('gladeAdFetched', e => this._onAdFetchedBound, false);
this.ad.addEventListener('gladeAdRendered', e => this._onAdRenderedBound, false);
Polymer.RenderStatus.beforeNextRender(this, () => {
document.body.appendChild(this.ad);
this._onIronResize();
});
this.addEventListener('iron-resize', this._onIronResizeBound, false);
}
disconnectedCallback() {
super.disconnectedCallback();
console.log('disconnected');
this.removeEventListener('iron-resize', this._onIronResizeBound);
Polymer.DfpLiteManager.instance.unregister(this.ad);
document.body.removeChild(this.ad);
this.ad = undefined;
this.ad.removeEventListener('gladeAdFetched', e => this._onAdFetchedBound);
this.ad.removeEventListener('gladeAdRendered', e => this._onAdRenderedBound);
}
_onPropertiesChanged(adUnitPath, clickUrl, pageUrl, cookieOptOut, targeting, categoryExclusions, tagForChildDirectedTreatment) {
this.ad.dataset.adUnitPath = adUnitPath;
// verge options with manager (i.e. add to or override global settings)
let json = deepmerge({
cookieOptOut: Polymer.DfpLiteManager.instance.cookieOptOut,
targeting: Polymer.DfpLiteManager.instance.targeting,
categoryExclusions: Polymer.DfpLiteManager.instance.categoryExclusions,
tagForChildDirectedTreatment: this._boolean(Polymer.DfpLiteManager.instance.tagForChildDirectedTreatment)
}, {
cookieOptOut: cookieOptOut,
targeting: targeting,
categoryExclusions: categoryExclusions,
tagForChildDirectedTreatment:this._boolean(tagForChildDirectedTreatment)
});
this.ad.dataset.json = JSON.stringify(json);
if (clickUrl) this.ad.dataset.clickUrl = clickUrl;
if (pageUrl) this.ad.dataset.pageUrl = pageUrl;
}
_boolean(val) {
if (val === true) return 1
if (val === false) return 0
return undefined;
}
// clear the ad
clear() {
this.ad.dataset.fetch = true;
while (this.ad.firstChild) {
this.ad.removeChild(this.ad.firstChild);
}
}
// render the ad
render() {
Polymer.DfpLiteManager.instance.render(this.ad);
}
// refresh the ad
refresh() {
this.clear();
this.render();
}
_onAdFetched(e) {
this.dispatchEvent(new CustomEvent('dfp-lite-ad-fetched', { bubbles: true, composed: true, detail: e.detail } ));
}
_onAdRendered(e) {
this.dispatchEvent(new CustomEvent('dfp-lite-ad-rendered', { bubbles: true, composed: true, detail: e.detail } ));
}
_onIronResize() {
this.reposition();
}
reposition() {
let rect = this.getBoundingClientRect();
// hide ad when *we* are hidden and don't bother resizing
if (rect.width === 0 || rect.height === 0) {
this.ad.style.display = 'none';
return;
}
// show the ad and match the size to our element
this.ad.style.display = 'block';
this.ad.style.top = rect.top + 'px';
this.ad.style.left = rect.left + 'px';
// if the size has changed, refresh the ad
if (rect.width !== this._prev.width || rect.height !== this._prev.height) {
this.ad.style.width = rect.width + 'px';
this.ad.style.height = rect.height + 'px';
// when size changes, clear any existing ad content immediately
// otherwise it looks strange (the css media sizing happens instantly)
this.clear();
// and debounce any loading in-case we're just travelling past this size
this._debouncer = Polymer.Debouncer.debounce(this._debouncer, Polymer.Async.timeOut.after(60), this.render.bind(this));
}
this._prev = rect;
}
}
window.customElements.define(DfpLiteAd.is, DfpLiteAd);
})();
</script>
</dom-module>