Skip to content

Commit

Permalink
feat(router): generate absolute URI from router
Browse files Browse the repository at this point in the history
Adds an extra boolean argument to `router.generate` to specify whether or not to generate an absolute URI.

Fixes aurelia#88.
  • Loading branch information
jwahyoung committed Feb 19, 2016
1 parent 372ae81 commit 810d221
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
15 changes: 10 additions & 5 deletions config.js
Expand Up @@ -18,11 +18,12 @@ System.config({
"aurelia-event-aggregator": "npm:aurelia-event-aggregator@1.0.0-beta.1.1.0",
"aurelia-history": "npm:aurelia-history@1.0.0-beta.1.1.1",
"aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.1.1",
"aurelia-pal-browser": "npm:aurelia-pal-browser@1.0.0-beta.1.1.3",
"aurelia-path": "npm:aurelia-path@1.0.0-beta.1.1.0",
"aurelia-route-recognizer": "npm:aurelia-route-recognizer@1.0.0-beta.1.1.0",
"babel": "npm:babel-core@5.8.35",
"babel-runtime": "npm:babel-runtime@5.8.35",
"core-js": "npm:core-js@2.0.3",
"core-js": "npm:core-js@2.1.0",
"github:jspm/nodelibs-assert@0.1.0": {
"assert": "npm:assert@1.3.0"
},
Expand All @@ -42,23 +43,27 @@ System.config({
"aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.1.1",
"aurelia-metadata": "npm:aurelia-metadata@1.0.0-beta.1.1.3",
"aurelia-pal": "npm:aurelia-pal@1.0.0-beta.1.1.1",
"core-js": "npm:core-js@2.0.3"
"core-js": "npm:core-js@2.1.0"
},
"npm:aurelia-event-aggregator@1.0.0-beta.1.1.0": {
"aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.1.1"
},
"npm:aurelia-metadata@1.0.0-beta.1.1.3": {
"aurelia-pal": "npm:aurelia-pal@1.0.0-beta.1.1.1",
"core-js": "npm:core-js@2.0.3"
"core-js": "npm:core-js@2.1.0"
},
"npm:aurelia-pal-browser@1.0.0-beta.1.1.3": {
"aurelia-pal": "npm:aurelia-pal@1.0.0-beta.1.1.1",
"core-js": "npm:core-js@2.1.0"
},
"npm:aurelia-route-recognizer@1.0.0-beta.1.1.0": {
"aurelia-path": "npm:aurelia-path@1.0.0-beta.1.1.0",
"core-js": "npm:core-js@2.0.3"
"core-js": "npm:core-js@2.1.0"
},
"npm:babel-runtime@5.8.35": {
"process": "github:jspm/nodelibs-process@0.1.2"
},
"npm:core-js@2.0.3": {
"npm:core-js@2.1.0": {
"fs": "github:jspm/nodelibs-fs@0.1.2",
"path": "github:jspm/nodelibs-path@0.1.0",
"process": "github:jspm/nodelibs-process@0.1.2",
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -48,6 +48,7 @@
"core-js": "^2.0.3"
},
"devDependencies": {
"aurelia-pal-browser": "^1.0.0-beta.1.1.3",
"babel": "babel-core@^5.8.24",
"babel-runtime": "^5.8.24",
"core-js": "^2.0.3"
Expand Down
5 changes: 3 additions & 2 deletions src/router.js
Expand Up @@ -186,7 +186,7 @@ export class Router {
* @param params The route params to be used to populate the route pattern.
* @returns {string} A string containing the generated URL fragment.
*/
generate(name: string, params?: any): string {
generate(name: string, params?: any, absolute?: boolean = false): string {
let hasRoute = this._recognizer.hasRoute(name);
if ((!this.isConfigured || !hasRoute) && this.parent) {
return this.parent.generate(name, params);
Expand All @@ -197,7 +197,8 @@ export class Router {
}

let path = this._recognizer.generate(name, params);
return _createRootedPath(path, this.baseUrl, this.history._hasPushState);
let rootedPath = _createRootedPath(path, this.baseUrl, this.history._hasPushState, absolute);
return absolute ? `${this.history.getAbsoluteRoot()}${rootedPath}` : rootedPath;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/util.js
@@ -1,12 +1,16 @@
export function _normalizeAbsolutePath(path, hasPushState) {
export function _normalizeAbsolutePath(path, hasPushState, absolute = false) {
if (!hasPushState && path[0] !== '#') {
path = '#' + path;
}

if (hasPushState && absolute) {
path = path.substring(1, path.length);
}

return path;
}

export function _createRootedPath(fragment, baseUrl, hasPushState) {
export function _createRootedPath(fragment, baseUrl, hasPushState, absolute) {
if (isAbsoluteUrl.test(fragment)) {
return fragment;
}
Expand All @@ -27,7 +31,7 @@ export function _createRootedPath(fragment, baseUrl, hasPushState) {
path = path.substring(0, path.length - 1);
}

return _normalizeAbsolutePath(path + fragment, hasPushState);
return _normalizeAbsolutePath(path + fragment, hasPushState, absolute);
}

export function _resolveUrl(fragment, baseUrl, hasPushState) {
Expand Down
19 changes: 19 additions & 0 deletions test/router.spec.js
Expand Up @@ -3,11 +3,16 @@ import {Container} from 'aurelia-dependency-injection';
import {AppRouter} from '../src/app-router';
import {PipelineProvider} from '../src/pipeline-provider';

let absoluteRoot = 'http://aurelia.io/docs/';

class MockHistory extends History {
activate() {}
deactivate() {}
navigate() {}
navigateBack() {}
getAbsoluteRoot() {
return absoluteRoot;
}
}

describe('the router', () => {
Expand Down Expand Up @@ -109,6 +114,20 @@ describe('the router', () => {
done();
});
});

it('should return a fully-qualified URL when "absolute" is true', (done) => {
const child = router.createChild(new Container());
child.configure(config => config.map({ name: 'test', route: 'test/:id', moduleId: './test' }))
.then(() => {
expect(child.generate('test', { id: 1 }, true)).toBe(`${absoluteRoot}#/test/1`);

router.history._hasPushState = true;

expect(child.generate('test', { id: 1 }, true)).toBe(`${absoluteRoot}test/1`);

done();
});
});
});

describe('navigate', () => {
Expand Down
2 changes: 2 additions & 0 deletions test/setup.js
@@ -0,0 +1,2 @@
import {initialize} from 'aurelia-pal-browser';
initialize();

0 comments on commit 810d221

Please sign in to comment.