in the URL. This makes the precedence of parameters: * params in URL * params in Layer * DEFAULT_PARAMS on the Layer. git-svn-id: http://svn.openlayers.org/trunk/openlayers@1907 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
154 lines
4.9 KiB
JavaScript
154 lines
4.9 KiB
JavaScript
/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
|
|
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
|
|
* for the full text of the license. */
|
|
|
|
|
|
/**
|
|
* @class
|
|
*
|
|
* @requires OpenLayers/Layer.js
|
|
*/
|
|
OpenLayers.Layer.HTTPRequest = OpenLayers.Class.create();
|
|
OpenLayers.Layer.HTTPRequest.prototype =
|
|
OpenLayers.Class.inherit( OpenLayers.Layer, {
|
|
|
|
/** @type String */
|
|
url: null,
|
|
|
|
/** Hashtable of key/value parameters
|
|
* @type Object */
|
|
params: null,
|
|
|
|
/** Whether layer should reproject itself based on base layer locations.
|
|
* This allows reprojection onto commercial layers. Default is false:
|
|
* Most layers can't reproject, but layers which can create non-square
|
|
* geographic pixels can, like WMS.
|
|
*
|
|
* @type Boolean
|
|
*/
|
|
reproject: false,
|
|
|
|
/**
|
|
* @constructor
|
|
*
|
|
* @param {String} name
|
|
* @param {String} url
|
|
* @param {Object} params
|
|
* @param {Object} options Hashtable of extra options to tag onto the layer
|
|
*/
|
|
initialize: function(name, url, params, options) {
|
|
var newArguments = arguments;
|
|
newArguments = [name, options];
|
|
OpenLayers.Layer.prototype.initialize.apply(this, newArguments);
|
|
this.url = url;
|
|
this.params = OpenLayers.Util.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);
|
|
}
|
|
|
|
//get all additions from superclasses
|
|
obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
|
|
|
|
// copy/set any non-init, non-simple values here
|
|
|
|
return obj;
|
|
},
|
|
|
|
/**
|
|
* @param {String} newUrl
|
|
*/
|
|
setUrl: function(newUrl) {
|
|
this.url = newUrl;
|
|
},
|
|
|
|
/**
|
|
* @param {Object} newParams
|
|
*/
|
|
mergeNewParams:function(newParams) {
|
|
this.params = OpenLayers.Util.extend(this.params, newParams);
|
|
},
|
|
|
|
|
|
/** combine url with layer's 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
|
|
* @param {String} altUrl Use this as the url instead of the layer's url
|
|
*
|
|
* @type String
|
|
*/
|
|
getFullRequestString:function(newParams, altUrl) {
|
|
|
|
// use layer's url unless altUrl passed in
|
|
var url = (altUrl == null) ? this.url : altUrl;
|
|
|
|
// if url is not a string, it should be an array of strings,
|
|
// in which case we will randomly select one of them in order
|
|
// to evenly distribute requests to different urls.
|
|
if (typeof url == "object") {
|
|
url = url[Math.floor(Math.random()*url.length)];
|
|
}
|
|
// requestString always starts with url
|
|
var requestString = url;
|
|
|
|
// create a new params hashtable with all the layer params and the
|
|
// new params together. then convert to string
|
|
var allParams = OpenLayers.Util.extend(new Object(), this.params);
|
|
allParams = OpenLayers.Util.extend(allParams, newParams);
|
|
// ignore parameters that are already in the url search string
|
|
var urlParams = OpenLayers.Util.upperCaseObject(
|
|
OpenLayers.Util.getArgs(url));
|
|
for(var key in allParams) {
|
|
if(key.toUpperCase() in urlParams) {
|
|
delete allParams[key];
|
|
}
|
|
}
|
|
var paramsString = OpenLayers.Util.getParameterString(allParams);
|
|
if (paramsString != "") {
|
|
var lastServerChar = url.charAt(url.length - 1);
|
|
if ((lastServerChar == "&") || (lastServerChar == "?")) {
|
|
requestString += paramsString;
|
|
} else {
|
|
if (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"
|
|
});
|