updated to support deep links to nested lists and dialog parent pages. Still battling a 2x back history issue on those.

This commit is contained in:
scottjehl 2011-07-29 10:06:33 -04:00
parent 51199dd14b
commit 5f2462d031

View file

@ -15,34 +15,59 @@
oldRegisterInternalEvents();
// Initial href without hash becomes base for hash changes
var initUrl = location.href.split( "#" )[0].match( /[^\/]*\/\/[^\/]+(.*)/ ) && RegExp.$1,
// TODO - get this value from $.mobile.path ?
var initialFilePath = location.href.split( "#" )[0].match( /[^\/]*\/\/[^\/]+(.*)/ ) && RegExp.$1,
// Begin with popstate listening disabled, since it fires at onload in chrome
popListeningEnabled = false;
popListeningEnabled = false,
// Support for nested lists and any other clientside-generated subpages
subkey = "&" + $.mobile.subPageUrlKey,
// Support for dialog hash urls
dialog = "&ui-state";
$( window ).bind( "hashchange replacehash", function( e ) {
if( $.support.pushState ){
history.replaceState( { hash: location.hash || "#" + initUrl, title: document.title }, document.title, location.href.split( "#" )[ 1 ] );
var hash = location.hash || "#" + initialFilePath,
href = hash.replace( "#", "" );
//support dialog urls
if( href ){
if( href.indexOf( dialog ) > -1 ){
href = href.split( dialog ).join( "#" + dialog );
}
else if( href.indexOf( subkey ) > -1 ){
href = href.split( subkey ).join( "#" + subkey );
}
}
history.replaceState( { hash: hash, title: document.title }, document.title, href );
}
});
// Handle popstate events the occur through history changes
$( window ).bind( "popstate", function( e ) {
if( popListeningEnabled ){
if( e.originalEvent.state ){
history.replaceState( e.originalEvent.state, e.originalEvent.state.title, initUrl + e.originalEvent.state.hash );
history.replaceState( e.originalEvent.state, e.originalEvent.state.title, initialFilePath + e.originalEvent.state.hash );
$( window ).trigger( "hashchange" );
}
}
});
// Replace the hash before pushstate listening is enabled
$( window ).trigger( "replacehash" );
// Enable pushstate listening after window onload
// Enable pushstate listening *after window onload
// to ignore the initial pop that Chrome calls at onload
$( window ).load( function(){
setTimeout(function(){
popListeningEnabled = true;
}, 10 );
}, 0 );
});
};