@@ -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 }
0 commit comments