[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Control.js |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Class: OpenLayers.Control.MouseDefaults |
---|
| 12 | * This class is DEPRECATED in 2.4 and will be removed by 3.0. |
---|
| 13 | * If you need this functionality, use <OpenLayers.Control.Navigation> |
---|
| 14 | * instead!!! |
---|
| 15 | * |
---|
| 16 | * This class is DEPRECATED in 2.4 and will be removed by 3.0. |
---|
| 17 | * If you need this functionality, use Control.Navigation instead!!! |
---|
| 18 | * |
---|
| 19 | * Inherits from: |
---|
| 20 | * - <OpenLayers.Control> |
---|
| 21 | */ |
---|
| 22 | OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, { |
---|
| 23 | |
---|
| 24 | /** WARNING WARNING WARNING!!! |
---|
| 25 | This class is DEPRECATED in 2.4 and will be removed by 3.0. |
---|
| 26 | If you need this functionality, use Control.Navigation instead!!! */ |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Property: performedDrag |
---|
| 30 | * {Boolean} |
---|
| 31 | */ |
---|
| 32 | performedDrag: false, |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Property: wheelObserver |
---|
| 36 | * {Function} |
---|
| 37 | */ |
---|
| 38 | wheelObserver: null, |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Constructor: OpenLayers.Control.MouseDefaults |
---|
| 42 | */ |
---|
| 43 | initialize: function() { |
---|
| 44 | OpenLayers.Control.prototype.initialize.apply(this, arguments); |
---|
| 45 | }, |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * APIMethod: destroy |
---|
| 49 | */ |
---|
| 50 | destroy: function() { |
---|
| 51 | |
---|
| 52 | if (this.handler) { |
---|
| 53 | this.handler.destroy(); |
---|
| 54 | } |
---|
| 55 | this.handler = null; |
---|
| 56 | |
---|
| 57 | this.map.events.un({ |
---|
| 58 | "click": this.defaultClick, |
---|
| 59 | "dblclick": this.defaultDblClick, |
---|
| 60 | "mousedown": this.defaultMouseDown, |
---|
| 61 | "mouseup": this.defaultMouseUp, |
---|
| 62 | "mousemove": this.defaultMouseMove, |
---|
| 63 | "mouseout": this.defaultMouseOut, |
---|
| 64 | scope: this |
---|
| 65 | }); |
---|
| 66 | |
---|
| 67 | //unregister mousewheel events specifically on the window and document |
---|
| 68 | OpenLayers.Event.stopObserving(window, "DOMMouseScroll", |
---|
| 69 | this.wheelObserver); |
---|
| 70 | OpenLayers.Event.stopObserving(window, "mousewheel", |
---|
| 71 | this.wheelObserver); |
---|
| 72 | OpenLayers.Event.stopObserving(document, "mousewheel", |
---|
| 73 | this.wheelObserver); |
---|
| 74 | this.wheelObserver = null; |
---|
| 75 | |
---|
| 76 | OpenLayers.Control.prototype.destroy.apply(this, arguments); |
---|
| 77 | }, |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * Method: draw |
---|
| 81 | */ |
---|
| 82 | draw: function() { |
---|
| 83 | this.map.events.on({ |
---|
| 84 | "click": this.defaultClick, |
---|
| 85 | "dblclick": this.defaultDblClick, |
---|
| 86 | "mousedown": this.defaultMouseDown, |
---|
| 87 | "mouseup": this.defaultMouseUp, |
---|
| 88 | "mousemove": this.defaultMouseMove, |
---|
| 89 | "mouseout": this.defaultMouseOut, |
---|
| 90 | scope: this |
---|
| 91 | }); |
---|
| 92 | |
---|
| 93 | this.registerWheelEvents(); |
---|
| 94 | |
---|
| 95 | }, |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Method: registerWheelEvents |
---|
| 99 | */ |
---|
| 100 | registerWheelEvents: function() { |
---|
| 101 | |
---|
| 102 | this.wheelObserver = OpenLayers.Function.bindAsEventListener( |
---|
| 103 | this.onWheelEvent, this |
---|
| 104 | ); |
---|
| 105 | |
---|
| 106 | //register mousewheel events specifically on the window and document |
---|
| 107 | OpenLayers.Event.observe(window, "DOMMouseScroll", this.wheelObserver); |
---|
| 108 | OpenLayers.Event.observe(window, "mousewheel", this.wheelObserver); |
---|
| 109 | OpenLayers.Event.observe(document, "mousewheel", this.wheelObserver); |
---|
| 110 | }, |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Method: defaultClick |
---|
| 114 | * |
---|
| 115 | * Parameters: |
---|
| 116 | * evt - {Event} |
---|
| 117 | * |
---|
| 118 | * Returns: |
---|
| 119 | * {Boolean} |
---|
| 120 | */ |
---|
| 121 | defaultClick: function (evt) { |
---|
| 122 | if (!OpenLayers.Event.isLeftClick(evt)) { |
---|
| 123 | return; |
---|
| 124 | } |
---|
| 125 | var notAfterDrag = !this.performedDrag; |
---|
| 126 | this.performedDrag = false; |
---|
| 127 | return notAfterDrag; |
---|
| 128 | }, |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Method: defaultDblClick |
---|
| 132 | * |
---|
| 133 | * Parameters: |
---|
| 134 | * evt - {Event} |
---|
| 135 | */ |
---|
| 136 | defaultDblClick: function (evt) { |
---|
| 137 | var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); |
---|
| 138 | this.map.setCenter(newCenter, this.map.zoom + 1); |
---|
| 139 | OpenLayers.Event.stop(evt); |
---|
| 140 | return false; |
---|
| 141 | }, |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Method: defaultMouseDown |
---|
| 145 | * |
---|
| 146 | * Parameters: |
---|
| 147 | * evt - {Event} |
---|
| 148 | */ |
---|
| 149 | defaultMouseDown: function (evt) { |
---|
| 150 | if (!OpenLayers.Event.isLeftClick(evt)) { |
---|
| 151 | return; |
---|
| 152 | } |
---|
| 153 | this.mouseDragStart = evt.xy.clone(); |
---|
| 154 | this.performedDrag = false; |
---|
| 155 | if (evt.shiftKey) { |
---|
| 156 | this.map.div.style.cursor = "crosshair"; |
---|
| 157 | this.zoomBox = OpenLayers.Util.createDiv('zoomBox', |
---|
| 158 | this.mouseDragStart, |
---|
| 159 | null, |
---|
| 160 | null, |
---|
| 161 | "absolute", |
---|
| 162 | "2px solid red"); |
---|
| 163 | this.zoomBox.style.backgroundColor = "white"; |
---|
| 164 | this.zoomBox.style.filter = "alpha(opacity=50)"; // IE |
---|
| 165 | this.zoomBox.style.opacity = "0.50"; |
---|
| 166 | this.zoomBox.style.fontSize = "1px"; |
---|
| 167 | this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1; |
---|
| 168 | this.map.viewPortDiv.appendChild(this.zoomBox); |
---|
| 169 | } |
---|
| 170 | document.onselectstart = OpenLayers.Function.False; |
---|
| 171 | OpenLayers.Event.stop(evt); |
---|
| 172 | }, |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Method: defaultMouseMove |
---|
| 176 | * |
---|
| 177 | * Parameters: |
---|
| 178 | * evt - {Event} |
---|
| 179 | */ |
---|
| 180 | defaultMouseMove: function (evt) { |
---|
| 181 | // record the mouse position, used in onWheelEvent |
---|
| 182 | this.mousePosition = evt.xy.clone(); |
---|
| 183 | |
---|
| 184 | if (this.mouseDragStart != null) { |
---|
| 185 | if (this.zoomBox) { |
---|
| 186 | var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x); |
---|
| 187 | var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y); |
---|
| 188 | this.zoomBox.style.width = Math.max(1, deltaX) + "px"; |
---|
| 189 | this.zoomBox.style.height = Math.max(1, deltaY) + "px"; |
---|
| 190 | if (evt.xy.x < this.mouseDragStart.x) { |
---|
| 191 | this.zoomBox.style.left = evt.xy.x+"px"; |
---|
| 192 | } |
---|
| 193 | if (evt.xy.y < this.mouseDragStart.y) { |
---|
| 194 | this.zoomBox.style.top = evt.xy.y+"px"; |
---|
| 195 | } |
---|
| 196 | } else { |
---|
| 197 | var deltaX = this.mouseDragStart.x - evt.xy.x; |
---|
| 198 | var deltaY = this.mouseDragStart.y - evt.xy.y; |
---|
| 199 | var size = this.map.getSize(); |
---|
| 200 | var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX, |
---|
| 201 | size.h / 2 + deltaY); |
---|
| 202 | var newCenter = this.map.getLonLatFromViewPortPx( newXY ); |
---|
| 203 | this.map.setCenter(newCenter, null, true); |
---|
| 204 | this.mouseDragStart = evt.xy.clone(); |
---|
| 205 | this.map.div.style.cursor = "move"; |
---|
| 206 | } |
---|
| 207 | this.performedDrag = true; |
---|
| 208 | } |
---|
| 209 | }, |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * Method: defaultMouseUp |
---|
| 213 | * |
---|
| 214 | * Parameters: |
---|
| 215 | * evt - {<OpenLayers.Event>} |
---|
| 216 | */ |
---|
| 217 | defaultMouseUp: function (evt) { |
---|
| 218 | if (!OpenLayers.Event.isLeftClick(evt)) { |
---|
| 219 | return; |
---|
| 220 | } |
---|
| 221 | if (this.zoomBox) { |
---|
| 222 | this.zoomBoxEnd(evt); |
---|
| 223 | } else { |
---|
| 224 | if (this.performedDrag) { |
---|
| 225 | this.map.setCenter(this.map.center); |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | document.onselectstart=null; |
---|
| 229 | this.mouseDragStart = null; |
---|
| 230 | this.map.div.style.cursor = ""; |
---|
| 231 | }, |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * Method: defaultMouseOut |
---|
| 235 | * |
---|
| 236 | * Parameters: |
---|
| 237 | * evt - {Event} |
---|
| 238 | */ |
---|
| 239 | defaultMouseOut: function (evt) { |
---|
| 240 | if (this.mouseDragStart != null && |
---|
| 241 | OpenLayers.Util.mouseLeft(evt, this.map.div)) { |
---|
| 242 | if (this.zoomBox) { |
---|
| 243 | this.removeZoomBox(); |
---|
| 244 | } |
---|
| 245 | this.mouseDragStart = null; |
---|
| 246 | } |
---|
| 247 | }, |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * Method: defaultWheelUp |
---|
| 252 | * User spun scroll wheel up |
---|
| 253 | * |
---|
| 254 | */ |
---|
| 255 | defaultWheelUp: function(evt) { |
---|
| 256 | if (this.map.getZoom() <= this.map.getNumZoomLevels()) { |
---|
| 257 | this.map.setCenter(this.map.getLonLatFromPixel(evt.xy), |
---|
| 258 | this.map.getZoom() + 1); |
---|
| 259 | } |
---|
| 260 | }, |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Method: defaultWheelDown |
---|
| 264 | * User spun scroll wheel down |
---|
| 265 | */ |
---|
| 266 | defaultWheelDown: function(evt) { |
---|
| 267 | if (this.map.getZoom() > 0) { |
---|
| 268 | this.map.setCenter(this.map.getLonLatFromPixel(evt.xy), |
---|
| 269 | this.map.getZoom() - 1); |
---|
| 270 | } |
---|
| 271 | }, |
---|
| 272 | |
---|
| 273 | /** |
---|
| 274 | * Method: zoomBoxEnd |
---|
| 275 | * Zoombox function. |
---|
| 276 | */ |
---|
| 277 | zoomBoxEnd: function(evt) { |
---|
| 278 | if (this.mouseDragStart != null) { |
---|
| 279 | if (Math.abs(this.mouseDragStart.x - evt.xy.x) > 5 || |
---|
| 280 | Math.abs(this.mouseDragStart.y - evt.xy.y) > 5) { |
---|
| 281 | var start = this.map.getLonLatFromViewPortPx( this.mouseDragStart ); |
---|
| 282 | var end = this.map.getLonLatFromViewPortPx( evt.xy ); |
---|
| 283 | var top = Math.max(start.lat, end.lat); |
---|
| 284 | var bottom = Math.min(start.lat, end.lat); |
---|
| 285 | var left = Math.min(start.lon, end.lon); |
---|
| 286 | var right = Math.max(start.lon, end.lon); |
---|
| 287 | var bounds = new OpenLayers.Bounds(left, bottom, right, top); |
---|
| 288 | this.map.zoomToExtent(bounds); |
---|
| 289 | } else { |
---|
| 290 | var end = this.map.getLonLatFromViewPortPx( evt.xy ); |
---|
| 291 | this.map.setCenter(new OpenLayers.LonLat( |
---|
| 292 | (end.lon), |
---|
| 293 | (end.lat) |
---|
| 294 | ), this.map.getZoom() + 1); |
---|
| 295 | } |
---|
| 296 | this.removeZoomBox(); |
---|
| 297 | } |
---|
| 298 | }, |
---|
| 299 | |
---|
| 300 | /** |
---|
| 301 | * Method: removeZoomBox |
---|
| 302 | * Remove the zoombox from the screen and nullify our reference to it. |
---|
| 303 | */ |
---|
| 304 | removeZoomBox: function() { |
---|
| 305 | this.map.viewPortDiv.removeChild(this.zoomBox); |
---|
| 306 | this.zoomBox = null; |
---|
| 307 | }, |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | * Mouse ScrollWheel code thanks to http://adomas.org/javascript-mouse-wheel/ |
---|
| 312 | */ |
---|
| 313 | |
---|
| 314 | |
---|
| 315 | /** |
---|
| 316 | * Method: onWheelEvent |
---|
| 317 | * Catch the wheel event and handle it xbrowserly |
---|
| 318 | * |
---|
| 319 | * Parameters: |
---|
| 320 | * e - {Event} |
---|
| 321 | */ |
---|
| 322 | onWheelEvent: function(e){ |
---|
| 323 | |
---|
| 324 | // first determine whether or not the wheeling was inside the map |
---|
| 325 | var inMap = false; |
---|
| 326 | var elem = OpenLayers.Event.element(e); |
---|
| 327 | while(elem != null) { |
---|
| 328 | if (this.map && elem == this.map.div) { |
---|
| 329 | inMap = true; |
---|
| 330 | break; |
---|
| 331 | } |
---|
| 332 | elem = elem.parentNode; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | if (inMap) { |
---|
| 336 | |
---|
| 337 | var delta = 0; |
---|
| 338 | if (!e) { |
---|
| 339 | e = window.event; |
---|
| 340 | } |
---|
| 341 | if (e.wheelDelta) { |
---|
| 342 | delta = e.wheelDelta/120; |
---|
| 343 | if (window.opera && window.opera.version() < 9.2) { |
---|
| 344 | delta = -delta; |
---|
| 345 | } |
---|
| 346 | } else if (e.detail) { |
---|
| 347 | delta = -e.detail / 3; |
---|
| 348 | } |
---|
| 349 | if (delta) { |
---|
| 350 | // add the mouse position to the event because mozilla has a bug |
---|
| 351 | // with clientX and clientY (see https://bugzilla.mozilla.org/show_bug.cgi?id=352179) |
---|
| 352 | // getLonLatFromViewPortPx(e) returns wrong values |
---|
| 353 | e.xy = this.mousePosition; |
---|
| 354 | |
---|
| 355 | if (delta < 0) { |
---|
| 356 | this.defaultWheelDown(e); |
---|
| 357 | } else { |
---|
| 358 | this.defaultWheelUp(e); |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | //only wheel the map, not the window |
---|
| 363 | OpenLayers.Event.stop(e); |
---|
| 364 | } |
---|
| 365 | }, |
---|
| 366 | |
---|
| 367 | CLASS_NAME: "OpenLayers.Control.MouseDefaults" |
---|
| 368 | }); |
---|