mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 03:32:21 +08:00
wip back btn
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user