Skip to content

Commit

Permalink
Merge pull request #159 from grahammendick/state-context
Browse files Browse the repository at this point in the history
  • Loading branch information
grahammendick authored Feb 3, 2018
2 parents b2c60a2 + 61db016 commit 8b74ef2
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Navigation/src/StateContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class StateContext {
state: State = null;
data: any = {};
url: string = null;
asyncData: any = undefined;
title: string = null;
history = false;
crumbs: Crumb[] = [];
nextCrumb: Crumb = null;

Expand All @@ -25,7 +27,9 @@ class StateContext {
this.state = null;
this.data = {};
this.url = null;
this.asyncData = undefined;
this.title = null;
this.history = false;
this.crumbs = [];
this.nextCrumb = null;
}
Expand Down
10 changes: 6 additions & 4 deletions Navigation/src/StateNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ class StateNavigator {
this.states[states[i].key] = states[i];
}

private setStateContext(state: State, data: any, url: string) {
private setStateContext(state: State, data: any, url: string, asyncData: any, history: boolean) {
this.stateContext.oldState = this.stateContext.state;
this.stateContext.oldData = this.stateContext.data;
this.stateContext.oldUrl = this.stateContext.url;
this.stateContext.state = state;
this.stateContext.url = url;
this.stateContext.asyncData = asyncData;
this.stateContext.title = state.title;
this.stateContext.history = history;
this.stateContext.crumbs = data[state.crumbTrailKey];
delete data[state.crumbTrailKey];
this.stateContext.data = data;
Expand Down Expand Up @@ -117,7 +119,7 @@ class StateNavigator {
navigateLink(url: string, historyAction: 'add' | 'replace' | 'none' = 'add', history = false) {
var oldUrl = this.stateContext.url;
var { state, data } = this.stateHandler.parseLink(url);
var navigateContinuation = this.getNavigateContinuation(oldUrl, state, data, url, historyAction);
var navigateContinuation = this.getNavigateContinuation(oldUrl, state, data, url, historyAction, history);
var unloadContinuation = () => {
if (oldUrl === this.stateContext.url)
state.navigating(data, url, navigateContinuation, history);
Expand All @@ -128,10 +130,10 @@ class StateNavigator {
state.navigating(data, url, navigateContinuation, history);
}

private getNavigateContinuation(oldUrl: string, state: State, data: any, url: string, historyAction: 'add' | 'replace' | 'none'): () => void {
private getNavigateContinuation(oldUrl: string, state: State, data: any, url: string, historyAction: 'add' | 'replace' | 'none', history: boolean): () => void {
return (asyncData?: any) => {
if (oldUrl === this.stateContext.url) {
this.setStateContext(state, data, url);
this.setStateContext(state, data, url, asyncData, history);
if (this.stateContext.oldState && this.stateContext.oldState !== state)
this.stateContext.oldState.dispose();
state.navigated(this.stateContext.data, asyncData);
Expand Down
75 changes: 75 additions & 0 deletions Navigation/test/NavigationDataTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,77 @@ describe('Navigation Data', function () {
}
});

describe('Async Data', function() {
var stateNavigator: StateNavigator;
beforeEach(function() {
stateNavigator = new StateNavigator([
{ key: 's', route: 'r' }
]);
stateNavigator.states.s.navigating = (data, url, navigate) => {
navigate({s: 1});
}
});

describe('Navigate', function() {
beforeEach(function() {
stateNavigator.navigate('s');
});
test();
});

describe('Navigate Link', function() {
beforeEach(function() {
var link = stateNavigator.getNavigationLink('s');
stateNavigator.navigateLink(link);
});
test();
});

function test() {
it('should populate data', function () {
assert.strictEqual(stateNavigator.stateContext.data['s'], undefined);
assert.strictEqual(stateNavigator.stateContext.asyncData['s'], 1);
});
}
});

describe('No Async Data', function() {
var stateNavigator: StateNavigator;
beforeEach(function() {
stateNavigator = new StateNavigator([
{ key: 's0', route: 'r0' },
{ key: 's1', route: 'r1' },
]);
stateNavigator.states.s0.navigating = (data, url, navigate) => {
navigate({s: 1});
}
});

describe('Navigate', function() {
beforeEach(function() {
stateNavigator.navigate('s0');
stateNavigator.navigate('s1');
});
test();
});

describe('Navigate Link', function() {
beforeEach(function() {
var link = stateNavigator.getNavigationLink('s0');
stateNavigator.navigateLink(link);
link = stateNavigator.getNavigationLink('s1');
stateNavigator.navigateLink(link);
});
test();
});

function test() {
it('should not populate data', function () {
assert.strictEqual(stateNavigator.stateContext.asyncData, undefined);
});
}
});

describe('Navigate Data Back', function() {
var stateNavigator: StateNavigator;
beforeEach(function() {
Expand Down Expand Up @@ -6146,6 +6217,9 @@ describe('Navigation Data', function () {
stateNavigator = new StateNavigator([
{ key: 's', route: 'r' }
]);
stateNavigator.states.s.navigating = (data, url, navigate) => {
navigate({x: 'a'});
};
});
var data = {};
data['s'] = 'Hello';
Expand Down Expand Up @@ -6185,6 +6259,7 @@ describe('Navigation Data', function () {
assert.strictEqual(Object.keys(stateNavigator.stateContext.oldData).length, 0);
assert.strictEqual(Object.keys(stateNavigator.stateContext.previousData).length, 0);
assert.strictEqual(Object.keys(stateNavigator.stateContext.data).length, 0);
assert.strictEqual(stateNavigator.stateContext.asyncData, undefined);
});
}
});
Expand Down
58 changes: 56 additions & 2 deletions Navigation/test/NavigationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Navigation', function () {
function test(){
it('should populate context', function() {
assert.equal(stateNavigator.stateContext.state, stateNavigator.states['s']);
assert.equal(stateNavigator.stateContext.history, false);
assert.equal(stateNavigator.stateContext.crumbs.length, 0);
});
}
Expand Down Expand Up @@ -97,6 +98,58 @@ describe('Navigation', function () {
}
});

describe('History State', function() {
var stateNavigator: StateNavigator;
beforeEach(function() {
stateNavigator = new StateNavigator([
{ key: 's', route: 'r' }
]);
});

describe('Navigate Link', function() {
beforeEach(function() {
var link = stateNavigator.getNavigationLink('s');
stateNavigator.navigateLink(link, undefined, true);
});
test();
});

function test(){
it('should populate context', function() {
assert.equal(stateNavigator.stateContext.state, stateNavigator.states['s']);
assert.equal(stateNavigator.stateContext.history, true);
assert.equal(stateNavigator.stateContext.crumbs.length, 0);
});
}
});

describe('Not History State', function() {
var stateNavigator: StateNavigator;
beforeEach(function() {
stateNavigator = new StateNavigator([
{ key: 's', route: 'r' }
]);
});

describe('Navigate Link', function() {
beforeEach(function() {
var link = stateNavigator.getNavigationLink('s');
stateNavigator.navigateLink(link, undefined, true);
link = stateNavigator.getNavigationLink('s');
stateNavigator.navigateLink(link);
});
test();
});

function test(){
it('should populate context', function() {
assert.equal(stateNavigator.stateContext.state, stateNavigator.states['s']);
assert.equal(stateNavigator.stateContext.history, false);
assert.equal(stateNavigator.stateContext.crumbs.length, 0);
});
}
});

describe('Invalid State', function() {
var stateNavigator: StateNavigator;
beforeEach(function() {
Expand Down Expand Up @@ -3458,7 +3511,7 @@ describe('Navigation', function () {
var stateNavigator: StateNavigator;
beforeEach(function() {
stateNavigator = new StateNavigator([
{ key: 's', route: 'r' }
{ key: 's', route: 'r', trackCrumbTrail: true }
]);
});

Expand All @@ -3475,7 +3528,7 @@ describe('Navigation', function () {
var link = stateNavigator.getNavigationLink('s');
stateNavigator.navigateLink(link);
link = stateNavigator.getRefreshLink();
stateNavigator.navigateLink(link);
stateNavigator.navigateLink(link, undefined, true);
});
test();
});
Expand All @@ -3488,6 +3541,7 @@ describe('Navigation', function () {
assert.strictEqual(stateNavigator.stateContext.state, null);
assert.strictEqual(stateNavigator.stateContext.url, null);
assert.strictEqual(stateNavigator.stateContext.title, null);
assert.strictEqual(stateNavigator.stateContext.history, false);
assert.strictEqual(stateNavigator.stateContext.crumbs.length, 0);
assert.strictEqual(stateNavigator.stateContext.nextCrumb, null);
});
Expand Down
2 changes: 2 additions & 0 deletions Navigation/test/navigation-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ let state: State = stateNavigator.stateContext.state;
let url: string = stateNavigator.stateContext.url;
const title: string = stateNavigator.stateContext.title;
let page: number = stateNavigator.stateContext.data.page;
const peopleList = stateNavigator.stateContext.asyncData;
const history: boolean = stateNavigator.stateContext.history;
state = stateNavigator.stateContext.oldState;
url = stateNavigator.stateContext.oldUrl;
page = stateNavigator.stateContext.oldData.page;
Expand Down
10 changes: 9 additions & 1 deletion Navigation/test/node_modules/navigation.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion NavigationAngular/src/node_modules/navigation.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion NavigationCycle/src/node_modules/navigation.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion NavigationInferno/src/node_modules/navigation.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion NavigationKnockout/src/node_modules/navigation.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8b74ef2

Please sign in to comment.