1 | /* Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
2 | * Published under the BSD license. |
---|
3 | * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text |
---|
4 | * of the license. |
---|
5 | */ |
---|
6 | |
---|
7 | /** |
---|
8 | * @include GeoExt/widgets/tips/LayerOpacitySliderTip.js |
---|
9 | * @include GeoExt/data/LayerRecord.js |
---|
10 | */ |
---|
11 | |
---|
12 | /** api: (define) |
---|
13 | * module = GeoExt |
---|
14 | * class = LayerOpacitySlider |
---|
15 | * base_link = `Ext.Slider <http://dev.sencha.com/deploy/dev/docs/?class=Ext.Slider>`_ |
---|
16 | */ |
---|
17 | Ext.namespace("GeoExt"); |
---|
18 | |
---|
19 | /** api: example |
---|
20 | * Sample code to render a slider outside the map viewport: |
---|
21 | * |
---|
22 | * .. code-block:: javascript |
---|
23 | * |
---|
24 | * var slider = new GeoExt.LayerOpacitySlider({ |
---|
25 | * renderTo: document.body, |
---|
26 | * width: 200, |
---|
27 | * layer: layer |
---|
28 | * }); |
---|
29 | * |
---|
30 | * Sample code to add a slider to a map panel: |
---|
31 | * |
---|
32 | * .. code-block:: javascript |
---|
33 | * |
---|
34 | * var layer = new OpenLayers.Layer.WMS( |
---|
35 | * "Global Imagery", |
---|
36 | * "http://maps.opengeo.org/geowebcache/service/wms", |
---|
37 | * {layers: "bluemarble"} |
---|
38 | * ); |
---|
39 | * var panel = new GeoExt.MapPanel({ |
---|
40 | * renderTo: document.body, |
---|
41 | * height: 300, |
---|
42 | * width: 400, |
---|
43 | * map: { |
---|
44 | * controls: [new OpenLayers.Control.Navigation()] |
---|
45 | * }, |
---|
46 | * layers: [layer], |
---|
47 | * extent: [-5, 35, 15, 55], |
---|
48 | * items: [{ |
---|
49 | * xtype: "gx_opacityslider", |
---|
50 | * layer: layer, |
---|
51 | * aggressive: true, |
---|
52 | * vertical: true, |
---|
53 | * height: 100, |
---|
54 | * x: 10, |
---|
55 | * y: 20 |
---|
56 | * }] |
---|
57 | * }); |
---|
58 | */ |
---|
59 | |
---|
60 | /** api: constructor |
---|
61 | * .. class:: LayerOpacitySlider(config) |
---|
62 | * |
---|
63 | * Create a slider for controlling a layer's opacity. |
---|
64 | */ |
---|
65 | GeoExt.LayerOpacitySlider = Ext.extend(Ext.Slider, { |
---|
66 | |
---|
67 | /** api: config[layer] |
---|
68 | * ``OpenLayers.Layer`` or :class:`GeoExt.data.LayerRecord` |
---|
69 | * The layer this slider changes the opacity of. (required) |
---|
70 | */ |
---|
71 | /** private: property[layer] |
---|
72 | * ``OpenLayers.Layer`` |
---|
73 | */ |
---|
74 | layer: null, |
---|
75 | |
---|
76 | /** api: config[complementaryLayer] |
---|
77 | * ``OpenLayers.Layer`` or :class:`GeoExt.data.LayerRecord` |
---|
78 | * If provided, a layer that will be made invisible (its visibility is |
---|
79 | * set to false) when the slider value is set to its max value. If this |
---|
80 | * slider is used to fade visibility between to layers, setting |
---|
81 | * ``complementaryLayer`` and ``changeVisibility`` will make sure that |
---|
82 | * only visible tiles are loaded when the slider is set to its min or max |
---|
83 | * value. (optional) |
---|
84 | */ |
---|
85 | complementaryLayer: null, |
---|
86 | |
---|
87 | /** api: config[delay] |
---|
88 | * ``Number`` Time in milliseconds before setting the opacity value to the |
---|
89 | * layer. If the value change again within that time, the original value |
---|
90 | * is not set. Only applicable if aggressive is true. |
---|
91 | */ |
---|
92 | delay: 5, |
---|
93 | |
---|
94 | /** api: config[changeVisibilityDelay] |
---|
95 | * ``Number`` Time in milliseconds before changing the layer's visibility. |
---|
96 | * If the value changes again within that time, the layer's visibility |
---|
97 | * change does not occur. Only applicable if changeVisibility is true. |
---|
98 | * Defaults to 5. |
---|
99 | */ |
---|
100 | changeVisibilityDelay: 5, |
---|
101 | |
---|
102 | /** api: config[aggressive] |
---|
103 | * ``Boolean`` |
---|
104 | * If set to true, the opacity is changed as soon as the thumb is moved. |
---|
105 | * Otherwise when the thumb is released (default). |
---|
106 | */ |
---|
107 | aggressive: false, |
---|
108 | |
---|
109 | /** api: config[changeVisibility] |
---|
110 | * ``Boolean`` |
---|
111 | * If set to true, the layer's visibility is handled by the |
---|
112 | * slider, the slider makes the layer invisible when its |
---|
113 | * value is changed to the min value, and makes the layer |
---|
114 | * visible again when its value goes from the min value |
---|
115 | * to some other value. The layer passed to the constructor |
---|
116 | * must be visible, as its visibility is fully handled by |
---|
117 | * the slider. Defaults to false. |
---|
118 | */ |
---|
119 | changeVisibility: false, |
---|
120 | |
---|
121 | /** api: config[value] |
---|
122 | * ``Number`` |
---|
123 | * The value to initialize the slider with. This value is |
---|
124 | * taken into account only if the layer's opacity is null. |
---|
125 | * If the layer's opacity is null and this value is not |
---|
126 | * defined in the config object then the slider initializes |
---|
127 | * it to the max value. |
---|
128 | */ |
---|
129 | value: null, |
---|
130 | |
---|
131 | /** api: config[inverse] |
---|
132 | * ``Boolean`` |
---|
133 | * If true, we will work with transparency instead of with opacity. |
---|
134 | * Defaults to false. |
---|
135 | */ |
---|
136 | /** private: property[inverse] |
---|
137 | * ``Boolean`` |
---|
138 | */ |
---|
139 | inverse: false, |
---|
140 | |
---|
141 | /** private: method[constructor] |
---|
142 | * Construct the component. |
---|
143 | */ |
---|
144 | constructor: function(config) { |
---|
145 | if (config.layer) { |
---|
146 | this.layer = this.getLayer(config.layer); |
---|
147 | this.bind(); |
---|
148 | this.complementaryLayer = this.getLayer(config.complementaryLayer); |
---|
149 | // before we call getOpacityValue inverse should be set |
---|
150 | if (config.inverse !== undefined) { |
---|
151 | this.inverse = config.inverse; |
---|
152 | } |
---|
153 | config.value = (config.value !== undefined) ? |
---|
154 | config.value : this.getOpacityValue(this.layer); |
---|
155 | delete config.layer; |
---|
156 | delete config.complementaryLayer; |
---|
157 | } |
---|
158 | GeoExt.LayerOpacitySlider.superclass.constructor.call(this, config); |
---|
159 | }, |
---|
160 | |
---|
161 | /** private: method[bind] |
---|
162 | */ |
---|
163 | bind: function() { |
---|
164 | if (this.layer && this.layer.map) { |
---|
165 | this.layer.map.events.on({ |
---|
166 | changelayer: this.update, |
---|
167 | scope: this |
---|
168 | }); |
---|
169 | } |
---|
170 | }, |
---|
171 | |
---|
172 | /** private: method[unbind] |
---|
173 | */ |
---|
174 | unbind: function() { |
---|
175 | if (this.layer && this.layer.map) { |
---|
176 | this.layer.map.events.un({ |
---|
177 | changelayer: this.update, |
---|
178 | scope: this |
---|
179 | }); |
---|
180 | } |
---|
181 | }, |
---|
182 | |
---|
183 | /** private: method[update] |
---|
184 | * Registered as a listener for opacity change. Updates the value of the slider. |
---|
185 | */ |
---|
186 | update: function(evt) { |
---|
187 | if (evt.property === "opacity" && evt.layer == this.layer) { |
---|
188 | this.setValue(this.getOpacityValue(this.layer)); |
---|
189 | } |
---|
190 | }, |
---|
191 | |
---|
192 | /** api: method[setLayer] |
---|
193 | * :param layer: ``OpenLayers.Layer`` or :class:`GeoExt.data.LayerRecord` |
---|
194 | * |
---|
195 | * Bind a new layer to the opacity slider. |
---|
196 | */ |
---|
197 | setLayer: function(layer) { |
---|
198 | this.unbind(); |
---|
199 | this.layer = this.getLayer(layer); |
---|
200 | this.setValue(this.getOpacityValue(layer)); |
---|
201 | this.bind(); |
---|
202 | }, |
---|
203 | |
---|
204 | /** private: method[getOpacityValue] |
---|
205 | * :param layer: ``OpenLayers.Layer`` or :class:`GeoExt.data.LayerRecord` |
---|
206 | * :return: ``Integer`` The opacity for the layer. |
---|
207 | * |
---|
208 | * Returns the opacity value for the layer. |
---|
209 | */ |
---|
210 | getOpacityValue: function(layer) { |
---|
211 | var value; |
---|
212 | if (layer && layer.opacity !== null) { |
---|
213 | value = parseInt(layer.opacity * (this.maxValue - this.minValue)); |
---|
214 | } else { |
---|
215 | value = this.maxValue; |
---|
216 | } |
---|
217 | if (this.inverse === true) { |
---|
218 | value = (this.maxValue - this.minValue) - value; |
---|
219 | } |
---|
220 | return value; |
---|
221 | }, |
---|
222 | |
---|
223 | /** private: method[getLayer] |
---|
224 | * :param layer: ``OpenLayers.Layer`` or :class:`GeoExt.data.LayerRecord` |
---|
225 | * :return: ``OpenLayers.Layer`` The OpenLayers layer object |
---|
226 | * |
---|
227 | * Returns the OpenLayers layer object for a layer record or a plain layer |
---|
228 | * object. |
---|
229 | */ |
---|
230 | getLayer: function(layer) { |
---|
231 | if (layer instanceof OpenLayers.Layer) { |
---|
232 | return layer; |
---|
233 | } else if (layer instanceof GeoExt.data.LayerRecord) { |
---|
234 | return layer.getLayer(); |
---|
235 | } |
---|
236 | }, |
---|
237 | |
---|
238 | /** private: method[initComponent] |
---|
239 | * Initialize the component. |
---|
240 | */ |
---|
241 | initComponent: function() { |
---|
242 | |
---|
243 | GeoExt.LayerOpacitySlider.superclass.initComponent.call(this); |
---|
244 | |
---|
245 | if (this.changeVisibility && this.layer && |
---|
246 | (this.layer.opacity == 0 || |
---|
247 | (this.inverse === false && this.value == this.minValue) || |
---|
248 | (this.inverse === true && this.value == this.maxValue))) { |
---|
249 | this.layer.setVisibility(false); |
---|
250 | } |
---|
251 | |
---|
252 | if (this.complementaryLayer && |
---|
253 | ((this.layer && this.layer.opacity == 1) || |
---|
254 | (this.inverse === false && this.value == this.maxValue) || |
---|
255 | (this.inverse === true && this.value == this.minValue))) { |
---|
256 | this.complementaryLayer.setVisibility(false); |
---|
257 | } |
---|
258 | |
---|
259 | if (this.aggressive === true) { |
---|
260 | this.on('change', this.changeLayerOpacity, this, { |
---|
261 | buffer: this.delay |
---|
262 | }); |
---|
263 | } else { |
---|
264 | this.on('changecomplete', this.changeLayerOpacity, this); |
---|
265 | } |
---|
266 | |
---|
267 | if (this.changeVisibility === true) { |
---|
268 | this.on('change', this.changeLayerVisibility, this, { |
---|
269 | buffer: this.changeVisibilityDelay |
---|
270 | }); |
---|
271 | } |
---|
272 | |
---|
273 | if (this.complementaryLayer) { |
---|
274 | this.on('change', this.changeComplementaryLayerVisibility, this, { |
---|
275 | buffer: this.changeVisibilityDelay |
---|
276 | }); |
---|
277 | } |
---|
278 | this.on("beforedestroy", this.unbind, this); |
---|
279 | }, |
---|
280 | |
---|
281 | /** private: method[changeLayerOpacity] |
---|
282 | * :param slider: :class:`GeoExt.LayerOpacitySlider` |
---|
283 | * :param value: ``Number`` The slider value |
---|
284 | * |
---|
285 | * Updates the ``OpenLayers.Layer`` opacity value. |
---|
286 | */ |
---|
287 | changeLayerOpacity: function(slider, value) { |
---|
288 | if (this.layer) { |
---|
289 | value = value / (this.maxValue - this.minValue); |
---|
290 | if (this.inverse === true) { |
---|
291 | value = 1 - value; |
---|
292 | } |
---|
293 | this.layer.setOpacity(value); |
---|
294 | } |
---|
295 | }, |
---|
296 | |
---|
297 | /** private: method[changeLayerVisibility] |
---|
298 | * :param slider: :class:`GeoExt.LayerOpacitySlider` |
---|
299 | * :param value: ``Number`` The slider value |
---|
300 | * |
---|
301 | * Updates the ``OpenLayers.Layer`` visibility. |
---|
302 | */ |
---|
303 | changeLayerVisibility: function(slider, value) { |
---|
304 | var currentVisibility = this.layer.getVisibility(); |
---|
305 | if ((this.inverse === false && value == this.minValue) || |
---|
306 | (this.inverse === true && value == this.maxValue) && |
---|
307 | currentVisibility === true) { |
---|
308 | this.layer.setVisibility(false); |
---|
309 | } else if ((this.inverse === false && value > this.minValue) || |
---|
310 | (this.inverse === true && value < this.maxValue) && |
---|
311 | currentVisibility == false) { |
---|
312 | this.layer.setVisibility(true); |
---|
313 | } |
---|
314 | }, |
---|
315 | |
---|
316 | /** private: method[changeComplementaryLayerVisibility] |
---|
317 | * :param slider: :class:`GeoExt.LayerOpacitySlider` |
---|
318 | * :param value: ``Number`` The slider value |
---|
319 | * |
---|
320 | * Updates the complementary ``OpenLayers.Layer`` visibility. |
---|
321 | */ |
---|
322 | changeComplementaryLayerVisibility: function(slider, value) { |
---|
323 | var currentVisibility = this.complementaryLayer.getVisibility(); |
---|
324 | if ((this.inverse === false && value == this.maxValue) || |
---|
325 | (this.inverse === true && value == this.minValue) && |
---|
326 | currentVisibility === true) { |
---|
327 | this.complementaryLayer.setVisibility(false); |
---|
328 | } else if ((this.inverse === false && value < this.maxValue) || |
---|
329 | (this.inverse === true && value > this.minValue) && |
---|
330 | currentVisibility == false) { |
---|
331 | this.complementaryLayer.setVisibility(true); |
---|
332 | } |
---|
333 | }, |
---|
334 | |
---|
335 | /** private: method[addToMapPanel] |
---|
336 | * :param panel: :class:`GeoExt.MapPanel` |
---|
337 | * |
---|
338 | * Called by a MapPanel if this component is one of the items in the panel. |
---|
339 | */ |
---|
340 | addToMapPanel: function(panel) { |
---|
341 | this.on({ |
---|
342 | render: function() { |
---|
343 | var el = this.getEl(); |
---|
344 | el.setStyle({ |
---|
345 | position: "absolute", |
---|
346 | zIndex: panel.map.Z_INDEX_BASE.Control |
---|
347 | }); |
---|
348 | el.on({ |
---|
349 | mousedown: this.stopMouseEvents, |
---|
350 | click: this.stopMouseEvents |
---|
351 | }); |
---|
352 | }, |
---|
353 | scope: this |
---|
354 | }); |
---|
355 | }, |
---|
356 | |
---|
357 | /** private: method[removeFromMapPanel] |
---|
358 | * :param panel: :class:`GeoExt.MapPanel` |
---|
359 | * |
---|
360 | * Called by a MapPanel if this component is one of the items in the panel. |
---|
361 | */ |
---|
362 | removeFromMapPanel: function(panel) { |
---|
363 | var el = this.getEl(); |
---|
364 | el.un({ |
---|
365 | mousedown: this.stopMouseEvents, |
---|
366 | click: this.stopMouseEvents, |
---|
367 | scope: this |
---|
368 | }); |
---|
369 | this.unbind(); |
---|
370 | }, |
---|
371 | |
---|
372 | /** private: method[stopMouseEvents] |
---|
373 | * :param e: ``Object`` |
---|
374 | */ |
---|
375 | stopMouseEvents: function(e) { |
---|
376 | e.stopEvent(); |
---|
377 | } |
---|
378 | }); |
---|
379 | |
---|
380 | /** api: xtype = gx_opacityslider */ |
---|
381 | Ext.reg('gx_opacityslider', GeoExt.LayerOpacitySlider); |
---|