|
17 | 17 | // Helper function that splits a URL just the way we want it |
18 | 18 | var processHash = function( url ) { |
19 | 19 | var parsed = $.mobile.path.parseUrl( url ), |
| 20 | + queryParameters = {}, |
20 | 21 | hashQuery = parsed.hash.split( "?" ); |
21 | 22 |
|
| 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 | + |
22 | 33 | return { |
23 | 34 | parsed: parsed, |
24 | 35 | cleanHash: ( hashQuery.length > 0 ? hashQuery[ 0 ] : "" ), |
25 | | - queryParameters: ( hashQuery.length > 1 ? hashQuery[ 1 ] : "" ) |
| 36 | + queryParameters: queryParameters |
26 | 37 | }; |
27 | 38 | }; |
28 | 39 |
|
29 | 40 | $.mobile.document |
30 | | - .on( "pagebeforechange", function( event, data ) { |
31 | 41 |
|
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; |
38 | 48 |
|
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 ); |
61 | 58 |
|
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" ); |
64 | 62 |
|
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 | + } |
68 | 73 | } |
69 | 74 | }); |
70 | 75 | })( jQuery ); |
|
0 commit comments