forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadplugg.js
76 lines (67 loc) · 2.28 KB
/
adplugg.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
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {hasOwn} from '../src/utils/object';
import {loadScript, validateData} from '../3p/3p';
/**
* Make an AdPlugg iframe.
* @param {!Window} global
* @param {!Object} data
*/
export function adplugg(global, data) {
// Load ad.js
loadScript(global, 'https://www.adplugg.com/serve/js/ad.js');
// Validate the amp-ad attributes.
validateData(
data,
['accessCode', 'width', 'height'], //required
['zone'] //optional
);
// Get the amp wrapper element.
const ampwrapper = global.document.getElementById('c');
// Build and append the ad tag.
const adTag = global.document.createElement('div');
adTag.setAttribute('class', 'adplugg-tag');
adTag.setAttribute('data-adplugg-access-code', data['accessCode']);
if (data['zone']) {
adTag.setAttribute('data-adplugg-zone', data['zone']);
}
ampwrapper.appendChild(adTag);
// Get a handle on the AdPlugg SDK.
global.AdPlugg = global.AdPlugg || [];
const {AdPlugg} = global;
// Register event listeners (via async wrapper).
AdPlugg.push(function () {
const {AdPlugg} = global;
// Register the renderStart event listener.
AdPlugg.on(adTag, 'adplugg:renderStart', function (event) {
// Create the opt_data object.
const optData = {};
if (hasOwn(event, 'width')) {
optData.width = event.width;
}
if (hasOwn(event, 'height')) {
optData.height = event.height;
}
global.context.renderStart(optData);
});
// Register the noContentAvailable event listener.
AdPlugg.on(adTag, 'adplugg:noContentAvailable', function () {
global.context.noContentAvailable();
});
});
// Fill the tag.
AdPlugg.push({'command': 'run'});
}