-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
abstract-input.ts
332 lines (271 loc) · 9.47 KB
/
abstract-input.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
322
323
324
325
326
327
328
329
330
331
332
import { tracked } from '@ember/-internals/metal';
import { TargetActionSupport } from '@ember/-internals/runtime';
import { TextSupport } from '@ember/-internals/views';
import { EMBER_MODERNIZED_BUILT_IN_COMPONENTS } from '@ember/canary-features';
import { assert, deprecate } from '@ember/debug';
import { JQUERY_INTEGRATION, SEND_ACTION } from '@ember/deprecated-features';
import { action } from '@ember/object';
import { isConstRef, isUpdatableRef, Reference, updateRef, valueForRef } from '@glimmer/reference';
import Component from '../component';
import InternalComponent, {
DeprecatingInternalComponent,
EventListener,
handleDeprecatedArguments,
handleDeprecatedAttributeArguments,
handleDeprecatedEventArguments,
InternalComponentConstructor,
jQueryEventShim,
ObjectEntries,
ObjectValues,
} from './internal';
const UNINITIALIZED: unknown = Object.freeze({});
type VirtualEventListener = (value: string, event: Event) => void;
function elementForEvent(event: Event): HTMLInputElement | HTMLTextAreaElement {
assert(
'[BUG] event target must be an <input> or <textarea> element',
event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement
);
return event.target;
}
function valueForEvent(event: Event): string {
return elementForEvent(event).value;
}
function devirtualize(callback: VirtualEventListener): EventListener {
return (event: Event) => callback(valueForEvent(event), event);
}
export function valueFrom(reference?: Reference<unknown>): Value {
if (reference === undefined) {
return new LocalValue(undefined);
} else if (isConstRef(reference)) {
return new LocalValue(valueForRef(reference));
} else if (isUpdatableRef(reference)) {
return new UpstreamValue(reference);
} else {
return new ForkedValue(reference);
}
}
interface Value {
get(): unknown;
set(value: unknown): void;
}
class LocalValue implements Value {
@tracked private value: unknown;
constructor(value: unknown) {
this.value = value;
}
get(): unknown {
return this.value;
}
set(value: unknown): void {
this.value = value;
}
}
class UpstreamValue implements Value {
constructor(private reference: Reference<unknown>) {}
get(): unknown {
return valueForRef(this.reference);
}
set(value: unknown): void {
updateRef(this.reference, value);
}
}
class ForkedValue implements Value {
private local?: Value;
private upstream: Value;
private lastUpstreamValue = UNINITIALIZED;
constructor(reference: Reference<unknown>) {
this.upstream = new UpstreamValue(reference);
}
get(): unknown {
let upstreamValue = this.upstream.get();
if (upstreamValue !== this.lastUpstreamValue) {
this.lastUpstreamValue = upstreamValue;
this.local = new LocalValue(upstreamValue);
}
assert('[BUG] this.local must have been initialized at this point', this.local);
return this.local.get();
}
set(value: unknown): void {
assert('[BUG] this.local must have been initialized at this point', this.local);
this.local.set(value);
}
}
export default abstract class AbstractInput
extends InternalComponent
implements DeprecatingInternalComponent {
modernized = this.shouldModernize();
validateArguments(): void {
assert(
`The ${this.constructor} component does not take any positional arguments`,
this.args.positional.length === 0
);
super.validateArguments();
}
protected shouldModernize(): boolean {
return (
Boolean(EMBER_MODERNIZED_BUILT_IN_COMPONENTS) &&
Component._wasReopened === false &&
TextSupport._wasReopened === false &&
TargetActionSupport._wasReopened === false
);
}
private _value = valueFrom(this.args.named.value);
get value(): unknown {
return this._value.get();
}
set value(value: unknown) {
this._value.set(value);
}
@action valueDidChange(event: Event): void {
this.value = valueForEvent(event);
}
/**
* The `change` and `input` actions need to be overridden in the `Input`
* subclass. Unfortunately, some ember-source builds currently uses babel
* loose mode to transpile its classes. Having the `@action` decorator on the
* super class creates a getter on the prototype, and when the subclass
* overrides the method, the loose mode transpilation would emit something
* like `Subclass.prototype['change'] = function change() { ... }`, which
* fails because `prototype['change']` is getter-only/readonly. The correct
* solution is to use `Object.defineProperty(prototype, 'change', ...)` but
* that requires disabling loose mode. For now, the workaround is to add the
* decorator only on the subclass. This is more of a configuration issue on
* our own builds and doesn't really affect apps.
*/
/* @action */ change(event: Event): void {
this.valueDidChange(event);
}
/* @action */ input(event: Event): void {
this.valueDidChange(event);
}
@action keyUp(event: KeyboardEvent): void {
switch (event.key) {
case 'Enter':
this.listenerFor('enter')(event);
this.listenerFor('insert-newline')(event);
break;
case 'Escape':
this.listenerFor('escape-press')(event);
break;
}
}
protected listenerFor(name: string): EventListener {
let listener = super.listenerFor(name);
if (this.isVirtualEventListener(name, listener)) {
return devirtualize(listener);
} else {
return listener;
}
}
protected isVirtualEventListener(
name: string,
_listener: Function
): _listener is VirtualEventListener {
let virtualEvents = ['enter', 'insert-newline', 'escape-press'];
return virtualEvents.indexOf(name) !== -1;
}
}
export function handleDeprecatedFeatures(
target: InternalComponentConstructor<AbstractInput>,
attributeBindings: Array<string | [attribute: string, argument: string]>
): void {
if (SEND_ACTION) {
let angle = target.toString();
let { prototype } = target;
interface View {
send(action: string, ...args: unknown[]): void;
}
let isView = (target: {}): target is View => {
return typeof (target as Partial<View>).send === 'function';
};
let superListenerFor = prototype['listenerFor'];
Object.defineProperty(prototype, 'listenerFor', {
configurable: true,
enumerable: false,
value: function listenerFor(this: AbstractInput, name: string): EventListener {
const actionName = this.named(name);
if (typeof actionName === 'string') {
deprecate(
`Passing actions to components as strings (like \`<${angle} @${name}="${actionName}" />\`) is deprecated. ` +
`Please use closure actions instead (\`<${angle} @${name}={{action "${actionName}"}} />\`).`,
false,
{
id: 'ember-component.send-action',
for: 'ember-source',
since: {},
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-component-send-action',
}
);
const { caller } = this;
assert('[BUG] missing caller', caller && typeof caller === 'object');
let listener: Function;
if (isView(caller)) {
listener = (...args: unknown[]) => caller.send(actionName, ...args);
} else {
assert(
`The action '${actionName}' did not exist on ${caller}`,
typeof caller[actionName] === 'function'
);
listener = caller[actionName];
}
let deprecatedListener = (...args: unknown[]) => {
deprecate(
`Passing actions to components as strings (like \`<${angle} @${name}="${actionName}" />\`) is deprecated. ` +
`Please use closure actions instead (\`<${angle} @${name}={{action "${actionName}"}} />\`).`,
false,
{
id: 'ember-component.send-action',
for: 'ember-source',
since: {},
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-component-send-action',
}
);
return listener(...args);
};
if (this.isVirtualEventListener(name, deprecatedListener)) {
return devirtualize(deprecatedListener);
} else {
return deprecatedListener as EventListener;
}
} else {
return superListenerFor.call(this, name);
}
},
});
}
if (EMBER_MODERNIZED_BUILT_IN_COMPONENTS) {
let { prototype } = target;
let virtualEvents = {
focusin: 'focus-in',
focusout: 'focus-out',
keypress: 'key-press',
keyup: 'key-up',
keydown: 'key-down',
};
handleDeprecatedArguments(target);
handleDeprecatedAttributeArguments(target, attributeBindings);
handleDeprecatedEventArguments(target, ObjectEntries(virtualEvents));
{
let superIsVirtualEventListener = prototype['isVirtualEventListener'];
Object.defineProperty(prototype, 'isVirtualEventListener', {
configurable: true,
enumerable: false,
value: function isVirtualEventListener(
this: AbstractInput,
name: string,
listener: Function
): listener is VirtualEventListener {
return (
ObjectValues(virtualEvents).indexOf(name) !== -1 ||
superIsVirtualEventListener.call(this, name, listener)
);
},
});
}
}
if (JQUERY_INTEGRATION) {
jQueryEventShim(target);
}
}