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/Tile.js |
---|
9 | * @requires OpenLayers/Request/XMLHttpRequest.js |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Class: OpenLayers.Tile.WFS |
---|
14 | * Instances of OpenLayers.Tile.WFS are used to manage the image tiles |
---|
15 | * used by various layers. Create a new image tile with the |
---|
16 | * <OpenLayers.Tile.WFS> constructor. |
---|
17 | * |
---|
18 | * Inherits from: |
---|
19 | * - <OpenLayers.Tile> |
---|
20 | */ |
---|
21 | OpenLayers.Tile.WFS = OpenLayers.Class(OpenLayers.Tile, { |
---|
22 | |
---|
23 | /** |
---|
24 | * Property: features |
---|
25 | * {Array(<OpenLayers.Feature>)} list of features in this tile |
---|
26 | */ |
---|
27 | features: null, |
---|
28 | |
---|
29 | /** |
---|
30 | * Property: url |
---|
31 | * {String} |
---|
32 | */ |
---|
33 | url: null, |
---|
34 | |
---|
35 | /** |
---|
36 | * Property: request |
---|
37 | * {<OpenLayers.Request.XMLHttpRequest>} |
---|
38 | */ |
---|
39 | request: null, |
---|
40 | |
---|
41 | /** TBD 3.0 - reorder the parameters to the init function to put URL |
---|
42 | * as last, so we can continue to call tile.initialize() |
---|
43 | * without changing the arguments. |
---|
44 | * |
---|
45 | * Constructor: OpenLayers.Tile.WFS |
---|
46 | * Constructor for a new <OpenLayers.Tile.WFS> instance. |
---|
47 | * |
---|
48 | * Parameters: |
---|
49 | * layer - {<OpenLayers.Layer>} layer that the tile will go in. |
---|
50 | * position - {<OpenLayers.Pixel>} |
---|
51 | * bounds - {<OpenLayers.Bounds>} |
---|
52 | * url - {<String>} |
---|
53 | * size - {<OpenLayers.Size>} |
---|
54 | */ |
---|
55 | initialize: function(layer, position, bounds, url, size) { |
---|
56 | OpenLayers.Tile.prototype.initialize.apply(this, arguments); |
---|
57 | this.url = url; |
---|
58 | this.features = []; |
---|
59 | }, |
---|
60 | |
---|
61 | /** |
---|
62 | * APIMethod: destroy |
---|
63 | * nullify references to prevent circular references and memory leaks |
---|
64 | */ |
---|
65 | destroy: function() { |
---|
66 | OpenLayers.Tile.prototype.destroy.apply(this, arguments); |
---|
67 | this.destroyAllFeatures(); |
---|
68 | this.features = null; |
---|
69 | this.url = null; |
---|
70 | if(this.request) { |
---|
71 | this.request.abort(); |
---|
72 | //this.request.destroy(); |
---|
73 | this.request = null; |
---|
74 | } |
---|
75 | }, |
---|
76 | |
---|
77 | /** |
---|
78 | * Method: clear |
---|
79 | * Clear the tile of any bounds/position-related data so that it can |
---|
80 | * be reused in a new location. |
---|
81 | */ |
---|
82 | clear: function() { |
---|
83 | this.destroyAllFeatures(); |
---|
84 | }, |
---|
85 | |
---|
86 | /** |
---|
87 | * Method: draw |
---|
88 | * Check that a tile should be drawn, and load features for it. |
---|
89 | */ |
---|
90 | draw:function() { |
---|
91 | if (OpenLayers.Tile.prototype.draw.apply(this, arguments)) { |
---|
92 | if (this.isLoading) { |
---|
93 | //if already loading, send 'reload' instead of 'loadstart'. |
---|
94 | this.events.triggerEvent("reload"); |
---|
95 | } else { |
---|
96 | this.isLoading = true; |
---|
97 | this.events.triggerEvent("loadstart"); |
---|
98 | } |
---|
99 | this.loadFeaturesForRegion(this.requestSuccess); |
---|
100 | } |
---|
101 | }, |
---|
102 | |
---|
103 | /** |
---|
104 | * Method: loadFeaturesForRegion |
---|
105 | * Abort any pending requests and issue another request for data. |
---|
106 | * |
---|
107 | * Input are function pointers for what to do on success and failure. |
---|
108 | * |
---|
109 | * Parameters: |
---|
110 | * success - {function} |
---|
111 | * failure - {function} |
---|
112 | */ |
---|
113 | loadFeaturesForRegion:function(success, failure) { |
---|
114 | if(this.request) { |
---|
115 | this.request.abort(); |
---|
116 | } |
---|
117 | this.request = OpenLayers.Request.GET({ |
---|
118 | url: this.url, |
---|
119 | success: success, |
---|
120 | failure: failure, |
---|
121 | scope: this |
---|
122 | }); |
---|
123 | }, |
---|
124 | |
---|
125 | /** |
---|
126 | * Method: requestSuccess |
---|
127 | * Called on return from request succcess. Adds results via |
---|
128 | * layer.addFeatures in vector mode, addResults otherwise. |
---|
129 | * |
---|
130 | * Parameters: |
---|
131 | * request - {<OpenLayers.Request.XMLHttpRequest>} |
---|
132 | */ |
---|
133 | requestSuccess:function(request) { |
---|
134 | if (this.features) { |
---|
135 | var doc = request.responseXML; |
---|
136 | if (!doc || !doc.documentElement) { |
---|
137 | doc = request.responseText; |
---|
138 | } |
---|
139 | if (this.layer.vectorMode) { |
---|
140 | this.layer.addFeatures(this.layer.formatObject.read(doc)); |
---|
141 | } else { |
---|
142 | var xml = new OpenLayers.Format.XML(); |
---|
143 | if (typeof doc == "string") { |
---|
144 | doc = xml.read(doc); |
---|
145 | } |
---|
146 | var resultFeatures = xml.getElementsByTagNameNS( |
---|
147 | doc, "http://www.opengis.net/gml", "featureMember" |
---|
148 | ); |
---|
149 | this.addResults(resultFeatures); |
---|
150 | } |
---|
151 | } |
---|
152 | if (this.events) { |
---|
153 | this.events.triggerEvent("loadend"); |
---|
154 | } |
---|
155 | |
---|
156 | //request produced with success, we can delete the request object. |
---|
157 | //this.request.destroy(); |
---|
158 | this.request = null; |
---|
159 | }, |
---|
160 | |
---|
161 | /** |
---|
162 | * Method: addResults |
---|
163 | * Construct new feature via layer featureClass constructor, and add to |
---|
164 | * this.features. |
---|
165 | * |
---|
166 | * Parameters: |
---|
167 | * results - {Object} |
---|
168 | */ |
---|
169 | addResults: function(results) { |
---|
170 | for (var i=0; i < results.length; i++) { |
---|
171 | var feature = new this.layer.featureClass(this.layer, |
---|
172 | results[i]); |
---|
173 | this.features.push(feature); |
---|
174 | } |
---|
175 | }, |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * Method: destroyAllFeatures |
---|
180 | * Iterate through and call destroy() on each feature, removing it from |
---|
181 | * the local array |
---|
182 | */ |
---|
183 | destroyAllFeatures: function() { |
---|
184 | while(this.features.length > 0) { |
---|
185 | var feature = this.features.shift(); |
---|
186 | feature.destroy(); |
---|
187 | } |
---|
188 | }, |
---|
189 | |
---|
190 | CLASS_NAME: "OpenLayers.Tile.WFS" |
---|
191 | } |
---|
192 | ); |
---|