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 * The DragFeature control moves a feature with a drag of the mouse. Create a
15 * new control with the <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 OpenLayers.Element.removeClass(
166 this.map.viewPortDiv, this.displayClass + "Over"
168 return OpenLayers.Control.prototype.deactivate.apply(this, arguments);
172 * Method: overFeature
173 * Called when the feature handler detects a mouse-over on a feature.
174 * This activates the drag handler.
177 * feature - {<OpenLayers.Feature.Vector>} The selected feature.
179 overFeature: function(feature) {
180 if(!this.handlers.drag.dragging) {
181 this.feature = feature;
182 this.handlers.drag.activate();
184 OpenLayers.Element.addClass(this.map.viewPortDiv, this.displayClass + "Over");
186 if(this.feature.id == feature.id) {
195 * Method: downFeature
196 * Called when the drag handler detects a mouse-down.
199 * pixel - {<OpenLayers.Pixel>} Location of the mouse event.
201 downFeature: function(pixel) {
202 this.lastPixel = pixel;
203 this.onStart(this.feature, pixel);
207 * Method: moveFeature
208 * Called when the drag handler detects a mouse-move. Also calls the
209 * optional onDrag method.
212 * pixel - {<OpenLayers.Pixel>} Location of the mouse event.
214 moveFeature: function(pixel) {
215 var res = this.map.getResolution();
216 this.feature.geometry.move(res * (pixel.x - this.lastPixel.x),
217 res * (this.lastPixel.y - pixel.y));
218 this.layer.drawFeature(this.feature);
219 this.lastPixel = pixel;
220 this.onDrag(this.feature, pixel);
225 * Called when the drag handler detects a mouse-up.
228 * pixel - {<OpenLayers.Pixel>} Location of the mouse event.
230 upFeature: function(pixel) {
232 this.handlers.drag.deactivate();
237 * Method: doneDragging
238 * Called when the drag handler is done dragging.
241 * pixel - {<OpenLayers.Pixel>} The last event pixel location. If this event
242 * came from a mouseout, this may not be in the map viewport.
244 doneDragging: function(pixel) {
245 this.onComplete(this.feature, pixel);
250 * Called when the feature handler detects a mouse-out on a feature.
253 * feature - {<OpenLayers.Feature.Vector>} The feature that the mouse left.
255 outFeature: function(feature) {
256 if(!this.handlers.drag.dragging) {
258 this.handlers.drag.deactivate();
259 OpenLayers.Element.removeClass(
260 this.map.viewPortDiv, this.displayClass + "Over"
264 if(this.feature.id == feature.id) {
272 * Called when the drag handler detects a mouse-out (from the map viewport).
275 this.handlers.drag.deactivate();
281 * Set the map property for the control and all handlers.
284 * map - {<OpenLayers.Map>} The control's map.
286 setMap: function(map) {
287 this.handlers.drag.setMap(map);
288 this.handlers.feature.setMap(map);
289 OpenLayers.Control.prototype.setMap.apply(this, arguments);
292 CLASS_NAME: "OpenLayers.Control.DragFeature"