Merge query strings in $.path rather than duplicating

Fixes common duplications like ?locale=en_US&locale=en_US,
triggered e.g. by gridfield's showDetailView().

See https://github.com/silverstripe/silverstripe-translatable/issues/154
This commit is contained in:
Ingo Schommer 2013-12-07 11:14:46 +01:00
parent 8ad172cff5
commit 41031900f9

View File

@ -160,11 +160,32 @@
},
//Add search (aka query) params to the specified url.
// 2013-12-06 ischommer: Customized to merge with existing keys
addSearchParams: function( url, params ) {
var u = path.parseUrl( url ),
p = ( typeof params === "object" ) ? $.param( params ) : params,
s = u.search || "?";
return u.hrefNoSearch + s + ( s.charAt( s.length - 1 ) !== "?" ? "&" : "" ) + p + ( u.hash || "" );
params = ( typeof params === "string" ) ? path.convertSearchToArray( params ) : params,
newParams = $.extend( path.convertSearchToArray( u.search ), params );
return u.hrefNoSearch + '?' + $.param( newParams ) + ( u.hash || "" );
},
// 2013-12-06 ischommer: Added to allow merge with existing keys
getSearchParams: function(url) {
var u = path.parseUrl( url );
return path.convertSearchToArray( u.search );
},
// Converts query strings (foo=bar&baz=bla) to a hash.
// TODO Handle repeating elements (e.g. arr[]=one&arr[]=two)
// 2013-12-06 ischommer: Added to allow merge with existing keys
convertSearchToArray: function(search) {
var params = {},
search = search.replace( /^\?/, '' ),
parts = search ? search.split( '&' ) : [], i, tmp;
for(i=0; i < parts.length; i++) {
tmp = parts[i].split( '=' );
params[tmp[0]] = tmp[1];
}
return params;
},
convertUrlToDataUrl: function( absUrl ) {