-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClairineTest.js
296 lines (253 loc) · 9.59 KB
/
ClairineTest.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
test global
rp_adband
rp_elementMarker
*/
//const renderAd = require('../../modules/RenderAd');
/**
* @constructor
*/
const interScroller = function () {
const self = this;
let _document;
let parentWindow;
let rp_adband;
const containerID = 'rp-interscroller-container';
const creativeID = 'rp-interscroller-creative';
/**
* @function interscroller#render
* @param {object} ad response
* @summary processes the ad response and calls interscroller functions
*/
self.renderAd = (data) => {
rp_elementMarker = data.elementMarker;
// Assign the correct document context
_document = getCorrectDocument();
init(data.fullBids, data.elementMarker, data.pVisibility, data.iniTranslate);
};
/**
* @function init
* @param {object} adResponse to add the ad response
* creative to a generated friendly iframe
* @param {string} adDimensions ad dimensions in WxH format
* @summary adds the interscroller stylesheet,
* creates an iframe and adds the interscoller animation
*/
const init = (fullBids, rp_elementMarker, pVisibility, iniTranslate) => {
adDimensions = fullBids.size;
document.querySelector('iframe[name^=google_ads_iframe]').style.display = "none";
if (typeof iniTranslate == 'undefined') {
iniTranslate = -35;
}
if (typeof rp_elementMarker != 'undefined') {
appendStyleSheet(adDimensions, pVisibility);
const containerIframe = getContainerIframe(
adDimensions, rp_elementMarker
);
addCreative(containerIframe, fullBids.ad);
parallax(iniTranslate);
} else {
console.error('RP: No element marker defined');
}
};
/**
* @function appendStyleSheet
* @param {string} adDimensions ad dimensions in WxH format
* @summary adds a custom stylesheet to the dom.
* If rp_adband variable is present in the smart tag,
* additional ad band styles are added
*/
const appendStyleSheet = (adDimensions, pVisibility) => {
const customStyleSheet = _document.createElement('style');
customStyleSheet.id = 'rp-interscroller-styles';
const [x,y] = adDimensions.split('x');
const adBandStyles = `#${containerID}:before{
position:absolute;
content:"${rp_adband}";
top:0px;
height:20px;
text-align:center;
background-color:#000;
color:#fff;
width:${x}px;
z-index:3000;
}`;
/* eslint-disable no-magic-numbers */
const interScrollerStyles = `#${containerID}{
position:relative;
width:${x}px;
height:${parseInt(y) / (100 / pVisibility) }px;
margin:0px;
overflow:hidden;
padding-top:20px;
}
#${creativeID}{
height:600px;
width:300px;
position:absolute;
}`;
/* eslint-enable no-magic-numbers */
if (rp_adband !== '') {
customStyleSheet.innerHTML = interScrollerStyles + adBandStyles;
} else {
customStyleSheet.innerHTML = interScrollerStyles;
}
_document.querySelector('body').appendChild(customStyleSheet);
};
/**
* @function getContainerIframe
* @param {string} adDimensions ad dimensions in WxH format
* @param {string} containerElement the container div to
* hook the interscroller ad onto. By default,
* the div should be <div id='parallax'></div>
* @summary creates a div to inject the interscroller
* ad into and adds a friendly iframe to this div
* @returns {object} a friendly iframe
*/
const getContainerIframe = (adDimensions, containerElement) => {
const interScrollerContainer = _document.createElement('div');
interScrollerContainer.id = containerID;
const elem = _document.querySelector(containerElement) ||
parent.document.querySelector(containerElement);
elem.insertBefore(interScrollerContainer, null);
const creativeFrame = _document.createElement('iframe');
const [x,y] = adDimensions.split('x');
const styles = [
`width: ${x}px;`,
`height: ${y}px;`,
'border: 0;',
'margin: 0;',
'padding: 0;',
'overflow: hidden;',
].join('');
(creativeFrame.frameElement || creativeFrame).style.cssText = styles;
creativeFrame.setAttribute('scrolling', 'no');
creativeFrame.src = 'about:blank';
creativeFrame.id = creativeID;
_document
.getElementById(containerID)
.appendChild(creativeFrame);
const contentDocument = (creativeFrame.contentWindow) ?
creativeFrame.contentWindow.document :
creativeFrame.contentDocument.document;
return contentDocument;
};
/**
* @function addCreative
* @param {object} containerFrame a friendly iframe to add the creative into
* @param {object} adResponse the ad response containing
* the creative to add into the iframe
* @summary adds the ad response creative to the iframe
*/
const addCreative = (containerFrame, adResponse) => {
containerFrame.open().write(
`<html>
<head>
<script type='text/javascript'>
var inDapIF = true;
</script>
<body style='margin : 0; padding: 0;'>
<script type='text/javascript'>${adResponse}</script>
</body>
</html>`
);
containerFrame.close();
};
/**
* @function parallax
* @summary adds the parallax animation to move the ad up
* or down as the user scrolls the page
*/
const parallax = (iniTranslate) => {
let adUnitIsCompletelyVisible;
let maxSpaceLeft;
const useTransform = true;
const adUnit = inIframe() ?
window.frameElement :
document.querySelector(`#${containerID}`);
const adUnitImage = document.querySelector(`#${creativeID}`);
if (useTransform) {
adUnitImage.style.transform = 'translateY('+iniTranslate+'%)';
}
try {
parentWindow = parent.window;
} catch (e) {
console.error(`RP:Could not access the parent window.
Possibly due to cross domain iframe`);
parentWindow = window;
}
// As the user scrolls, a check is performed to see if the ad
// is partially or fully visible
parentWindow.addEventListener('scroll', () => {
const elemBounds = adUnit.getBoundingClientRect();
const elemImgBounds = adUnitImage.getBoundingClientRect();
if (isElementFullyVisible(adUnit)) {
adUnitIsCompletelyVisible = true;
if (!maxSpaceLeft) {
maxSpaceLeft = parentWindow.innerHeight - elemBounds.height;
}
} else {
adUnitIsCompletelyVisible = false;
}
if (adUnitIsCompletelyVisible) {
const spaceLeft = elemBounds.top;
/* eslint-disable no-magic-numbers */
if (useTransform) {
let tformAmount = (spaceLeft / maxSpaceLeft * 100) / 2;
tformAmount = tformAmount > 50 ? 50 : tformAmount;
adUnitImage
.style
.transform = `translateY(-${tformAmount}%)`;
}
if (!useTransform) {
// eslint-disable-next-line max-len
let topAmount = -((elemImgBounds.height / 2) + spaceLeft);
topAmount = topAmount < -(elemImgBounds.height / 2) ?
topAmount - (-(elemImgBounds.height / 2)) :
-(elemImgBounds.height / 2);
adUnitImage.style.top = topAmount +'px';
}
/* eslint-enable no-magic-numbers */
}
});
};
/**
* @function isElementFullyVisible
* @summary check to see if ad is completely in view
* @returns {boolean} true if ad is completely visible, fasle otherwise
*/
const isElementFullyVisible = (elem) => {
const elBounds = elem.getBoundingClientRect();
return elBounds.bottom < parentWindow.innerHeight &&
elBounds.top > 0;
};
/**
* @function getCorrectDocument
* @param {string} the div containing the iframe
* @summary return the current document
* @returns {object} document
*/
const getCorrectDocument = () => {
if (typeof rp_elementMarker !== 'undefined') {
if (document.querySelector(rp_elementMarker)) {
return window.document;
} else if (parent.window.document.querySelector(rp_elementMarker)) {
return parent.window.document;
} else {
console.error(`RP:Could not access the parent window.
Possibly due to cross domain iframe`);
}
} else {
console.error('RP: No element marker defined');
}
};
/**
* @function inIframe
* @summary test to see if the script is loaded inside an iframe
* @returns {boolean}
*/
const inIframe = () => {
return (window.top !== window.self);
};
};
window.interscroller = new interScroller();