Skip to content

Commit 6b01049

Browse files
committed
minor refactors in nav and pushstate for clarity
1 parent 82cce1f commit 6b01049

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

js/jquery.mobile.navigation.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@
293293
this.activeIndex = newActiveIndex !== undefined ? newActiveIndex : this.activeIndex;
294294

295295
if( back ) {
296-
opts.isBack( prev, back );
296+
(opts.either || opts.isBack)( back );
297297
} else if( forward ) {
298-
opts.isForward( prev, back );
298+
(opts.either || opts.isForward)( back );
299299
}
300300
},
301301

@@ -1185,12 +1185,14 @@
11851185
});
11861186
} );
11871187

1188-
$.mobile.handleHashChange = function( hash ) {
1188+
$.mobile._handleHashChange = function( hash ) {
11891189
//find first page via hash
1190-
var reverse, role, to = path.stripHash( hash ),
1190+
var to = path.stripHash( hash ),
11911191
//transition is false if it's the first page, undefined otherwise (and may be overridden by default)
11921192
transition = $.mobile.urlHistory.stack.length === 0 ? "none" : undefined,
11931193

1194+
// default options for the changPage calls made after examining the current state
1195+
// of the page and the hash
11941196
changePageOptions = {
11951197
transition: transition,
11961198
changeHash: false,
@@ -1220,34 +1222,43 @@
12201222
// prevent changepage
12211223
return;
12221224
} else {
1223-
var setTo = function( previousPage, isBack ) {
1224-
var active = $.mobile.urlHistory.getActive();
1225-
1226-
to = active.pageUrl;
1227-
role = active.role;
1228-
transition = active.transition;
1229-
reverse = isBack;
1230-
};
12311225
// if the current active page is a dialog and we're navigating
12321226
// to a dialog use the dialog objected saved in the stack
1233-
urlHistory.directHashChange({ currentUrl: to, isBack: setTo, isForward: setTo });
1227+
urlHistory.directHashChange({
1228+
currentUrl: to,
1229+
1230+
// regardless of the direction of the history change
1231+
// do the following
1232+
either: function( isBack ) {
1233+
var active = $.mobile.urlHistory.getActive();
1234+
1235+
to = active.pageUrl;
1236+
1237+
// make sure to set the role, transition and reversal
1238+
// as most of this is lost by the domCache cleaning
1239+
$.extend( changePageOptions, {
1240+
role: active.role,
1241+
transition: active.transition,
1242+
reverse: isBack
1243+
});
1244+
}
1245+
});
12341246
}
12351247
}
12361248

12371249
//if to is defined, load it
12381250
if ( to ) {
12391251
to = ( typeof to === "string" && !path.isPath( to ) ) ? ( '#' + to ) : to;
1240-
$.mobile.changePage( to, { transition: transition, changeHash: false, fromHashChange: true , role: role, reverse: reverse } );
1241-
}
1242-
//there's no hash, go to the first page in the dom
1243-
else {
1244-
$.mobile.changePage( $.mobile.firstPage, { transition: transition, changeHash: false, fromHashChange: true, roll: roll, reverse: reverse } );
1252+
$.mobile.changePage( to, changePageOptions );
1253+
} else {
1254+
//there's no hash, go to the first page in the dom
1255+
$.mobile.changePage( $.mobile.firstPage, changePageOptions );
12451256
}
12461257
};
12471258

12481259
//hashchange event handler
12491260
$window.bind( "hashchange", function( e, triggered ) {
1250-
$.mobile.handleHashChange( location.hash );
1261+
$.mobile._handleHashChange( location.hash );
12511262
});
12521263

12531264
//set page min-heights to be device specific

js/jquery.mobile.navigation.pushstate.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@
3535
},
3636

3737
isSubHashPage: function( page ) {
38+
// if the page is a dialog, a subpage, or an embedded page
39+
// then the hash will have been maintained
3840
return page.is( "[role='dialog']" ) ||
39-
page.jqmData("url").indexOf( $.mobile.subPageUrlKey ) >= 0;
41+
page.jqmData("url").indexOf( $.mobile.subPageUrlKey ) >= 0 ||
42+
page.jqmData("url") == page.attr( "id" );
4043
},
4144

4245
resetUIKeys: function( url ) {
4346
var dialog = $.mobile.dialogHashKey,
44-
subkey = "&" + $.mobile.subPageUrlKey;
47+
subkey = "&" + $.mobile.subPageUrlKey,
48+
dialogIndex = url.indexOf( dialog );
4549

46-
if( url.indexOf( dialog ) > -1 ) {
47-
var split = url.split( dialog );
48-
split.push( "" );
49-
url = split[0] + "#" + split.slice( 1, split.length ).join( dialog );
50+
if( dialogIndex > -1 ) {
51+
url = url.slice( 0, dialogIndex ) + "#" + url.slice( dialogIndex );
5052
} else if( url.indexOf( subkey ) > -1 ) {
5153
url = url.split( subkey ).join( "#" + subkey );
5254
}
@@ -88,13 +90,15 @@
8890
if( poppedState ) {
8991
// can't test the hash directly because the url has already been altered, possibly to
9092
// one without a hash, so we check if the page on display is one that would have
91-
// generated a hash
93+
// generated a hash. Generally speaking a crappy solution.
9294
if( self.isSubHashPage( $.mobile.activePage ) ){
9395
holdnexthashchange = true;
9496
}
9597

96-
$.mobile.handleHashChange( poppedState.hash );
98+
// change the page based on the hash
99+
$.mobile._handleHashChange( poppedState.hash );
97100

101+
// disable the next hash change if the page we came from has a hash
98102
$.mobile.urlHistory.ignoreNextHashChange = holdnexthashchange;
99103
}
100104
},

0 commit comments

Comments
 (0)