[76] | 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/data/LayerStore.js |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /** api: (define) |
---|
| 14 | * module = GeoExt |
---|
| 15 | * class = MapPanel |
---|
| 16 | * base_link = `Ext.Panel <http://dev.sencha.com/deploy/dev/docs/?class=Ext.Panel>`_ |
---|
| 17 | */ |
---|
| 18 | Ext.namespace("GeoExt"); |
---|
| 19 | |
---|
| 20 | /** api: example |
---|
| 21 | * Sample code to create a panel with a new map: |
---|
| 22 | * |
---|
| 23 | * .. code-block:: javascript |
---|
| 24 | * |
---|
| 25 | * var mapPanel = new GeoExt.MapPanel({ |
---|
| 26 | * border: false, |
---|
| 27 | * renderTo: "div-id", |
---|
| 28 | * map: { |
---|
| 29 | * maxExtent: new OpenLayers.Bounds(-90, -45, 90, 45) |
---|
| 30 | * } |
---|
| 31 | * }); |
---|
| 32 | * |
---|
| 33 | * Sample code to create a map panel with a bottom toolbar in a Window: |
---|
| 34 | * |
---|
| 35 | * .. code-block:: javascript |
---|
| 36 | * |
---|
| 37 | * var win = new Ext.Window({ |
---|
| 38 | * title: "My Map", |
---|
| 39 | * items: [{ |
---|
| 40 | * xtype: "gx_mappanel", |
---|
| 41 | * bbar: new Ext.Toolbar() |
---|
| 42 | * }] |
---|
| 43 | * }); |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | /** api: constructor |
---|
| 47 | * .. class:: MapPanel(config) |
---|
| 48 | * |
---|
| 49 | * Create a panel container for a map. |
---|
| 50 | */ |
---|
| 51 | GeoExt.MapPanel = Ext.extend(Ext.Panel, { |
---|
| 52 | |
---|
| 53 | /** api: config[map] |
---|
| 54 | * ``OpenLayers.Map or Object`` A configured map or a configuration object |
---|
| 55 | * for the map constructor. A configured map will be available after |
---|
| 56 | * construction through the :attr:`map` property. |
---|
| 57 | */ |
---|
| 58 | |
---|
| 59 | /** api: property[map] |
---|
| 60 | * ``OpenLayers.Map`` or ``Object`` A map or map configuration. |
---|
| 61 | */ |
---|
| 62 | map: null, |
---|
| 63 | |
---|
| 64 | /** api: config[layers] |
---|
| 65 | * ``GeoExt.data.LayerStore or GeoExt.data.GroupingStore or Array(OpenLayers.Layer)`` |
---|
| 66 | * A store holding records. The layers provided here will be added to this |
---|
| 67 | * MapPanel's map when it is rendered. |
---|
| 68 | */ |
---|
| 69 | |
---|
| 70 | /** api: property[layers] |
---|
| 71 | * :class:`GeoExt.data.LayerStore` A store containing |
---|
| 72 | * :class:`GeoExt.data.LayerRecord` objects. |
---|
| 73 | */ |
---|
| 74 | layers: null, |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | /** api: config[center] |
---|
| 78 | * ``OpenLayers.LonLat or Array(Number)`` A location for the map center. If |
---|
| 79 | * an array is provided, the first two items should represent x & y coordinates. |
---|
| 80 | */ |
---|
| 81 | center: null, |
---|
| 82 | |
---|
| 83 | /** api: config[zoom] |
---|
| 84 | * ``Number`` An initial zoom level for the map. |
---|
| 85 | */ |
---|
| 86 | zoom: null, |
---|
| 87 | |
---|
| 88 | /** api: config[prettyStateKeys] |
---|
| 89 | * ``Boolean`` Set this to true if you want pretty strings in the MapPanel's |
---|
| 90 | * state keys. More specifically, layer.name instead of layer.id will be used |
---|
| 91 | * in the state keys if this option is set to true. But in that case you have |
---|
| 92 | * to make sure you don't have two layers with the same name. Defaults to |
---|
| 93 | * false. |
---|
| 94 | */ |
---|
| 95 | prettyStateKeys: false, |
---|
| 96 | |
---|
| 97 | /** api: config[extent] |
---|
| 98 | * ``OpenLayers.Bounds or Array(Number)`` An initial extent for the map (used |
---|
| 99 | * if center and zoom are not provided. If an array, the first four items |
---|
| 100 | * should be minx, miny, maxx, maxy. |
---|
| 101 | */ |
---|
| 102 | extent: null, |
---|
| 103 | |
---|
| 104 | /** private: property[stateEvents] |
---|
| 105 | * ``Array(String)`` Array of state events |
---|
| 106 | */ |
---|
| 107 | stateEvents: ["aftermapmove", |
---|
| 108 | "afterlayervisibilitychange", |
---|
| 109 | "afterlayeropacitychange"], |
---|
| 110 | |
---|
| 111 | /** private: method[initComponent] |
---|
| 112 | * Initializes the map panel. Creates an OpenLayers map if |
---|
| 113 | * none was provided in the config options passed to the |
---|
| 114 | * constructor. |
---|
| 115 | */ |
---|
| 116 | initComponent: function(){ |
---|
| 117 | if(!(this.map instanceof OpenLayers.Map)) { |
---|
| 118 | this.map = new OpenLayers.Map( |
---|
| 119 | Ext.applyIf(this.map || {}, {allOverlays: true}) |
---|
| 120 | ); |
---|
| 121 | } |
---|
| 122 | var layers = this.layers; |
---|
| 123 | if(!layers || layers instanceof Array) { |
---|
| 124 | this.layers = new GeoExt.data.LayerStore({ |
---|
| 125 | layers: layers, |
---|
| 126 | map: this.map.layers.length > 0 ? this.map : null |
---|
| 127 | }); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | if(typeof this.center == "string") { |
---|
| 131 | this.center = OpenLayers.LonLat.fromString(this.center); |
---|
| 132 | } else if(this.center instanceof Array) { |
---|
| 133 | this.center = new OpenLayers.LonLat(this.center[0], this.center[1]); |
---|
| 134 | } |
---|
| 135 | if(typeof this.extent == "string") { |
---|
| 136 | this.extent = OpenLayers.Bounds.fromString(this.extent); |
---|
| 137 | } else if(this.extent instanceof Array) { |
---|
| 138 | this.extent = OpenLayers.Bounds.fromArray(this.extent); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | GeoExt.MapPanel.superclass.initComponent.call(this); |
---|
| 142 | |
---|
| 143 | this.addEvents( |
---|
| 144 | /** private: event[aftermapmove] |
---|
| 145 | * Fires after the map is moved. |
---|
| 146 | */ |
---|
| 147 | "aftermapmove", |
---|
| 148 | |
---|
| 149 | /** private: event[afterlayervisibilitychange] |
---|
| 150 | * Fires after a layer changed visibility. |
---|
| 151 | */ |
---|
| 152 | "afterlayervisibilitychange", |
---|
| 153 | |
---|
| 154 | /** private: event[afterlayeropacitychange] |
---|
| 155 | * Fires after a layer changed opacity. |
---|
| 156 | */ |
---|
| 157 | "afterlayeropacitychange" |
---|
| 158 | ); |
---|
| 159 | this.map.events.on({ |
---|
| 160 | "moveend": this.onMoveend, |
---|
| 161 | "changelayer": this.onLayerchange, |
---|
| 162 | scope: this |
---|
| 163 | }); |
---|
| 164 | }, |
---|
| 165 | |
---|
| 166 | /** private: method[onMoveend] |
---|
| 167 | * |
---|
| 168 | * The "moveend" listener. |
---|
| 169 | */ |
---|
| 170 | onMoveend: function() { |
---|
| 171 | this.fireEvent("aftermapmove"); |
---|
| 172 | }, |
---|
| 173 | |
---|
| 174 | /** private: method[onLayerchange] |
---|
| 175 | * :param e: ``Object`` |
---|
| 176 | * |
---|
| 177 | * The "changelayer" listener. |
---|
| 178 | */ |
---|
| 179 | onLayerchange: function(e) { |
---|
| 180 | if(e.property) { |
---|
| 181 | if(e.property === "visibility") { |
---|
| 182 | this.fireEvent("afterlayervisibilitychange"); |
---|
| 183 | } else if(e.property === "opacity") { |
---|
| 184 | this.fireEvent("afterlayeropacitychange"); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | }, |
---|
| 188 | |
---|
| 189 | /** private: method[applyState] |
---|
| 190 | * :param state: ``Object`` The state to apply. |
---|
| 191 | * |
---|
| 192 | * Apply the state provided as an argument. |
---|
| 193 | */ |
---|
| 194 | applyState: function(state) { |
---|
| 195 | |
---|
| 196 | // if we get strings for state.x, state.y or state.zoom |
---|
| 197 | // OpenLayers will take care of converting them to the |
---|
| 198 | // appropriate types so we don't bother with that |
---|
| 199 | this.center = new OpenLayers.LonLat(state.x, state.y); |
---|
| 200 | this.zoom = state.zoom; |
---|
| 201 | |
---|
| 202 | // set layer visibility and opacity |
---|
| 203 | var i, l, layer, layerId, visibility, opacity; |
---|
| 204 | var layers = this.map.layers; |
---|
| 205 | for(i=0, l=layers.length; i<l; i++) { |
---|
| 206 | layer = layers[i]; |
---|
| 207 | layerId = this.prettyStateKeys ? layer.name : layer.id; |
---|
| 208 | visibility = state["visibility_" + layerId]; |
---|
| 209 | if(visibility !== undefined) { |
---|
| 210 | // convert to boolean |
---|
| 211 | visibility = (/^true$/i).test(visibility); |
---|
| 212 | if(layer.isBaseLayer) { |
---|
| 213 | if(visibility) { |
---|
| 214 | this.map.setBaseLayer(layer); |
---|
| 215 | } |
---|
| 216 | } else { |
---|
| 217 | layer.setVisibility(visibility); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | opacity = state["opacity_" + layerId]; |
---|
| 221 | if(opacity !== undefined) { |
---|
| 222 | layer.setOpacity(opacity); |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | }, |
---|
| 226 | |
---|
| 227 | /** private: method[getState] |
---|
| 228 | * :return: ``Object`` The state. |
---|
| 229 | * |
---|
| 230 | * Returns the current state for the map panel. |
---|
| 231 | */ |
---|
| 232 | getState: function() { |
---|
| 233 | var state; |
---|
| 234 | |
---|
| 235 | // Ext delays the call to getState when a state event |
---|
| 236 | // occurs, so the MapPanel may have been destroyed |
---|
| 237 | // between the time the event occurred and the time |
---|
| 238 | // getState is called |
---|
| 239 | if(!this.map) { |
---|
| 240 | return; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | // record location and zoom level |
---|
| 244 | var center = this.map.getCenter(); |
---|
| 245 | state = { |
---|
| 246 | x: center.lon, |
---|
| 247 | y: center.lat, |
---|
| 248 | zoom: this.map.getZoom() |
---|
| 249 | }; |
---|
| 250 | |
---|
| 251 | // record layer visibility and opacity |
---|
| 252 | var i, l, layer, layerId, layers = this.map.layers; |
---|
| 253 | for(i=0, l=layers.length; i<l; i++) { |
---|
| 254 | layer = layers[i]; |
---|
| 255 | layerId = this.prettyStateKeys ? layer.name : layer.id; |
---|
| 256 | state["visibility_" + layerId] = layer.getVisibility(); |
---|
| 257 | state["opacity_" + layerId] = layer.opacity == null ? |
---|
| 258 | 1 : layer.opacity; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | return state; |
---|
| 262 | }, |
---|
| 263 | |
---|
| 264 | /** private: method[updateMapSize] |
---|
| 265 | * Tell the map that it needs to recalculate its size and position. |
---|
| 266 | */ |
---|
| 267 | updateMapSize: function() { |
---|
| 268 | if(this.map) { |
---|
| 269 | this.map.updateSize(); |
---|
| 270 | } |
---|
| 271 | }, |
---|
| 272 | |
---|
| 273 | /** private: method[renderMap] |
---|
| 274 | * Private method called after the panel has been rendered or after it |
---|
| 275 | * has been laid out by its parent's layout. |
---|
| 276 | */ |
---|
| 277 | renderMap: function() { |
---|
| 278 | var map = this.map; |
---|
| 279 | map.render(this.body.dom); |
---|
| 280 | |
---|
| 281 | this.layers.bind(map); |
---|
| 282 | |
---|
| 283 | if(map.layers.length > 0) { |
---|
| 284 | if(this.center || this.zoom != null) { |
---|
| 285 | // both do not have to be defined |
---|
| 286 | map.setCenter(this.center, this.zoom); |
---|
| 287 | } else if(this.extent) { |
---|
| 288 | map.zoomToExtent(this.extent); |
---|
| 289 | } else { |
---|
| 290 | map.zoomToMaxExtent(); |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | }, |
---|
| 294 | |
---|
| 295 | /** private: method[afterRender] |
---|
| 296 | * Private method called after the panel has been rendered. |
---|
| 297 | */ |
---|
| 298 | afterRender: function() { |
---|
| 299 | GeoExt.MapPanel.superclass.afterRender.apply(this, arguments); |
---|
| 300 | if(!this.ownerCt) { |
---|
| 301 | this.renderMap(); |
---|
| 302 | } else { |
---|
| 303 | this.ownerCt.on("move", this.updateMapSize, this); |
---|
| 304 | this.ownerCt.on({ |
---|
| 305 | "afterlayout": { |
---|
| 306 | fn: this.renderMap, |
---|
| 307 | scope: this, |
---|
| 308 | single: true |
---|
| 309 | } |
---|
| 310 | }); |
---|
| 311 | } |
---|
| 312 | }, |
---|
| 313 | |
---|
| 314 | /** private: method[onResize] |
---|
| 315 | * Private method called after the panel has been resized. |
---|
| 316 | */ |
---|
| 317 | onResize: function() { |
---|
| 318 | GeoExt.MapPanel.superclass.onResize.apply(this, arguments); |
---|
| 319 | this.updateMapSize(); |
---|
| 320 | }, |
---|
| 321 | |
---|
| 322 | /** private: method[onBeforeAdd] |
---|
| 323 | * Private method called before a component is added to the panel. |
---|
| 324 | */ |
---|
| 325 | onBeforeAdd: function(item) { |
---|
| 326 | if(typeof item.addToMapPanel === "function") { |
---|
| 327 | item.addToMapPanel(this); |
---|
| 328 | } |
---|
| 329 | GeoExt.MapPanel.superclass.onBeforeAdd.apply(this, arguments); |
---|
| 330 | }, |
---|
| 331 | |
---|
| 332 | /** private: method[remove] |
---|
| 333 | * Private method called when a component is removed from the panel. |
---|
| 334 | */ |
---|
| 335 | remove: function(item, autoDestroy) { |
---|
| 336 | if(typeof item.removeFromMapPanel === "function") { |
---|
| 337 | item.removeFromMapPanel(this); |
---|
| 338 | } |
---|
| 339 | GeoExt.MapPanel.superclass.remove.apply(this, arguments); |
---|
| 340 | }, |
---|
| 341 | |
---|
| 342 | /** private: method[beforeDestroy] |
---|
| 343 | * Private method called during the destroy sequence. |
---|
| 344 | */ |
---|
| 345 | beforeDestroy: function() { |
---|
| 346 | if(this.ownerCt) { |
---|
| 347 | this.ownerCt.un("move", this.updateMapSize, this); |
---|
| 348 | } |
---|
| 349 | if(this.map && this.map.events) { |
---|
| 350 | this.map.events.un({ |
---|
| 351 | "moveend": this.onMoveend, |
---|
| 352 | "changelayer": this.onLayerchange, |
---|
| 353 | scope: this |
---|
| 354 | }); |
---|
| 355 | } |
---|
| 356 | // if the map panel was passed a map instance, this map instance |
---|
| 357 | // is under the user's responsibility |
---|
| 358 | if(!this.initialConfig.map || |
---|
| 359 | !(this.initialConfig.map instanceof OpenLayers.Map)) { |
---|
| 360 | // we created the map, we destroy it |
---|
| 361 | if(this.map && this.map.destroy) { |
---|
| 362 | this.map.destroy(); |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | delete this.map; |
---|
| 366 | GeoExt.MapPanel.superclass.beforeDestroy.apply(this, arguments); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | }); |
---|
| 370 | |
---|
| 371 | /** api: function[guess] |
---|
| 372 | * :return: ``GeoExt.MapPanel`` The first map panel found by the Ext |
---|
| 373 | * component manager. |
---|
| 374 | * |
---|
| 375 | * Convenience function for guessing the map panel of an application. This |
---|
| 376 | * can reliably be used for all applications that just have one map panel |
---|
| 377 | * in the viewport. |
---|
| 378 | */ |
---|
| 379 | GeoExt.MapPanel.guess = function() { |
---|
| 380 | return Ext.ComponentMgr.all.find(function(o) { |
---|
| 381 | return o instanceof GeoExt.MapPanel; |
---|
| 382 | }); |
---|
| 383 | }; |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | /** api: xtype = gx_mappanel */ |
---|
| 387 | Ext.reg('gx_mappanel', GeoExt.MapPanel); |
---|