wip back btn

This commit is contained in:
Adam Bradley
2015-07-05 22:48:52 -05:00
parent db5b83de1a
commit 3d259725a3
2 changed files with 36 additions and 12 deletions

View File

@ -36,7 +36,9 @@ export class IonicRouter {
}
}
init() {
init(window) {
this.initHistory(window);
let rootViewCtrl = this.activeViewController();
if (rootViewCtrl) {
let matchedRoute = this.match( this.getCurrentPath() ) || this.otherwise();
@ -44,6 +46,36 @@ export class IonicRouter {
}
}
initHistory(window) {
this.location = window.location;
this.history = window.history;
window.addEventListener('popstate', (ev) => {
this.onPopState(ev);
});
}
onPopState(ev) {
let routeName = (ev.state && ev.state.name);
console.log('onPopState', routeName);
let activeViewCtrl = this.activeViewController();
if (activeViewCtrl) {
activeViewCtrl.pop();
}
}
pushState(route) {
let newPath = route.path;
if (this.location.hash !== '#' + newPath) {
let state = {
name: route.name
};
this.history.pushState(state, '', '#' + newPath);
}
}
match(path) {
let matchedRoute = null;
let routeMatch = null;
@ -101,20 +133,12 @@ export class IonicRouter {
let matchedRoute = this.match(routeConfig.path);
if (matchedRoute) {
this.updateState(matchedRoute);
this.pushState(matchedRoute);
}
}
}
}
updateState(route) {
let newPath = route.path;
if (window.location.hash !== '#' + newPath) {
console.log('updateState', newPath);
window.location.hash = newPath;
}
}
addViewController(viewCtrl) {
this._viewCtrls.push(viewCtrl);
}
@ -127,7 +151,7 @@ export class IonicRouter {
}
getCurrentPath() {
let hash = window.location.hash;
let hash = this.location.hash;
// Grab the path without the leading hash
return hash.slice(1);
}