comparison PalanthirExplorer/libs/jqm.page.params.js @ 45:33d67e1ab173

r
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Sep 2012 13:24:59 +0200
parents PalantirExplorer/libs/jqm.page.params.js@3959d33612cc
children
comparison
equal deleted inserted replaced
43:9be852ad33d2 45:33d67e1ab173
1 // jqm.page.params.js - version 0.1
2 // Copyright (c) 2011, Kin Blas
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the <organization> nor the
13 // names of its contributors may be used to endorse or promote products
14 // derived from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 (function( $, window, undefined ) {
28
29 // Given a query string, convert all the name/value pairs
30 // into a property/value object. If a name appears more than
31 // once in a query string, the value is automatically turned
32 // into an array.
33 function queryStringToObject( qstr )
34 {
35 var result = {},
36 nvPairs = ( ( qstr || "" ).replace( /^\?/, "" ).split( /&/ ) ),
37 i, pair, n, v;
38
39 for ( i = 0; i < nvPairs.length; i++ ) {
40 var pstr = nvPairs[ i ];
41 if ( pstr ) {
42 pair = pstr.split( /=/ );
43 n = pair[ 0 ];
44 v = pair[ 1 ];
45 if ( result[ n ] === undefined ) {
46 result[ n ] = v;
47 } else {
48 if ( typeof result[ n ] !== "object" ) {
49 result[ n ] = [ result[ n ] ];
50 }
51 result[ n ].push( v );
52 }
53 }
54 }
55
56 return result;
57 }
58
59 // The idea here is to listen for any pagebeforechange notifications from
60 // jQuery Mobile, and then muck with the toPage and options so that query
61 // params can be passed to embedded/internal pages. So for example, if a
62 // changePage() request for a URL like:
63 //
64 // http://mycompany.com/myapp/#page-1?foo=1&bar=2
65 //
66 // is made, the page that will actually get shown is:
67 //
68 // http://mycompany.com/myapp/#page-1
69 //
70 // The browser's location will still be updated to show the original URL.
71 // The query params for the embedded page are also added as a property/value
72 // object on the options object. You can access it from your page notifications
73 // via data.options.pageData.
74 $( document ).bind( "pagebeforechange", function( e, data ) {
75
76 // We only want to handle the case where we are being asked
77 // to go to a page by URL, and only if that URL is referring
78 // to an internal page by id.
79
80 if ( typeof data.toPage === "string" ) {
81 var u = $.mobile.path.parseUrl( data.toPage );
82 if ( $.mobile.path.isEmbeddedPage( u ) ) {
83 // The request is for an internal page, if the hash
84 // contains query (search) params, strip them off the
85 // toPage URL and then set options.dataUrl appropriately
86 // so the location.hash shows the originally requested URL
87 // that hash the query params in the hash.
88
89 var u2 = $.mobile.path.parseUrl( u.hash.replace( /^#/, "" ) );
90 if ( u2.search ) {
91 if ( !data.options.dataUrl ) {
92 data.options.dataUrl = data.toPage;
93 }
94 data.options.pageData = queryStringToObject( u2.search );
95 data.toPage = u.hrefNoHash + "#" + u2.pathname;
96 }
97 }
98 }
99 });
100
101 })( jQuery, window );
102
103
104
105
106 // http://stackoverflow.com/a/8295488
107 $(document).bind("pagebeforechange", function( event, data ) {
108 $.mobile.pageData = (data && data.options && data.options.pageData)
109 ? data.options.pageData
110 : {};
111 });