Debian Codegraph @

dojox/geo/charting/MouseInteractionSupport.js

raw symbols SCIP
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 dojox.geo.charting.__MouseInteractionSupportCtorArgs = {
14 // summary:
15 // The keyword arguments that can be passed in a MouseInteractionSupport constructor.
16 // enableZoom: Boolean?
17 // Indicates whether zooming is enabled.
18 // enablePan: Boolean?
19 // Indicates whether panning is enabled.
20 // mouseClickThreshold: Number?
21 // The threshold (in pixel) that differentiates a drag from a click.
22 };
23 =====*/
24
25 return declare("dojox.geo.charting.MouseInteractionSupport", null, {
26 // summary:
27 // class to handle mouse interactions on a dojox.geo.charting.Map widget
28 // tags:
29 // private
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 // summary:
52 // Constructs a new _MouseInteractionSupport instance
53 // map: dojox/geo/charting/Map
54 // the Map widget this class provides touch navigation for.
55 // options: dojox.geo.charting.__MouseInteractionSupportCtorArgs?
56 // An object defining additional configuration properties.
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 // summary:
72 // enables mouse zoom on the map
73 // enable: Boolean
74 // Indicates whether mouse zoom is enabled.
75 if(enable && !this._mouseWheelListener){
76 // enable
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 // disable
81 connect.disconnect(this._mouseWheelListener);
82 this._mouseWheelListener = null;
83 }
84 this._zoomEnabled = enable;
85 },
86
87 setEnablePan: function(enable){
88 // summary:
89 // enables mouse panning on the map
90 // enable: Boolean
91 // Indicates whether mouse zoom is enabled.
92 this._panEnabled = enable;
93 },
94
95 connect: function(){
96 // summary:
97 // connects this mouse support class to the Map component
98
99 // install mouse listeners
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 // summary:
114 // disconnects any installed listeners
115
116 // store zoomPan state
117 var isZoom = this._zoomEnabled;
118
119 // disable zoom (disconnects listeners..)
120 this.setEnableZoom(false);
121
122 // restore value
123 this._zoomEnabled = isZoom;
124
125 // disconnect remaining listeners
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 // summary:
143 // action performed on the map when a mouse click was performed
144 // mouseEvent: MouseEvent
145 // the mouse event
146 // tags:
147 // private
148
149 var feature = this._getFeatureFromMouseEvent(mouseEvent);
150
151 if(feature){
152 // call feature handler
153 feature._onclickHandler(mouseEvent);
154 }else{
155 // unselect all
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 // summary:
166 // action performed on the map when a mouse down was performed
167 // mouseEvent: MouseEvent
168 // the mouse event
169 // tags:
170 // private
171
172
173 event.stop(mouseEvent);
174
175 this._map.focused = true;
176 // set various status parameters
177 this._cancelMouseClick = false;
178 this._screenClickLocation.x = mouseEvent.pageX;
179 this._screenClickLocation.y = mouseEvent.pageY;
180
181 // store map location where mouse down occurred
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 // install drag listener if pan is enabled
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 // execute mouse click handler
207 this._mouseClickHandler(mouseEvent);
208 }
209 this._cancelMouseClick = false;
210
211 },
212
213 _mouseUpHandler: function(mouseEvent){
214 // summary:
215 // action performed on the map when a mouse up was performed
216 // mouseEvent: MouseEvent
217 // the mouse event
218 // tags:
219 // private
220
221 event.stop(mouseEvent);
222
223 this._map.mapObj.marker._needTooltipRefresh = true;
224
225 // disconnect listeners
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 // summary:
246 // utility function to return the feature located at this mouse event location
247 // mouseEvent: MouseEvent
248 // the mouse event
249 // returns:
250 // the feature found if any, null otherwise.
251 // tags:
252 // private
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; // dojox/geo/charting/Feature
258 },
259
260 _mouseMoveHandler: function(mouseEvent){
261 // summary:
262 // action performed on the map when a mouse move was performed
263 // mouseEvent: MouseEvent
264 // the mouse event
265 // tags:
266 // private
267
268
269 // do nothing more if dragging
270 if(this._mouseDragListener && this._panEnabled){
271 return;
272 }
273
274 // hover and highlight
275 var feature = this._getFeatureFromMouseEvent(mouseEvent);
276
277 // set/unset highlight
278 if(feature != this._currentFeature){
279 if(this._currentFeature){
280 // mouse leaving component
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 // summary:
297 // action performed on the map when a mouse drag was performed
298 // mouseEvent: MouseEvent
299 // the mouse event
300 // tags:
301 // private
302
303 // prevent browser interaction
304 event.stop(mouseEvent);
305
306
307 // find out if this the movement discards the "mouse click" gesture
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 // cancel mouse click
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 // compute map offset
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 // adjust map center
332 var currentMapCenter = this._map.getMapCenter();
333 this._map.setMapCenter(currentMapCenter.x - mapOffsetX, currentMapCenter.y - mapOffsetY);
334
335 },
336
337 _mouseWheelHandler: function(mouseEvent){
338 // summary:
339 // action performed on the map when a mouse wheel up/down was performed
340 // mouseEvent: MouseEvent
341 // the mouse event
342 // tags:
343 // private
344
345 // prevent browser interaction
346 event.stop(mouseEvent);
347
348 // hide tooltip
349 this._map.mapObj.marker.hide();
350
351 // event coords within component
352 var containerBounds = this._map._getContainerBounds();
353 var offX = mouseEvent.pageX - containerBounds.x,
354 offY = mouseEvent.pageY - containerBounds.y;
355
356 // current map point before zooming
357 var invariantMapPoint = this._map.screenCoordsToMapCoords(offX,offY);
358
359 // zoom increment power
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 });