-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
link-to.ts
717 lines (599 loc) · 20.8 KB
/
link-to.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import { Owner } from '@ember/-internals/owner';
import { RouterState, RoutingService } from '@ember/-internals/routing';
import { QueryParam } from '@ember/-internals/routing/lib/system/router';
import { TargetActionSupport } from '@ember/-internals/runtime';
import { isSimpleClick } from '@ember/-internals/views';
import { EMBER_MODERNIZED_BUILT_IN_COMPONENTS } from '@ember/canary-features';
import { assert, debugFreeze, deprecate, warn } from '@ember/debug';
import { JQUERY_INTEGRATION } from '@ember/deprecated-features';
import { EngineInstance, getEngineParent } from '@ember/engine';
import { flaggedInstrument } from '@ember/instrumentation';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { DEBUG } from '@glimmer/env';
import { Maybe, Option } from '@glimmer/interfaces';
import { consumeTag, createCache, getValue, tagFor, untrack } from '@glimmer/validator';
import { Transition } from 'router_js';
import Component from '../component';
import LinkToTemplate from '../templates/link-to';
import LegacyLinkTo from './-link-to';
import InternalComponent, {
DeprecatingInternalComponent,
handleDeprecatedArguments,
handleDeprecatedAttributeArguments,
handleDeprecatedEventArguments,
jQueryEventShim,
opaquify,
} from './internal';
const EMPTY_ARRAY: {}[] = [];
const EMPTY_QUERY_PARAMS = {};
debugFreeze(EMPTY_ARRAY);
debugFreeze(EMPTY_QUERY_PARAMS);
function isMissing<T>(value: Maybe<T>): value is null | undefined {
return value === null || value === undefined;
}
function isPresent<T>(value: Maybe<T>): value is T {
return !isMissing(value);
}
interface QueryParams {
isQueryParams: true;
values: Option<{}>;
}
function isQueryParams(value: unknown): value is QueryParams {
return typeof value === 'object' && value !== null && value['isQueryParams'] === true;
}
class LinkTo extends InternalComponent implements DeprecatingInternalComponent {
static toString(): string {
return 'LinkTo';
}
modernized = this.shouldModernize();
@service('-routing') private declare routing: RoutingService;
validateArguments(): void {
assert(
'You attempted to use the <LinkTo> component within a routeless engine, this is not supported. ' +
'If you are using the ember-engines addon, use the <LinkToExternal> component instead. ' +
'See https://ember-engines.com/docs/links for more info.',
!this.modernized || !this.isEngine || this.engineMountPoint !== undefined
);
assert(
'You must provide at least one of the `@route`, `@model`, `@models` or `@query` argument to `<LinkTo>`.',
!this.modernized ||
'route' in this.args.named ||
'model' in this.args.named ||
'models' in this.args.named ||
'query' in this.args.named
);
assert(
'You cannot provide both the `@model` and `@models` arguments to the <LinkTo> component.',
!('model' in this.args.named && 'models' in this.args.named)
);
super.validateArguments();
}
get class(): string {
let classes = 'ember-view';
if (this.isActive) {
classes += this.classFor('active');
if (this.willBeActive === false) {
classes += ' ember-transitioning-out';
}
} else if (this.willBeActive) {
classes += ' ember-transitioning-in';
}
if (this.isLoading) {
classes += this.classFor('loading');
}
if (this.isDisabled) {
classes += this.classFor('disabled');
}
return classes;
}
get href() {
if (this.isLoading) {
return '#';
}
let { routing, route, models, query } = this;
assert('[BUG] route can only be missing if isLoading is true', isPresent(route));
// consume the current router state so we invalidate when QP changes
// TODO: can we narrow this down to QP changes only?
consumeTag(tagFor(routing, 'currentState'));
if (DEBUG) {
try {
return routing.generateURL(route, models, query);
} catch (e) {
// tslint:disable-next-line:max-line-length
e.message = `While generating link to route "${route}": ${e.message}`;
throw e;
}
} else {
return routing.generateURL(route, models, query);
}
}
@action click(event: Event): void {
if (!isSimpleClick(event)) {
return;
}
let element = event.currentTarget;
assert('[BUG] must be an <a> element', element instanceof HTMLAnchorElement);
let isSelf = element.target === '' || element.target === '_self';
if (isSelf) {
this.preventDefault(event);
} else {
return;
}
if (this.isDisabled) {
return;
}
if (this.isLoading) {
warn(
'This link is in an inactive loading state because at least one of its models ' +
'currently has a null/undefined value, or the provided route name is invalid.',
false,
{
id: 'ember-glimmer.link-to.inactive-loading-state',
}
);
return;
}
let { routing, route, models, query, replace } = this;
let payload = {
routeName: route,
queryParams: query,
transition: undefined as Transition | undefined,
};
flaggedInstrument('interaction.link-to', payload, () => {
assert('[BUG] route can only be missing if isLoading is true', isPresent(route));
// TODO: is the signature wrong? this.query is definitely NOT a QueryParam!
payload.transition = routing.transitionTo(route, models, query as QueryParam, replace);
});
}
private get route(): Maybe<string> {
if ('route' in this.args.named) {
let route = this.named('route');
assert(
'The `@route` argument to the <LinkTo> component must be a string',
isMissing(route) || typeof route === 'string'
);
return route && this.namespaceRoute(route);
} else {
return this.currentRoute;
}
}
// GH #17963
private currentRouteCache = createCache<Maybe<string>>(() => {
consumeTag(tagFor(this.routing, 'currentState'));
return untrack(() => this.routing.currentRouteName);
});
private get currentRoute(): Maybe<string> {
return getValue(this.currentRouteCache);
}
// TODO: not sure why generateURL takes {}[] instead of unknown[]
private get models(): {}[] {
if ('models' in this.args.named) {
let models = this.named('models');
assert(
'The `@models` argument to the <LinkTo> component must be an array.',
Array.isArray(models)
);
return models;
} else if ('model' in this.args.named) {
return [this.named('model') as {}];
} else {
return EMPTY_ARRAY;
}
}
// TODO: this should probably be Record<string, unknown> or something
private get query(): {} {
if ('query' in this.args.named) {
let query = this.named('query');
assert(
'The `@query` argument to the <LinkTo> component must be an object.',
query !== null && typeof query === 'object'
);
return { ...query };
} else {
return EMPTY_QUERY_PARAMS;
}
}
private get replace(): boolean {
return this.named('replace') === true;
}
private get isActive(): boolean {
return this.isActiveForState(this.routing.currentState as Maybe<RouterState>);
}
private get willBeActive(): Option<boolean> {
let current = this.routing.currentState as Maybe<RouterState>;
let target = this.routing.targetState as Maybe<RouterState>;
if (current === target) {
return null;
} else {
return this.isActiveForState(target);
}
}
private get isLoading(): boolean {
return isMissing(this.route) || this.models.some((model) => isMissing(model));
}
private get isDisabled(): boolean {
return Boolean(this.named('disabled'));
}
private get isEngine(): boolean {
return getEngineParent(this.owner as EngineInstance) !== undefined;
}
private get engineMountPoint(): string | undefined {
return (this.owner as Owner | EngineInstance).mountPoint;
}
private classFor(state: 'active' | 'loading' | 'disabled'): string {
let className = this.named(`${state}Class`);
assert(
`The \`@${state}Class\` argument to the <LinkTo> component must be a string or boolean`,
isMissing(className) || typeof className === 'string' || typeof className === 'boolean'
);
if (className === true || isMissing(className)) {
return ` ${state}`;
} else if (className) {
return ` ${className}`;
} else {
return '';
}
}
private namespaceRoute(route: string): string {
let { engineMountPoint } = this;
if (engineMountPoint === undefined) {
return route;
} else if (route === 'application') {
return engineMountPoint;
} else {
return `${engineMountPoint}.${route}`;
}
}
private isActiveForState(state: Maybe<RouterState>): boolean {
if (!isPresent(state)) {
return false;
}
if (this.isLoading) {
return false;
}
let currentWhen = this.named('current-when');
if (typeof currentWhen === 'boolean') {
return currentWhen;
} else if (typeof currentWhen === 'string') {
let { models, routing } = this;
return currentWhen
.split(' ')
.some((route) =>
routing.isActiveForRoute(models, undefined, this.namespaceRoute(route), state)
);
} else {
let { route, models, query, routing } = this;
assert('[BUG] route can only be missing if isLoading is true', isPresent(route));
// TODO: is the signature wrong? this.query is definitely NOT a QueryParam!
return routing.isActiveForRoute(models, query as QueryParam, route, state);
}
}
private preventDefault(event: Event): void {
event.preventDefault();
}
private shouldModernize(): boolean {
return (
Boolean(EMBER_MODERNIZED_BUILT_IN_COMPONENTS) &&
Component._wasReopened === false &&
TargetActionSupport._wasReopened === false &&
LegacyLinkTo._wasReopened === false
);
}
protected isSupportedArgument(name: string): boolean {
let supportedArguments = [
'route',
'model',
'models',
'query',
'replace',
'disabled',
'current-when',
'activeClass',
'loadingClass',
'disabledClass',
];
return supportedArguments.indexOf(name) !== -1 || super.isSupportedArgument(name);
}
}
// Deprecated features
if (EMBER_MODERNIZED_BUILT_IN_COMPONENTS) {
let { prototype } = LinkTo;
let descriptorFor = (target: object, property: string): Option<PropertyDescriptor> => {
if (target) {
return (
Object.getOwnPropertyDescriptor(target, property) ||
descriptorFor(Object.getPrototypeOf(target), property)
);
} else {
return null;
}
};
handleDeprecatedArguments(LinkTo);
handleDeprecatedAttributeArguments(LinkTo, [
// Component
'id',
['id', 'elementId'],
'class',
['class', 'classNames'],
['role', 'ariaRole'],
// LinkTo
'title',
'rel',
'tabindex',
'target',
]);
handleDeprecatedEventArguments(LinkTo);
// @href
{
let superOnUnsupportedArgument = prototype['onUnsupportedArgument'];
Object.defineProperty(prototype, 'onUnsupportedArgument', {
configurable: true,
enumerable: false,
value: function onUnsupportedArgument(this: LinkTo, name: string): void {
if (name === 'href') {
assert(`Passing the \`@href\` argument to <LinkTo> is not supported.`);
} else {
superOnUnsupportedArgument.call(this, name);
}
},
});
}
// @tagName
{
let superOnUnsupportedArgument = prototype['onUnsupportedArgument'];
Object.defineProperty(prototype, 'onUnsupportedArgument', {
configurable: true,
enumerable: false,
value: function onUnsupportedArgument(this: LinkTo, name: string): void {
if (name === 'tagName') {
let tagName = this.named('tagName');
deprecate(
`Passing the \`@tagName\` argument to <LinkTo> is deprecated. Using a <${tagName}> ` +
'element for navigation is not recommended as it creates issues with assistive ' +
'technologies. Remove this argument to use the default <a> element. In the rare ' +
'cases that calls for using a different element, refactor to use the router ' +
'service inside a custom event handler instead.',
false,
{
id: 'ember.link-to.tag-name',
for: 'ember-source',
since: {},
until: '4.0.0',
}
);
this.modernized = false;
} else {
superOnUnsupportedArgument.call(this, name);
}
},
});
}
// @bubbles & @preventDefault
{
let superIsSupportedArgument = prototype['isSupportedArgument'];
Object.defineProperty(prototype, 'isSupportedArgument', {
configurable: true,
enumerable: false,
value: function isSupportedArgument(this: LinkTo, name: string): boolean {
if (this.modernized) {
if (name === 'bubbles') {
deprecate(
'Passing the `@bubbles` argument to <LinkTo> is deprecated. ' +
'Use the {{on}} modifier to attach a custom event handler to ' +
'control event propagation.',
false,
{
id: 'ember.built-in-components.legacy-arguments',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
until: '4.0.0',
url:
'https://deprecations.emberjs.com/v3.x#toc_ember-built-in-components-legacy-arguments',
}
);
return true;
}
if (name === 'preventDefault') {
deprecate(
'Passing the `@preventDefault` argument to <LinkTo> is deprecated. ' +
'`preventDefault()` is called automatically on events that are ' +
'handled by the <LinkTo> component to prevent the browser from ' +
'navigating away from the page.',
false,
{
id: 'ember.built-in-components.legacy-arguments',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
until: '4.0.0',
url:
'https://deprecations.emberjs.com/v3.x#toc_ember-built-in-components-legacy-arguments',
}
);
return true;
}
}
return superIsSupportedArgument.call(this, name);
},
});
Object.defineProperty(prototype, 'preventDefault', {
configurable: true,
enumerable: false,
value: function preventDefault(this: LinkTo, event: Event): void {
let shouldPreventDefault = true;
let shouldStopPropagation = false;
if ('preventDefault' in this.args.named) {
let value = this.named('preventDefault');
if (isMissing(value) || value) {
deprecate(
'Passing the `@preventDefault` argument to <LinkTo> is deprecated. ' +
'`preventDefault()` is called automatically on events that are ' +
'handled by the <LinkTo> component to prevent the browser from ' +
'navigating away from the page.',
false,
{
id: 'ember.built-in-components.legacy-arguments',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
until: '4.0.0',
url:
'https://deprecations.emberjs.com/v3.x#toc_ember-built-in-components-legacy-arguments',
}
);
} else {
deprecate(
'Passing the `@preventDefault` argument to <LinkTo> is deprecated. ' +
'`preventDefault()` should always be called on events that are ' +
'handled by the <LinkTo> component to prevent the browser from ' +
'navigating away from the page.',
false,
{
id: 'ember.built-in-components.legacy-arguments',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
until: '4.0.0',
url:
'https://deprecations.emberjs.com/v3.x#toc_ember-built-in-components-legacy-arguments',
}
);
shouldPreventDefault = false;
}
}
if ('bubbles' in this.args.named) {
let value = this.named('bubbles');
if (value === false) {
deprecate(
'Passing the `@bubbles` argument to <LinkTo> is deprecated. ' +
'Use the {{on}} modifier to attach a custom event handler to ' +
'control event propagation.',
false,
{
id: 'ember.built-in-components.legacy-arguments',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
until: '4.0.0',
url:
'https://deprecations.emberjs.com/v3.x#toc_ember-built-in-components-legacy-arguments',
}
);
shouldStopPropagation = true;
} else {
deprecate(
'Passing the `@bubbles` argument to <LinkTo> is deprecated. ' +
'`stopPropagation()` is not automatically called so there is ' +
'no need to pass this argument when you DO want the event to ' +
'propagate normally',
false,
{
id: 'ember.built-in-components.legacy-arguments',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
until: '4.0.0',
url:
'https://deprecations.emberjs.com/v3.x#toc_ember-built-in-components-legacy-arguments',
}
);
}
}
if (shouldPreventDefault) {
event.preventDefault();
}
if (shouldStopPropagation) {
event.stopPropagation();
}
},
});
}
// QP
{
let superModelsDescriptor = descriptorFor(prototype, 'models');
assert(
`[BUG] expecting models to be a getter on <LinkTo>`,
superModelsDescriptor && typeof superModelsDescriptor.get === 'function'
);
let superModelsGetter = superModelsDescriptor.get as (this: LinkTo) => {}[];
Object.defineProperty(prototype, 'models', {
configurable: true,
enumerable: false,
get: function models(this: LinkTo): {}[] {
let models = superModelsGetter.call(this);
if (models.length > 0 && !('query' in this.args.named)) {
if (isQueryParams(models[models.length - 1])) {
models = models.slice(0, -1);
}
}
return models;
},
});
let superQueryDescriptor = descriptorFor(prototype, 'query');
assert(
`[BUG] expecting query to be a getter on <LinkTo>`,
superQueryDescriptor && typeof superQueryDescriptor.get === 'function'
);
let superQueryGetter = superQueryDescriptor.get as (this: LinkTo) => {};
Object.defineProperty(prototype, 'query', {
configurable: true,
enumerable: false,
get: function query(this: LinkTo): {} {
if ('query' in this.args.named) {
let qp = superQueryGetter.call(this);
if (isQueryParams(qp)) {
return qp.values ?? EMPTY_QUERY_PARAMS;
} else {
return qp;
}
} else {
let models = superModelsGetter.call(this);
if (models.length > 0) {
let qp = models[models.length - 1];
if (isQueryParams(qp) && qp.values !== null) {
return qp.values;
}
}
return EMPTY_QUERY_PARAMS;
}
},
});
}
// Positional Arguments
{
let superValidateArguments = prototype['validateArguments'];
Object.defineProperty(prototype, 'validateArguments', {
configurable: true,
enumerable: false,
value: function validateArguments(this: LinkTo): void {
if (this.args.positional.length !== 0 || 'params' in this.args.named) {
// Already deprecated in the legacy implementation
this.modernized = false;
}
superValidateArguments.call(this);
},
});
let superOnUnsupportedArgument = prototype['onUnsupportedArgument'];
Object.defineProperty(prototype, 'onUnsupportedArgument', {
configurable: true,
enumerable: false,
value: function onUnsupportedArgument(this: LinkTo, name: string): void {
if (name !== 'params') {
superOnUnsupportedArgument.call(this, name);
}
},
});
}
}
if (JQUERY_INTEGRATION) {
jQueryEventShim(LinkTo);
}
export default opaquify(LinkTo, LinkToTemplate);