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