-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathaction.ts
321 lines (268 loc) · 9.23 KB
/
action.ts
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
import { Owner } from '@ember/-internals/owner';
import { uuid } from '@ember/-internals/utils';
import { ActionManager, EventDispatcher, isSimpleClick } from '@ember/-internals/views';
import { assert, deprecate } from '@ember/debug';
import { flaggedInstrument } from '@ember/instrumentation';
import { join } from '@ember/runloop';
import { registerDestructor } from '@glimmer/destroyable';
import { DEBUG } from '@glimmer/env';
import {
CapturedArguments,
CapturedNamedArguments,
CapturedPositionalArguments,
InternalModifierManager,
} from '@glimmer/interfaces';
import { setInternalModifierManager } from '@glimmer/manager';
import { isInvokableRef, updateRef, valueForRef } from '@glimmer/reference';
import { createUpdatableTag, UpdatableTag } from '@glimmer/validator';
import { SimpleElement } from '@simple-dom/interface';
import { INVOKE } from '../helpers/action';
const MODIFIERS = ['alt', 'shift', 'meta', 'ctrl'];
const POINTER_EVENT_TYPE_REGEX = /^click|mouse|touch/;
function isAllowedEvent(event: Event, allowedKeys: any) {
if (allowedKeys === null || 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: ActionState) {
let { actionId } = actionState;
ActionManager.registeredActions[actionId] = actionState;
return actionId;
},
unregisterAction(actionState: ActionState) {
let { actionId } = actionState;
delete ActionManager.registeredActions[actionId];
},
};
export class ActionState {
public element: SimpleElement;
public owner: Owner;
public actionId: number;
public actionName: any;
public actionArgs: any;
public namedArgs: CapturedNamedArguments;
public positional: CapturedPositionalArguments;
public implicitTarget: any;
public eventName: any;
public tag = createUpdatableTag();
constructor(
element: SimpleElement,
owner: Owner,
actionId: number,
actionArgs: any[],
namedArgs: CapturedNamedArguments,
positionalArgs: CapturedPositionalArguments
) {
this.element = element;
this.owner = owner;
this.actionId = actionId;
this.actionArgs = actionArgs;
this.namedArgs = namedArgs;
this.positional = positionalArgs;
this.eventName = this.getEventName();
registerDestructor(this, () => ActionHelper.unregisterAction(this));
}
getEventName() {
let { on } = this.namedArgs;
return on !== undefined ? valueForRef(on) : 'click';
}
getActionArgs() {
let result = new Array(this.actionArgs.length);
for (let i = 0; i < this.actionArgs.length; i++) {
result[i] = valueForRef(this.actionArgs[i]);
}
return result;
}
getTarget(): any {
let { implicitTarget, namedArgs } = this;
let { target } = namedArgs;
return target !== undefined ? valueForRef(target) : valueForRef(implicitTarget);
}
handler(event: Event): boolean {
let { actionName, namedArgs } = this;
let { bubbles, preventDefault, allowedKeys } = namedArgs;
let bubblesVal = bubbles !== undefined ? valueForRef(bubbles) : undefined;
let preventDefaultVal = preventDefault !== undefined ? valueForRef(preventDefault) : undefined;
let allowedKeysVal = allowedKeys !== undefined ? valueForRef(allowedKeys) : undefined;
let target = this.getTarget();
let shouldBubble = bubblesVal !== false;
if (!isAllowedEvent(event, allowedKeysVal)) {
return true;
}
if (preventDefaultVal !== false) {
event.preventDefault();
}
if (!shouldBubble) {
event.stopPropagation();
}
join(() => {
let args = this.getActionArgs();
let payload = {
args,
target,
name: null,
};
if (typeof actionName[INVOKE] === 'function') {
deprecate(
`Usage of the private INVOKE API to make an object callable via action or fn is no longer supported. Please update to pass in a callback function instead. Received: ${String(
actionName
)}`,
false,
{
until: '3.25.0',
id: 'actions.custom-invoke-invokable',
for: 'ember-source',
since: {
enabled: '3.23.0-beta.1',
},
}
);
flaggedInstrument('interaction.ember-action', payload, () => {
actionName[INVOKE].apply(actionName, args);
});
return;
}
if (isInvokableRef(actionName)) {
flaggedInstrument('interaction.ember-action', payload, () => {
updateRef(actionName, args[0]);
});
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);
});
}
});
return shouldBubble;
}
}
class ActionModifierManager implements InternalModifierManager<ActionState, object> {
create(
owner: Owner,
element: SimpleElement,
_state: object,
{ named, positional }: CapturedArguments
): ActionState {
let actionArgs: any[] = [];
// 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[i]);
}
let actionId = uuid();
let actionState = new ActionState(element, owner, actionId, actionArgs, named, positional);
deprecate(
`Using the \`{{action}}\` modifier with \`${actionState.eventName}\` events has been deprecated.`,
actionState.eventName !== 'mouseEnter' &&
actionState.eventName !== 'mouseLeave' &&
actionState.eventName !== 'mouseMove',
{
id: 'ember-views.event-dispatcher.mouseenter-leave-move',
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x#toc_action-mouseenter-leave-move',
for: 'ember-source',
since: {
enabled: '3.13.0-beta.1',
},
}
);
return actionState;
}
getDebugName(): string {
return 'action';
}
install(actionState: ActionState): void {
let { element, actionId, positional } = actionState;
let actionName;
let actionNameRef: any;
let implicitTarget;
if (positional.length > 1) {
implicitTarget = positional[0];
actionNameRef = positional[1];
if (isInvokableRef(actionNameRef)) {
actionName = actionNameRef;
} else {
actionName = valueForRef(actionNameRef);
if (DEBUG) {
let actionPath = actionNameRef.debugLabel;
let actionPathParts = actionPath.split('.');
let actionLabel = actionPathParts[actionPathParts.length - 1];
assert(
'You specified a quoteless path, `' +
actionPath +
'`, 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'
);
}
}
}
actionState.actionName = actionName;
actionState.implicitTarget = implicitTarget;
this.ensureEventSetup(actionState);
ActionHelper.registerAction(actionState);
element.setAttribute('data-ember-action', '');
element.setAttribute(`data-ember-action-${actionId}`, String(actionId));
}
update(actionState: ActionState): void {
let { positional } = actionState;
let actionNameRef = positional[1];
if (!isInvokableRef(actionNameRef)) {
actionState.actionName = valueForRef(actionNameRef);
}
let newEventName = actionState.getEventName();
if (newEventName !== actionState.eventName) {
this.ensureEventSetup(actionState);
actionState.eventName = actionState.getEventName();
}
}
ensureEventSetup(actionState: ActionState): void {
let dispatcher = actionState.owner.lookup<EventDispatcher>('event_dispatcher:main');
dispatcher?.setupHandlerForEmberEvent(actionState.eventName);
}
getTag(actionState: ActionState): UpdatableTag {
return actionState.tag;
}
getDestroyable(actionState: ActionState): object {
return actionState;
}
}
const ACTION_MODIFIER_MANAGER = new ActionModifierManager();
export default setInternalModifierManager(ACTION_MODIFIER_MANAGER, {});