mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-05-04 12:54:41 +00:00
minor refactor for comprehension
This commit is contained in:
parent
c864dac3b1
commit
43b1b776d9
1 changed files with 62 additions and 63 deletions
|
|
@ -5,86 +5,85 @@
|
|||
* http://jquery.org/license
|
||||
*/
|
||||
( function( $, window ) {
|
||||
|
||||
// For now, let's Monkeypatch this onto the end of $.mobile._registerInternalEvents
|
||||
var oldRegisterInternalEvents = $.mobile._registerInternalEvents;
|
||||
var pushStateHandler = self = {},
|
||||
oldRegisterInternalEvents = $.mobile._registerInternalEvents,
|
||||
$win = $( window );
|
||||
|
||||
$.mobile._registerInternalEvents = function(){
|
||||
|
||||
// Call previous function
|
||||
oldRegisterInternalEvents();
|
||||
|
||||
// Initial href without hash becomes base for hash changes
|
||||
var initialFilePath = location.pathname,
|
||||
|
||||
// Begin with popstate listening disabled, since it fires at onload in chrome
|
||||
popListeningEnabled = false,
|
||||
|
||||
// Support for nested lists and any other clientside-generated subpages
|
||||
subkey = "&" + $.mobile.subPageUrlKey,
|
||||
|
||||
// Support for dialog hash urls
|
||||
dialog = "&ui-state",
|
||||
|
||||
// Cache window
|
||||
$win = $( window ),
|
||||
$.extend( pushStateHandler, {
|
||||
initialFilePath: location.pathname,
|
||||
|
||||
// Flag for tracking if a Hashchange naturally occurs after each popstate + replace
|
||||
hashchangeFired = false;
|
||||
// Begin with popstate listening disabled, since it fires at onload in chrome
|
||||
popListeningEnabled: false,
|
||||
|
||||
$win.bind( "hashchange replacehash", function( e ) {
|
||||
// Support for nested lists and any other clientside-generated subpages
|
||||
subkey: "&" + $.mobile.subPageUrlKey,
|
||||
|
||||
hashchangeFired = true;
|
||||
// Support for dialog hash urls
|
||||
dialog: "&ui-state",
|
||||
|
||||
if( $.support.pushState ){
|
||||
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 );
|
||||
// Flag for tracking if a Hashchange naturally occurs after each popstate + replace
|
||||
hashchangeFired: false,
|
||||
|
||||
onHashAlter: function( e ) {
|
||||
self.hashchangeFired = true;
|
||||
|
||||
if( $.support.pushState ) {
|
||||
var hash = location.hash || "#" + self.initialFilePath,
|
||||
href = hash.replace( "#", "" );
|
||||
|
||||
//support dialog urls
|
||||
if( href ) {
|
||||
if( href.indexOf( self.dialog ) > -1 ) {
|
||||
href = href.split( self.dialog ).join( "#" + self.dialog );
|
||||
} else if( href.indexOf( self.subkey ) > -1 ) {
|
||||
href = href.split( self.subkey ).join( "#" + self.subkey );
|
||||
}
|
||||
}
|
||||
|
||||
history.replaceState( { hash: hash, title: document.title }, document.title, href );
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Handle popstate events the occur through history changes
|
||||
$win.bind( "popstate", function( e ) {
|
||||
if( popListeningEnabled ){
|
||||
|
||||
if( e.originalEvent.state ) {
|
||||
history.replaceState( e.originalEvent.state, e.originalEvent.state.title, initialFilePath + e.originalEvent.state.hash );
|
||||
onPopState: function( e ) {
|
||||
if( self.popListeningEnabled && e.originalEvent.state ) {
|
||||
history.replaceState( e.originalEvent.state, e.originalEvent.state.title, self.initialFilePath + e.originalEvent.state.hash );
|
||||
|
||||
// Urls that reference subpages will fire their own hashchange, so we don't want to trigger 2 in that case.
|
||||
hashchangeFired = false;
|
||||
// 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( !hashchangeFired ) {
|
||||
$win.trigger( "hashchange" );
|
||||
}
|
||||
hashchangeFired = false;
|
||||
}, 0);
|
||||
setTimeout(function() {
|
||||
if( !self.hashchangeFired ) {
|
||||
$win.trigger( "hashchange" );
|
||||
}
|
||||
|
||||
}
|
||||
self.hashchangeFired = false;
|
||||
}, 0);
|
||||
}
|
||||
});
|
||||
|
||||
// Replace the hash before pushstate listening is enabled
|
||||
$win.trigger( "replacehash" );
|
||||
},
|
||||
|
||||
// Enable pushstate listening *after window onload
|
||||
// To ignore the initial pop that Chrome calls at onload
|
||||
$win.load(function(){
|
||||
setTimeout(function(){
|
||||
popListeningEnabled = true;
|
||||
}, 0 );
|
||||
});
|
||||
init: function() {
|
||||
$win.bind( "hashchange replacehash", self.onHashAlter );
|
||||
|
||||
// Handle popstate events the occur through history changes
|
||||
$win.bind( "popstate", self.onPopState );
|
||||
|
||||
// Replace the hash before pushstate listening is enabled
|
||||
$win.trigger( "replacehash" );
|
||||
|
||||
// Enable pushstate listening *after window onload
|
||||
// To ignore the initial pop that Chrome calls at onload
|
||||
$win.load(function() {
|
||||
setTimeout(function() {
|
||||
self.popListeningEnabled = true;
|
||||
}, 0 );
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$.mobile._registerInternalEvents = function(){
|
||||
// Call previous function
|
||||
oldRegisterInternalEvents();
|
||||
pushStateHandler.init();
|
||||
};
|
||||
|
||||
})( jQuery, this );
|
||||
Loading…
Reference in a new issue