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

Commit 921bf77

Browse files
committed
fix for transitions
1 parent 8ee4318 commit 921bf77

File tree

6 files changed

+63
-38
lines changed

6 files changed

+63
-38
lines changed

js/jquery.mobile.navigation.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ define( [
6666

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

@@ -792,7 +793,14 @@ define( [
792793

793794
// Normally, we tack on a dialog hash key, but if this is the location of a stale dialog,
794795
// we reuse the URL from the entry
795-
url = ( active.url || "" ) + ( alreadyThere ? "" : dialogHashKey );
796+
url = ( active.url || "" );
797+
798+
// account for absolute urls instead of just relative urls use as hashes
799+
if( !alreadyThere && url.indexOf("#") > -1 ) {
800+
url += dialogHashKey;
801+
} else {
802+
url += "#" + dialogHashKey;
803+
}
796804

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

11251133
// default options for the changPage calls made after examining the current state
1126-
// of the page and the hash
1134+
// of the page and the hash, NOTE that the transition is derived from the previous
1135+
// history entry
11271136
changePageOptions = {
1128-
transition: transition,
1137+
transition: (urlHistory.getLast() || {}).transition || transition,
11291138
changeHash: false,
11301139
fromHashChange: true,
11311140
reverse: data.direction === "back"

js/navigation/navigate.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,34 @@ define([
1717
// so that end users don't have to think about it. Punting for now
1818
// TODO !! move the event bindings into callbacks on the navigate event
1919
$.navigate = function( url, data, noEvents ) {
20-
var state, href, parsed, hash,
21-
resolutionUrl = path.isPath(url) ? path.getLocation() : $.mobile.getDocumentUrl();
20+
var state, href, parsed, loc, hash,
21+
resolutionUrl = path.isPath( url ) ? path.getLocation() : $.mobile.getDocumentUrl();
2222

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

2626
// Grab the hash for recording. If the passed url is a path
2727
// we used the parsed version of the squashed url to reconstruct,
2828
// otherwise we assume it's a hash and store it directly
29-
parsed = path.parseUrl( href );
30-
hash = path.isPath(url) ? parsed.pathname + parsed.search : url;
29+
parsed = path.parseUrl( url );
30+
loc = path.parseLocation();
31+
32+
if( loc.pathname + loc.search === parsed.pathname + parsed.search ) {
33+
// If the pathname and search of the passed url is identical to the current loc
34+
// then we must use the hash. Otherwise there will be no event
35+
// eg, url = "/foo/bar?baz#bang", location.href = "http://example.com/foo/bar?baz"
36+
hash = parsed.hash ? parsed.hash : parsed.pathname + parsed.search;
37+
} else if ( path.isPath(url) ) {
38+
var resolved = path.parseUrl( href );
39+
// If the passed url is a path, make it domain relative and remove any trailing hash
40+
hash = resolved.pathname + resolved.search;
41+
} else {
42+
hash = url;
43+
}
3144

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

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

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

267-
getPrev: function() {
268-
return this.stack[ this.activeIndex - 1 ];
280+
getLast: function() {
281+
return this.stack[ this.previousIndex ];
269282
},
270283

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

288+
getPrev: function() {
289+
return this.stack[ this.activeIndex - 1 ];
290+
},
291+
275292
// addNew is used whenever a new page is added
276293
add: function( url, data ){
277294
data = data || {};
@@ -341,7 +358,11 @@ define([
341358
var newActiveIndex = this.closest( opts.url ), a = this.activeIndex;
342359

343360
// save new page index, null check to prevent falsey 0 result
344-
this.activeIndex = newActiveIndex !== undefined ? newActiveIndex : this.activeIndex;
361+
// record the previous index for reference
362+
if( newActiveIndex !== undefined ) {
363+
this.activeIndex = newActiveIndex;
364+
this.previousIndex = a;
365+
}
345366

346367
// invoke callbacks where appropriate
347368
//
@@ -350,10 +371,6 @@ define([
350371
( opts.present || opts.back || $.noop )( this.getActive(), 'back' );
351372
} else if ( newActiveIndex > a ) {
352373
( opts.present || opts.forward || $.noop )( this.getActive(), 'forward' );
353-
} else if ( newActiveIndex === a ) {
354-
if( opts.current ) {
355-
opts.current( this.getActiveIndex );
356-
}
357374
} else if ( newActiveIndex === undefined && opts.missing ){
358375
opts.missing( this.getActive() );
359376
}

js/navigation/path.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,6 @@ define([
292292
reqUrl.search( /^https?:/ ) !== -1;
293293
},
294294

295-
resetUIKeys: function( url ) {
296-
var dialog = $.mobile.dialogHashKey,
297-
subkey = "&" + $.mobile.subPageUrlKey,
298-
dialogIndex = url.indexOf( dialog );
299-
300-
if ( dialogIndex > -1 ) {
301-
url = url.slice( 0, dialogIndex ).replace("#", "") + "#" + url.slice( dialogIndex );
302-
} else if ( url.indexOf( subkey ) > -1 ) {
303-
url = url.split( subkey ).join( "#" + subkey );
304-
}
305-
306-
return url;
307-
},
308-
309295
mergeSearch: function( first, second ) {
310296
var firstSearch, secondSearch,
311297
resolvedSearch = {},
@@ -360,12 +346,18 @@ define([
360346
// Get a hash where possible and, as long as it's not a path
361347
// preserve it on the current url
362348
preservedHash = (hashUri.hash || path.parseLocation().hash);
363-
preservedHash = path.isPath( preservedHash ) ? "" : preservedHash;
349+
350+
if( path.isPath( preservedHash )) {
351+
preservedHash = "";
352+
}
353+
354+
if( preservedHash.indexOf( "#" ) == -1 && preservedHash !== "" ){
355+
preservedHash = "#" + preservedHash;
356+
}
364357

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

371363
return href;

tests/unit/navigation/navigate_method.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,13 @@ $.testHelper.setPushState();
189189

190190
$.navigate.squash( url.pathname + url.search );
191191
});
192+
193+
194+
test( "navigating with an absolute url matching the current url save for the hash should transplant the hash", function() {
195+
var loc = $.mobile.path.parseLocation();
196+
197+
$.navigate( loc.hrefNoHash + loc.hash + "foo" );
198+
equal( location.hash, loc.hash + "foo", "the hash is set properly" );
199+
});
192200
}
193201
})( jQuery );

tests/unit/navigation/navigation_base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@
7979
});
8080

8181
// Navigate to an internal page.
82+
console.log( $("#content-page-2 .ip1") );
83+
window.foo = true;
8284
$("#content-page-2 .ip1").click();
8385
},
8486

8587
function(){
88+
window.foo = false;
8689
// Verify that we are on the expected page.
8790
// the hash based nav result (hash:) is dictate by the fact that #internal-page-1
8891
// is the original root page element

tests/unit/navigation/navigation_core.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ $.testHelper.delayStart();
126126

127127
asyncTest( "external page is cached in the DOM after pagehide", function(){
128128
$.testHelper.pageSequence([
129-
navigateTestRoot,
130-
131129
function(){
132130
$.mobile.changePage( "cached-external.html" );
133131
},
@@ -148,8 +146,6 @@ $.testHelper.delayStart();
148146

149147
asyncTest( "external page is cached in the DOM after pagehide when option is set globally", function(){
150148
$.testHelper.pageSequence([
151-
navigateTestRoot,
152-
153149
function(){
154150
$.mobile.page.prototype.options.domCache = true;
155151
$.mobile.changePage( "external.html" );

0 commit comments

Comments
 (0)