From 41031900f98e24513fdf674a8f301d38c45d0b24 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sat, 7 Dec 2013 11:14:46 +0100 Subject: [PATCH] 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 --- admin/javascript/lib.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/admin/javascript/lib.js b/admin/javascript/lib.js index d4b989610..a8ad7b6d2 100644 --- a/admin/javascript/lib.js +++ b/admin/javascript/lib.js @@ -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 ) {