forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmapping-router.js.es6
134 lines (115 loc) · 3.32 KB
/
mapping-router.js.es6
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
import { defaultHomepage } from 'discourse/lib/utilities';
import { rewritePath } from 'discourse/lib/url';
const rootURL = Discourse.BaseUri;
const BareRouter = Ember.Router.extend({
rootURL,
location: Ember.testing ? 'none': 'discourse-location',
handleURL(url) {
url = rewritePath(url);
const params = url.split('?');
if (params[0] === "/") {
url = defaultHomepage();
if (params[1] && params[1].length) {
url = `${url}?${params[1]}`;
}
}
return this._super(url);
}
});
// Ember's router can't be extended. We need to allow plugins to add routes to routes that were defined
// in the core app. This class has the same API as Ember's `Router.map` but saves the results in a tree.
// The tree is applied after all plugins are defined.
class RouteNode {
constructor(name, opts={}, depth=0) {
this.name = name;
this.opts = opts;
this.depth = depth;
this.children = [];
this.childrenByName = {};
this.paths = {};
if (!opts.path) {
opts.path = name;
}
this.paths[opts.path] = true;
}
route(name, opts, fn) {
if (typeof opts === 'function') {
fn = opts;
opts = {};
} else {
opts = opts || {};
}
const existing = this.childrenByName[name];
if (existing) {
if (opts.path) {
existing.paths[opts.path] = true;
}
existing.extract(fn);
} else {
const node = new RouteNode(name, opts, this.depth+1);
node.extract(fn);
this.childrenByName[name] = node;
this.children.push(node);
}
}
extract(fn) {
if (!fn) { return; }
fn.call(this);
}
mapRoutes(router) {
const children = this.children;
if (this.name === 'root') {
children.forEach(c => c.mapRoutes(router));
} else {
const builder = (children.length === 0) ? undefined : function() {
children.forEach(c => c.mapRoutes(this));
};
router.route(this.name, this.opts, builder);
}
}
findSegment(segments) {
if (segments && segments.length) {
const first = segments.shift();
const node = this.childrenByName[first];
if (node) {
return (segments.length === 0) ? node : node.findSegment(segments);
}
}
}
findPath(path) {
if (path) {
return this.findSegment(path.split('.'));
}
}
}
export function mapRoutes() {
const tree = new RouteNode('root');
const extras = [];
// If a module is defined as `route-map` in discourse or a plugin, its routes
// will be built automatically. You can supply a `resource` property to
// automatically put it in that resource, such as `admin`. That way plugins
// can define admin routes.
Object.keys(requirejs._eak_seen).forEach(function(key) {
if (/route-map$/.test(key)) {
var module = require(key, null, null, true);
if (!module || !module.default) { throw new Error(key + ' must export a route map.'); }
const mapObj = module.default;
if (typeof mapObj === 'function') {
tree.extract(mapObj);
} else {
extras.push(mapObj);
}
}
});
extras.forEach(extra => {
const node = tree.findPath(extra.resource);
if (node) {
node.extract(extra.map);
}
});
return BareRouter.extend().map(function() {
tree.mapRoutes(this);
this.route('unknown', {path: '*path'});
});
}
export default BareRouter;