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
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the 2-clause BSD license.
* See license.txt in the OpenLayers distribution or repository for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
* @requires OpenLayers/Handler/Drag.js
* @requires OpenLayers/Handler/Feature.js
*/
/**
* Class: OpenLayers.Control.DragFeature
* The DragFeature control moves a feature with a drag of the mouse. Create a
* new control with the <OpenLayers.Control.DragFeature> constructor.
*
* Inherits From:
* - <OpenLayers.Control>
*/
OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
/**
* APIProperty: geometryTypes
* {Array(String)} To restrict dragging to a limited set of geometry types,
* send a list of strings corresponding to the geometry class names.
*/
geometryTypes: null,
/**
* APIProperty: onStart
* {Function} Define this function if you want to know when a drag starts.
* The function should expect to receive two arguments: the feature
* that is about to be dragged and the pixel location of the mouse.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that is about to be
* dragged.
* pixel - {<OpenLayers.Pixel>} The pixel location of the mouse.
*/
onStart: function(feature, pixel) {},
/**
* APIProperty: onDrag
* {Function} Define this function if you want to know about each move of a
* feature. The function should expect to receive two arguments: the
* feature that is being dragged and the pixel location of the mouse.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that was dragged.
* pixel - {<OpenLayers.Pixel>} The pixel location of the mouse.
*/
onDrag: function(feature, pixel) {},
/**
* APIProperty: onComplete
* {Function} Define this function if you want to know when a feature is
* done dragging. The function should expect to receive two arguments:
* the feature that is being dragged and the pixel location of the
* mouse.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that was dragged.
* pixel - {<OpenLayers.Pixel>} The pixel location of the mouse.
*/
onComplete: function(feature, pixel) {},
/**
* APIProperty: onEnter
* {Function} Define this function if you want to know when the mouse
* goes over a feature and thereby makes this feature a candidate
* for dragging.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that is ready
* to be dragged.
*/
onEnter: function(feature) {},
/**
* APIProperty: onLeave
* {Function} Define this function if you want to know when the mouse
* goes out of the feature that was dragged.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that was dragged.
*/
onLeave: function(feature) {},
/**
* APIProperty: documentDrag
* {Boolean} If set to true, mouse dragging will continue even if the
* mouse cursor leaves the map viewport. Default is false.
*/
documentDrag: false,
/**
* Property: layer
* {<OpenLayers.Layer.Vector>}
*/
layer: null,
/**
* Property: feature
* {<OpenLayers.Feature.Vector>}
*/
feature: null,
/**
* Property: dragCallbacks
* {Object} The functions that are sent to the drag handler for callback.
*/
dragCallbacks: {},
/**
* Property: featureCallbacks
* {Object} The functions that are sent to the feature handler for callback.
*/
featureCallbacks: {},
/**
* Property: lastPixel
* {<OpenLayers.Pixel>}
*/
lastPixel: null,
/**
* Constructor: OpenLayers.Control.DragFeature
* Create a new control to drag features.
*
* Parameters:
* layer - {<OpenLayers.Layer.Vector>} The layer containing features to be
* dragged.
* options - {Object} Optional object whose properties will be set on the
* control.
*/
initialize: function(layer, options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.layer = layer;
this.handlers = {
drag: new OpenLayers.Handler.Drag(
this, OpenLayers.Util.extend({
down: this.downFeature,
move: this.moveFeature,
up: this.upFeature,
out: this.cancel,
done: this.doneDragging
}, this.dragCallbacks), {
documentDrag: this.documentDrag
}
),
feature: new OpenLayers.Handler.Feature(
this, this.layer, OpenLayers.Util.extend({
// 'click' and 'clickout' callback are for the mobile
// support: no 'over' or 'out' in touch based browsers.
click: this.clickFeature,
clickout: this.clickoutFeature,
over: this.overFeature,
out: this.outFeature
}, this.featureCallbacks),
{geometryTypes: this.geometryTypes}
)
};
},
/**
* Method: clickFeature
* Called when the feature handler detects a click-in on a feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
*/
clickFeature: function(feature) {
if (this.handlers.feature.touch && !this.over && this.overFeature(feature)) {
this.handlers.drag.dragstart(this.handlers.feature.evt);
// to let the events propagate to the feature handler (click callback)
this.handlers.drag.stopDown = false;
}
},
/**
* Method: clickoutFeature
* Called when the feature handler detects a click-out on a feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
*/
clickoutFeature: function(feature) {
if (this.handlers.feature.touch && this.over) {
this.outFeature(feature);
this.handlers.drag.stopDown = true;
}
},
/**
* APIMethod: destroy
* Take care of things that are not handled in superclass
*/
destroy: function() {
this.layer = null;
OpenLayers.Control.prototype.destroy.apply(this, []);
},
/**
* APIMethod: activate
* Activate the control and the feature handler.
*
* Returns:
* {Boolean} Successfully activated the control and feature handler.
*/
activate: function() {
return (this.handlers.feature.activate() &&
OpenLayers.Control.prototype.activate.apply(this, arguments));
},
/**
* APIMethod: deactivate
* Deactivate the control and all handlers.
*
* Returns:
* {Boolean} Successfully deactivated the control.
*/
deactivate: function() {
// the return from the handlers is unimportant in this case
this.handlers.drag.deactivate();
this.handlers.feature.deactivate();
this.feature = null;
this.dragging = false;
this.lastPixel = null;
OpenLayers.Element.removeClass(
this.map.viewPortDiv, this.displayClass + "Over"
);
return OpenLayers.Control.prototype.deactivate.apply(this, arguments);
},
/**
* Method: overFeature
* Called when the feature handler detects a mouse-over on a feature.
* This activates the drag handler.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The selected feature.
*
* Returns:
* {Boolean} Successfully activated the drag handler.
*/
overFeature: function(feature) {
var activated = false;
if(!this.handlers.drag.dragging) {
this.feature = feature;
this.handlers.drag.activate();
activated = true;
this.over = true;
OpenLayers.Element.addClass(this.map.viewPortDiv, this.displayClass + "Over");
this.onEnter(feature);
} else {
if(this.feature.id == feature.id) {
this.over = true;
} else {
this.over = false;
}
}
return activated;
},
/**
* Method: downFeature
* Called when the drag handler detects a mouse-down.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} Location of the mouse event.
*/
downFeature: function(pixel) {
this.lastPixel = pixel;
this.onStart(this.feature, pixel);
},
/**
* Method: moveFeature
* Called when the drag handler detects a mouse-move. Also calls the
* optional onDrag method.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} Location of the mouse event.
*/
moveFeature: function(pixel) {
var res = this.map.getResolution();
this.feature.geometry.move(res * (pixel.x - this.lastPixel.x),
res * (this.lastPixel.y - pixel.y));
this.layer.drawFeature(this.feature);
this.lastPixel = pixel;
this.onDrag(this.feature, pixel);
},
/**
* Method: upFeature
* Called when the drag handler detects a mouse-up.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} Location of the mouse event.
*/
upFeature: function(pixel) {
if(!this.over) {
this.handlers.drag.deactivate();
}
},
/**
* Method: doneDragging
* Called when the drag handler is done dragging.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The last event pixel location. If this event
* came from a mouseout, this may not be in the map viewport.
*/
doneDragging: function(pixel) {
this.onComplete(this.feature, pixel);
},
/**
* Method: outFeature
* Called when the feature handler detects a mouse-out on a feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>} The feature that the mouse left.
*/
outFeature: function(feature) {
if(!this.handlers.drag.dragging) {
this.over = false;
this.handlers.drag.deactivate();
OpenLayers.Element.removeClass(
this.map.viewPortDiv, this.displayClass + "Over"
);
this.onLeave(feature);
this.feature = null;
} else {
if(this.feature.id == feature.id) {
this.over = false;
}
}
},
/**
* Method: cancel
* Called when the drag handler detects a mouse-out (from the map viewport).
*/
cancel: function() {
this.handlers.drag.deactivate();
this.over = false;
},
/**
* Method: setMap
* Set the map property for the control and all handlers.
*
* Parameters:
* map - {<OpenLayers.Map>} The control's map.
*/
setMap: function(map) {
this.handlers.drag.setMap(map);
this.handlers.feature.setMap(map);
OpenLayers.Control.prototype.setMap.apply(this, arguments);
},
CLASS_NAME: "OpenLayers.Control.DragFeature"
});
|