rename and deprecate getArgs() function in favor of getParameters(), make it such that it parses comma-separated values from key/value pairs into Arrays (since they are encoded that way). (Closes #860)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@4052 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -769,8 +769,63 @@ OpenLayers.Util.distVincenty=function(p1, p2) {
|
||||
return d;
|
||||
};
|
||||
|
||||
/**
|
||||
* Function: getParameters
|
||||
* Parse the parameters from a URL or from the current page itself into a
|
||||
* JavaScript Object. Note that parameter values with commas are separated
|
||||
* out into an Array.
|
||||
*
|
||||
* Parameters:
|
||||
* url - {String} Optional url used to extract the query string.
|
||||
* If null, query string is taken from page location.
|
||||
*
|
||||
* Return:
|
||||
* {Object} An object of key/value pairs from the query string.
|
||||
*/
|
||||
OpenLayers.Util.getParameters = function(url) {
|
||||
//if no url specified, take it from the location bar
|
||||
url = url || window.location.href
|
||||
if(url == null) {
|
||||
url = window.location.href;
|
||||
}
|
||||
|
||||
//parse out parameters portion of url string
|
||||
var paramsString = "";
|
||||
if (url.contains('?')) {
|
||||
var start = url.indexOf('?') + 1;
|
||||
var end = url.contains("#") ? url.indexOf('#') : url.length;
|
||||
paramsString = url.substring(start, end);
|
||||
}
|
||||
|
||||
var parameters = {};
|
||||
var pairs = paramsString.split(/[&;]/);
|
||||
for(var i = 0; i < pairs.length; ++i) {
|
||||
var keyValue = pairs[i].split('=');
|
||||
if (keyValue[0]) {
|
||||
var key = decodeURIComponent(keyValue[0]);
|
||||
var value = keyValue[1] || ''; //empty string if no value
|
||||
|
||||
//decode individual values
|
||||
value = value.split(",");
|
||||
for(var j=0; j < value.length; j++) {
|
||||
value[j] = decodeURIComponent(value[j]);
|
||||
}
|
||||
|
||||
//if there's only one value, do not return as array
|
||||
if (value.length == 1) {
|
||||
value = value[0];
|
||||
}
|
||||
|
||||
parameters[key] = value;
|
||||
}
|
||||
}
|
||||
return parameters;
|
||||
};
|
||||
|
||||
/**
|
||||
* Function: getArgs
|
||||
* Deprecated - Will be removed in 3.0.
|
||||
* Please use instead OpenLayers.Util.getParameters
|
||||
*
|
||||
* Parameters:
|
||||
* url - {String} Optional url used to extract the query string.
|
||||
@@ -780,37 +835,12 @@ OpenLayers.Util.distVincenty=function(p1, p2) {
|
||||
* {Object} An object of key/value pairs from the query string.
|
||||
*/
|
||||
OpenLayers.Util.getArgs = function(url) {
|
||||
if(url == null) {
|
||||
url = window.location.href;
|
||||
}
|
||||
|
||||
var start = url.indexOf('?');
|
||||
var stop = url.indexOf('#');
|
||||
|
||||
if (start != -1) {
|
||||
if (stop != -1) {
|
||||
var query = url.substring(start + 1, stop);
|
||||
} else {
|
||||
var query = url.substring(start + 1);
|
||||
}
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
|
||||
var args = {};
|
||||
pairs = query.split(/[&;]/);
|
||||
for(var i = 0; i < pairs.length; ++i) {
|
||||
keyValue = pairs[i].split('=');
|
||||
if (keyValue[0]) {
|
||||
if (keyValue[1]) {
|
||||
args[decodeURIComponent(keyValue[0])] = decodeURIComponent(keyValue[1]);
|
||||
} else {
|
||||
args[decodeURIComponent(keyValue[0])] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
var err = "The getArgs() function is deprecated and will be removed " +
|
||||
"with the 3.0 version of OpenLayers. Please instead use " +
|
||||
"OpenLayers.Util.getParameters().";
|
||||
OpenLayers.Console.warn(err);
|
||||
return OpenLayers.Util.getParameters(url);
|
||||
};
|
||||
|
||||
/**
|
||||
* Property: lastSeqID
|
||||
|
||||
@@ -567,16 +567,43 @@
|
||||
t.eq(OpenLayers.Util.createDiv().id, "OpenLayersDiv3", "Div created is sequential, starting at lastSeqID in Util.");
|
||||
}
|
||||
|
||||
function test_Util_getArgs(t) {
|
||||
t.plan(5);
|
||||
t.eq(OpenLayers.Util.getArgs('http://www.example.com'), {}, "getArgs works when args = ''");
|
||||
t.eq(OpenLayers.Util.getArgs('http://www.example.com?'), {}, "getArgs works when args = '?'");
|
||||
t.eq(OpenLayers.Util.getArgs('http://www.example.com?hello=world&foo=bar'),
|
||||
function test_Util_getParameters(t) {
|
||||
t.plan(6);
|
||||
t.eq(OpenLayers.Util.getParameters('http://www.example.com'), {}, "getArgs works when args = ''");
|
||||
t.eq(OpenLayers.Util.getParameters('http://www.example.com?'), {}, "getArgs works when args = '?'");
|
||||
t.eq(OpenLayers.Util.getParameters('http://www.example.com?hello=world&foo=bar'),
|
||||
{'hello' : 'world', 'foo': 'bar'}, "getArgs works when args = '?hello=world&foo=bar'");
|
||||
t.eq(OpenLayers.Util.getArgs('http://www.example.com?hello=&foo=bar'),
|
||||
t.eq(OpenLayers.Util.getParameters('http://www.example.com?hello=&foo=bar'),
|
||||
{'hello' : '', 'foo': 'bar'}, "getArgs works when args = '?hello=&foo=bar'");
|
||||
t.eq(OpenLayers.Util.getArgs('http://www.example.com?foo=bar#bugssucks'),
|
||||
t.eq(OpenLayers.Util.getParameters('http://www.example.com?foo=bar#bugssucks'),
|
||||
{'foo': 'bar'}, "getArgs works when using a fragment identifier");
|
||||
t.eq(OpenLayers.Util.getParameters('http://www.example.com?foo=bar,pub,disco'),
|
||||
{'foo': ['bar', 'pub', 'disco']}, "getArgs works with a comma-separated value (parses into array)");
|
||||
}
|
||||
|
||||
function test_Util_getArgs(t) {
|
||||
//DEPRECATED -- to be removed in 3.0
|
||||
t.plan(3);
|
||||
|
||||
var temp = OpenLayers.Console.warn;
|
||||
OpenLayers.Console.warn = function(err) {
|
||||
t.ok(err != null, "warning is fired on use of getArgs()");
|
||||
}
|
||||
|
||||
var temp2 = OpenLayers.Util.getParameters;
|
||||
OpenLayers.Util.getParameters = function(url) {
|
||||
t.eq(url, g_Url, "correct url passed to getParameters()");
|
||||
return g_Params;
|
||||
}
|
||||
|
||||
g_Params = {};
|
||||
g_Url = {};
|
||||
|
||||
var ret = OpenLayers.Util.getArgs(g_Url);
|
||||
t.ok( ret == g_Params, "correctly returns value from getParameters");
|
||||
|
||||
OpenLayers.Console.warn = temp;
|
||||
OpenLayers.Util.getParameters = temp2;
|
||||
}
|
||||
|
||||
function tests_Util_extend(t) {
|
||||
|
||||
Reference in New Issue
Block a user