1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | |
---|
7 | /** |
---|
8 | * @requires OpenLayers/Layer.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Layer.HTTPRequest |
---|
13 | * |
---|
14 | * Inherits from: |
---|
15 | * - <OpenLayers.Layer> |
---|
16 | */ |
---|
17 | OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, { |
---|
18 | |
---|
19 | /** |
---|
20 | * Constant: URL_HASH_FACTOR |
---|
21 | * {Float} Used to hash URL param strings for multi-WMS server selection. |
---|
22 | * Set to the Golden Ratio per Knuth's recommendation. |
---|
23 | */ |
---|
24 | URL_HASH_FACTOR: (Math.sqrt(5) - 1) / 2, |
---|
25 | |
---|
26 | /** |
---|
27 | * Property: url |
---|
28 | * {Array(String) or String} This is either an array of url strings or |
---|
29 | * a single url string. |
---|
30 | */ |
---|
31 | url: null, |
---|
32 | |
---|
33 | /** |
---|
34 | * Property: params |
---|
35 | * {Object} Hashtable of key/value parameters |
---|
36 | */ |
---|
37 | params: null, |
---|
38 | |
---|
39 | /** |
---|
40 | * APIProperty: reproject |
---|
41 | * *Deprecated*. See http://trac.openlayers.org/wiki/SpatialMercator |
---|
42 | * for information on the replacement for this functionality. |
---|
43 | * {Boolean} Whether layer should reproject itself based on base layer |
---|
44 | * locations. This allows reprojection onto commercial layers. |
---|
45 | * Default is false: Most layers can't reproject, but layers |
---|
46 | * which can create non-square geographic pixels can, like WMS. |
---|
47 | * |
---|
48 | */ |
---|
49 | reproject: false, |
---|
50 | |
---|
51 | /** |
---|
52 | * Constructor: OpenLayers.Layer.HTTPRequest |
---|
53 | * |
---|
54 | * Parameters: |
---|
55 | * name - {String} |
---|
56 | * url - {Array(String) or String} |
---|
57 | * params - {Object} |
---|
58 | * options - {Object} Hashtable of extra options to tag onto the layer |
---|
59 | */ |
---|
60 | initialize: function(name, url, params, options) { |
---|
61 | var newArguments = arguments; |
---|
62 | newArguments = [name, options]; |
---|
63 | OpenLayers.Layer.prototype.initialize.apply(this, newArguments); |
---|
64 | this.url = url; |
---|
65 | this.params = OpenLayers.Util.extend( {}, params); |
---|
66 | }, |
---|
67 | |
---|
68 | /** |
---|
69 | * APIMethod: destroy |
---|
70 | */ |
---|
71 | destroy: function() { |
---|
72 | this.url = null; |
---|
73 | this.params = null; |
---|
74 | OpenLayers.Layer.prototype.destroy.apply(this, arguments); |
---|
75 | }, |
---|
76 | |
---|
77 | /** |
---|
78 | * APIMethod: clone |
---|
79 | * |
---|
80 | * Parameters: |
---|
81 | * obj - {Object} |
---|
82 | * |
---|
83 | * Returns: |
---|
84 | * {<OpenLayers.Layer.HTTPRequest>} An exact clone of this |
---|
85 | * <OpenLayers.Layer.HTTPRequest> |
---|
86 | */ |
---|
87 | clone: function (obj) { |
---|
88 | |
---|
89 | if (obj == null) { |
---|
90 | obj = new OpenLayers.Layer.HTTPRequest(this.name, |
---|
91 | this.url, |
---|
92 | this.params, |
---|
93 | this.getOptions()); |
---|
94 | } |
---|
95 | |
---|
96 | //get all additions from superclasses |
---|
97 | obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]); |
---|
98 | |
---|
99 | // copy/set any non-init, non-simple values here |
---|
100 | |
---|
101 | return obj; |
---|
102 | }, |
---|
103 | |
---|
104 | /** |
---|
105 | * APIMethod: setUrl |
---|
106 | * |
---|
107 | * Parameters: |
---|
108 | * newUrl - {String} |
---|
109 | */ |
---|
110 | setUrl: function(newUrl) { |
---|
111 | this.url = newUrl; |
---|
112 | }, |
---|
113 | |
---|
114 | /** |
---|
115 | * APIMethod: mergeNewParams |
---|
116 | * |
---|
117 | * Parameters: |
---|
118 | * newParams - {Object} |
---|
119 | * |
---|
120 | * Returns: |
---|
121 | * redrawn: {Boolean} whether the layer was actually redrawn. |
---|
122 | */ |
---|
123 | mergeNewParams:function(newParams) { |
---|
124 | this.params = OpenLayers.Util.extend(this.params, newParams); |
---|
125 | var ret = this.redraw(); |
---|
126 | if(this.map != null) { |
---|
127 | this.map.events.triggerEvent("changelayer", { |
---|
128 | layer: this, |
---|
129 | property: "params" |
---|
130 | }); |
---|
131 | } |
---|
132 | return ret; |
---|
133 | }, |
---|
134 | |
---|
135 | /** |
---|
136 | * APIMethod: redraw |
---|
137 | * Redraws the layer. Returns true if the layer was redrawn, false if not. |
---|
138 | * |
---|
139 | * Parameters: |
---|
140 | * force - {Boolean} Force redraw by adding random parameter. |
---|
141 | * |
---|
142 | * Returns: |
---|
143 | * {Boolean} The layer was redrawn. |
---|
144 | */ |
---|
145 | redraw: function(force) { |
---|
146 | if (force) { |
---|
147 | return this.mergeNewParams({"_olSalt": Math.random()}); |
---|
148 | } else { |
---|
149 | return OpenLayers.Layer.prototype.redraw.apply(this, []); |
---|
150 | } |
---|
151 | }, |
---|
152 | |
---|
153 | /** |
---|
154 | * Method: selectUrl |
---|
155 | * selectUrl() implements the standard floating-point multiplicative |
---|
156 | * hash function described by Knuth, and hashes the contents of the |
---|
157 | * given param string into a float between 0 and 1. This float is then |
---|
158 | * scaled to the size of the provided urls array, and used to select |
---|
159 | * a URL. |
---|
160 | * |
---|
161 | * Parameters: |
---|
162 | * paramString - {String} |
---|
163 | * urls - {Array(String)} |
---|
164 | * |
---|
165 | * Returns: |
---|
166 | * {String} An entry from the urls array, deterministically selected based |
---|
167 | * on the paramString. |
---|
168 | */ |
---|
169 | selectUrl: function(paramString, urls) { |
---|
170 | var product = 1; |
---|
171 | for (var i=0, len=paramString.length; i<len; i++) { |
---|
172 | product *= paramString.charCodeAt(i) * this.URL_HASH_FACTOR; |
---|
173 | product -= Math.floor(product); |
---|
174 | } |
---|
175 | return urls[Math.floor(product * urls.length)]; |
---|
176 | }, |
---|
177 | |
---|
178 | /** |
---|
179 | * Method: getFullRequestString |
---|
180 | * Combine url with layer's params and these newParams. |
---|
181 | * |
---|
182 | * does checking on the serverPath variable, allowing for cases when it |
---|
183 | * is supplied with trailing ? or &, as well as cases where not. |
---|
184 | * |
---|
185 | * return in formatted string like this: |
---|
186 | * "server?key1=value1&key2=value2&key3=value3" |
---|
187 | * |
---|
188 | * WARNING: The altUrl parameter is deprecated and will be removed in 3.0. |
---|
189 | * |
---|
190 | * Parameters: |
---|
191 | * newParams - {Object} |
---|
192 | * altUrl - {String} Use this as the url instead of the layer's url |
---|
193 | * |
---|
194 | * Returns: |
---|
195 | * {String} |
---|
196 | */ |
---|
197 | getFullRequestString:function(newParams, altUrl) { |
---|
198 | |
---|
199 | // if not altUrl passed in, use layer's url |
---|
200 | var url = altUrl || this.url; |
---|
201 | |
---|
202 | // create a new params hashtable with all the layer params and the |
---|
203 | // new params together. then convert to string |
---|
204 | var allParams = OpenLayers.Util.extend({}, this.params); |
---|
205 | allParams = OpenLayers.Util.extend(allParams, newParams); |
---|
206 | var paramsString = OpenLayers.Util.getParameterString(allParams); |
---|
207 | |
---|
208 | // if url is not a string, it should be an array of strings, |
---|
209 | // in which case we will deterministically select one of them in |
---|
210 | // order to evenly distribute requests to different urls. |
---|
211 | // |
---|
212 | if (url instanceof Array) { |
---|
213 | url = this.selectUrl(paramsString, url); |
---|
214 | } |
---|
215 | |
---|
216 | // ignore parameters that are already in the url search string |
---|
217 | var urlParams = |
---|
218 | OpenLayers.Util.upperCaseObject(OpenLayers.Util.getParameters(url)); |
---|
219 | for(var key in allParams) { |
---|
220 | if(key.toUpperCase() in urlParams) { |
---|
221 | delete allParams[key]; |
---|
222 | } |
---|
223 | } |
---|
224 | paramsString = OpenLayers.Util.getParameterString(allParams); |
---|
225 | |
---|
226 | return OpenLayers.Util.urlAppend(url, paramsString); |
---|
227 | }, |
---|
228 | |
---|
229 | CLASS_NAME: "OpenLayers.Layer.HTTPRequest" |
---|
230 | }); |
---|