From 3d259725a3311c9badcb25e5ae20d6985147819b Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Sun, 5 Jul 2015 22:48:52 -0500 Subject: [PATCH] wip back btn --- ionic/components/app/app.ts | 2 +- ionic/routing/router.ts | 46 ++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/ionic/components/app/app.ts b/ionic/components/app/app.ts index 3f593a170b..7d779ab322 100644 --- a/ionic/components/app/app.ts +++ b/ionic/components/app/app.ts @@ -205,7 +205,7 @@ export function ionicBootstrap(ComponentType, config, router) { bootstrap(ComponentType, injectableBindings).then(appRef => { app.load(appRef); - router.init(); + router.init(window); // resolve that the app has loaded resolve(app); diff --git a/ionic/routing/router.ts b/ionic/routing/router.ts index 2cf624b362..3dd93fbd85 100644 --- a/ionic/routing/router.ts +++ b/ionic/routing/router.ts @@ -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); }