1 | /** |
---|
2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
3 | * |
---|
4 | * Published under the BSD license. |
---|
5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
6 | * of the license. |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @include GeoExt/widgets/LegendImage.js |
---|
11 | * @requires GeoExt/widgets/LayerLegend.js |
---|
12 | */ |
---|
13 | |
---|
14 | /** api: (define) |
---|
15 | * module = GeoExt |
---|
16 | * class = WMSLegend |
---|
17 | */ |
---|
18 | |
---|
19 | /** api: (extends) |
---|
20 | * GeoExt/widgets/LayerLegend.js |
---|
21 | */ |
---|
22 | Ext.namespace('GeoExt'); |
---|
23 | |
---|
24 | /** api: constructor |
---|
25 | * .. class:: WMSLegend(config) |
---|
26 | * |
---|
27 | * Show a legend image for a WMS layer. The image can be read from the styles |
---|
28 | * field of a layer record (if the record comes e.g. from a |
---|
29 | * :class:`GeoExt.data.WMSCapabilitiesReader`). If not provided, a |
---|
30 | * GetLegendGraphic request will be issued to retrieve the image. |
---|
31 | */ |
---|
32 | GeoExt.WMSLegend = Ext.extend(GeoExt.LayerLegend, { |
---|
33 | |
---|
34 | /** api: config[defaultStyleIsFirst] |
---|
35 | * ``Boolean`` |
---|
36 | * The WMS spec does not say if the first style advertised for a layer in |
---|
37 | * a Capabilities document is the default style that the layer is |
---|
38 | * rendered with. We make this assumption by default. To be strictly WMS |
---|
39 | * compliant, set this to false, but make sure to configure a STYLES |
---|
40 | * param with your WMS layers, otherwise LegendURLs advertised in the |
---|
41 | * GetCapabilities document cannot be used. |
---|
42 | */ |
---|
43 | defaultStyleIsFirst: true, |
---|
44 | |
---|
45 | /** api: config[useScaleParameter] |
---|
46 | * ``Boolean`` |
---|
47 | * Should we use the optional SCALE parameter in the SLD WMS |
---|
48 | * GetLegendGraphic request? Defaults to true. |
---|
49 | */ |
---|
50 | useScaleParameter: true, |
---|
51 | |
---|
52 | /** api: config[baseParams] |
---|
53 | * ``Object`` |
---|
54 | * Optional parameters to add to the legend url, this can e.g. be used to |
---|
55 | * support vendor-specific parameters in a SLD WMS GetLegendGraphic |
---|
56 | * request. To override the default MIME type of image/gif use the |
---|
57 | * FORMAT parameter in baseParams. |
---|
58 | * |
---|
59 | * .. code-block:: javascript |
---|
60 | * |
---|
61 | * var legendPanel = new GeoExt.LegendPanel({ |
---|
62 | * map: map, |
---|
63 | * title: 'Legend Panel', |
---|
64 | * defaults: { |
---|
65 | * style: 'padding:5px', |
---|
66 | * baseParams: { |
---|
67 | * FORMAT: 'image/png', |
---|
68 | * LEGEND_OPTIONS: 'forceLabels:on' |
---|
69 | * } |
---|
70 | * } |
---|
71 | * }); |
---|
72 | */ |
---|
73 | baseParams: null, |
---|
74 | |
---|
75 | /** private: method[initComponent] |
---|
76 | * Initializes the WMS legend. For group layers it will create multiple |
---|
77 | * image box components. |
---|
78 | */ |
---|
79 | initComponent: function() { |
---|
80 | GeoExt.WMSLegend.superclass.initComponent.call(this); |
---|
81 | var layer = this.layerRecord.getLayer(); |
---|
82 | this._noMap = !layer.map; |
---|
83 | layer.events.register("moveend", this, this.onLayerMoveend); |
---|
84 | this.update(); |
---|
85 | }, |
---|
86 | |
---|
87 | /** private: method[onLayerMoveend] |
---|
88 | * :param e: ``Object`` |
---|
89 | */ |
---|
90 | onLayerMoveend: function(e) { |
---|
91 | if ((e.zoomChanged === true && this.useScaleParameter === true) || |
---|
92 | this._noMap) { |
---|
93 | delete this._noMap; |
---|
94 | this.update(); |
---|
95 | } |
---|
96 | }, |
---|
97 | |
---|
98 | /** private: method[getLegendUrl] |
---|
99 | * :param layerName: ``String`` A sublayer. |
---|
100 | * :param layerNames: ``Array(String)`` The array of sublayers, |
---|
101 | * read from this.layerRecord if not provided. |
---|
102 | * :return: ``String`` The legend URL. |
---|
103 | * |
---|
104 | * Get the legend URL of a sublayer. |
---|
105 | */ |
---|
106 | getLegendUrl: function(layerName, layerNames) { |
---|
107 | var rec = this.layerRecord; |
---|
108 | var url; |
---|
109 | var styles = rec && rec.get("styles"); |
---|
110 | var layer = rec.getLayer(); |
---|
111 | layerNames = layerNames || [layer.params.LAYERS].join(",").split(","); |
---|
112 | |
---|
113 | var styleNames = layer.params.STYLES && |
---|
114 | [layer.params.STYLES].join(",").split(","); |
---|
115 | var idx = layerNames.indexOf(layerName); |
---|
116 | var styleName = styleNames && styleNames[idx]; |
---|
117 | // check if we have a legend URL in the record's |
---|
118 | // "styles" data field |
---|
119 | if(styles && styles.length > 0) { |
---|
120 | if(styleName) { |
---|
121 | Ext.each(styles, function(s) { |
---|
122 | url = (s.name == styleName && s.legend) && s.legend.href; |
---|
123 | return !url; |
---|
124 | }); |
---|
125 | } else if(this.defaultStyleIsFirst === true && !styleNames && |
---|
126 | !layer.params.SLD && !layer.params.SLD_BODY) { |
---|
127 | url = styles[0].legend && styles[0].legend.href; |
---|
128 | } |
---|
129 | } |
---|
130 | if(!url) { |
---|
131 | url = layer.getFullRequestString({ |
---|
132 | REQUEST: "GetLegendGraphic", |
---|
133 | WIDTH: null, |
---|
134 | HEIGHT: null, |
---|
135 | EXCEPTIONS: "application/vnd.ogc.se_xml", |
---|
136 | LAYER: layerName, |
---|
137 | LAYERS: null, |
---|
138 | STYLE: (styleName !== '') ? styleName: null, |
---|
139 | STYLES: null, |
---|
140 | SRS: null, |
---|
141 | FORMAT: null |
---|
142 | }); |
---|
143 | } |
---|
144 | // add scale parameter - also if we have the url from the record's |
---|
145 | // styles data field and it is actually a GetLegendGraphic request. |
---|
146 | if(this.useScaleParameter === true && |
---|
147 | url.toLowerCase().indexOf("request=getlegendgraphic") != -1) { |
---|
148 | var scale = layer.map.getScale(); |
---|
149 | url = Ext.urlAppend(url, "SCALE=" + scale); |
---|
150 | } |
---|
151 | var params = this.baseParams || {}; |
---|
152 | Ext.applyIf(params, {FORMAT: 'image/gif'}); |
---|
153 | if(url.indexOf('?') > 0) { |
---|
154 | url = Ext.urlEncode(params, url); |
---|
155 | } |
---|
156 | |
---|
157 | return url; |
---|
158 | }, |
---|
159 | |
---|
160 | /** private: method[update] |
---|
161 | * Update the legend, adding, removing or updating |
---|
162 | * the per-sublayer box component. |
---|
163 | */ |
---|
164 | update: function() { |
---|
165 | var layer = this.layerRecord.getLayer(); |
---|
166 | // In some cases, this update function is called on a layer |
---|
167 | // that has just been removed, see ticket #238. |
---|
168 | // The following check bypass the update if map is not set. |
---|
169 | if(!(layer && layer.map)) { |
---|
170 | return; |
---|
171 | } |
---|
172 | GeoExt.WMSLegend.superclass.update.apply(this, arguments); |
---|
173 | |
---|
174 | var layerNames, layerName, i, len; |
---|
175 | |
---|
176 | layerNames = [layer.params.LAYERS].join(",").split(","); |
---|
177 | |
---|
178 | var destroyList = []; |
---|
179 | var textCmp = this.items.get(0); |
---|
180 | this.items.each(function(cmp) { |
---|
181 | i = layerNames.indexOf(cmp.itemId); |
---|
182 | if(i < 0 && cmp != textCmp) { |
---|
183 | destroyList.push(cmp); |
---|
184 | } else if(cmp !== textCmp){ |
---|
185 | layerName = layerNames[i]; |
---|
186 | var newUrl = this.getLegendUrl(layerName, layerNames); |
---|
187 | if(!OpenLayers.Util.isEquivalentUrl(newUrl, cmp.url)) { |
---|
188 | cmp.setUrl(newUrl); |
---|
189 | } |
---|
190 | } |
---|
191 | }, this); |
---|
192 | for(i = 0, len = destroyList.length; i<len; i++) { |
---|
193 | var cmp = destroyList[i]; |
---|
194 | // cmp.destroy() does not remove the cmp from |
---|
195 | // its parent container! |
---|
196 | this.remove(cmp); |
---|
197 | cmp.destroy(); |
---|
198 | } |
---|
199 | |
---|
200 | for(i = 0, len = layerNames.length; i<len; i++) { |
---|
201 | layerName = layerNames[i]; |
---|
202 | if(!this.items || !this.getComponent(layerName)) { |
---|
203 | this.add({ |
---|
204 | xtype: "gx_legendimage", |
---|
205 | url: this.getLegendUrl(layerName, layerNames), |
---|
206 | itemId: layerName |
---|
207 | }); |
---|
208 | } |
---|
209 | } |
---|
210 | this.doLayout(); |
---|
211 | }, |
---|
212 | |
---|
213 | /** private: method[beforeDestroy] |
---|
214 | */ |
---|
215 | beforeDestroy: function() { |
---|
216 | if (this.useScaleParameter === true) { |
---|
217 | var layer = this.layerRecord.getLayer() |
---|
218 | layer && layer.events && |
---|
219 | layer.events.unregister("moveend", this, this.onLayerMoveend); |
---|
220 | } |
---|
221 | GeoExt.WMSLegend.superclass.beforeDestroy.apply(this, arguments); |
---|
222 | } |
---|
223 | |
---|
224 | }); |
---|
225 | |
---|
226 | /** private: method[supports] |
---|
227 | * Private override |
---|
228 | */ |
---|
229 | GeoExt.WMSLegend.supports = function(layerRecord) { |
---|
230 | return layerRecord.getLayer() instanceof OpenLayers.Layer.WMS; |
---|
231 | }; |
---|
232 | |
---|
233 | /** api: legendtype = gx_wmslegend */ |
---|
234 | GeoExt.LayerLegend.types["gx_wmslegend"] = GeoExt.WMSLegend; |
---|
235 | |
---|
236 | /** api: xtype = gx_wmslegend */ |
---|
237 | Ext.reg('gx_wmslegend', GeoExt.WMSLegend); |
---|