forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamp-action-macro.js
More file actions
128 lines (114 loc) · 3.5 KB
/
amp-action-macro.js
File metadata and controls
128 lines (114 loc) · 3.5 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
/**
* 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 {LayoutPriority} from '../../../src/layout';
import {Services} from '../../../src/services';
import {userAssert} from '../../../src/log';
/** @const {string} */
const TAG = 'amp-action-macro';
/**
* The <amp-action-macro> element is used to define a reusable action.
*/
export class AmpActionMacro extends AMP.BaseElement {
/** @param {!AmpElement} element */
constructor(element) {
super(element);
/** @private {?../../../src/service/action-impl.ActionService} */
this.actions_ = null;
/** @private {!Array<string>} */
this.arguments_ = [];
}
/** @override */
buildCallback() {
userAssert(
/* isExperimentOn(this.win, 'amp-action-macro') // launched: true */
true,
'Experiment is off'
);
const {element} = this;
this.actions_ = Services.actionServiceForDoc(element);
const argVarNames = element.getAttribute('arguments');
if (argVarNames) {
this.arguments_ = argVarNames.split(',').map((s) => s.trim());
}
this.registerAction('execute', this.execute_.bind(this));
}
/** @override */
getLayoutPriority() {
// Loads after other content.
return LayoutPriority.METADATA;
}
/**
* Invoke the action defined on the macro.
* @param {!../../../src/service/action-impl.ActionInvocation} invocation
* @private
*/
execute_(invocation) {
const {actionEventType, args, event, trust} = invocation;
if (args && this.arguments_.length > 0) {
// Verify that the argument variable names defined on the macro are used
// in the caller invocation.
for (const arg in args) {
userAssert(
this.arguments_.includes(arg),
'Variable argument name "%s" is not defined in %s',
arg,
this.element
);
}
}
if (invocation.caller.tagName.toLowerCase() === TAG) {
userAssert(
this.isValidMacroReference_(invocation.caller),
'Action macro with ID "%s" cannot reference itself or macros defined ' +
'after it',
this.element.getAttribute('id')
);
}
// Trigger the macro's action.
this.actions_.trigger(
this.element,
`${actionEventType}`,
event,
trust,
args
);
}
/** @override */
renderOutsideViewport() {
// We want the macro to be available wherever it is in the document.
return true;
}
/** @override */
isLayoutSupported(unusedLayout) {
return true;
}
/**
* Checks if the invoking element is defined after the action being invoked.
* This constraint is to prevent possible recursive calls.
* @param {!Element} invokingElement
* @return {boolean}
* @private
*/
isValidMacroReference_(invokingElement) {
return !!(
this.element.compareDocumentPosition(invokingElement) &
Node.DOCUMENT_POSITION_FOLLOWING
);
}
}
AMP.extension(TAG, '0.1', (AMP) => {
AMP.registerElement(TAG, AmpActionMacro);
});