Skip to content

Commit

Permalink
fix the chrome bug and reduce the number of crazy hash juggles we do
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbender committed Aug 27, 2011
1 parent 328d58b commit 82cce1f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
33 changes: 23 additions & 10 deletions js/jquery.mobile.navigation.js
Expand Up @@ -275,7 +275,7 @@
},

directHashChange: function( opts ) {
var back , forward, newActiveIndex;
var back , forward, newActiveIndex, prev = this.getActive();

// check if url isp in history and if it's ahead or behind current page
$.each( urlHistory.stack, function( i, historyEntry ) {
Expand All @@ -293,9 +293,9 @@
this.activeIndex = newActiveIndex !== undefined ? newActiveIndex : this.activeIndex;

if( back ) {
opts.isBack();
opts.isBack( prev, back );
} else if( forward ) {
opts.isForward();
opts.isForward( prev, back );
}
},

Expand Down Expand Up @@ -1185,12 +1185,17 @@
});
} );

//hashchange event handler
$window.bind( "hashchange", function( e, triggered ) {
$.mobile.handleHashChange = function( hash ) {
//find first page via hash
var role, to = path.stripHash( location.hash ),
var reverse, role, to = path.stripHash( hash ),
//transition is false if it's the first page, undefined otherwise (and may be overridden by default)
transition = $.mobile.urlHistory.stack.length === 0 ? "none" : undefined;
transition = $.mobile.urlHistory.stack.length === 0 ? "none" : undefined,

changePageOptions = {
transition: transition,
changeHash: false,
fromHashChange: true
};

//if listening is disabled (either globally or temporarily), or it's a dialog hash
if( !$.mobile.hashListeningEnabled || urlHistory.ignoreNextHashChange ) {
Expand All @@ -1215,10 +1220,13 @@
// prevent changepage
return;
} else {
var setTo = function() {
var setTo = function( previousPage, isBack ) {
var active = $.mobile.urlHistory.getActive();

to = active.pageUrl;
role = active.role;
transition = active.transition;
reverse = isBack;
};
// if the current active page is a dialog and we're navigating
// to a dialog use the dialog objected saved in the stack
Expand All @@ -1229,12 +1237,17 @@
//if to is defined, load it
if ( to ) {
to = ( typeof to === "string" && !path.isPath( to ) ) ? ( '#' + to ) : to;
$.mobile.changePage( to, { transition: transition, changeHash: false, fromHashChange: true , role: role} );
$.mobile.changePage( to, { transition: transition, changeHash: false, fromHashChange: true , role: role, reverse: reverse } );
}
//there's no hash, go to the first page in the dom
else {
$.mobile.changePage( $.mobile.firstPage, { transition: transition, changeHash: false, fromHashChange: true, roll: rolle } );
$.mobile.changePage( $.mobile.firstPage, { transition: transition, changeHash: false, fromHashChange: true, roll: roll, reverse: reverse } );
}
};

//hashchange event handler
$window.bind( "hashchange", function( e, triggered ) {
$.mobile.handleHashChange( location.hash );
});

//set page min-heights to be device specific
Expand Down
34 changes: 18 additions & 16 deletions js/jquery.mobile.navigation.pushstate.js
Expand Up @@ -34,13 +34,20 @@
};
},

isSubHashPage: function( page ) {
return page.is( "[role='dialog']" ) ||
page.jqmData("url").indexOf( $.mobile.subPageUrlKey ) >= 0;
},

resetUIKeys: function( url ) {
var dialog = $.mobile.dialogHashKey,
subkey = "&" + $.mobile.subPageUrlKey;

if( url.indexOf( dialog ) > -1 ) {
url = url.split( dialog ).join( "#" + dialog );
} else if( url.indexOf( subkey ) > -1 ) {
var split = url.split( dialog );
split.push( "" );
url = split[0] + "#" + split.slice( 1, split.length ).join( dialog );
} else if( url.indexOf( subkey ) > -1 ) {
url = url.split( subkey ).join( "#" + subkey );
}

Expand Down Expand Up @@ -74,26 +81,21 @@
// on popstate (ie back or forward) we need to replace the hash that was there previously
// cleaned up by the additional hash handling
onPopState: function( e ) {
var poppedState = e.originalEvent.state;
var poppedState = e.originalEvent.state, holdnexthashchange = false;

// if there's no state its not a popstate we care about, ie chrome's initial popstate
// or forward popstate
if( poppedState ) {
// can't test the hash directly because the url has already been altered, possibly to
// one without a hash, so we check if the page on display is one that would have
// generated a hash
if( self.isSubHashPage( $.mobile.activePage ) ){
holdnexthashchange = true;
}

// replace the current url with the equivelant hash so that the hashchange binding in vanilla nav
// can do its thing one triggered below
history.replaceState( poppedState, poppedState.title, poppedState.initialHref + poppedState.hash );

// Urls that reference subpages will fire their own hashchange, so we don't want to trigger 2 in that case.
self.hashchangeFired = false;

setTimeout(function() {
if( !self.hashchangeFired ) {
$win.trigger( "hashchange" );
}
$.mobile.handleHashChange( poppedState.hash );

self.hashchangeFired = false;
}, 0);
$.mobile.urlHistory.ignoreNextHashChange = holdnexthashchange;
}
},

Expand Down

0 comments on commit 82cce1f

Please sign in to comment.