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/Handler.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Handler.Hover |
---|
12 | * The hover handler is to be used to emulate mouseovers on objects |
---|
13 | * on the map that aren't DOM elements. For example one can use |
---|
14 | * this handler to send WMS/GetFeatureInfo requests as the user |
---|
15 | * moves the mouve over the map. |
---|
16 | * |
---|
17 | * Inherits from: |
---|
18 | * - <OpenLayers.Handler> |
---|
19 | */ |
---|
20 | OpenLayers.Handler.Hover = OpenLayers.Class(OpenLayers.Handler, { |
---|
21 | |
---|
22 | /** |
---|
23 | * APIProperty: delay |
---|
24 | * {Integer} - Number of milliseconds between mousemoves before |
---|
25 | * the event is considered a hover. Default is 500. |
---|
26 | */ |
---|
27 | delay: 500, |
---|
28 | |
---|
29 | /** |
---|
30 | * APIProperty: pixelTolerance |
---|
31 | * {Integer} - Maximum number of pixels between mousemoves for |
---|
32 | * an event to be considered a hover. Default is null. |
---|
33 | */ |
---|
34 | pixelTolerance: null, |
---|
35 | |
---|
36 | /** |
---|
37 | * APIProperty: stopMove |
---|
38 | * {Boolean} - Stop other listeners from being notified on mousemoves. |
---|
39 | * Default is false. |
---|
40 | */ |
---|
41 | stopMove: false, |
---|
42 | |
---|
43 | /** |
---|
44 | * Property: px |
---|
45 | * {<OpenLayers.Pixel>} - The location of the last mousemove, expressed |
---|
46 | * in pixels. |
---|
47 | */ |
---|
48 | px: null, |
---|
49 | |
---|
50 | /** |
---|
51 | * Property: timerId |
---|
52 | * {Number} - The id of the timer. |
---|
53 | */ |
---|
54 | timerId: null, |
---|
55 | |
---|
56 | /** |
---|
57 | * Constructor: OpenLayers.Handler.Hover |
---|
58 | * Construct a hover handler. |
---|
59 | * |
---|
60 | * Parameters: |
---|
61 | * control - {<OpenLayers.Control>} The control that initialized this |
---|
62 | * handler. The control is assumed to have a valid map property; that |
---|
63 | * map is used in the handler's own setMap method. |
---|
64 | * callbacks - {Object} An object with keys corresponding to callbacks |
---|
65 | * that will be called by the handler. The callbacks should |
---|
66 | * expect to receive a single argument, the event. Callbacks for |
---|
67 | * 'move', the mouse is moving, and 'pause', the mouse is pausing, |
---|
68 | * are supported. |
---|
69 | * options - {Object} An optional object whose properties will be set on |
---|
70 | * the handler. |
---|
71 | */ |
---|
72 | initialize: function(control, callbacks, options) { |
---|
73 | OpenLayers.Handler.prototype.initialize.apply(this, arguments); |
---|
74 | }, |
---|
75 | |
---|
76 | /** |
---|
77 | * Method: mousemove |
---|
78 | * Called when the mouse moves on the map. |
---|
79 | * |
---|
80 | * Parameters: |
---|
81 | * evt - {<OpenLayers.Event>} |
---|
82 | * |
---|
83 | * Returns: |
---|
84 | * {Boolean} Continue propagating this event. |
---|
85 | */ |
---|
86 | mousemove: function(evt) { |
---|
87 | if(this.passesTolerance(evt.xy)) { |
---|
88 | this.clearTimer(); |
---|
89 | this.callback('move', [evt]); |
---|
90 | this.px = evt.xy; |
---|
91 | // clone the evt so original properties can be accessed even |
---|
92 | // if the browser deletes them during the delay |
---|
93 | evt = OpenLayers.Util.extend({}, evt); |
---|
94 | this.timerId = window.setTimeout( |
---|
95 | OpenLayers.Function.bind(this.delayedCall, this, evt), |
---|
96 | this.delay |
---|
97 | ); |
---|
98 | } |
---|
99 | return !this.stopMove; |
---|
100 | }, |
---|
101 | |
---|
102 | /** |
---|
103 | * Method: mouseout |
---|
104 | * Called when the mouse goes out of the map. |
---|
105 | * |
---|
106 | * Parameters: |
---|
107 | * evt - {<OpenLayers.Event>} |
---|
108 | * |
---|
109 | * Returns: |
---|
110 | * {Boolean} Continue propagating this event. |
---|
111 | */ |
---|
112 | mouseout: function(evt) { |
---|
113 | if (OpenLayers.Util.mouseLeft(evt, this.map.div)) { |
---|
114 | this.clearTimer(); |
---|
115 | this.callback('move', [evt]); |
---|
116 | } |
---|
117 | return true; |
---|
118 | }, |
---|
119 | |
---|
120 | /** |
---|
121 | * Method: passesTolerance |
---|
122 | * Determine whether the mouse move is within the optional pixel tolerance. |
---|
123 | * |
---|
124 | * Parameters: |
---|
125 | * px - {<OpenLayers.Pixel>} |
---|
126 | * |
---|
127 | * Returns: |
---|
128 | * {Boolean} The mouse move is within the pixel tolerance. |
---|
129 | */ |
---|
130 | passesTolerance: function(px) { |
---|
131 | var passes = true; |
---|
132 | if(this.pixelTolerance && this.px) { |
---|
133 | var dpx = Math.sqrt( |
---|
134 | Math.pow(this.px.x - px.x, 2) + |
---|
135 | Math.pow(this.px.y - px.y, 2) |
---|
136 | ); |
---|
137 | if(dpx < this.pixelTolerance) { |
---|
138 | passes = false; |
---|
139 | } |
---|
140 | } |
---|
141 | return passes; |
---|
142 | }, |
---|
143 | |
---|
144 | /** |
---|
145 | * Method: clearTimer |
---|
146 | * Clear the timer and set <timerId> to null. |
---|
147 | */ |
---|
148 | clearTimer: function() { |
---|
149 | if(this.timerId != null) { |
---|
150 | window.clearTimeout(this.timerId); |
---|
151 | this.timerId = null; |
---|
152 | } |
---|
153 | }, |
---|
154 | |
---|
155 | /** |
---|
156 | * Method: delayedCall |
---|
157 | * Triggers pause callback. |
---|
158 | * |
---|
159 | * Parameters: |
---|
160 | * evt - {<OpenLayers.Event>} |
---|
161 | */ |
---|
162 | delayedCall: function(evt) { |
---|
163 | this.callback('pause', [evt]); |
---|
164 | }, |
---|
165 | |
---|
166 | /** |
---|
167 | * APIMethod: deactivate |
---|
168 | * Deactivate the handler. |
---|
169 | * |
---|
170 | * Returns: |
---|
171 | * {Boolean} The handler was successfully deactivated. |
---|
172 | */ |
---|
173 | deactivate: function() { |
---|
174 | var deactivated = false; |
---|
175 | if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { |
---|
176 | this.clearTimer(); |
---|
177 | deactivated = true; |
---|
178 | } |
---|
179 | return deactivated; |
---|
180 | }, |
---|
181 | |
---|
182 | CLASS_NAME: "OpenLayers.Handler.Hover" |
---|
183 | }); |
---|