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/Markers.js |
---|
9 | * @requires OpenLayers/Request/XMLHttpRequest.js |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Class: OpenLayers.Layer.GeoRSS |
---|
14 | * Add GeoRSS Point features to your map. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Layer.Markers> |
---|
18 | * - <OpenLayers.Layer> |
---|
19 | */ |
---|
20 | OpenLayers.Layer.GeoRSS = OpenLayers.Class(OpenLayers.Layer.Markers, { |
---|
21 | |
---|
22 | /** |
---|
23 | * Property: location |
---|
24 | * {String} store url of text file |
---|
25 | */ |
---|
26 | location: null, |
---|
27 | |
---|
28 | /** |
---|
29 | * Property: features |
---|
30 | * {Array(<OpenLayers.Feature>)} |
---|
31 | */ |
---|
32 | features: null, |
---|
33 | |
---|
34 | /** |
---|
35 | * APIProperty: formatOptions |
---|
36 | * {Object} Hash of options which should be passed to the format when it is |
---|
37 | * created. Must be passed in the constructor. |
---|
38 | */ |
---|
39 | formatOptions: null, |
---|
40 | |
---|
41 | /** |
---|
42 | * Property: selectedFeature |
---|
43 | * {<OpenLayers.Feature>} |
---|
44 | */ |
---|
45 | selectedFeature: null, |
---|
46 | |
---|
47 | /** |
---|
48 | * APIProperty: icon |
---|
49 | * {<OpenLayers.Icon>}. This determines the Icon to be used on the map |
---|
50 | * for this GeoRSS layer. |
---|
51 | */ |
---|
52 | icon: null, |
---|
53 | |
---|
54 | /** |
---|
55 | * APIProperty: popupSize |
---|
56 | * {<OpenLayers.Size>} This determines the size of GeoRSS popups. If |
---|
57 | * not provided, defaults to 250px by 120px. |
---|
58 | */ |
---|
59 | popupSize: null, |
---|
60 | |
---|
61 | /** |
---|
62 | * APIProperty: useFeedTitle |
---|
63 | * {Boolean} Set layer.name to the first <title> element in the feed. Default is true. |
---|
64 | */ |
---|
65 | useFeedTitle: true, |
---|
66 | |
---|
67 | /** |
---|
68 | * Constructor: OpenLayers.Layer.GeoRSS |
---|
69 | * Create a GeoRSS Layer. |
---|
70 | * |
---|
71 | * Parameters: |
---|
72 | * name - {String} |
---|
73 | * location - {String} |
---|
74 | * options - {Object} |
---|
75 | */ |
---|
76 | initialize: function(name, location, options) { |
---|
77 | OpenLayers.Layer.Markers.prototype.initialize.apply(this, [name, options]); |
---|
78 | this.location = location; |
---|
79 | this.features = []; |
---|
80 | }, |
---|
81 | |
---|
82 | /** |
---|
83 | * Method: destroy |
---|
84 | */ |
---|
85 | destroy: function() { |
---|
86 | // Warning: Layer.Markers.destroy() must be called prior to calling |
---|
87 | // clearFeatures() here, otherwise we leak memory. Indeed, if |
---|
88 | // Layer.Markers.destroy() is called after clearFeatures(), it won't be |
---|
89 | // able to remove the marker image elements from the layer's div since |
---|
90 | // the markers will have been destroyed by clearFeatures(). |
---|
91 | OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments); |
---|
92 | this.clearFeatures(); |
---|
93 | this.features = null; |
---|
94 | }, |
---|
95 | |
---|
96 | /** |
---|
97 | * Method: loadRSS |
---|
98 | * Start the load of the RSS data. Don't do this when we first add the layer, |
---|
99 | * since we may not be visible at any point, and it would therefore be a waste. |
---|
100 | */ |
---|
101 | loadRSS: function() { |
---|
102 | if (!this.loaded) { |
---|
103 | this.events.triggerEvent("loadstart"); |
---|
104 | OpenLayers.Request.GET({ |
---|
105 | url: this.location, |
---|
106 | success: this.parseData, |
---|
107 | scope: this |
---|
108 | }); |
---|
109 | this.loaded = true; |
---|
110 | } |
---|
111 | }, |
---|
112 | |
---|
113 | /** |
---|
114 | * Method: moveTo |
---|
115 | * If layer is visible and RSS has not been loaded, load RSS. |
---|
116 | * |
---|
117 | * Parameters: |
---|
118 | * bounds - {Object} |
---|
119 | * zoomChanged - {Object} |
---|
120 | * minor - {Object} |
---|
121 | */ |
---|
122 | moveTo:function(bounds, zoomChanged, minor) { |
---|
123 | OpenLayers.Layer.Markers.prototype.moveTo.apply(this, arguments); |
---|
124 | if(this.visibility && !this.loaded){ |
---|
125 | this.loadRSS(); |
---|
126 | } |
---|
127 | }, |
---|
128 | |
---|
129 | /** |
---|
130 | * Method: parseData |
---|
131 | * Parse the data returned from the Events call. |
---|
132 | * |
---|
133 | * Parameters: |
---|
134 | * ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>} |
---|
135 | */ |
---|
136 | parseData: function(ajaxRequest) { |
---|
137 | var doc = ajaxRequest.responseXML; |
---|
138 | if (!doc || !doc.documentElement) { |
---|
139 | doc = OpenLayers.Format.XML.prototype.read(ajaxRequest.responseText); |
---|
140 | } |
---|
141 | |
---|
142 | if (this.useFeedTitle) { |
---|
143 | var name = null; |
---|
144 | try { |
---|
145 | name = doc.getElementsByTagNameNS('*', 'title')[0].firstChild.nodeValue; |
---|
146 | } |
---|
147 | catch (e) { |
---|
148 | name = doc.getElementsByTagName('title')[0].firstChild.nodeValue; |
---|
149 | } |
---|
150 | if (name) { |
---|
151 | this.setName(name); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | var options = {}; |
---|
156 | |
---|
157 | OpenLayers.Util.extend(options, this.formatOptions); |
---|
158 | |
---|
159 | if (this.map && !this.projection.equals(this.map.getProjectionObject())) { |
---|
160 | options.externalProjection = this.projection; |
---|
161 | options.internalProjection = this.map.getProjectionObject(); |
---|
162 | } |
---|
163 | |
---|
164 | var format = new OpenLayers.Format.GeoRSS(options); |
---|
165 | var features = format.read(doc); |
---|
166 | |
---|
167 | for (var i=0, len=features.length; i<len; i++) { |
---|
168 | var data = {}; |
---|
169 | var feature = features[i]; |
---|
170 | |
---|
171 | // we don't support features with no geometry in the GeoRSS |
---|
172 | // layer at this time. |
---|
173 | if (!feature.geometry) { |
---|
174 | continue; |
---|
175 | } |
---|
176 | |
---|
177 | var title = feature.attributes.title ? |
---|
178 | feature.attributes.title : "Untitled"; |
---|
179 | |
---|
180 | var description = feature.attributes.description ? |
---|
181 | feature.attributes.description : "No description."; |
---|
182 | |
---|
183 | var link = feature.attributes.link ? feature.attributes.link : ""; |
---|
184 | |
---|
185 | var location = feature.geometry.getBounds().getCenterLonLat(); |
---|
186 | |
---|
187 | |
---|
188 | data.icon = this.icon == null ? |
---|
189 | OpenLayers.Marker.defaultIcon() : |
---|
190 | this.icon.clone(); |
---|
191 | |
---|
192 | data.popupSize = this.popupSize ? |
---|
193 | this.popupSize.clone() : |
---|
194 | new OpenLayers.Size(250, 120); |
---|
195 | |
---|
196 | if (title || description) { |
---|
197 | // we have supplemental data, store them. |
---|
198 | data.title = title; |
---|
199 | data.description = description; |
---|
200 | |
---|
201 | var contentHTML = '<div class="olLayerGeoRSSClose">[x]</div>'; |
---|
202 | contentHTML += '<div class="olLayerGeoRSSTitle">'; |
---|
203 | if (link) { |
---|
204 | contentHTML += '<a class="link" href="'+link+'" target="_blank">'; |
---|
205 | } |
---|
206 | contentHTML += title; |
---|
207 | if (link) { |
---|
208 | contentHTML += '</a>'; |
---|
209 | } |
---|
210 | contentHTML += '</div>'; |
---|
211 | contentHTML += '<div style="" class="olLayerGeoRSSDescription">'; |
---|
212 | contentHTML += description; |
---|
213 | contentHTML += '</div>'; |
---|
214 | data['popupContentHTML'] = contentHTML; |
---|
215 | } |
---|
216 | var feature = new OpenLayers.Feature(this, location, data); |
---|
217 | this.features.push(feature); |
---|
218 | var marker = feature.createMarker(); |
---|
219 | marker.events.register('click', feature, this.markerClick); |
---|
220 | this.addMarker(marker); |
---|
221 | } |
---|
222 | this.events.triggerEvent("loadend"); |
---|
223 | }, |
---|
224 | |
---|
225 | /** |
---|
226 | * Method: markerClick |
---|
227 | * |
---|
228 | * Parameters: |
---|
229 | * evt - {Event} |
---|
230 | */ |
---|
231 | markerClick: function(evt) { |
---|
232 | var sameMarkerClicked = (this == this.layer.selectedFeature); |
---|
233 | this.layer.selectedFeature = (!sameMarkerClicked) ? this : null; |
---|
234 | for(var i=0, len=this.layer.map.popups.length; i<len; i++) { |
---|
235 | this.layer.map.removePopup(this.layer.map.popups[i]); |
---|
236 | } |
---|
237 | if (!sameMarkerClicked) { |
---|
238 | var popup = this.createPopup(); |
---|
239 | OpenLayers.Event.observe(popup.div, "click", |
---|
240 | OpenLayers.Function.bind(function() { |
---|
241 | for(var i=0, len=this.layer.map.popups.length; i<len; i++) { |
---|
242 | this.layer.map.removePopup(this.layer.map.popups[i]); |
---|
243 | } |
---|
244 | }, this) |
---|
245 | ); |
---|
246 | this.layer.map.addPopup(popup); |
---|
247 | } |
---|
248 | OpenLayers.Event.stop(evt); |
---|
249 | }, |
---|
250 | |
---|
251 | /** |
---|
252 | * Method: clearFeatures |
---|
253 | * Destroy all features in this layer. |
---|
254 | */ |
---|
255 | clearFeatures: function() { |
---|
256 | if (this.features != null) { |
---|
257 | while(this.features.length > 0) { |
---|
258 | var feature = this.features[0]; |
---|
259 | OpenLayers.Util.removeItem(this.features, feature); |
---|
260 | feature.destroy(); |
---|
261 | } |
---|
262 | } |
---|
263 | }, |
---|
264 | |
---|
265 | CLASS_NAME: "OpenLayers.Layer.GeoRSS" |
---|
266 | }); |
---|