discourse/app/assets/javascripts/discourse/mapping-router.js.es6

84 lines
2.4 KiB
Plaintext
Raw Normal View History

const rootURL = Discourse.BaseUri;
2015-04-29 14:57:08 -04:00
2015-05-01 16:35:52 -04:00
const BareRouter = Ember.Router.extend({
2015-04-29 14:57:08 -04:00
rootURL,
2015-04-29 16:26:14 -04:00
location: Ember.testing ? 'none': 'discourse-location'
2015-04-29 14:57:08 -04:00
});
export function mapRoutes() {
2015-05-01 16:35:52 -04:00
var Router = BareRouter.extend();
2015-04-29 14:57:08 -04:00
const resources = {};
const paths = {};
// 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) {
2015-04-29 14:57:08 -04:00
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.'); }
var mapObj = module.default;
if (typeof mapObj === 'function') {
mapObj = { resource: 'root', map: mapObj };
}
if (!resources[mapObj.resource]) { resources[mapObj.resource] = []; }
resources[mapObj.resource].push(mapObj.map);
if (mapObj.path) { paths[mapObj.resource] = mapObj.path; }
}
});
2015-05-01 16:35:52 -04:00
return Router.map(function() {
2015-04-29 14:57:08 -04:00
var router = this;
// Do the root resources first
if (resources.root) {
resources.root.forEach(function(m) {
m.call(router);
});
delete resources.root;
}
// Even if no plugins set it up, we need an `adminPlugins` route
var adminPlugins = 'admin.adminPlugins';
resources[adminPlugins] = resources[adminPlugins] || [Ember.K];
paths[adminPlugins] = paths[adminPlugins] || "/plugins";
var segments = {},
standalone = [];
Object.keys(resources).forEach(function(r) {
var m = /^([^\.]+)\.(.*)$/.exec(r);
if (m) {
segments[m[1]] = m[2];
} else {
standalone.push(r);
}
});
// Apply other resources next. A little hacky but works!
standalone.forEach(function(r) {
router.resource(r, {path: paths[r]}, function() {
var res = this;
resources[r].forEach(function(m) { m.call(res); });
var s = segments[r];
if (s) {
var full = r + '.' + s;
res.resource(s, {path: paths[full]}, function() {
var nestedRes = this;
resources[full].forEach(function(m) { m.call(nestedRes); });
});
}
});
});
this.route('unknown', {path: '*path'});
});
}
2015-05-01 16:35:52 -04:00
export default BareRouter;