]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/EventPane.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / EventPane.js
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5
6 /**
7  * @requires OpenLayers/Layer.js
8  * @requires OpenLayers/Util.js
9  */
10
11 /**
12  * Class: OpenLayers.Layer.EventPane
13  * Base class for 3rd party layers.  Create a new event pane layer with the
14  * <OpenLayers.Layer.EventPane> constructor.
15  * 
16  * Inherits from:
17  *  - <OpenLayers.Layer>
18  */
19 OpenLayers.Layer.EventPane = OpenLayers.Class(OpenLayers.Layer, {
20     
21     /**
22      * APIProperty: smoothDragPan
23      * {Boolean} smoothDragPan determines whether non-public/internal API
24      *     methods are used for better performance while dragging EventPane 
25      *     layers. When not in sphericalMercator mode, the smoother dragging 
26      *     doesn't actually move north/south directly with the number of 
27      *     pixels moved, resulting in a slight offset when you drag your mouse 
28      *     north south with this option on. If this visual disparity bothers 
29      *     you, you should turn this option off, or use spherical mercator. 
30      *     Default is on.
31      */
32     smoothDragPan: true,
33
34     /**
35      * Property: isBaseLayer
36      * {Boolean} EventPaned layers are always base layers, by necessity.
37      */ 
38     isBaseLayer: true,
39
40     /**
41      * APIProperty: isFixed
42      * {Boolean} EventPaned layers are fixed by default.
43      */ 
44     isFixed: true,
45
46     /**
47      * Property: pane
48      * {DOMElement} A reference to the element that controls the events.
49      */
50     pane: null,
51
52
53     /**
54      * Property: mapObject
55      * {Object} This is the object which will be used to load the 3rd party library
56      * in the case of the google layer, this will be of type GMap, 
57      * in the case of the ve layer, this will be of type VEMap
58      */ 
59     mapObject: null,
60
61
62     /**
63      * Constructor: OpenLayers.Layer.EventPane
64      * Create a new event pane layer
65      *
66      * Parameters:
67      * name - {String}
68      * options - {Object} Hashtable of extra options to tag onto the layer
69      */
70     initialize: function(name, options) {
71         OpenLayers.Layer.prototype.initialize.apply(this, arguments);
72         if (this.pane == null) {
73             this.pane = OpenLayers.Util.createDiv(this.div.id + "_EventPane");
74         }
75     },
76     
77     /**
78      * APIMethod: destroy
79      * Deconstruct this layer.
80      */
81     destroy: function() {
82         this.mapObject = null;
83         OpenLayers.Layer.prototype.destroy.apply(this, arguments); 
84     },
85
86     
87     /**
88      * Method: setMap
89      * Set the map property for the layer. This is done through an accessor
90      * so that subclasses can override this and take special action once 
91      * they have their map variable set. 
92      *
93      * Parameters:
94      * map - {<OpenLayers.Map>}
95      */
96     setMap: function(map) {
97         OpenLayers.Layer.prototype.setMap.apply(this, arguments);
98         
99         this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
100         this.pane.style.display = this.div.style.display;
101         this.pane.style.width="100%";
102         this.pane.style.height="100%";
103         if (OpenLayers.Util.getBrowserName() == "msie") {
104             this.pane.style.background = 
105                 "url(" + OpenLayers.Util.getImagesLocation() + "blank.gif)";
106         }
107
108         if (this.isFixed) {
109             this.map.viewPortDiv.appendChild(this.pane);
110         } else {
111             this.map.layerContainerDiv.appendChild(this.pane);
112         }
113
114         // once our layer has been added to the map, we can load it
115         this.loadMapObject();
116     
117         // if map didn't load, display warning
118         if (this.mapObject == null) {
119             this.loadWarningMessage();
120         }
121     },
122
123     /**
124      * APIMethod: removeMap
125      * On being removed from the map, we'll like to remove the invisible 'pane'
126      *     div that we added to it on creation. 
127      * 
128      * Parameters:
129      * map - {<OpenLayers.Map>}
130      */
131     removeMap: function(map) {
132         if (this.pane && this.pane.parentNode) {
133             this.pane.parentNode.removeChild(this.pane);
134             this.pane = null;
135         }
136         OpenLayers.Layer.prototype.removeMap.apply(this, arguments);
137     },
138   
139     /**
140      * Method: loadWarningMessage
141      * If we can't load the map lib, then display an error message to the 
142      *     user and tell them where to go for help.
143      * 
144      *     This function sets up the layout for the warning message. Each 3rd
145      *     party layer must implement its own getWarningHTML() function to 
146      *     provide the actual warning message.
147      */
148     loadWarningMessage:function() {
149
150         this.div.style.backgroundColor = "darkblue";
151
152         var viewSize = this.map.getSize();
153         
154         var msgW = Math.min(viewSize.w, 300);
155         var msgH = Math.min(viewSize.h, 200);
156         var size = new OpenLayers.Size(msgW, msgH);
157
158         var centerPx = new OpenLayers.Pixel(viewSize.w/2, viewSize.h/2);
159
160         var topLeft = centerPx.add(-size.w/2, -size.h/2);            
161
162         var div = OpenLayers.Util.createDiv(this.name + "_warning", 
163                                             topLeft, 
164                                             size,
165                                             null,
166                                             null,
167                                             null,
168                                             "auto");
169
170         div.style.padding = "7px";
171         div.style.backgroundColor = "yellow";
172
173         div.innerHTML = this.getWarningHTML();
174         this.div.appendChild(div);
175     },
176   
177     /** 
178      * Method: getWarningHTML
179      * To be implemented by subclasses.
180      * 
181      * Returns:
182      * {String} String with information on why layer is broken, how to get
183      *          it working.
184      */
185     getWarningHTML:function() {
186         //should be implemented by subclasses
187         return "";
188     },
189   
190     /**
191      * Method: display
192      * Set the display on the pane
193      *
194      * Parameters:
195      * display - {Boolean}
196      */
197     display: function(display) {
198         OpenLayers.Layer.prototype.display.apply(this, arguments);
199         this.pane.style.display = this.div.style.display;
200     },
201   
202     /**
203      * Method: setZIndex
204      * Set the z-index order for the pane.
205      * 
206      * Parameters:
207      * zIndex - {int}
208      */
209     setZIndex: function (zIndex) {
210         OpenLayers.Layer.prototype.setZIndex.apply(this, arguments);
211         this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
212     },
213
214     /**
215      * Method: moveTo
216      * Handle calls to move the layer.
217      * 
218      * Parameters:
219      * bounds - {<OpenLayers.Bounds>}
220      * zoomChanged - {Boolean}
221      * dragging - {Boolean}
222      */
223     moveTo:function(bounds, zoomChanged, dragging) {
224         OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
225
226         if (this.mapObject != null) {
227
228             var newCenter = this.map.getCenter();
229             var newZoom = this.map.getZoom();
230
231             if (newCenter != null) {
232
233                 var moOldCenter = this.getMapObjectCenter();
234                 var oldCenter = this.getOLLonLatFromMapObjectLonLat(moOldCenter);
235
236                 var moOldZoom = this.getMapObjectZoom();
237                 var oldZoom= this.getOLZoomFromMapObjectZoom(moOldZoom);
238
239                 if ( !(newCenter.equals(oldCenter)) || 
240                      !(newZoom == oldZoom) ) {
241
242                     if (dragging && this.dragPanMapObject && 
243                         this.smoothDragPan) {
244                         var oldPx = this.map.getViewPortPxFromLonLat(oldCenter);
245                         var newPx = this.map.getViewPortPxFromLonLat(newCenter);
246                         this.dragPanMapObject(newPx.x-oldPx.x, oldPx.y-newPx.y);
247                     } else {
248                         var center = this.getMapObjectLonLatFromOLLonLat(newCenter);
249                         var zoom = this.getMapObjectZoomFromOLZoom(newZoom);
250                         this.setMapObjectCenter(center, zoom, dragging);
251                     }
252                 }
253             }
254         }
255     },
256
257
258   /********************************************************/
259   /*                                                      */
260   /*                 Baselayer Functions                  */
261   /*                                                      */
262   /********************************************************/
263
264     /**
265      * Method: getLonLatFromViewPortPx
266      * Get a map location from a pixel location
267      * 
268      * Parameters:
269      * viewPortPx - {<OpenLayers.Pixel>}
270      *
271      * Returns:
272      *  {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in view
273      *  port OpenLayers.Pixel, translated into lon/lat by map lib
274      *  If the map lib is not loaded or not centered, returns null
275      */
276     getLonLatFromViewPortPx: function (viewPortPx) {
277         var lonlat = null;
278         if ( (this.mapObject != null) && 
279              (this.getMapObjectCenter() != null) ) {
280             var moPixel = this.getMapObjectPixelFromOLPixel(viewPortPx);
281             var moLonLat = this.getMapObjectLonLatFromMapObjectPixel(moPixel);
282             lonlat = this.getOLLonLatFromMapObjectLonLat(moLonLat);
283         }
284         return lonlat;
285     },
286
287  
288     /**
289      * Method: getViewPortPxFromLonLat
290      * Get a pixel location from a map location
291      *
292      * Parameters:
293      * lonlat - {<OpenLayers.LonLat>}
294      *
295      * Returns:
296      * {<OpenLayers.Pixel>} An OpenLayers.Pixel which is the passed-in
297      * OpenLayers.LonLat, translated into view port pixels by map lib
298      * If map lib is not loaded or not centered, returns null
299      */
300     getViewPortPxFromLonLat: function (lonlat) {
301         var viewPortPx = null;
302         if ( (this.mapObject != null) && 
303              (this.getMapObjectCenter() != null) ) {
304
305             var moLonLat = this.getMapObjectLonLatFromOLLonLat(lonlat);
306             var moPixel = this.getMapObjectPixelFromMapObjectLonLat(moLonLat);
307         
308             viewPortPx = this.getOLPixelFromMapObjectPixel(moPixel);
309         }
310         return viewPortPx;
311     },
312
313   /********************************************************/
314   /*                                                      */
315   /*               Translation Functions                  */
316   /*                                                      */
317   /*   The following functions translate Map Object and   */
318   /*            OL formats for Pixel, LonLat              */
319   /*                                                      */
320   /********************************************************/
321
322   //
323   // TRANSLATION: MapObject LatLng <-> OpenLayers.LonLat
324   //
325
326     /**
327      * Method: getOLLonLatFromMapObjectLonLat
328      * Get an OL style map location from a 3rd party style map location
329      *
330      * Parameters
331      * moLonLat - {Object}
332      * 
333      * Returns:
334      * {<OpenLayers.LonLat>} An OpenLayers.LonLat, translated from the passed in 
335      *          MapObject LonLat
336      *          Returns null if null value is passed in
337      */
338     getOLLonLatFromMapObjectLonLat: function(moLonLat) {
339         var olLonLat = null;
340         if (moLonLat != null) {
341             var lon = this.getLongitudeFromMapObjectLonLat(moLonLat);
342             var lat = this.getLatitudeFromMapObjectLonLat(moLonLat);
343             olLonLat = new OpenLayers.LonLat(lon, lat);
344         }
345         return olLonLat;
346     },
347
348     /**
349      * Method: getMapObjectLonLatFromOLLonLat
350      * Get a 3rd party map location from an OL map location.
351      *
352      * Parameters:
353      * olLonLat - {<OpenLayers.LonLat>}
354      * 
355      * Returns:
356      * {Object} A MapObject LonLat, translated from the passed in 
357      *          OpenLayers.LonLat
358      *          Returns null if null value is passed in
359      */
360     getMapObjectLonLatFromOLLonLat: function(olLonLat) {
361         var moLatLng = null;
362         if (olLonLat != null) {
363             moLatLng = this.getMapObjectLonLatFromLonLat(olLonLat.lon,
364                                                          olLonLat.lat);
365         }
366         return moLatLng;
367     },
368
369
370   //
371   // TRANSLATION: MapObject Pixel <-> OpenLayers.Pixel
372   //
373
374     /**
375      * Method: getOLPixelFromMapObjectPixel
376      * Get an OL pixel location from a 3rd party pixel location.
377      *
378      * Parameters:
379      * moPixel - {Object}
380      * 
381      * Returns:
382      * {<OpenLayers.Pixel>} An OpenLayers.Pixel, translated from the passed in 
383      *          MapObject Pixel
384      *          Returns null if null value is passed in
385      */
386     getOLPixelFromMapObjectPixel: function(moPixel) {
387         var olPixel = null;
388         if (moPixel != null) {
389             var x = this.getXFromMapObjectPixel(moPixel);
390             var y = this.getYFromMapObjectPixel(moPixel);
391             olPixel = new OpenLayers.Pixel(x, y);
392         }
393         return olPixel;
394     },
395
396     /**
397      * Method: getMapObjectPixelFromOLPixel
398      * Get a 3rd party pixel location from an OL pixel location
399      *
400      * Parameters:
401      * olPixel - {<OpenLayers.Pixel>}
402      * 
403      * Returns:
404      * {Object} A MapObject Pixel, translated from the passed in 
405      *          OpenLayers.Pixel
406      *          Returns null if null value is passed in
407      */
408     getMapObjectPixelFromOLPixel: function(olPixel) {
409         var moPixel = null;
410         if (olPixel != null) {
411             moPixel = this.getMapObjectPixelFromXY(olPixel.x, olPixel.y);
412         }
413         return moPixel;
414     },
415
416     CLASS_NAME: "OpenLayers.Layer.EventPane"
417 });