-
Notifications
You must be signed in to change notification settings - Fork 15
/
proper-links.ts
140 lines (107 loc) · 3.73 KB
/
proper-links.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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { assert } from '@ember/debug';
import { registerDestructor } from '@ember/destroyable';
import { getOwner } from '@ember/owner';
import { getAnchor, shouldHandle } from 'should-handle-link';
import type EmberRouter from '@ember/routing/router';
import type RouterService from '@ember/routing/router-service';
export { shouldHandle } from 'should-handle-link';
type Constructor<T extends object = object> = { new (...args: any[]): T };
export interface Options {
ignore?: string[];
}
export function properLinks(
options: Options
): <Instance extends object, Klass = { new (...args: any[]): Instance }>(klass: Klass) => Klass;
export function properLinks<Instance extends object, Klass = { new (...args: any[]): Instance }>(
klass: Klass
): Klass;
/**
* @internal
*/
export function properLinks<Instance extends object, Klass = { new (...args: any[]): Instance }>(
options: Options,
klass: Klass
): Klass;
export function properLinks<Instance extends object, Klass = { new (...args: any[]): Instance }>(
...args: [Options] | [Klass] | [Options, Klass]
): Klass | ((klass: Klass) => Klass) {
let options: Options = {};
let klass: undefined | Klass = undefined;
if (args.length === 2) {
options = args[0] as Options;
klass = args[1] as Klass;
} else if (args.length === 1) {
if (typeof args[0] === 'object') {
// TODO: how to get first arg type correct?
return (klass: Klass) => properLinks(args[0] as any, klass);
} else {
klass = args[0];
}
}
let ignore = options.ignore || [];
assert(`klass was not defined. possibile incorrect arity given to properLinks`, klass);
return class RouterWithProperLinks extends (klass as unknown as Constructor<EmberRouter>) {
// SAFETY: we literally do not care about the args' type here,
// because we just call super
constructor(...args: any[]) {
super(...args);
setup(this, ignore);
}
} as unknown as Klass;
}
/**
* Setup proper links without a decorator.
* This function only requires that a framework object with an owner is passed.
*/
export function setup(parent: object, ignore?: string[]) {
const handler = (event: MouseEvent) => {
/**
* event.target may not be an anchor,
* it may be a span, svg, img, or any number of elements nested in <a>...</a>
*/
let interactive = getAnchor(event);
if (!interactive) return;
let owner = getOwner(parent);
assert('owner is not present', owner);
let routerService = owner.lookup('service:router');
handle(routerService, interactive, ignore ?? [], event);
};
document.body.addEventListener('click', handler, false);
registerDestructor(parent, () => document.body.removeEventListener('click', handler));
}
export function handle(
router: RouterService,
element: HTMLAnchorElement,
ignore: string[],
event: MouseEvent
) {
if (!shouldHandle(location.href, element, event, ignore)) {
return;
}
let url = new URL(element.href);
let fullHref = `${url.pathname}${url.search}${url.hash}`;
let rootURL = router.rootURL;
let withoutRootURL = fullHref.slice(rootURL.length);
// re-add the "root" sigil
// we removed it when we chopped off the rootURL,
// because the rootURL often has this attached to it as well
if (!withoutRootURL.startsWith('/')) {
withoutRootURL = `/${withoutRootURL}`;
}
try {
let routeInfo = router.recognize(fullHref);
if (routeInfo) {
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
router.transitionTo(withoutRootURL);
return false;
}
} catch (e) {
if (e instanceof Error && e.name === 'UnrecognizedURLError') {
return;
}
throw e;
}
}