-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathaction.js
245 lines (203 loc) · 6.2 KB
/
action.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
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
import { uuid } from 'ember-utils';
import { assert, run, flaggedInstrument } from 'ember-metal';
import {
isSimpleClick,
ActionManager
} from 'ember-views';
import { INVOKE } from '../helpers/action';
const MODIFIERS = ['alt', 'shift', 'meta', 'ctrl'];
const POINTER_EVENT_TYPE_REGEX = /^click|mouse|touch/;
function isAllowedEvent(event, allowedKeys) {
if (allowedKeys === null || typeof allowedKeys === 'undefined') {
if (POINTER_EVENT_TYPE_REGEX.test(event.type)) {
return isSimpleClick(event);
} else {
allowedKeys = '';
}
}
if (allowedKeys.indexOf('any') >= 0) {
return true;
}
for (let i = 0; i < MODIFIERS.length; i++) {
if (event[MODIFIERS[i] + 'Key'] && allowedKeys.indexOf(MODIFIERS[i]) === -1) {
return false;
}
}
return true;
}
export let ActionHelper = {
// registeredActions is re-exported for compatibility with older plugins
// that were using this undocumented API.
registeredActions: ActionManager.registeredActions,
registerAction(actionState) {
let { actionId } = actionState;
let actions = ActionManager.registeredActions[actionId];
if (!actions) {
actions = ActionManager.registeredActions[actionId] = [];
}
actions.push(actionState);
return actionId;
},
unregisterAction(actionState) {
let { actionId } = actionState;
let actions = ActionManager.registeredActions[actionId];
if (!actions) {
return;
}
let index = actions.indexOf(actionState);
if (index !== -1) {
actions.splice(index, 1);
}
if (actions.length === 0) {
delete ActionManager.registeredActions[actionId];
}
}
};
export class ActionState {
constructor(element, actionId, actionName, actionArgs, namedArgs, positionalArgs, implicitTarget, dom) {
this.element = element;
this.actionId = actionId;
this.actionName = actionName;
this.actionArgs = actionArgs;
this.namedArgs = namedArgs;
this.positional = positionalArgs;
this.implicitTarget = implicitTarget;
this.dom = dom;
this.eventName = this.getEventName();
}
getEventName() {
return this.namedArgs.get('on').value() || 'click';
}
getActionArgs() {
let result = new Array(this.actionArgs.length);
for (let i = 0; i < this.actionArgs.length; i++) {
result[i] = this.actionArgs[i].value();
}
return result;
}
getTarget() {
let { implicitTarget, namedArgs } = this;
let target;
if (namedArgs.has('target')) {
target = namedArgs.get('target').value();
} else {
target = implicitTarget.value();
}
return target;
}
handler(event) {
let { actionName, namedArgs } = this;
let bubbles = namedArgs.get('bubbles');
let preventDefault = namedArgs.get('preventDefault');
let allowedKeys = namedArgs.get('allowedKeys');
let target = this.getTarget();
if (!isAllowedEvent(event, allowedKeys.value())) {
return true;
}
if (preventDefault.value() !== false) {
event.preventDefault();
}
if (bubbles.value() === false) {
event.stopPropagation();
}
run(() => {
let args = this.getActionArgs();
let payload = {
args,
target
};
if (typeof actionName[INVOKE] === 'function') {
flaggedInstrument('interaction.ember-action', payload, () => {
actionName[INVOKE].apply(actionName, args);
});
return;
}
if (typeof actionName === 'function') {
flaggedInstrument('interaction.ember-action', payload, () => {
actionName.apply(target, args);
});
return;
}
payload.name = actionName;
if (target.send) {
flaggedInstrument('interaction.ember-action', payload, () => {
target.send.apply(target, [actionName, ...args]);
});
} else {
assert(
`The action '${actionName}' did not exist on ${target}`,
typeof target[actionName] === 'function'
);
flaggedInstrument('interaction.ember-action', payload, () => {
target[actionName].apply(target, args);
});
}
});
}
destroy() {
ActionHelper.unregisterAction(this);
}
}
// implements ModifierManager<Action>
export default class ActionModifierManager {
create(element, args, dynamicScope, dom) {
let { named, positional } = args;
let implicitTarget;
let actionName;
let actionNameRef;
if (positional.length > 1) {
implicitTarget = positional.at(0);
actionNameRef = positional.at(1);
if (actionNameRef[INVOKE]) {
actionName = actionNameRef;
} else {
let actionLabel = actionNameRef._propertyKey;
actionName = actionNameRef.value();
assert(
'You specified a quoteless path, `' + actionLabel + '`, to the ' +
'{{action}} helper which did not resolve to an action name (a ' +
'string). Perhaps you meant to use a quoted actionName? (e.g. ' +
'{{action "' + actionLabel + '"}}).',
typeof actionName === 'string' || typeof actionName === 'function'
);
}
}
let actionArgs = [];
// The first two arguments are (1) `this` and (2) the action name.
// Everything else is a param.
for (let i = 2; i < positional.length; i++) {
actionArgs.push(positional.at(i));
}
let actionId = uuid();
return new ActionState(
element,
actionId,
actionName,
actionArgs,
named,
positional,
implicitTarget,
dom
);
}
install(actionState) {
let { dom, element, actionId } = actionState;
ActionHelper.registerAction(actionState);
dom.setAttribute(element, 'data-ember-action', '');
dom.setAttribute(element, `data-ember-action-${actionId}`, actionId);
}
update(actionState) {
let { positional } = actionState;
let actionNameRef = positional.at(1);
if (!actionNameRef[INVOKE]) {
actionState.actionName = actionNameRef.value();
}
actionState.eventName = actionState.getEventName();
// Not sure if this is needed? If we mutate the actionState is that good enough?
ActionHelper.unregisterAction(actionState);
ActionHelper.registerAction(actionState);
}
getDestructor(modifier) {
return modifier;
}
}