| 1 |
define([ |
| 2 |
"dojo/_base/lang", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/event", |
| 5 |
"dojo/_base/connect", |
| 6 |
"dojo/_base/window", |
| 7 |
"dojo/_base/html", |
| 8 |
"dojo/dom", |
| 9 |
"dojo/_base/sniff" |
| 10 |
], function(lang, declare, event, connect, win, html, dom, has){ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
return declare("dojox.geo.charting.MouseInteractionSupport", null, { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
_map: null, |
| 32 |
_mapClickLocation: null, |
| 33 |
_screenClickLocation: null, |
| 34 |
_mouseDragListener: null, |
| 35 |
_mouseUpListener: null, |
| 36 |
_mouseUpClickListener: null, |
| 37 |
_mouseDownListener: null, |
| 38 |
_mouseMoveListener: null, |
| 39 |
_mouseWheelListener: null, |
| 40 |
_currentFeature: null, |
| 41 |
_cancelMouseClick: null, |
| 42 |
_zoomEnabled: false, |
| 43 |
_panEnabled: false, |
| 44 |
_onDragStartListener: null, |
| 45 |
_onSelectStartListener: null, |
| 46 |
|
| 47 |
|
| 48 |
mouseClickThreshold: 2, |
| 49 |
|
| 50 |
constructor : function(map, options){ |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
this._map = map; |
| 58 |
this._mapClickLocation = {x: 0,y: 0}; |
| 59 |
this._screenClickLocation = {x: 0,y: 0}; |
| 60 |
this._cancelMouseClick = false; |
| 61 |
if(options){ |
| 62 |
this._zoomEnabled = options.enableZoom; |
| 63 |
this._panEnabled = options.enablePan; |
| 64 |
if(options.mouseClickThreshold && options.mouseClickThreshold > 0){ |
| 65 |
this.mouseClickThreshold = options.mouseClickThreshold; |
| 66 |
} |
| 67 |
} |
| 68 |
}, |
| 69 |
|
| 70 |
setEnableZoom: function(enable){ |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if(enable && !this._mouseWheelListener){ |
| 76 |
|
| 77 |
var wheelEventName = !has("mozilla") ? "onmousewheel" : "DOMMouseScroll"; |
| 78 |
this._mouseWheelListener = this._map.surface.connect(wheelEventName, this, this._mouseWheelHandler); |
| 79 |
}else if(!enable && this._mouseWheelListener){ |
| 80 |
|
| 81 |
connect.disconnect(this._mouseWheelListener); |
| 82 |
this._mouseWheelListener = null; |
| 83 |
} |
| 84 |
this._zoomEnabled = enable; |
| 85 |
}, |
| 86 |
|
| 87 |
setEnablePan: function(enable){ |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
this._panEnabled = enable; |
| 93 |
}, |
| 94 |
|
| 95 |
connect: function(){ |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
this._mouseMoveListener = this._map.surface.connect("onmousemove", this, this._mouseMoveHandler); |
| 101 |
this._mouseDownListener = this._map.surface.connect("onmousedown", this, this._mouseDownHandler); |
| 102 |
|
| 103 |
if(has("ie")){ |
| 104 |
_onDragStartListener = connect.connect(win.doc,"ondragstart",this,event.stop); |
| 105 |
_onSelectStartListener = connect.connect(win.doc,"onselectstart",this,event.stop); |
| 106 |
} |
| 107 |
|
| 108 |
this.setEnableZoom(this._zoomEnabled); |
| 109 |
this.setEnablePan(this._panEnabled); |
| 110 |
}, |
| 111 |
|
| 112 |
disconnect: function(){ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
var isZoom = this._zoomEnabled; |
| 118 |
|
| 119 |
|
| 120 |
this.setEnableZoom(false); |
| 121 |
|
| 122 |
|
| 123 |
this._zoomEnabled = isZoom; |
| 124 |
|
| 125 |
|
| 126 |
if(this._mouseMoveListener){ |
| 127 |
connect.disconnect(this._mouseMoveListener); |
| 128 |
this._mouseMoveListener = null; |
| 129 |
connect.disconnect(this._mouseDownListener); |
| 130 |
this._mouseDownListener = null; |
| 131 |
} |
| 132 |
if(this._onDragStartListener){ |
| 133 |
connect.disconnect(this._onDragStartListener); |
| 134 |
this._onDragStartListener = null; |
| 135 |
connect.disconnect(this._onSelectStartListener); |
| 136 |
this._onSelectStartListener = null; |
| 137 |
} |
| 138 |
|
| 139 |
}, |
| 140 |
|
| 141 |
_mouseClickHandler: function(mouseEvent){ |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
var feature = this._getFeatureFromMouseEvent(mouseEvent); |
| 150 |
|
| 151 |
if(feature){ |
| 152 |
|
| 153 |
feature._onclickHandler(mouseEvent); |
| 154 |
}else{ |
| 155 |
|
| 156 |
for(var name in this._map.mapObj.features){ |
| 157 |
this._map.mapObj.features[name].select(false); |
| 158 |
} |
| 159 |
this._map.onFeatureClick(null); |
| 160 |
} |
| 161 |
|
| 162 |
}, |
| 163 |
|
| 164 |
_mouseDownHandler: function(mouseEvent){ |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
event.stop(mouseEvent); |
| 174 |
|
| 175 |
this._map.focused = true; |
| 176 |
|
| 177 |
this._cancelMouseClick = false; |
| 178 |
this._screenClickLocation.x = mouseEvent.pageX; |
| 179 |
this._screenClickLocation.y = mouseEvent.pageY; |
| 180 |
|
| 181 |
|
| 182 |
var containerBounds = this._map._getContainerBounds(); |
| 183 |
var offX = mouseEvent.pageX - containerBounds.x, |
| 184 |
offY = mouseEvent.pageY - containerBounds.y; |
| 185 |
var mapPoint = this._map.screenCoordsToMapCoords(offX,offY); |
| 186 |
this._mapClickLocation.x = mapPoint.x; |
| 187 |
this._mapClickLocation.y = mapPoint.y; |
| 188 |
|
| 189 |
|
| 190 |
if(!has("ie")){ |
| 191 |
this._mouseDragListener = connect.connect(win.doc,"onmousemove",this,this._mouseDragHandler); |
| 192 |
this._mouseUpClickListener = this._map.surface.connect("onmouseup", this, this._mouseUpClickHandler); |
| 193 |
this._mouseUpListener = connect.connect(win.doc,"onmouseup",this, this._mouseUpHandler); |
| 194 |
}else{ |
| 195 |
var node = dom.byId(this._map.container); |
| 196 |
this._mouseDragListener = connect.connect(node,"onmousemove",this,this._mouseDragHandler); |
| 197 |
this._mouseUpClickListener = this._map.surface.connect("onmouseup", this, this._mouseUpClickHandler); |
| 198 |
this._mouseUpListener = this._map.surface.connect("onmouseup", this, this._mouseUpHandler); |
| 199 |
this._map.surface.rawNode.setCapture(); |
| 200 |
} |
| 201 |
}, |
| 202 |
|
| 203 |
_mouseUpClickHandler: function(mouseEvent){ |
| 204 |
|
| 205 |
if(!this._cancelMouseClick){ |
| 206 |
|
| 207 |
this._mouseClickHandler(mouseEvent); |
| 208 |
} |
| 209 |
this._cancelMouseClick = false; |
| 210 |
|
| 211 |
}, |
| 212 |
|
| 213 |
_mouseUpHandler: function(mouseEvent){ |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
event.stop(mouseEvent); |
| 222 |
|
| 223 |
this._map.mapObj.marker._needTooltipRefresh = true; |
| 224 |
|
| 225 |
|
| 226 |
if(this._mouseDragListener){ |
| 227 |
connect.disconnect(this._mouseDragListener); |
| 228 |
this._mouseDragListener = null; |
| 229 |
} |
| 230 |
if(this._mouseUpClickListener){ |
| 231 |
connect.disconnect(this._mouseUpClickListener); |
| 232 |
this._mouseUpClickListener = null; |
| 233 |
} |
| 234 |
if(this._mouseUpListener){ |
| 235 |
connect.disconnect(this._mouseUpListener); |
| 236 |
this._mouseUpListener = null; |
| 237 |
} |
| 238 |
|
| 239 |
if(has("ie")){ |
| 240 |
this._map.surface.rawNode.releaseCapture(); |
| 241 |
} |
| 242 |
}, |
| 243 |
|
| 244 |
_getFeatureFromMouseEvent: function(mouseEvent){ |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
var feature = null; |
| 254 |
if(mouseEvent.gfxTarget && mouseEvent.gfxTarget.getParent){ |
| 255 |
feature = this._map.mapObj.features[mouseEvent.gfxTarget.getParent().id]; |
| 256 |
} |
| 257 |
return feature; |
| 258 |
}, |
| 259 |
|
| 260 |
_mouseMoveHandler: function(mouseEvent){ |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
if(this._mouseDragListener && this._panEnabled){ |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
var feature = this._getFeatureFromMouseEvent(mouseEvent); |
| 276 |
|
| 277 |
|
| 278 |
if(feature != this._currentFeature){ |
| 279 |
if(this._currentFeature){ |
| 280 |
|
| 281 |
this._currentFeature._onmouseoutHandler(); |
| 282 |
} |
| 283 |
this._currentFeature = feature; |
| 284 |
|
| 285 |
if(feature){ |
| 286 |
feature._onmouseoverHandler(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if(feature){ |
| 291 |
feature._onmousemoveHandler(mouseEvent); |
| 292 |
} |
| 293 |
}, |
| 294 |
|
| 295 |
_mouseDragHandler: function(mouseEvent){ |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
event.stop(mouseEvent); |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
var dx = Math.abs(mouseEvent.pageX - this._screenClickLocation.x); |
| 309 |
var dy = Math.abs(mouseEvent.pageY - this._screenClickLocation.y); |
| 310 |
if(!this._cancelMouseClick && (dx > this.mouseClickThreshold || dy > this.mouseClickThreshold)){ |
| 311 |
|
| 312 |
this._cancelMouseClick = true; |
| 313 |
if(this._panEnabled){ |
| 314 |
this._map.mapObj.marker.hide(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if(!this._panEnabled){ |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
var cBounds = this._map._getContainerBounds(); |
| 323 |
var offX = mouseEvent.pageX - cBounds.x, |
| 324 |
offY = mouseEvent.pageY - cBounds.y; |
| 325 |
|
| 326 |
|
| 327 |
var mapPoint = this._map.screenCoordsToMapCoords(offX,offY); |
| 328 |
var mapOffsetX = mapPoint.x - this._mapClickLocation.x; |
| 329 |
var mapOffsetY = mapPoint.y - this._mapClickLocation.y; |
| 330 |
|
| 331 |
|
| 332 |
var currentMapCenter = this._map.getMapCenter(); |
| 333 |
this._map.setMapCenter(currentMapCenter.x - mapOffsetX, currentMapCenter.y - mapOffsetY); |
| 334 |
|
| 335 |
}, |
| 336 |
|
| 337 |
_mouseWheelHandler: function(mouseEvent){ |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
event.stop(mouseEvent); |
| 347 |
|
| 348 |
|
| 349 |
this._map.mapObj.marker.hide(); |
| 350 |
|
| 351 |
|
| 352 |
var containerBounds = this._map._getContainerBounds(); |
| 353 |
var offX = mouseEvent.pageX - containerBounds.x, |
| 354 |
offY = mouseEvent.pageY - containerBounds.y; |
| 355 |
|
| 356 |
|
| 357 |
var invariantMapPoint = this._map.screenCoordsToMapCoords(offX,offY); |
| 358 |
|
| 359 |
|
| 360 |
var power = mouseEvent[(has("mozilla") ? "detail" : "wheelDelta")] / (has("mozilla") ? - 3 : 120) ; |
| 361 |
var scaleFactor = Math.pow(1.2,power); |
| 362 |
this._map.setMapScaleAt(this._map.getMapScale()*scaleFactor ,invariantMapPoint.x,invariantMapPoint.y,false); |
| 363 |
this._map.mapObj.marker._needTooltipRefresh = true; |
| 364 |
} |
| 365 |
}); |
| 366 |
}); |