1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3
* full text of the license. */
7
* @requires OpenLayers/Control.js
8
* @requires OpenLayers/Handler/Drag.js
9
* @requires OpenLayers/Handler/Feature.js
13
* Class: OpenLayers.Control.DragFeature
14
* Move a feature with a drag. Create a new control with the
15
* <OpenLayers.Control.DragFeature> constructor.
18
* - <OpenLayers.Control>
20
OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, {
23
* APIProperty: geometryTypes
24
* {Array(String)} To restrict dragging to a limited set of geometry types,
25
* send a list of strings corresponding to the geometry class names.
30
* APIProperty: onStart
31
* {Function} Define this function if you want to know when a drag starts.
32
* The function should expect to receive two arguments: the feature
33
* that is about to be dragged and the pixel location of the mouse.
36
* feature - {<OpenLayers.Feature.Vector>} The feature that is about to be
38
* pixel - {<OpenLayers.Pixel>} The pixel location of the mouse.
40
onStart: function(feature, pixel) {},
44
* {Function} Define this function if you want to know about each move of a
45
* feature. The function should expect to receive two arguments: the
46
* feature that is being dragged and the pixel location of the mouse.
49
* feature - {<OpenLayers.Feature.Vector>} The feature that was dragged.
50
* pixel - {<OpenLayers.Pixel>} The pixel location of the mouse.
52
onDrag: function(feature, pixel) {},
55
* APIProperty: onComplete
56
* {Function} Define this function if you want to know when a feature is
57
* done dragging. The function should expect to receive two arguments:
58
* the feature that is being dragged and the pixel location of the
62
* feature - {<OpenLayers.Feature.Vector>} The feature that was dragged.
63
* pixel - {<OpenLayers.Pixel>} The pixel location of the mouse.
65
onComplete: function(feature, pixel) {},
69
* {<OpenLayers.Layer.Vector>}
75
* {<OpenLayers.Feature.Vector>}
80
* Property: dragCallbacks
81
* {Object} The functions that are sent to the drag handler for callback.
86
* Property: featureCallbacks
87
* {Object} The functions that are sent to the feature handler for callback.
93
* {<OpenLayers.Pixel>}
98
* Constructor: OpenLayers.Control.DragFeature
99
* Create a new control to drag features.
102
* layer - {<OpenLayers.Layer.Vector>} The layer containing features to be
104
* options - {Object} Optional object whose properties will be set on the
107
initialize: function(layer, options) {
108
OpenLayers.Control.prototype.initialize.apply(this, [options]);
111
drag: new OpenLayers.Handler.Drag(
112
this, OpenLayers.Util.extend({
113
down: this.downFeature,
114
move: this.moveFeature,
117
done: this.doneDragging
118
}, this.dragCallbacks)
120
feature: new OpenLayers.Handler.Feature(
121
this, this.layer, OpenLayers.Util.extend({
122
over: this.overFeature,
124
}, this.featureCallbacks),
125
{geometryTypes: this.geometryTypes}
132
* Take care of things that are not handled in superclass
134
destroy: function() {
136
OpenLayers.Control.prototype.destroy.apply(this, []);
140
* APIMethod: activate
141
* Activate the control and the feature handler.
144
* {Boolean} Successfully activated the control and feature handler.
146
activate: function() {
147
return (this.handlers.feature.activate() &&
148
OpenLayers.Control.prototype.activate.apply(this, arguments));
152
* APIMethod: deactivate
153
* Deactivate the control and all handlers.
156
* {Boolean} Successfully deactivated the control.
158
deactivate: function() {
159
// the return from the handlers is unimportant in this case
160
this.handlers.drag.deactivate();
161
this.handlers.feature.deactivate();
163
this.dragging = false;
164
this.lastPixel = null;
165
return OpenLayers.Control.prototype.deactivate.apply(this, arguments);
169
* Method: overFeature
170
* Called when the feature handler detects a mouse-over on a feature.
171
* This activates the drag handler.
174
* feature - {<OpenLayers.Feature.Vector>} The selected feature.
176
overFeature: function(feature) {
177
if(!this.handlers.drag.dragging) {
178
this.feature = feature;
179
this.handlers.drag.activate();
181
// TBD replace with CSS classes
182
this.map.div.style.cursor = "move";
184
if(this.feature.id == feature.id) {
193
* Method: downFeature
194
* Called when the drag handler detects a mouse-down.
197
* pixel - {<OpenLayers.Pixel>} Location of the mouse event.
199
downFeature: function(pixel) {
200
this.lastPixel = pixel;
201
this.onStart(this.feature, pixel);
205
* Method: moveFeature
206
* Called when the drag handler detects a mouse-move. Also calls the
207
* optional onDrag method.
210
* pixel - {<OpenLayers.Pixel>} Location of the mouse event.
212
moveFeature: function(pixel) {
213
var res = this.map.getResolution();
214
this.feature.geometry.move(res * (pixel.x - this.lastPixel.x),
215
res * (this.lastPixel.y - pixel.y));
216
this.layer.drawFeature(this.feature);
217
this.lastPixel = pixel;
218
this.onDrag(this.feature, pixel);
223
* Called when the drag handler detects a mouse-up. Also calls the
224
* optional onComplete method.
227
* pixel - {<OpenLayers.Pixel>} Location of the mouse event.
229
upFeature: function(pixel) {
231
this.handlers.drag.deactivate();
233
// TBD replace with CSS classes
234
this.map.div.style.cursor = "default";
236
// the drag handler itself resetted the cursor, so
237
// set it back to "move" here
238
this.map.div.style.cursor = "move";
243
* Method: doneDragging
244
* Called when the drag handler is done dragging.
247
* pixel - {<OpenLayers.Pixel>} The last event pixel location. If this event
248
* came from a mouseout, this may not be in the map viewport.
250
doneDragging: function(pixel) {
251
this.onComplete(this.feature, pixel);
256
* Called when the feature handler detects a mouse-out on a feature.
259
* feature - {<OpenLayers.Feature.Vector>} The feature that the mouse left.
261
outFeature: function(feature) {
262
if(!this.handlers.drag.dragging) {
264
this.handlers.drag.deactivate();
265
// TBD replace with CSS classes
266
this.map.div.style.cursor = "default";
269
if(this.feature.id == feature.id) {
277
* Called when the drag handler detects a mouse-out (from the map viewport).
280
this.handlers.drag.deactivate();
286
* Set the map property for the control and all handlers.
289
* map - {<OpenLayers.Map>} The control's map.
291
setMap: function(map) {
292
this.handlers.drag.setMap(map);
293
this.handlers.feature.setMap(map);
294
OpenLayers.Control.prototype.setMap.apply(this, arguments);
297
CLASS_NAME: "OpenLayers.Control.DragFeature"