Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
fix for transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbender committed Dec 3, 2012
1 parent 8ee4318 commit 921bf77
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 38 deletions.
15 changes: 12 additions & 3 deletions js/jquery.mobile.navigation.js
Expand Up @@ -66,6 +66,7 @@ define( [

//set the generated BASE element's href attribute to a new page's base path
set: function( href ) {
href = path.parseUrl(href).hrefNoHash;
base.element.attr( "href", path.makeUrlAbsolute( href, documentBase ) );
},

Expand Down Expand Up @@ -792,7 +793,14 @@ define( [

// Normally, we tack on a dialog hash key, but if this is the location of a stale dialog,
// we reuse the URL from the entry
url = ( active.url || "" ) + ( alreadyThere ? "" : dialogHashKey );
url = ( active.url || "" );

// account for absolute urls instead of just relative urls use as hashes
if( !alreadyThere && url.indexOf("#") > -1 ) {
url += dialogHashKey;
} else {
url += "#" + dialogHashKey;
}

// tack on another dialogHashKey if this is the same as the initial hash
// this makes sure that a history entry is created for this dialog
Expand Down Expand Up @@ -1123,9 +1131,10 @@ define( [
transition = $.mobile.urlHistory.stack.length === 0 ? "none" : undefined,

// default options for the changPage calls made after examining the current state
// of the page and the hash
// of the page and the hash, NOTE that the transition is derived from the previous
// history entry
changePageOptions = {
transition: transition,
transition: (urlHistory.getLast() || {}).transition || transition,
changeHash: false,
fromHashChange: true,
reverse: data.direction === "back"
Expand Down
47 changes: 32 additions & 15 deletions js/navigation/navigate.js
Expand Up @@ -17,21 +17,34 @@ define([
// so that end users don't have to think about it. Punting for now
// TODO !! move the event bindings into callbacks on the navigate event
$.navigate = function( url, data, noEvents ) {
var state, href, parsed, hash,
resolutionUrl = path.isPath(url) ? path.getLocation() : $.mobile.getDocumentUrl();
var state, href, parsed, loc, hash,
resolutionUrl = path.isPath( url ) ? path.getLocation() : $.mobile.getDocumentUrl();

// Get the url as it would look squashed on to the current resolution url
href = path.squash( url, resolutionUrl );
href = path.squash( url );

// Grab the hash for recording. If the passed url is a path
// we used the parsed version of the squashed url to reconstruct,
// otherwise we assume it's a hash and store it directly
parsed = path.parseUrl( href );
hash = path.isPath(url) ? parsed.pathname + parsed.search : url;
parsed = path.parseUrl( url );
loc = path.parseLocation();

if( loc.pathname + loc.search === parsed.pathname + parsed.search ) {
// If the pathname and search of the passed url is identical to the current loc
// then we must use the hash. Otherwise there will be no event
// eg, url = "/foo/bar?baz#bang", location.href = "http://example.com/foo/bar?baz"
hash = parsed.hash ? parsed.hash : parsed.pathname + parsed.search;
} else if ( path.isPath(url) ) {
var resolved = path.parseUrl( href );
// If the passed url is a path, make it domain relative and remove any trailing hash
hash = resolved.pathname + resolved.search;
} else {
hash = url;
}

// Here we prevent the next hash change or popstate event from doing any
// history management. In the case of hashchange we don't swallow it
// if there will be not hashchange fired (since that won't reset the value)
// if there will be no hashchange fired (since that won't reset the value)
// and will swallow the following hashchange
history.ignoreNextHashChange = true;
if( noEvents && hash !== path.stripHash(path.parseLocation().hash) ) {
Expand Down Expand Up @@ -76,7 +89,7 @@ define([
// record the history entry so that the information can be included
// in hashchange event driven navigate events in a similar fashion to
// the state that's provided by popstate
history.add( url, state );
history.add( state.url, state );
};

// TODO this whole method is absolute trash :(
Expand Down Expand Up @@ -172,7 +185,7 @@ define([
// If all else fails this is a popstate that comes from the back or forward buttons
// make sure to set the state of our history stack properly, and record the directionality
history.direct({
url: (event.originalEvent.state || {}).hash || hash,
url: (event.originalEvent.state || {}).url || hash,

// When the url is either forward or backward in history include the entry
// as data on the event object for merging as data in the navigate event
Expand Down Expand Up @@ -264,14 +277,18 @@ define([
return this.stack[ this.activeIndex ];
},

getPrev: function() {
return this.stack[ this.activeIndex - 1 ];
getLast: function() {
return this.stack[ this.previousIndex ];
},

getNext: function() {
return this.stack[ this.activeIndex + 1 ];
},

getPrev: function() {
return this.stack[ this.activeIndex - 1 ];
},

// addNew is used whenever a new page is added
add: function( url, data ){
data = data || {};
Expand Down Expand Up @@ -341,7 +358,11 @@ define([
var newActiveIndex = this.closest( opts.url ), a = this.activeIndex;

// save new page index, null check to prevent falsey 0 result
this.activeIndex = newActiveIndex !== undefined ? newActiveIndex : this.activeIndex;
// record the previous index for reference
if( newActiveIndex !== undefined ) {
this.activeIndex = newActiveIndex;
this.previousIndex = a;
}

// invoke callbacks where appropriate
//
Expand All @@ -350,10 +371,6 @@ define([
( opts.present || opts.back || $.noop )( this.getActive(), 'back' );
} else if ( newActiveIndex > a ) {
( opts.present || opts.forward || $.noop )( this.getActive(), 'forward' );
} else if ( newActiveIndex === a ) {
if( opts.current ) {
opts.current( this.getActiveIndex );
}
} else if ( newActiveIndex === undefined && opts.missing ){
opts.missing( this.getActive() );
}
Expand Down
24 changes: 8 additions & 16 deletions js/navigation/path.js
Expand Up @@ -292,20 +292,6 @@ define([
reqUrl.search( /^https?:/ ) !== -1;
},

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

if ( dialogIndex > -1 ) {
url = url.slice( 0, dialogIndex ).replace("#", "") + "#" + url.slice( dialogIndex );
} else if ( url.indexOf( subkey ) > -1 ) {
url = url.split( subkey ).join( "#" + subkey );
}

return url;
},

mergeSearch: function( first, second ) {
var firstSearch, secondSearch,
resolvedSearch = {},
Expand Down Expand Up @@ -360,12 +346,18 @@ define([
// Get a hash where possible and, as long as it's not a path
// preserve it on the current url
preservedHash = (hashUri.hash || path.parseLocation().hash);
preservedHash = path.isPath( preservedHash ) ? "" : preservedHash;

if( path.isPath( preservedHash )) {
preservedHash = "";
}

if( preservedHash.indexOf( "#" ) == -1 && preservedHash !== "" ){
preservedHash = "#" + preservedHash;
}

// reconstruct each of the pieces with the new search string and hash
href = path.parseUrl( href );
href = href.protocol + "//" + href.host + href.pathname + hashUri.search + preservedHash;
href = path.resetUIKeys( href );
}

return href;
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/navigation/navigate_method.js
Expand Up @@ -189,5 +189,13 @@ $.testHelper.setPushState();

$.navigate.squash( url.pathname + url.search );
});


test( "navigating with an absolute url matching the current url save for the hash should transplant the hash", function() {
var loc = $.mobile.path.parseLocation();

$.navigate( loc.hrefNoHash + loc.hash + "foo" );
equal( location.hash, loc.hash + "foo", "the hash is set properly" );
});
}
})( jQuery );
3 changes: 3 additions & 0 deletions tests/unit/navigation/navigation_base.js
Expand Up @@ -79,10 +79,13 @@
});

// Navigate to an internal page.
console.log( $("#content-page-2 .ip1") );
window.foo = true;
$("#content-page-2 .ip1").click();
},

function(){
window.foo = false;
// Verify that we are on the expected page.
// the hash based nav result (hash:) is dictate by the fact that #internal-page-1
// is the original root page element
Expand Down
4 changes: 0 additions & 4 deletions tests/unit/navigation/navigation_core.js
Expand Up @@ -126,8 +126,6 @@ $.testHelper.delayStart();

asyncTest( "external page is cached in the DOM after pagehide", function(){
$.testHelper.pageSequence([
navigateTestRoot,

function(){
$.mobile.changePage( "cached-external.html" );
},
Expand All @@ -148,8 +146,6 @@ $.testHelper.delayStart();

asyncTest( "external page is cached in the DOM after pagehide when option is set globally", function(){
$.testHelper.pageSequence([
navigateTestRoot,

function(){
$.mobile.page.prototype.options.domCache = true;
$.mobile.changePage( "external.html" );
Expand Down

0 comments on commit 921bf77

Please sign in to comment.