forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbeopinion.js
124 lines (111 loc) · 3.58 KB
/
beopinion.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
/**
* 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 {loadScript} from './3p';
import {setStyles} from '../src/style';
/**
* Produces the Twitter API object for the passed in callback. If the current
* frame is the master frame it makes a new one by injecting the respective
* script, otherwise it schedules the callback for the script from the master
* window.
* @param {!Window} global
*/
function getBeOpinion(global) {
loadScript(global, 'https://widget.beopinion.com/sdk.js', function () {});
}
/**
* @param {!Window} global
* @description Make canonicalUrl available from iframe
*/
function addCanonicalLinkTag(global) {
const {canonicalUrl} = global.context;
if (canonicalUrl) {
const link = global.document.createElement('link');
link.setAttribute('rel', 'canonical');
link.setAttribute('href', canonicalUrl);
global.document.head.appendChild(link);
}
}
/**
* @param {!Window} global
* @param {!Object} data
* @return {?Node}
*/
function createContainer(global, data) {
// add canonical link tag
addCanonicalLinkTag(global);
// create div
const container = global.document.createElement('container');
container.className = 'BeOpinionWidget';
// get content id
if (data['content'] !== null) {
container.setAttribute('data-content', data['content']);
}
// get my-content value, forcing it to '1' if it is not an amp-ad
if (global.context.tagName === 'AMP-BEOPINION') {
container.setAttribute('data-my-content', '1');
} else if (data['my-content'] !== null) {
container.setAttribute('data-my-content', data['my-content']);
}
// get slot name
if (data['name'] !== null) {
container.setAttribute('data-name', data['name']);
}
setStyles(container, {
width: '100%',
display: 'block',
});
return container;
}
/**
* @param {*} global
* @param {*} accountId
* @return {!Function}
*/
function getBeOpinionAsyncInit(global, accountId) {
const {context} = global;
return function () {
context.onResizeDenied(function (requestedHeight, requestedWidth) {
context.requestResize(requestedWidth, requestedHeight);
});
global.BeOpinionSDK.init({
account: accountId,
onContentReceive: function (hasContent) {
if (hasContent) {
context.renderStart();
} else {
context.noContentAvailable();
}
},
onHeightChange: function (newHeight) {
const c = global.document.getElementById('c');
const boundingClientRect = c./*REVIEW*/ getBoundingClientRect();
context.requestResize(boundingClientRect.width, newHeight);
},
});
global.BeOpinionSDK['watch'](); // global.BeOpinionSDK.watch() fails 'gulp check-types' validation
};
}
/**
* @param {!Window} global
* @param {!Object} data
*/
export function beopinion(global, data) {
const container = createContainer(global, data);
const c = global.document.getElementById('c');
c.appendChild(container);
global.beOpinionAsyncInit = getBeOpinionAsyncInit(global, data.account);
getBeOpinion(global);
}