Skip to content

Commit 4ef44a6

Browse files
author
Gabriel Schulhof
committed
Navigation: Hash processing demo updates page halfway through transition
Closes jquery-archivegh-7792 Fixes jquery-archivegh-7775
1 parent fecdc49 commit 4ef44a6

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

demos/navigation-hash-processing/index.php

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,59 @@
1717
// Helper function that splits a URL just the way we want it
1818
var processHash = function( url ) {
1919
var parsed = $.mobile.path.parseUrl( url ),
20+
queryParameters = {},
2021
hashQuery = parsed.hash.split( "?" );
2122

23+
// Create name: value pairs from the query parameters
24+
$.each( ( hashQuery.length > 1 ? hashQuery[ 1 ] : "" ).split( "&" ), function() {
25+
var pair = this.split( "=" );
26+
27+
if ( pair.length > 0 && pair[ 0 ] ) {
28+
queryParameters[ pair[ 0 ] ] =
29+
( pair.length > 1 ? pair[ 1 ] : true );
30+
}
31+
});
32+
2233
return {
2334
parsed: parsed,
2435
cleanHash: ( hashQuery.length > 0 ? hashQuery[ 0 ] : "" ),
25-
queryParameters: ( hashQuery.length > 1 ? hashQuery[ 1 ] : "" )
36+
queryParameters: queryParameters
2637
};
2738
};
2839

2940
$.mobile.document
30-
.on( "pagebeforechange", function( event, data ) {
3141

32-
// When we go from #secondary-page to #secondary-page we wish to indicate
33-
// that a transition to the same page is allowed.
34-
if ( $.type( data.toPage ) === "string" &&
35-
data.options.fromPage &&
36-
data.options.fromPage.attr( "id" ) === "secondary-page" &&
37-
processHash( data.toPage ).cleanHash === "#secondary-page" ) {
42+
// When the page is about to change, we may want to modify the navigation process to
43+
// accommodate same-page navigation. Since we wish to make it appear as though we're navigating
44+
// between different pages, we need to queue the page update to occur right at the halfway
45+
// point of the transition associated with page-to-page navigation.
46+
.on( "pagecontainerbeforechange", function( event, data ) {
47+
var processedHash;
3848

39-
data.options.allowSamePageTransition = true;
40-
}
41-
})
42-
.on( "pagecontainerbeforetransition", function( event, data ) {
43-
var queryParameters = {},
44-
processedHash = processHash( data.absUrl );
45-
46-
// We only modify default behaviour when navigating to the secondary page
47-
if ( processedHash.cleanHash === "#secondary-page" ) {
48-
49-
// Assemble query parameters object from the query string
50-
if ( processedHash.queryParameters ) {
51-
$.each( processedHash.queryParameters.split( "&" ),
52-
function( index, value ) {
53-
var pair = value.split( "=" );
54-
55-
if ( pair.length > 0 && pair[ 0 ] ) {
56-
queryParameters[ pair[ 0 ] ] =
57-
( pair.length > 1 ? pair[ 1 ] : true );
58-
}
59-
});
60-
}
49+
if ( typeof data.toPage === "string" ) {
50+
processedHash = processHash( data.toPage );
51+
52+
// We only affect navigation behavior when going to #secondary-page
53+
if ( processedHash.cleanHash === "#secondary-page" ) {
54+
55+
// Set the url of the page - this will be used by navigation to set the
56+
// URL in the location bar
57+
$( "#secondary-page" ).jqmData( "url", processedHash.parsed.hash );
6158

62-
// Set the title from the query parameters
63-
$( "#section" ).text( queryParameters.section );
59+
// Allow same-page transition when coming from #secondary page
60+
data.options.allowSamePageTransition = ( data.options.fromPage &&
61+
data.options.fromPage.attr( "id" ) === "secondary-page" );
6462

65-
// Set the url of the page - this will be used by navigation to set the
66-
// URL in the location bar
67-
$( "#secondary-page" ).jqmData( "url", processedHash.parsed.hash );
63+
// Update the page when the outgoing animation completes. This involves two things:
64+
// 1. Removing the active class from the button used for navigation.
65+
// 2. Updating the page to make it look like the destination page.
66+
$.mobile.activePage.animationComplete( function() {
67+
$.mobile.removeActiveLinkClass( true );
68+
69+
// Set the title from the query parameters
70+
$( "#section" ).text( processedHash.queryParameters.section );
71+
});
72+
}
6873
}
6974
});
7075
})( jQuery );

0 commit comments

Comments
 (0)