[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/widgets/tips/ZoomSliderTip.js |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /** api: (define) |
---|
| 14 | * module = GeoExt |
---|
| 15 | * class = ZoomSlider |
---|
| 16 | * base_link = `Ext.Slider <http://dev.sencha.com/deploy/dev/docs/?class=Ext.Slider>`_ |
---|
| 17 | */ |
---|
| 18 | Ext.namespace("GeoExt"); |
---|
| 19 | |
---|
| 20 | /** api: example |
---|
| 21 | * Sample code to render a slider outside the map viewport: |
---|
| 22 | * |
---|
| 23 | * .. code-block:: javascript |
---|
| 24 | * |
---|
| 25 | * var slider = new GeoExt.ZoomSlider({ |
---|
| 26 | * renderTo: document.body, |
---|
| 27 | * width: 200, |
---|
| 28 | * map: map |
---|
| 29 | * }); |
---|
| 30 | * |
---|
| 31 | * Sample code to add a slider to a map panel: |
---|
| 32 | * |
---|
| 33 | * .. code-block:: javascript |
---|
| 34 | * |
---|
| 35 | * var panel = new GeoExt.MapPanel({ |
---|
| 36 | * renderTo: document.body, |
---|
| 37 | * height: 300, |
---|
| 38 | * width: 400, |
---|
| 39 | * map: { |
---|
| 40 | * controls: [new OpenLayers.Control.Navigation()] |
---|
| 41 | * }, |
---|
| 42 | * layers: [new OpenLayers.Layer.WMS( |
---|
| 43 | * "Global Imagery", |
---|
| 44 | * "http://maps.opengeo.org/geowebcache/service/wms", |
---|
| 45 | * {layers: "bluemarble"} |
---|
| 46 | * )], |
---|
| 47 | * extent: [-5, 35, 15, 55], |
---|
| 48 | * items: [{ |
---|
| 49 | * xtype: "gx_zoomslider", |
---|
| 50 | * aggressive: true, |
---|
| 51 | * vertical: true, |
---|
| 52 | * height: 100, |
---|
| 53 | * x: 10, |
---|
| 54 | * y: 20 |
---|
| 55 | * }] |
---|
| 56 | * }); |
---|
| 57 | */ |
---|
| 58 | |
---|
| 59 | /** api: constructor |
---|
| 60 | * .. class:: ZoomSlider(config) |
---|
| 61 | * |
---|
| 62 | * Create a slider for controlling a map's zoom level. |
---|
| 63 | */ |
---|
| 64 | GeoExt.ZoomSlider = Ext.extend(Ext.Slider, { |
---|
| 65 | |
---|
| 66 | /** api: config[map] |
---|
| 67 | * ``OpenLayers.Map`` or :class:`GeoExt.MapPanel` |
---|
| 68 | * The map that the slider controls. |
---|
| 69 | */ |
---|
| 70 | map: null, |
---|
| 71 | |
---|
| 72 | /** api: config[baseCls] |
---|
| 73 | * ``String`` |
---|
| 74 | * The CSS class name for the slider elements. Default is "gx-zoomslider". |
---|
| 75 | */ |
---|
| 76 | baseCls: "gx-zoomslider", |
---|
| 77 | |
---|
| 78 | /** api: config[aggressive] |
---|
| 79 | * ``Boolean`` |
---|
| 80 | * If set to true, the map is zoomed as soon as the thumb is moved. Otherwise |
---|
| 81 | * the map is zoomed when the thumb is released (default). |
---|
| 82 | */ |
---|
| 83 | aggressive: false, |
---|
| 84 | |
---|
| 85 | /** private: property[updating] |
---|
| 86 | * ``Boolean`` |
---|
| 87 | * The slider position is being updated by itself (based on map zoomend). |
---|
| 88 | */ |
---|
| 89 | updating: false, |
---|
| 90 | |
---|
| 91 | /** private: method[initComponent] |
---|
| 92 | * Initialize the component. |
---|
| 93 | */ |
---|
| 94 | initComponent: function() { |
---|
| 95 | GeoExt.ZoomSlider.superclass.initComponent.call(this); |
---|
| 96 | |
---|
| 97 | if(this.map) { |
---|
| 98 | if(this.map instanceof GeoExt.MapPanel) { |
---|
| 99 | this.map = this.map.map; |
---|
| 100 | } |
---|
| 101 | this.bind(this.map); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | if (this.aggressive === true) { |
---|
| 105 | this.on('change', this.changeHandler, this); |
---|
| 106 | } else { |
---|
| 107 | this.on('changecomplete', this.changeHandler, this); |
---|
| 108 | } |
---|
| 109 | this.on("beforedestroy", this.unbind, this); |
---|
| 110 | }, |
---|
| 111 | |
---|
| 112 | /** private: method[onRender] |
---|
| 113 | * Override onRender to set base css class. |
---|
| 114 | */ |
---|
| 115 | onRender: function() { |
---|
| 116 | GeoExt.ZoomSlider.superclass.onRender.apply(this, arguments); |
---|
| 117 | this.el.addClass(this.baseCls); |
---|
| 118 | }, |
---|
| 119 | |
---|
| 120 | /** private: method[afterRender] |
---|
| 121 | * Override afterRender because the render event is fired too early |
---|
| 122 | * to call update. |
---|
| 123 | */ |
---|
| 124 | afterRender : function(){ |
---|
| 125 | Ext.Slider.superclass.afterRender.apply(this, arguments); |
---|
| 126 | this.update(); |
---|
| 127 | }, |
---|
| 128 | |
---|
| 129 | /** private: method[addToMapPanel] |
---|
| 130 | * :param panel: :class:`GeoExt.MapPanel` |
---|
| 131 | * |
---|
| 132 | * Called by a MapPanel if this component is one of the items in the panel. |
---|
| 133 | */ |
---|
| 134 | addToMapPanel: function(panel) { |
---|
| 135 | this.on({ |
---|
| 136 | render: function() { |
---|
| 137 | var el = this.getEl(); |
---|
| 138 | el.setStyle({ |
---|
| 139 | position: "absolute", |
---|
| 140 | zIndex: panel.map.Z_INDEX_BASE.Control |
---|
| 141 | }); |
---|
| 142 | el.on({ |
---|
| 143 | mousedown: this.stopMouseEvents, |
---|
| 144 | click: this.stopMouseEvents |
---|
| 145 | }); |
---|
| 146 | }, |
---|
| 147 | afterrender: function() { |
---|
| 148 | this.bind(panel.map); |
---|
| 149 | }, |
---|
| 150 | scope: this |
---|
| 151 | }); |
---|
| 152 | }, |
---|
| 153 | |
---|
| 154 | /** private: method[stopMouseEvents] |
---|
| 155 | * :param e: ``Object`` |
---|
| 156 | */ |
---|
| 157 | stopMouseEvents: function(e) { |
---|
| 158 | e.stopEvent(); |
---|
| 159 | }, |
---|
| 160 | |
---|
| 161 | /** private: method[removeFromMapPanel] |
---|
| 162 | * :param panel: :class:`GeoExt.MapPanel` |
---|
| 163 | * |
---|
| 164 | * Called by a MapPanel if this component is one of the items in the panel. |
---|
| 165 | */ |
---|
| 166 | removeFromMapPanel: function(panel) { |
---|
| 167 | var el = this.getEl(); |
---|
| 168 | el.un("mousedown", this.stopMouseEvents, this); |
---|
| 169 | el.un("click", this.stopMouseEvents, this); |
---|
| 170 | this.unbind(); |
---|
| 171 | }, |
---|
| 172 | |
---|
| 173 | /** private: method[bind] |
---|
| 174 | * :param map: ``OpenLayers.Map`` |
---|
| 175 | */ |
---|
| 176 | bind: function(map) { |
---|
| 177 | this.map = map; |
---|
| 178 | this.map.events.on({ |
---|
| 179 | zoomend: this.update, |
---|
| 180 | changebaselayer: this.initZoomValues, |
---|
| 181 | scope: this |
---|
| 182 | }); |
---|
| 183 | if(this.map.baseLayer) { |
---|
| 184 | this.initZoomValues(); |
---|
| 185 | this.update(); |
---|
| 186 | } |
---|
| 187 | }, |
---|
| 188 | |
---|
| 189 | /** private: method[unbind] |
---|
| 190 | */ |
---|
| 191 | unbind: function() { |
---|
| 192 | if(this.map) { |
---|
| 193 | this.map.events.un({ |
---|
| 194 | zoomend: this.update, |
---|
| 195 | changebaselayer: this.initZoomValues, |
---|
| 196 | scope: this |
---|
| 197 | }); |
---|
| 198 | } |
---|
| 199 | }, |
---|
| 200 | |
---|
| 201 | /** private: method[initZoomValues] |
---|
| 202 | * Set the min/max values for the slider if not set in the config. |
---|
| 203 | */ |
---|
| 204 | initZoomValues: function() { |
---|
| 205 | var layer = this.map.baseLayer; |
---|
| 206 | if(this.initialConfig.minValue === undefined) { |
---|
| 207 | this.minValue = layer.minZoomLevel || 0; |
---|
| 208 | } |
---|
| 209 | if(this.initialConfig.maxValue === undefined) { |
---|
| 210 | this.maxValue = layer.minZoomLevel == null ? |
---|
| 211 | layer.numZoomLevels - 1 : layer.maxZoomLevel; |
---|
| 212 | } |
---|
| 213 | }, |
---|
| 214 | |
---|
| 215 | /** api: method[getZoom] |
---|
| 216 | * :return: ``Number`` The map zoom level. |
---|
| 217 | * |
---|
| 218 | * Get the zoom level for the associated map based on the slider value. |
---|
| 219 | */ |
---|
| 220 | getZoom: function() { |
---|
| 221 | return this.getValue(); |
---|
| 222 | }, |
---|
| 223 | |
---|
| 224 | /** api: method[getScale] |
---|
| 225 | * :return: ``Number`` The map scale denominator. |
---|
| 226 | * |
---|
| 227 | * Get the scale denominator for the associated map based on the slider value. |
---|
| 228 | */ |
---|
| 229 | getScale: function() { |
---|
| 230 | return OpenLayers.Util.getScaleFromResolution( |
---|
| 231 | this.map.getResolutionForZoom(this.getValue()), |
---|
| 232 | this.map.getUnits() |
---|
| 233 | ); |
---|
| 234 | }, |
---|
| 235 | |
---|
| 236 | /** api: method[getResolution] |
---|
| 237 | * :return: ``Number`` The map resolution. |
---|
| 238 | * |
---|
| 239 | * Get the resolution for the associated map based on the slider value. |
---|
| 240 | */ |
---|
| 241 | getResolution: function() { |
---|
| 242 | return this.map.getResolutionForZoom(this.getValue()); |
---|
| 243 | }, |
---|
| 244 | |
---|
| 245 | /** private: method[changeHandler] |
---|
| 246 | * Registered as a listener for slider changecomplete. Zooms the map. |
---|
| 247 | */ |
---|
| 248 | changeHandler: function() { |
---|
| 249 | if(this.map && !this.updating) { |
---|
| 250 | this.map.zoomTo(this.getValue()); |
---|
| 251 | } |
---|
| 252 | }, |
---|
| 253 | |
---|
| 254 | /** private: method[update] |
---|
| 255 | * Registered as a listener for map zoomend. Updates the value of the slider. |
---|
| 256 | */ |
---|
| 257 | update: function() { |
---|
| 258 | if(this.rendered && this.map) { |
---|
| 259 | this.updating = true; |
---|
| 260 | this.setValue(this.map.getZoom()); |
---|
| 261 | this.updating = false; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | }); |
---|
| 266 | |
---|
| 267 | /** api: xtype = gx_zoomslider */ |
---|
| 268 | Ext.reg('gx_zoomslider', GeoExt.ZoomSlider); |
---|