/
link-to.ts
699 lines (554 loc) · 18.1 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
import type Route from '@ember/routing/route';
import type { RouterState, RoutingService } from '@ember/routing/-internals';
import { isSimpleClick } from '@ember/-internals/views';
import { assert, debugFreeze, inspect, warn } from '@ember/debug';
import { getEngineParent } from '@ember/engine';
import EngineInstance from '@ember/engine/instance';
import { flaggedInstrument } from '@ember/instrumentation';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { DEBUG } from '@glimmer/env';
import type { Maybe } from '@glimmer/interfaces';
import type { Nullable } from '@ember/-internals/utility-types';
import { consumeTag, createCache, getValue, tagFor, untrack } from '@glimmer/validator';
import type { Transition } from 'router_js';
import LinkToTemplate from '../templates/link-to';
import InternalComponent, { type OpaqueInternalComponentConstructor, opaquify } from './internal';
import { type Opaque } from '@ember/-internals/utility-types';
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: Nullable<{}>;
}
function isQueryParams(value: unknown): value is QueryParams {
return (
typeof value === 'object' && value !== null && (value as QueryParams)['isQueryParams'] === true
);
}
/**
The `LinkTo` component renders a link to the supplied `routeName` passing an optionally
supplied model to the route as its `model` context of the route. The block for `LinkTo`
becomes the contents of the rendered element:
```handlebars
<LinkTo @route='photoGallery'>
Great Hamster Photos
</LinkTo>
```
This will result in:
```html
<a href="/hamster-photos">
Great Hamster Photos
</a>
```
### Disabling the `LinkTo` component
The `LinkTo` component can be disabled by using the `disabled` argument. A disabled link
doesn't result in a transition when activated, and adds the `disabled` class to the `<a>`
element.
(The class name to apply to the element can be overridden by using the `disabledClass`
argument)
```handlebars
<LinkTo @route='photoGallery' @disabled={{true}}>
Great Hamster Photos
</LinkTo>
```
### Handling `href`
`<LinkTo>` will use your application's Router to fill the element's `href` property with a URL
that matches the path to the supplied `routeName`.
### Handling current route
The `LinkTo` component will apply a CSS class name of 'active' when the application's current
route matches the supplied routeName. For example, if the application's current route is
'photoGallery.recent', then the following invocation of `LinkTo`:
```handlebars
<LinkTo @route='photoGallery.recent'>
Great Hamster Photos
</LinkTo>
```
will result in
```html
<a href="/hamster-photos/this-week" class="active">
Great Hamster Photos
</a>
```
The CSS class used for active classes can be customized by passing an `activeClass` argument:
```handlebars
<LinkTo @route='photoGallery.recent' @activeClass="current-url">
Great Hamster Photos
</LinkTo>
```
```html
<a href="/hamster-photos/this-week" class="current-url">
Great Hamster Photos
</a>
```
### Keeping a link active for other routes
If you need a link to be 'active' even when it doesn't match the current route, you can use the
`current-when` argument.
```handlebars
<LinkTo @route='photoGallery' @current-when='photos'>
Photo Gallery
</LinkTo>
```
This may be helpful for keeping links active for:
* non-nested routes that are logically related
* some secondary menu approaches
* 'top navigation' with 'sub navigation' scenarios
A link will be active if `current-when` is `true` or the current
route is the route this link would transition to.
To match multiple routes 'space-separate' the routes:
```handlebars
<LinkTo @route='gallery' @current-when='photos drawings paintings'>
Art Gallery
</LinkTo>
```
### Supplying a model
An optional `model` argument can be used for routes whose
paths contain dynamic segments. This argument will become
the model context of the linked route:
```javascript
Router.map(function() {
this.route("photoGallery", {path: "hamster-photos/:photo_id"});
});
```
```handlebars
<LinkTo @route='photoGallery' @model={{this.aPhoto}}>
{{aPhoto.title}}
</LinkTo>
```
```html
<a href="/hamster-photos/42">
Tomster
</a>
```
### Supplying multiple models
For deep-linking to route paths that contain multiple
dynamic segments, the `models` argument can be used.
As the router transitions through the route path, each
supplied model argument will become the context for the
route with the dynamic segments:
```javascript
Router.map(function() {
this.route("photoGallery", { path: "hamster-photos/:photo_id" }, function() {
this.route("comment", {path: "comments/:comment_id"});
});
});
```
This argument will become the model context of the linked route:
```handlebars
<LinkTo @route='photoGallery.comment' @models={{array this.aPhoto this.comment}}>
{{comment.body}}
</LinkTo>
```
```html
<a href="/hamster-photos/42/comments/718">
A+++ would snuggle again.
</a>
```
### Supplying an explicit dynamic segment value
If you don't have a model object available to pass to `LinkTo`,
an optional string or integer argument can be passed for routes whose
paths contain dynamic segments. This argument will become the value
of the dynamic segment:
```javascript
Router.map(function() {
this.route("photoGallery", { path: "hamster-photos/:photo_id" });
});
```
```handlebars
<LinkTo @route='photoGallery' @model={{aPhotoId}}>
{{this.aPhoto.title}}
</LinkTo>
```
```html
<a href="/hamster-photos/42">
Tomster
</a>
```
When transitioning into the linked route, the `model` hook will
be triggered with parameters including this passed identifier.
### Supplying query parameters
If you need to add optional key-value pairs that appear to the right of the ? in a URL,
you can use the `query` argument.
```handlebars
<LinkTo @route='photoGallery' @query={{hash page=1 per_page=20}}>
Great Hamster Photos
</LinkTo>
```
This will result in:
```html
<a href="/hamster-photos?page=1&per_page=20">
Great Hamster Photos
</a>
```
@for Ember.Templates.components
@method LinkTo
@public
*/
/**
@module @ember/routing
*/
/**
See [Ember.Templates.components.LinkTo](/ember/release/classes/Ember.Templates.components/methods/input?anchor=LinkTo).
@for Ember.Templates.helpers
@method link-to
@see {Ember.Templates.components.LinkTo}
@public
**/
/**
An opaque interface which can be imported and used in strict-mode
templates to call <LinkTo>.
See [Ember.Templates.components.LinkTo](/ember/release/classes/Ember.Templates.components/methods/input?anchor=LinkTo).
@for @ember/routing
@method LinkTo
@see {Ember.Templates.components.LinkTo}
@public
**/
class _LinkTo extends InternalComponent {
static toString(): string {
return 'LinkTo';
}
@service('-routing') private declare routing: RoutingService<Route>;
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.isEngine || this.engineMountPoint !== undefined
);
assert(
'You must provide at least one of the `@route`, `@model`, `@models` or `@query` arguments to `<LinkTo>`.',
'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) {
let details = e instanceof Error ? e.message : inspect(e);
let message = `While generating link to route "${route}": ${details}`;
if (e instanceof Error) {
e.message = message;
throw e;
} else {
throw message;
}
}
} 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));
payload.transition = routing.transitionTo(route, models, query, 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;
}
}
private get query(): Record<string, unknown> {
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(): Nullable<boolean> {
let current = this.routing.currentState;
let target = this.routing.targetState;
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 {
let owner = this.owner;
return owner instanceof EngineInstance && getEngineParent(owner) !== undefined;
}
private get engineMountPoint(): string | undefined {
let owner = this.owner;
return owner instanceof EngineInstance ? owner.mountPoint : undefined;
}
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));
return routing.isActiveForRoute(models, query, route, state);
}
}
private preventDefault(event: Event): void {
event.preventDefault();
}
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);
}
}
let { prototype } = _LinkTo;
let descriptorFor = (target: object, property: string): Nullable<PropertyDescriptor> => {
if (target) {
return (
Object.getOwnPropertyDescriptor(target, property) ||
descriptorFor(Object.getPrototypeOf(target), property)
);
} else {
return null;
}
};
// @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);
}
},
});
}
// 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 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);
}
},
});
}
const LinkTo = opaquify(_LinkTo, LinkToTemplate) as LinkTo;
interface LinkTo extends Opaque<'component:link-to'>, OpaqueInternalComponentConstructor {}
export default LinkTo;