forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamp-story-access.js
More file actions
333 lines (291 loc) · 9.18 KB
/
amp-story-access.js
File metadata and controls
333 lines (291 loc) · 9.18 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* 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 {
Action,
StateProperty,
getStoreService,
} from './amp-story-store-service';
import {Layout} from '../../../src/layout';
import {assertHttpsUrl} from '../../../src/url';
import {
closest,
closestAncestorElementBySelector,
copyChildren,
removeChildren,
} from '../../../src/dom';
import {dev, user} from '../../../src/log';
import {htmlFor} from '../../../src/static-template';
import {isArray, isObject} from '../../../src/types';
import {parseJson} from '../../../src/json';
import {setImportantStyles} from '../../../src/style';
/** @const {string} */
const TAG = 'amp-story-access';
/**
* @enum {string}
*/
export const Type = {
BLOCKING: 'blocking',
NOTIFICATION: 'notification',
};
/**
* Story access blocking type template.
* @param {!Element} element
* @return {!Element}
*/
const getBlockingTemplate = (element) => {
return htmlFor(element)`
<div class="i-amphtml-story-access-overflow">
<div class="i-amphtml-story-access-container">
<div class="i-amphtml-story-access-header">
<div class="i-amphtml-story-access-logo"></div>
</div>
<div class="i-amphtml-story-access-content"></div>
</div>
</div>`;
};
/**
* Story access notification type template.
* @param {!Element} element
* @return {!Element}
*/
const getNotificationTemplate = (element) => {
return htmlFor(element)`
<div class="i-amphtml-story-access-overflow">
<div class="i-amphtml-story-access-container">
<div class="i-amphtml-story-access-content">
<span class="i-amphtml-story-access-close-button" role="button">
×
</span>
</div>
</div>
</div>`;
};
/**
* The <amp-story-access> custom element.
*/
export class AmpStoryAccess extends AMP.BaseElement {
/** @param {!AmpElement} element */
constructor(element) {
super(element);
/** @private {?Element} */
this.containerEl_ = null;
/** @private @const {!./amp-story-store-service.AmpStoryStoreService} */
this.storeService_ = getStoreService(this.win);
}
/** @override */
buildCallback() {
// Defaults to blocking paywall.
if (!this.element.hasAttribute('type')) {
this.element.setAttribute('type', Type.BLOCKING);
}
const drawerEl = this.renderDrawerEl_();
this.containerEl_ = dev().assertElement(
drawerEl.querySelector('.i-amphtml-story-access-container')
);
const contentEl = dev().assertElement(
drawerEl.querySelector('.i-amphtml-story-access-content')
);
copyChildren(this.element, contentEl);
removeChildren(this.element);
this.element.appendChild(drawerEl);
this.allowlistActions_();
this.initializeListeners_();
}
/** @override */
isLayoutSupported(layout) {
return layout == Layout.CONTAINER;
}
/**
* @private
*/
initializeListeners_() {
this.storeService_.subscribe(StateProperty.ACCESS_STATE, (isAccess) => {
this.onAccessStateChange_(isAccess);
});
this.storeService_.subscribe(
StateProperty.CURRENT_PAGE_INDEX,
(currentPageIndex) => {
this.onCurrentPageIndexChange_(currentPageIndex);
},
true /** callToInitialize */
);
this.element.addEventListener('click', (event) => this.onClick_(event));
}
/**
* Reacts to access state updates, and shows/hides the UI accordingly.
* @param {boolean} isAccess
* @private
*/
onAccessStateChange_(isAccess) {
if (this.getType_() === Type.BLOCKING) {
this.toggle_(isAccess);
}
}
/**
* Reacts to story active page index update, and maybe display the
* "notification" story-access.
* @param {number} currentPageIndex
*/
onCurrentPageIndexChange_(currentPageIndex) {
if (this.getType_() === Type.NOTIFICATION) {
// Only show the notification if on the first page of the story.
// Note: this can be overriden by an amp-access attribute that might
// show/hide the notification based on the user's authorizations.
this.toggle_(currentPageIndex === 0);
}
}
/**
* Handles click events and maybe closes the paywall.
* @param {!Event} event
* @return {*} TODO(#23582): Specify return type
* @private
*/
onClick_(event) {
const el = dev().assertElement(event.target);
if (el.classList.contains('i-amphtml-story-access-close-button')) {
return this.toggle_(false);
}
// Closes the menu if click happened outside of the main container.
if (!closest(el, (el) => el === this.containerEl_, this.element)) {
this.storeService_.dispatch(Action.TOGGLE_ACCESS, false);
}
}
/**
* @param {boolean} show
* @private
*/
toggle_(show) {
this.mutateElement(() => {
this.element.classList.toggle('i-amphtml-story-access-visible', show);
});
}
/**
* Returns the element's type.
* @return {string}
* @private
*/
getType_() {
return this.element.getAttribute('type').toLowerCase();
}
/**
* Renders and returns an empty drawer element element that will contain the
* publisher provided DOM, depending on the type of <amp-story-access>.
* Blocking template gets a header containing the publisher's logo, and
* notification template gets a "dismiss" button.
* @return {!Element|undefined}
* @private
*/
renderDrawerEl_() {
switch (this.getType_()) {
case Type.BLOCKING:
const drawerEl = getBlockingTemplate(this.element);
const logoSrc = this.getLogoSrc_();
if (logoSrc) {
const logoEl = dev().assertElement(
drawerEl.querySelector('.i-amphtml-story-access-logo')
);
setImportantStyles(logoEl, {'background-image': `url(${logoSrc})`});
}
return drawerEl;
break;
case Type.NOTIFICATION:
return getNotificationTemplate(this.element);
break;
default:
user().error(
TAG,
'Unknown "type" attribute, expected one of: ' +
'blocking, notification.'
);
}
}
/**
* Retrieves the publisher-logo-src set on the <amp-story> element, and
* validates it's a valid https or relative URL.
* @return {?string}
* @private
*/
getLogoSrc_() {
const storyEl = dev().assertElement(
closestAncestorElementBySelector(this.element, 'AMP-STORY')
);
const logoSrc = storyEl && storyEl.getAttribute('publisher-logo-src');
if (logoSrc) {
assertHttpsUrl(logoSrc, storyEl, 'publisher-logo-src');
} else {
user().warn(
TAG,
'Expected "publisher-logo-src" attribute on <amp-story>'
);
}
return logoSrc;
}
/**
* Allowlists the <amp-access> actions.
* Depending on the publisher configuration, actions can be:
* - login
* - login-<namespace>
* - login-<namespace>-<type>
*
* Publishers can provide one (object) or multiple (array) configurations,
* identified by their "namespace" property.
* Each configuration can have one or multiple login URLs, called "type".
* All the namespace/type pairs have to be allowlisted.
* @private
*/
allowlistActions_() {
const accessEl = dev().assertElement(
this.win.document.getElementById('amp-access'),
'Cannot find the amp-access configuration'
);
// Configuration validation is handled by the amp-access extension.
let accessConfig = /** @type {!Array|!Object} */ (parseJson(
accessEl.textContent
));
if (!isArray(accessConfig)) {
accessConfig = [accessConfig];
// If there is only one configuration and the publisher provided a
// namespace, we want to allow actions with or without namespace.
if (accessConfig[0].namespace) {
accessConfig.push({...accessConfig[0], namespace: undefined});
}
}
const actions = [];
/** @type {!Array} */ (accessConfig).forEach((config) => {
const {login, namespace} = /** @type {{login, namespace}} */ (config);
if (isObject(login)) {
const types = Object.keys(login);
types.forEach((type) =>
actions.push(this.getActionObject_(namespace, type))
);
} else {
actions.push(this.getActionObject_(namespace));
}
});
this.storeService_.dispatch(Action.ADD_TO_ACTIONS_ALLOWLIST, actions);
}
/**
* Allowlists an action for the given namespace / type pair.
* @param {string=} namespace
* @param {string=} type
* @return {*} TODO(#23582): Specify return type
* @private
*/
getActionObject_(namespace = undefined, type = undefined) {
const method = ['login', namespace, type].filter((s) => !!s).join('-');
return {tagOrTarget: 'SCRIPT', method};
}
}