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 | /** |
---|
8 | * @requires OpenLayers/Handler.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Handler.Feature |
---|
13 | * Handler to respond to mouse events related to a drawn feature. Callbacks |
---|
14 | * with the following keys will be notified of the following events |
---|
15 | * associated with features: click, clickout, over, out, and dblclick. |
---|
16 | * |
---|
17 | * This handler stops event propagation for mousedown and mouseup if those |
---|
18 | * browser events target features that can be selected. |
---|
19 | */ |
---|
20 | OpenLayers.Handler.Feature = OpenLayers.Class(OpenLayers.Handler, { |
---|
21 | |
---|
22 | /** |
---|
23 | * Property: EVENTMAP |
---|
24 | * {Object} A object mapping the browser events to objects with callback |
---|
25 | * keys for in and out. |
---|
26 | */ |
---|
27 | EVENTMAP: { |
---|
28 | 'click': {'in': 'click', 'out': 'clickout'}, |
---|
29 | 'mousemove': {'in': 'over', 'out': 'out'}, |
---|
30 | 'dblclick': {'in': 'dblclick', 'out': null}, |
---|
31 | 'mousedown': {'in': null, 'out': null}, |
---|
32 | 'mouseup': {'in': null, 'out': null} |
---|
33 | }, |
---|
34 | |
---|
35 | /** |
---|
36 | * Property: feature |
---|
37 | * {<OpenLayers.Feature.Vector>} The last feature that was hovered. |
---|
38 | */ |
---|
39 | feature: null, |
---|
40 | |
---|
41 | /** |
---|
42 | * Property: lastFeature |
---|
43 | * {<OpenLayers.Feature.Vector>} The last feature that was handled. |
---|
44 | */ |
---|
45 | lastFeature: null, |
---|
46 | |
---|
47 | /** |
---|
48 | * Property: down |
---|
49 | * {<OpenLayers.Pixel>} The location of the last mousedown. |
---|
50 | */ |
---|
51 | down: null, |
---|
52 | |
---|
53 | /** |
---|
54 | * Property: up |
---|
55 | * {<OpenLayers.Pixel>} The location of the last mouseup. |
---|
56 | */ |
---|
57 | up: null, |
---|
58 | |
---|
59 | /** |
---|
60 | * Property: clickTolerance |
---|
61 | * {Number} The number of pixels the mouse can move between mousedown |
---|
62 | * and mouseup for the event to still be considered a click. |
---|
63 | * Dragging the map should not trigger the click and clickout callbacks |
---|
64 | * unless the map is moved by less than this tolerance. Defaults to 4. |
---|
65 | */ |
---|
66 | clickTolerance: 4, |
---|
67 | |
---|
68 | /** |
---|
69 | * Property: geometryTypes |
---|
70 | * To restrict dragging to a limited set of geometry types, send a list |
---|
71 | * of strings corresponding to the geometry class names. |
---|
72 | * |
---|
73 | * @type Array(String) |
---|
74 | */ |
---|
75 | geometryTypes: null, |
---|
76 | |
---|
77 | /** |
---|
78 | * Property: stopClick |
---|
79 | * {Boolean} If stopClick is set to true, handled clicks do not |
---|
80 | * propagate to other click listeners. Otherwise, handled clicks |
---|
81 | * do propagate. Unhandled clicks always propagate, whatever the |
---|
82 | * value of stopClick. Defaults to true. |
---|
83 | */ |
---|
84 | stopClick: true, |
---|
85 | |
---|
86 | /** |
---|
87 | * Property: stopDown |
---|
88 | * {Boolean} If stopDown is set to true, handled mousedowns do not |
---|
89 | * propagate to other mousedown listeners. Otherwise, handled |
---|
90 | * mousedowns do propagate. Unhandled mousedowns always propagate, |
---|
91 | * whatever the value of stopDown. Defaults to true. |
---|
92 | */ |
---|
93 | stopDown: true, |
---|
94 | |
---|
95 | /** |
---|
96 | * Property: stopUp |
---|
97 | * {Boolean} If stopUp is set to true, handled mouseups do not |
---|
98 | * propagate to other mouseup listeners. Otherwise, handled mouseups |
---|
99 | * do propagate. Unhandled mouseups always propagate, whatever the |
---|
100 | * value of stopUp. Defaults to false. |
---|
101 | */ |
---|
102 | stopUp: false, |
---|
103 | |
---|
104 | /** |
---|
105 | * Constructor: OpenLayers.Handler.Feature |
---|
106 | * |
---|
107 | * Parameters: |
---|
108 | * control - {<OpenLayers.Control>} |
---|
109 | * layer - {<OpenLayers.Layer.Vector>} |
---|
110 | * callbacks - {Object} An object with a 'over' property whos value is |
---|
111 | * a function to be called when the mouse is over a feature. The |
---|
112 | * callback should expect to recieve a single argument, the feature. |
---|
113 | * options - {Object} |
---|
114 | */ |
---|
115 | initialize: function(control, layer, callbacks, options) { |
---|
116 | OpenLayers.Handler.prototype.initialize.apply(this, [control, callbacks, options]); |
---|
117 | this.layer = layer; |
---|
118 | }, |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | * Method: mousedown |
---|
123 | * Handle mouse down. Stop propagation if a feature is targeted by this |
---|
124 | * event (stops map dragging during feature selection). |
---|
125 | * |
---|
126 | * Parameters: |
---|
127 | * evt - {Event} |
---|
128 | */ |
---|
129 | mousedown: function(evt) { |
---|
130 | this.down = evt.xy; |
---|
131 | return this.handle(evt) ? !this.stopDown : true; |
---|
132 | }, |
---|
133 | |
---|
134 | /** |
---|
135 | * Method: mouseup |
---|
136 | * Handle mouse up. Stop propagation if a feature is targeted by this |
---|
137 | * event. |
---|
138 | * |
---|
139 | * Parameters: |
---|
140 | * evt - {Event} |
---|
141 | */ |
---|
142 | mouseup: function(evt) { |
---|
143 | this.up = evt.xy; |
---|
144 | return this.handle(evt) ? !this.stopUp : true; |
---|
145 | }, |
---|
146 | |
---|
147 | /** |
---|
148 | * Method: click |
---|
149 | * Handle click. Call the "click" callback if click on a feature, |
---|
150 | * or the "clickout" callback if click outside any feature. |
---|
151 | * |
---|
152 | * Parameters: |
---|
153 | * evt - {Event} |
---|
154 | * |
---|
155 | * Returns: |
---|
156 | * {Boolean} |
---|
157 | */ |
---|
158 | click: function(evt) { |
---|
159 | return this.handle(evt) ? !this.stopClick : true; |
---|
160 | }, |
---|
161 | |
---|
162 | /** |
---|
163 | * Method: mousemove |
---|
164 | * Handle mouse moves. Call the "over" callback if moving in to a feature, |
---|
165 | * or the "out" callback if moving out of a feature. |
---|
166 | * |
---|
167 | * Parameters: |
---|
168 | * evt - {Event} |
---|
169 | * |
---|
170 | * Returns: |
---|
171 | * {Boolean} |
---|
172 | */ |
---|
173 | mousemove: function(evt) { |
---|
174 | if (!this.callbacks['over'] && !this.callbacks['out']) { |
---|
175 | return true; |
---|
176 | } |
---|
177 | this.handle(evt); |
---|
178 | return true; |
---|
179 | }, |
---|
180 | |
---|
181 | /** |
---|
182 | * Method: dblclick |
---|
183 | * Handle dblclick. Call the "dblclick" callback if dblclick on a feature. |
---|
184 | * |
---|
185 | * Parameters: |
---|
186 | * evt - {Event} |
---|
187 | * |
---|
188 | * Returns: |
---|
189 | * {Boolean} |
---|
190 | */ |
---|
191 | dblclick: function(evt) { |
---|
192 | return !this.handle(evt); |
---|
193 | }, |
---|
194 | |
---|
195 | /** |
---|
196 | * Method: geometryTypeMatches |
---|
197 | * Return true if the geometry type of the passed feature matches |
---|
198 | * one of the geometry types in the geometryTypes array. |
---|
199 | * |
---|
200 | * Parameters: |
---|
201 | * feature - {<OpenLayers.Vector.Feature>} |
---|
202 | * |
---|
203 | * Returns: |
---|
204 | * {Boolean} |
---|
205 | */ |
---|
206 | geometryTypeMatches: function(feature) { |
---|
207 | return this.geometryTypes == null || |
---|
208 | OpenLayers.Util.indexOf(this.geometryTypes, |
---|
209 | feature.geometry.CLASS_NAME) > -1; |
---|
210 | }, |
---|
211 | |
---|
212 | /** |
---|
213 | * Method: handle |
---|
214 | * |
---|
215 | * Parameters: |
---|
216 | * evt - {Event} |
---|
217 | * |
---|
218 | * Returns: |
---|
219 | * {Boolean} The event occurred over a relevant feature. |
---|
220 | */ |
---|
221 | handle: function(evt) { |
---|
222 | if(this.feature && !this.feature.layer) { |
---|
223 | // feature has been destroyed |
---|
224 | this.feature = null; |
---|
225 | } |
---|
226 | var type = evt.type; |
---|
227 | var handled = false; |
---|
228 | var previouslyIn = !!(this.feature); // previously in a feature |
---|
229 | var click = (type == "click" || type == "dblclick"); |
---|
230 | this.feature = this.layer.getFeatureFromEvent(evt); |
---|
231 | if(this.feature && !this.feature.layer) { |
---|
232 | // feature has been destroyed |
---|
233 | this.feature = null; |
---|
234 | } |
---|
235 | if(this.lastFeature && !this.lastFeature.layer) { |
---|
236 | // last feature has been destroyed |
---|
237 | this.lastFeature = null; |
---|
238 | } |
---|
239 | if(this.feature) { |
---|
240 | var inNew = (this.feature != this.lastFeature); |
---|
241 | if(this.geometryTypeMatches(this.feature)) { |
---|
242 | // in to a feature |
---|
243 | if(previouslyIn && inNew) { |
---|
244 | // out of last feature and in to another |
---|
245 | if(this.lastFeature) { |
---|
246 | this.triggerCallback(type, 'out', [this.lastFeature]); |
---|
247 | } |
---|
248 | this.triggerCallback(type, 'in', [this.feature]); |
---|
249 | } else if(!previouslyIn || click) { |
---|
250 | // in feature for the first time |
---|
251 | this.triggerCallback(type, 'in', [this.feature]); |
---|
252 | } |
---|
253 | this.lastFeature = this.feature; |
---|
254 | handled = true; |
---|
255 | } else { |
---|
256 | // not in to a feature |
---|
257 | if(this.lastFeature && (previouslyIn && inNew || click)) { |
---|
258 | // out of last feature for the first time |
---|
259 | this.triggerCallback(type, 'out', [this.lastFeature]); |
---|
260 | } |
---|
261 | // next time the mouse goes in a feature whose geometry type |
---|
262 | // doesn't match we don't want to call the 'out' callback |
---|
263 | // again, so let's set this.feature to null so that |
---|
264 | // previouslyIn will evaluate to false the next time |
---|
265 | // we enter handle. Yes, a bit hackish... |
---|
266 | this.feature = null; |
---|
267 | } |
---|
268 | } else { |
---|
269 | if(this.lastFeature && (previouslyIn || click)) { |
---|
270 | this.triggerCallback(type, 'out', [this.lastFeature]); |
---|
271 | } |
---|
272 | } |
---|
273 | return handled; |
---|
274 | }, |
---|
275 | |
---|
276 | /** |
---|
277 | * Method: triggerCallback |
---|
278 | * Call the callback keyed in the event map with the supplied arguments. |
---|
279 | * For click and clickout, the <clickTolerance> is checked first. |
---|
280 | * |
---|
281 | * Parameters: |
---|
282 | * type - {String} |
---|
283 | */ |
---|
284 | triggerCallback: function(type, mode, args) { |
---|
285 | var key = this.EVENTMAP[type][mode]; |
---|
286 | if(key) { |
---|
287 | if(type == 'click' && this.up && this.down) { |
---|
288 | // for click/clickout, only trigger callback if tolerance is met |
---|
289 | var dpx = Math.sqrt( |
---|
290 | Math.pow(this.up.x - this.down.x, 2) + |
---|
291 | Math.pow(this.up.y - this.down.y, 2) |
---|
292 | ); |
---|
293 | if(dpx <= this.clickTolerance) { |
---|
294 | this.callback(key, args); |
---|
295 | } |
---|
296 | } else { |
---|
297 | this.callback(key, args); |
---|
298 | } |
---|
299 | } |
---|
300 | }, |
---|
301 | |
---|
302 | /** |
---|
303 | * Method: activate |
---|
304 | * Turn on the handler. Returns false if the handler was already active. |
---|
305 | * |
---|
306 | * Returns: |
---|
307 | * {Boolean} |
---|
308 | */ |
---|
309 | activate: function() { |
---|
310 | var activated = false; |
---|
311 | if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) { |
---|
312 | this.moveLayerToTop(); |
---|
313 | this.map.events.on({ |
---|
314 | "removelayer": this.handleMapEvents, |
---|
315 | "changelayer": this.handleMapEvents, |
---|
316 | scope: this |
---|
317 | }); |
---|
318 | activated = true; |
---|
319 | } |
---|
320 | return activated; |
---|
321 | }, |
---|
322 | |
---|
323 | /** |
---|
324 | * Method: deactivate |
---|
325 | * Turn off the handler. Returns false if the handler was already active. |
---|
326 | * |
---|
327 | * Returns: |
---|
328 | * {Boolean} |
---|
329 | */ |
---|
330 | deactivate: function() { |
---|
331 | var deactivated = false; |
---|
332 | if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { |
---|
333 | this.moveLayerBack(); |
---|
334 | this.feature = null; |
---|
335 | this.lastFeature = null; |
---|
336 | this.down = null; |
---|
337 | this.up = null; |
---|
338 | this.map.events.un({ |
---|
339 | "removelayer": this.handleMapEvents, |
---|
340 | "changelayer": this.handleMapEvents, |
---|
341 | scope: this |
---|
342 | }); |
---|
343 | deactivated = true; |
---|
344 | } |
---|
345 | return deactivated; |
---|
346 | }, |
---|
347 | |
---|
348 | /** |
---|
349 | * Method handleMapEvents |
---|
350 | * |
---|
351 | * Parameters: |
---|
352 | * evt - {Object} |
---|
353 | */ |
---|
354 | handleMapEvents: function(evt) { |
---|
355 | if (!evt.property || evt.property == "order") { |
---|
356 | this.moveLayerToTop(); |
---|
357 | } |
---|
358 | }, |
---|
359 | |
---|
360 | /** |
---|
361 | * Method: moveLayerToTop |
---|
362 | * Moves the layer for this handler to the top, so mouse events can reach |
---|
363 | * it. |
---|
364 | */ |
---|
365 | moveLayerToTop: function() { |
---|
366 | var index = Math.max(this.map.Z_INDEX_BASE['Feature'] - 1, |
---|
367 | this.layer.getZIndex()) + 1; |
---|
368 | this.layer.setZIndex(index); |
---|
369 | |
---|
370 | }, |
---|
371 | |
---|
372 | /** |
---|
373 | * Method: moveLayerBack |
---|
374 | * Moves the layer back to the position determined by the map's layers |
---|
375 | * array. |
---|
376 | */ |
---|
377 | moveLayerBack: function() { |
---|
378 | var index = this.layer.getZIndex() - 1; |
---|
379 | if (index >= this.map.Z_INDEX_BASE['Feature']) { |
---|
380 | this.layer.setZIndex(index); |
---|
381 | } else { |
---|
382 | this.map.setLayerZIndex(this.layer, |
---|
383 | this.map.getLayerIndex(this.layer)); |
---|
384 | } |
---|
385 | }, |
---|
386 | |
---|
387 | CLASS_NAME: "OpenLayers.Handler.Feature" |
---|
388 | }); |
---|