adding new HTTPRequest class that adds a 'url' and 'params' properties to a Layer, and gives a getFullRequestString() function. Thorough tests.
git-svn-id: http://svn.openlayers.org/trunk/openlayers@886 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -67,6 +67,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") {
|
||||
// "OpenLayers/Layer/Google.js",
|
||||
// "OpenLayers/Layer/VirtualEarth.js",
|
||||
// "OpenLayers/Layer/Yahoo.js",
|
||||
"OpenLayers/Layer/HTTPRequest.js",
|
||||
"OpenLayers/Layer/Grid.js",
|
||||
"OpenLayers/Layer/KaMap.js",
|
||||
"OpenLayers/Layer/Markers.js",
|
||||
|
||||
136
lib/OpenLayers/Layer/HTTPRequest.js
Normal file
136
lib/OpenLayers/Layer/HTTPRequest.js
Normal file
@@ -0,0 +1,136 @@
|
||||
/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
|
||||
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
|
||||
* text of the license. */
|
||||
// @require: OpenLayers/Layer.js
|
||||
// @require: OpenLayers/Util.js
|
||||
|
||||
/**
|
||||
* @class
|
||||
*/
|
||||
OpenLayers.Layer.HTTPRequest = Class.create();
|
||||
OpenLayers.Layer.HTTPRequest.prototype =
|
||||
Object.extend( new OpenLayers.Layer(), {
|
||||
|
||||
/** @type String */
|
||||
url: null,
|
||||
|
||||
/** @type Hash */
|
||||
params: null,
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*
|
||||
* @param {str} name
|
||||
* @param {str} url
|
||||
* @param {hash} params
|
||||
* @param {Object} options Hash of extra options to tag onto the layer
|
||||
*/
|
||||
initialize: function(name, url, params, options) {
|
||||
var newArguments = arguments;
|
||||
if (arguments.length > 0) {
|
||||
newArguments = [name, options];
|
||||
}
|
||||
OpenLayers.Layer.prototype.initialize.apply(this, newArguments);
|
||||
this.url = url;
|
||||
this.params = Object.extend( new Object(), params);
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
destroy: function() {
|
||||
this.url = null;
|
||||
this.params = null;
|
||||
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Object} obj
|
||||
*
|
||||
* @returns An exact clone of this OpenLayers.Layer.HTTPRequest
|
||||
* @type OpenLayers.Layer.HTTPRequest
|
||||
*/
|
||||
clone: function (obj) {
|
||||
|
||||
if (obj == null) {
|
||||
obj = new OpenLayers.Layer.HTTPRequest(this.name,
|
||||
this.url,
|
||||
this.params,
|
||||
this.options);
|
||||
}
|
||||
|
||||
//set any non-init vars here
|
||||
|
||||
//get all additions from superclasses
|
||||
return OpenLayers.Layer.prototype.clone.apply(this, [obj]);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} newUrl
|
||||
*/
|
||||
setUrl: function(newUrl) {
|
||||
this.url = newUrl;
|
||||
},
|
||||
|
||||
|
||||
/** Deprecated wrapper for mergeNewParams() just so as to not break
|
||||
* anyone's code who might be using this
|
||||
*
|
||||
* @deprecated
|
||||
* @param {Object} newParams
|
||||
*/
|
||||
changeParams:function(newParams) {
|
||||
this.mergeNewParams(newParams);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Object} newParams
|
||||
*/
|
||||
mergeNewParams:function(newParams) {
|
||||
this.params = Object.extend(this.params, newParams);
|
||||
},
|
||||
|
||||
|
||||
/** combine the layer's url with its params and these newParams.
|
||||
*
|
||||
* does checking on the serverPath variable, allowing for cases when it
|
||||
* is supplied with trailing ? or &, as well as cases where not.
|
||||
*
|
||||
* return in formatted string like this:
|
||||
* "server?key1=value1&key2=value2&key3=value3"
|
||||
*
|
||||
* @param {Object} newParams
|
||||
*
|
||||
* @type String
|
||||
*/
|
||||
getFullRequestString:function(newParams) {
|
||||
|
||||
//requestString always starts with url
|
||||
var requestString = this.url;
|
||||
|
||||
// create a new params hash with all the layer params and the
|
||||
// new params together. then convert to string
|
||||
var allParams = Object.extend(new Object(), this.params);
|
||||
var allParams = Object.extend(allParams, newParams);
|
||||
var paramsString = OpenLayers.Util.getParameterString(allParams);
|
||||
|
||||
if (paramsString != "") {
|
||||
var lastServerChar = this.url.charAt(this.url.length - 1);
|
||||
if ((lastServerChar == "&") || (lastServerChar == "?")) {
|
||||
requestString += paramsString;
|
||||
} else {
|
||||
if (this.url.indexOf('?') == -1) {
|
||||
//serverPath has no ? -- add one
|
||||
requestString += '?' + paramsString;
|
||||
} else {
|
||||
//serverPath contains ?, so must already have paramsString at the end
|
||||
requestString += '&' + paramsString;
|
||||
}
|
||||
}
|
||||
}
|
||||
return requestString;
|
||||
},
|
||||
|
||||
/** @final @type String */
|
||||
CLASS_NAME: "OpenLayers.Layer.HTTPRequest"
|
||||
});
|
||||
@@ -10,6 +10,7 @@
|
||||
<li>test_Events.html</li>
|
||||
<li>test_Util.html</li>
|
||||
<li>test_Layer.html</li>
|
||||
<li>test_Layer_HTTPRequest.html</li>
|
||||
<li>test_Layer_Markers.html</li>
|
||||
<li>test_Layer_Text.html</li>
|
||||
<li>test_Layer_GeoRSS.html</li>
|
||||
|
||||
193
tests/test_Layer_HTTPRequest.html
Normal file
193
tests/test_Layer_HTTPRequest.html
Normal file
@@ -0,0 +1,193 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="../lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript"><!--
|
||||
var layer;
|
||||
|
||||
var name = "Test Layer";
|
||||
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
|
||||
var params = { map: '/mapdata/vmap_wms.map',
|
||||
layers: 'basic',
|
||||
format: 'image/png'};
|
||||
var options = { chicken: 151, foo: "bar" };
|
||||
|
||||
function test_01_Layer_HTTPRequest_constructor (t) {
|
||||
t.plan( 5 );
|
||||
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options);
|
||||
|
||||
t.ok( layer instanceof OpenLayers.Layer.HTTPRequest, "new OpenLayers.Layer.HTTPRequest returns correctly typed object" );
|
||||
|
||||
// correct bubbling up to Layer.initialize()
|
||||
t.eq( layer.name, name, "layer.name is correct" );
|
||||
t.ok( ((layer.options["chicken"] == 151) && (layer.options["foo"] == "bar")), "layer.options correctly set" );
|
||||
|
||||
// HTTPRequest-specific properties
|
||||
t.eq( layer.url, url, "layer.name is correct" );
|
||||
t.ok( ((layer.params["map"] == '/mapdata/vmap_wms.map') &&
|
||||
(layer.params["layers"] == "basic") &&
|
||||
(layer.params["format"] == "image/png")), "layer.params correctly set" );
|
||||
|
||||
|
||||
}
|
||||
|
||||
function test_02_Layer_HTTPRequest_clone (t) {
|
||||
t.plan( 6 );
|
||||
|
||||
var toClone = new OpenLayers.Layer.HTTPRequest(name, url, params, options);
|
||||
toClone.chocolate = 5;
|
||||
|
||||
var layer = toClone.clone();
|
||||
|
||||
t.eq(layer.chocolate, 5, "correctly copied randomly assigned property");
|
||||
|
||||
t.ok( layer instanceof OpenLayers.Layer.HTTPRequest, "new OpenLayers.Layer.HTTPRequest returns correctly typed object" );
|
||||
|
||||
// correct bubbling up to Layer.initialize()
|
||||
t.eq( layer.name, name, "layer.name is correct" );
|
||||
t.ok( ((layer.options["chicken"] == 151) && (layer.options["foo"] == "bar")), "layer.options correctly set" );
|
||||
|
||||
// HTTPRequest-specific properties
|
||||
t.eq( layer.url, url, "layer.name is correct" );
|
||||
t.ok( ((layer.params["map"] == '/mapdata/vmap_wms.map') &&
|
||||
(layer.params["layers"] == "basic") &&
|
||||
(layer.params["format"] == "image/png")), "layer.params correctly set" );
|
||||
|
||||
}
|
||||
|
||||
function test_03_Layer_HTTPRequest_setUrl (t) {
|
||||
t.plan( 1 );
|
||||
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options);
|
||||
|
||||
layer.setUrl("foo");
|
||||
t.eq( layer.url, "foo", "setUrl() works");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
function test_04_Layer_HTTPRequest_changeParams (t) {
|
||||
t.plan( 3 );
|
||||
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options);
|
||||
|
||||
var newParams = { layers: 'sooper',
|
||||
chickpeas: 'image/png'};
|
||||
|
||||
layer.changeParams(newParams);
|
||||
|
||||
t.eq( layer.params.layers, "sooper", "changeParams() overwrites well");
|
||||
t.eq( layer.params.chickpeas, "image/png", "changeParams() adds well");
|
||||
|
||||
newParams.chickpeas = 151;
|
||||
|
||||
t.eq( layer.params.chickpeas, "image/png", "changeParams() makes clean copy of hash");
|
||||
|
||||
|
||||
}
|
||||
|
||||
function test_05_Layer_HTTPRequest_mergeNewParams (t) {
|
||||
t.plan( 3 );
|
||||
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, url, params, options);
|
||||
|
||||
var newParams = { layers: 'sooper',
|
||||
chickpeas: 'image/png'};
|
||||
|
||||
layer.mergeNewParams(newParams);
|
||||
|
||||
t.eq( layer.params.layers, "sooper", "changeParams() overwrites well");
|
||||
t.eq( layer.params.chickpeas, "image/png", "changeParams() adds well");
|
||||
|
||||
newParams.chickpeas = 151;
|
||||
|
||||
t.eq( layer.params.chickpeas, "image/png", "changeParams() makes clean copy of hash");
|
||||
|
||||
|
||||
}
|
||||
|
||||
function test_06_Layer_HTTPRequest_getFullRequestString (t) {
|
||||
|
||||
tParams = { layers: 'basic',
|
||||
format: 'image/png'};
|
||||
|
||||
t.plan( 7 );
|
||||
|
||||
// without ?
|
||||
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv";
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null);
|
||||
str = layer.getFullRequestString();
|
||||
t.eq(str, "http://octo.metacarta.com/cgi-bin/mapserv?layers=basic&format=image/png", "getFullRequestString() works for url sans ?");
|
||||
|
||||
|
||||
// with ?
|
||||
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv?";
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null);
|
||||
str = layer.getFullRequestString();
|
||||
t.eq(str, "http://octo.metacarta.com/cgi-bin/mapserv?layers=basic&format=image/png", "getFullRequestString() works for url with ?");
|
||||
|
||||
// with ?param1=5
|
||||
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv?param1=5";
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null);
|
||||
str = layer.getFullRequestString();
|
||||
t.eq(str, "http://octo.metacarta.com/cgi-bin/mapserv?param1=5&layers=basic&format=image/png", "getFullRequestString() works for url with ?param1=5");
|
||||
|
||||
|
||||
// with ?param1=5&
|
||||
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv?param1=5&";
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null);
|
||||
str = layer.getFullRequestString();
|
||||
t.eq(str, "http://octo.metacarta.com/cgi-bin/mapserv?param1=5&layers=basic&format=image/png", "getFullRequestString() works for url with ?param1=5&");
|
||||
|
||||
// passing in new params
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, tParams, null);
|
||||
str = layer.getFullRequestString( { chicken: 6,
|
||||
layers:"road" } );
|
||||
t.eq(str, "http://octo.metacarta.com/cgi-bin/mapserv?param1=5&layers=road&format=image/png&chicken=6", "getFullRequestString() works for passing in new params");
|
||||
|
||||
// layer with null params
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, null, null);
|
||||
str = layer.getFullRequestString();
|
||||
t.eq(str, "http://octo.metacarta.com/cgi-bin/mapserv?param1=5&", "getFullRequestString() works for layer with null params");
|
||||
|
||||
// layer with null params passing in new params
|
||||
layer = new OpenLayers.Layer.HTTPRequest(name, tUrl, null, null);
|
||||
str = layer.getFullRequestString( { chicken: 6,
|
||||
layers:"road" } );
|
||||
t.eq(str, "http://octo.metacarta.com/cgi-bin/mapserv?param1=5&chicken=6&layers=road", "getFullRequestString() works for layer with null params passing in new params");
|
||||
|
||||
}
|
||||
|
||||
function test_99_Layer_HTTPRequest_destroy (t) {
|
||||
t.plan( 6 );
|
||||
|
||||
var map = new OpenLayers.Map('map');
|
||||
|
||||
layer = new OpenLayers.Layer.HTTPRequest("Test Layer",
|
||||
"http://www.openlayers.org",
|
||||
{ foo: 2, bar: 3},
|
||||
{ opt1: 8, opt2: 9});
|
||||
|
||||
map.addLayer(layer);
|
||||
layer.destroy();
|
||||
|
||||
// Ensure Layer.destroy() is called
|
||||
t.eq( layer.name, null, "layer.name is null after destroy" );
|
||||
t.eq( layer.div, null, "layer.div is null after destroy" );
|
||||
t.eq( layer.map, null, "layer.map is null after destroy" );
|
||||
t.eq( layer.options, null, "layer.options is null after destroy" );
|
||||
|
||||
|
||||
// Specific to HTTPRequest
|
||||
t.eq( layer.url, null, "layer.url is null after destroy" );
|
||||
t.eq( layer.params, null, "layer.params is null after destroy" );
|
||||
}
|
||||
// -->
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user