root/core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js

Revision 811, 6.0 kB (checked in by bartvde, 1 year ago)

oops, part of that change was not meant to be committed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
Line 
1 /* Copyright (C) 2008-2009 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  * ¹ pending approval */
7
8 /**
9  * @include GeoExt/data/LayerStore.js
10  */
11
12 /** api: (define)
13  *  module = GeoExt
14  *  class = MapPanel
15  *  base_link = `Ext.Panel <http://extjs.com/deploy/dev/docs/?class=Ext.Panel>`_
16  */
17 Ext.namespace("GeoExt");
18
19 /** api: example
20  *  Sample code to create a panel with a new map:
21  *
22  *  .. code-block:: javascript
23  *     
24  *      var mapPanel = new GeoExt.MapPanel({
25  *          border: false,
26  *          renderTo: "div-id",
27  *          map: {
28  *              maxExtent: new OpenLayers.Bounds(-90, -45, 90, 45)
29  *          }
30  *      });
31  *     
32  *  Sample code to create a map panel with a bottom toolbar in a Window:
33  *
34  *  .. code-block:: javascript
35  *
36  *      var win = new Ext.Window({
37  *          title: "My Map",
38  *          items: [{
39  *              xtype: "gx_mappanel",
40  *              bbar: new Ext.Toolbar()
41  *          }]
42  *      });
43  */
44
45 /** api: constructor
46  *  .. class:: MapPanel(config)
47  *   
48  *      Create a panel container for a map.
49  */
50 GeoExt.MapPanel = Ext.extend(Ext.Panel, {
51
52     /** api: config[map]
53      *  ``OpenLayers.Map or Object``  A configured map or a configuration object
54      *  for the map constructor.  A configured map will be available after
55      *  construction through the :attr:`map` property.
56      */
57
58     /** api: property[map]
59      *  ``OpenLayers.Map``  A configured map object.
60      */
61     map: null,
62    
63     /** api: config[layers]
64      *  ``GeoExt.data.LayerStore or GeoExt.data.GroupingStore or Array(OpenLayers.Layer)``
65      *  A store holding records. If not provided, an empty
66      *  :class:`GeoExt.data.LayerStore` will be created.
67      */
68    
69     /** api: property[layers]
70      *  :class:`GeoExt.data.LayerStore`  A store containing
71      *  :class:`GeoExt.data.LayerRecord` objects.
72      */
73     layers: null,
74
75    
76     /** api: config[center]
77      *  ``OpenLayers.LonLat or Array(Number)``  A location for the map center.  If
78      *  an array is provided, the first two items should represent x & y coordinates.
79      */
80     center: null,
81
82     /** api: config[zoom]
83      *  ``Number``  An initial zoom level for the map.
84      */
85     zoom: null,
86
87     /** api: config[extent]
88      *  ``OpenLayers.Bounds or Array(Number)``  An initial extent for the map (used
89      *  if center and zoom are not provided.  If an array, the first four items
90      *  should be minx, miny, maxx, maxy.
91      */
92     extent: null,
93    
94     /** private: method[initComponent]
95      *  Initializes the map panel. Creates an OpenLayers map if
96      *  none was provided in the config options passed to the
97      *  constructor.
98      */
99     initComponent: function(){
100         if(!(this.map instanceof OpenLayers.Map)) {
101             this.map = new OpenLayers.Map(
102                 Ext.applyIf(this.map || {}, {allOverlays: true})
103             );
104         }
105         var layers = this.layers;
106         if(!layers || layers instanceof Array) {
107             this.layers = new GeoExt.data.LayerStore({
108                 layers: layers,
109                 map: this.map
110             });
111         }
112        
113         if(typeof this.center == "string") {
114             this.center = OpenLayers.LonLat.fromString(this.center);
115         } else if(this.center instanceof Array) {
116             this.center = new OpenLayers.LonLat(this.center[0], this.center[1]);
117         }
118         if(typeof this.extent == "string") {
119             this.extent = OpenLayers.Bounds.fromString(this.extent);
120         } else if(this.extent instanceof Array) {
121             this.extent = OpenLayers.Bounds.fromArray(this.extent);
122         }
123        
124         GeoExt.MapPanel.superclass.initComponent.call(this);       
125     },
126    
127     /** private: method[updateMapSize]
128      *  Tell the map that it needs to recaculate its size and position.
129      */
130     updateMapSize: function() {
131         if(this.map) {
132             this.map.updateSize();
133         }
134     },
135    
136     /** private: method[onRender]
137      *  Private method called after the panel has been
138      *  rendered.
139      */
140     onRender: function() {
141         GeoExt.MapPanel.superclass.onRender.apply(this, arguments);
142         this.map.render(this.body.dom);
143         if(this.map.layers.length > 0) {
144             if(this.center) {
145                 // zoom does not have to be defined
146                 this.map.setCenter(this.center, this.zoom);
147             }  else if(this.extent) {
148                 this.map.zoomToExtent(this.extent);
149             } else {
150                 this.map.zoomToMaxExtent();
151             }
152         }
153     },
154    
155     /** private: method[afterRender]
156      *  Private method called after the panel has been rendered.
157      */
158     afterRender: function() {
159         GeoExt.MapPanel.superclass.afterRender.apply(this, arguments);
160         if(this.ownerCt) {
161             this.ownerCt.on("move", this.updateMapSize, this);
162         }
163     },   
164
165     /** private: method[onResize]
166      *  Private method called after the panel has been resized.
167      */
168     onResize: function() {
169         GeoExt.MapPanel.superclass.onResize.apply(this, arguments);
170         this.updateMapSize();
171     },
172    
173     /** private: method[onDestroy]
174      *  Private method called during the destroy sequence.
175      */
176     onDestroy: function() {
177         if(this.ownerCt) {
178             this.ownerCt.un("move", this.updateMapSize, this);
179         }
180         GeoExt.MapPanel.superclass.onDestroy.apply(this, arguments);
181     }
182    
183 });
184
185 /** api: function[guess]
186  *  :return: ``GeoExt.MapPanel`` The first map panel found by the Ext
187  *      component manager.
188  * 
189  *  Convenience function for guessing the map panel of an application. This
190  *     can reliably be used for all applications that just have one map panel
191  *     in the viewport.
192  */
193 GeoExt.MapPanel.guess = function() {
194     return Ext.ComponentMgr.all.find(function(o) {
195         return o instanceof GeoExt.MapPanel;
196     });
197 };
198
199
200 /** api: xtype = gx_mappanel */
201 Ext.reg('gx_mappanel', GeoExt.MapPanel);
Note: See TracBrowser for help on using the browser.