Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidied Up Url Handling #82

Merged
merged 4 commits into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Navigation/src/StateNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class StateNavigator {
}

start(url?: string) {
this.navigateLink(url ? url : this.historyManager.getCurrentUrl());
this.navigateLink(url != null ? url : this.historyManager.getCurrentUrl());
};
}
export = StateNavigator;
14 changes: 9 additions & 5 deletions Navigation/src/history/HTML5HistoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HTML5HistoryManager implements HistoryManager {
disabled: boolean = (typeof window === 'undefined') || !(window.history && window.history.pushState);

constructor(applicationPath: string = '') {
this.applicationPath = applicationPath;
this.applicationPath = HTML5HistoryManager.prependSlash(applicationPath);
}

init(navigateHistory: () => void) {
Expand All @@ -26,22 +26,26 @@ class HTML5HistoryManager implements HistoryManager {
}

getCurrentUrl(): string {
return location.pathname.substring(this.applicationPath.length) + location.search;
return this.getUrl(location);
}

getHref(url: string): string {
if (url == null)
throw new Error('The Url is invalid');
return this.applicationPath + url;
return this.applicationPath + HTML5HistoryManager.prependSlash(url);
}

getUrl(anchor: HTMLAnchorElement) {
return anchor.pathname.substring(this.applicationPath.length) + anchor.search;
getUrl(hrefElement: HTMLAnchorElement | Location) {
return hrefElement.pathname.substring(this.applicationPath.length) + hrefElement.search;
}

stop() {
if (!this.disabled)
window.removeEventListener('popstate', this.navigateHistory);
}

private static prependSlash(url: string): string {
return (url && url.substring(0, 1) !== '/') ? '/' + url : url;
}
}
export = HTML5HistoryManager;
6 changes: 3 additions & 3 deletions Navigation/src/history/HashHistoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HashHistoryManager implements HistoryManager {
}

getCurrentUrl(): string {
return this.decode(location.hash.substring(1));
return this.getUrl(location);
}

getHref(url: string): string {
Expand All @@ -39,8 +39,8 @@ class HashHistoryManager implements HistoryManager {
return '#' + this.encode(url);
}

getUrl(anchor: HTMLAnchorElement) {
return this.decode(anchor.hash.substring(1));
getUrl(hrefElement: HTMLAnchorElement | Location) {
return this.decode(hrefElement.hash.substring(1));
}

stop() {
Expand Down
2 changes: 1 addition & 1 deletion Navigation/src/history/HistoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
addHistory(url: string, replace: boolean): void;
getCurrentUrl(): string;
getHref(url: string): string;
getUrl(anchor: HTMLAnchorElement): string;
getUrl(hrefElement: HTMLAnchorElement | Location): string;
stop(): void;
}
export = HistoryManager;
12 changes: 6 additions & 6 deletions Navigation/src/navigation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ declare namespace Navigation {
*/
getHref(url: string): string;
/**
* Gets a Url from the anchor
* Gets a Url from the anchor or location
*/
getUrl(anchor: HTMLAnchorElement): string;
getUrl(hrefElement: HTMLAnchorElement | Location): string;
/**
* Removes browser history event listeners
*/
Expand Down Expand Up @@ -236,9 +236,9 @@ declare namespace Navigation {
*/
getHref(url: string): string;
/**
* Gets a Url from the anchor
* Gets a Url from the anchor or location
*/
getUrl(anchor: HTMLAnchorElement): string;
getUrl(hrefElement: HTMLAnchorElement | Location): string;
/**
* Removes a listener for the hashchange event
*/
Expand Down Expand Up @@ -287,9 +287,9 @@ declare namespace Navigation {
*/
getHref(url: string): string;
/**
* Gets a Url from the anchor
* Gets a Url from the anchor or location
*/
getUrl(anchor: HTMLAnchorElement): string;
getUrl(hrefElement: HTMLAnchorElement | Location): string;
/**
* Removes a listener for the popstate event
*/
Expand Down
36 changes: 36 additions & 0 deletions Navigation/test/NavigationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4933,4 +4933,40 @@ describe('Navigation', function () {
});
}
});

describe('Start Route', function () {
it('should navigate', function() {
var stateNavigator = new Navigation.StateNavigator([
{ key: 's0', route: '' },
{ key: 's1', route: 'ab' }
]);
stateNavigator.start('/ab');
assert.strictEqual(stateNavigator.stateContext.state, stateNavigator.states['s1']);
});
});

describe('Start Empty Route', function () {
it('should navigate', function() {
var stateNavigator = new Navigation.StateNavigator([
{ key: 's0', route: '' },
{ key: 's1', route: 'ab' }
]);
stateNavigator.start('');
assert.strictEqual(stateNavigator.stateContext.state, stateNavigator.states['s0']);
});
});

describe('HTML5 History', function () {
it('should prepend slash', function() {
var history = new Navigation.HTML5HistoryManager();
assert.strictEqual(history.getHref('a'), '/a');
assert.strictEqual(history.getHref('/a'), '/a');
history = new Navigation.HTML5HistoryManager('a');
assert.strictEqual(history.getHref('b'), '/a/b');
assert.strictEqual(history.getHref('/b'), '/a/b');
history = new Navigation.HTML5HistoryManager('/a');
assert.strictEqual(history.getHref('b'), '/a/b');
assert.strictEqual(history.getHref('/b'), '/a/b');
});
});
});