forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamp-story-cta-layer.js
More file actions
101 lines (93 loc) · 2.97 KB
/
amp-story-cta-layer.js
File metadata and controls
101 lines (93 loc) · 2.97 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
/**
* 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.
*/
/**
* @fileoverview This is a layer that allows a call to action in a story page.
* With this, a user could link to an external site from inside a story using
* the call to action layer, for example.
*
* Example:
* ...
* <amp-story-page>
* <amp-story-cta-layer>
* <a href="wwww.google.com"> Visit my site! </a>
* </amp-story-cta-layer>
* <amp-story-page>
* ...
*/
import {AmpStoryBaseLayer} from './amp-story-base-layer';
import {addAttributesToElement, matches, removeElement} from '../../../src/dom';
import {dict} from '../../../src/utils/object';
import {user} from '../../../src/log';
/**
* @type {string}
* @const
*/
const TAG = 'amp-story-cta-layer';
/**
* Call to action button layer template.
*
* No pre-rendering to let more computing-intensive elements (like
* videos) get pre-rendered first. Since this layer will not contain
* computing-intensive resources such as videos, we can just risk rendering
* while the user is looking.
*/
export class AmpStoryCtaLayer extends AmpStoryBaseLayer {
/** @override */
buildCallback() {
super.buildCallback();
this.setOrOverwriteAttributes_();
this.checkAndRemoveLayerIfOnFirstPage_();
}
/**
* Overwrite or set target attributes that are cta-layer-specific.
* @private
*/
setOrOverwriteAttributes_() {
const ctaLinks = this.element.querySelectorAll('a');
for (let i = 0; i < ctaLinks.length; i++) {
addAttributesToElement(ctaLinks[i], dict({'target': '_blank'}));
if (!ctaLinks[i].getAttribute('role')) {
addAttributesToElement(ctaLinks[i], dict({'role': 'link'}));
}
}
const ctaButtons = this.element.querySelectorAll('button');
for (let i = 0; i < ctaButtons.length; i++) {
if (!ctaButtons[i].getAttribute('role')) {
addAttributesToElement(ctaButtons[i], dict({'role': 'button'}));
}
}
}
/**
* CTA links or buttons are not allowed on the first amp-story page. Remove
* the amp-story-cta-layer if it is found on the first page of the story.
* @private
*/
checkAndRemoveLayerIfOnFirstPage_() {
if (
matches(
this.element,
'amp-story-page:first-of-type > amp-story-cta-layer'
)
) {
removeElement(this.element);
user().error(
TAG,
'amp-story-cta-layer is not allowed on the first page' +
' of an amp-story.'
);
}
}
}