[76] | 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/Control.js |
---|
| 9 | * @requires OpenLayers/Handler/Drag.js |
---|
| 10 | * @requires OpenLayers/Handler/Feature.js |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Class: OpenLayers.Control.DragFeature |
---|
| 15 | * The DragFeature control moves a feature with a drag of the mouse. Create a |
---|
| 16 | * new control with the <OpenLayers.Control.DragFeature> constructor. |
---|
| 17 | * |
---|
| 18 | * Inherits From: |
---|
| 19 | * - <OpenLayers.Control> |
---|
| 20 | */ |
---|
| 21 | OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * APIProperty: geometryTypes |
---|
| 25 | * {Array(String)} To restrict dragging to a limited set of geometry types, |
---|
| 26 | * send a list of strings corresponding to the geometry class names. |
---|
| 27 | */ |
---|
| 28 | geometryTypes: null, |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * APIProperty: onStart |
---|
| 32 | * {Function} Define this function if you want to know when a drag starts. |
---|
| 33 | * The function should expect to receive two arguments: the feature |
---|
| 34 | * that is about to be dragged and the pixel location of the mouse. |
---|
| 35 | * |
---|
| 36 | * Parameters: |
---|
| 37 | * feature - {<OpenLayers.Feature.Vector>} The feature that is about to be |
---|
| 38 | * dragged. |
---|
| 39 | * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. |
---|
| 40 | */ |
---|
| 41 | onStart: function(feature, pixel) {}, |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * APIProperty: onDrag |
---|
| 45 | * {Function} Define this function if you want to know about each move of a |
---|
| 46 | * feature. The function should expect to receive two arguments: the |
---|
| 47 | * feature that is being dragged and the pixel location of the mouse. |
---|
| 48 | * |
---|
| 49 | * Parameters: |
---|
| 50 | * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged. |
---|
| 51 | * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. |
---|
| 52 | */ |
---|
| 53 | onDrag: function(feature, pixel) {}, |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * APIProperty: onComplete |
---|
| 57 | * {Function} Define this function if you want to know when a feature is |
---|
| 58 | * done dragging. The function should expect to receive two arguments: |
---|
| 59 | * the feature that is being dragged and the pixel location of the |
---|
| 60 | * mouse. |
---|
| 61 | * |
---|
| 62 | * Parameters: |
---|
| 63 | * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged. |
---|
| 64 | * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. |
---|
| 65 | */ |
---|
| 66 | onComplete: function(feature, pixel) {}, |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * APIProperty: documentDrag |
---|
| 70 | * {Boolean} If set to true, mouse dragging will continue even if the |
---|
| 71 | * mouse cursor leaves the map viewport. Default is false. |
---|
| 72 | */ |
---|
| 73 | documentDrag: false, |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Property: layer |
---|
| 77 | * {<OpenLayers.Layer.Vector>} |
---|
| 78 | */ |
---|
| 79 | layer: null, |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Property: feature |
---|
| 83 | * {<OpenLayers.Feature.Vector>} |
---|
| 84 | */ |
---|
| 85 | feature: null, |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Property: dragCallbacks |
---|
| 89 | * {Object} The functions that are sent to the drag handler for callback. |
---|
| 90 | */ |
---|
| 91 | dragCallbacks: {}, |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Property: featureCallbacks |
---|
| 95 | * {Object} The functions that are sent to the feature handler for callback. |
---|
| 96 | */ |
---|
| 97 | featureCallbacks: {}, |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Property: lastPixel |
---|
| 101 | * {<OpenLayers.Pixel>} |
---|
| 102 | */ |
---|
| 103 | lastPixel: null, |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Constructor: OpenLayers.Control.DragFeature |
---|
| 107 | * Create a new control to drag features. |
---|
| 108 | * |
---|
| 109 | * Parameters: |
---|
| 110 | * layer - {<OpenLayers.Layer.Vector>} The layer containing features to be |
---|
| 111 | * dragged. |
---|
| 112 | * options - {Object} Optional object whose properties will be set on the |
---|
| 113 | * control. |
---|
| 114 | */ |
---|
| 115 | initialize: function(layer, options) { |
---|
| 116 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
---|
| 117 | this.layer = layer; |
---|
| 118 | this.handlers = { |
---|
| 119 | drag: new OpenLayers.Handler.Drag( |
---|
| 120 | this, OpenLayers.Util.extend({ |
---|
| 121 | down: this.downFeature, |
---|
| 122 | move: this.moveFeature, |
---|
| 123 | up: this.upFeature, |
---|
| 124 | out: this.cancel, |
---|
| 125 | done: this.doneDragging |
---|
| 126 | }, this.dragCallbacks), { |
---|
| 127 | documentDrag: this.documentDrag |
---|
| 128 | } |
---|
| 129 | ), |
---|
| 130 | feature: new OpenLayers.Handler.Feature( |
---|
| 131 | this, this.layer, OpenLayers.Util.extend({ |
---|
| 132 | over: this.overFeature, |
---|
| 133 | out: this.outFeature |
---|
| 134 | }, this.featureCallbacks), |
---|
| 135 | {geometryTypes: this.geometryTypes} |
---|
| 136 | ) |
---|
| 137 | }; |
---|
| 138 | }, |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * APIMethod: destroy |
---|
| 142 | * Take care of things that are not handled in superclass |
---|
| 143 | */ |
---|
| 144 | destroy: function() { |
---|
| 145 | this.layer = null; |
---|
| 146 | OpenLayers.Control.prototype.destroy.apply(this, []); |
---|
| 147 | }, |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * APIMethod: activate |
---|
| 151 | * Activate the control and the feature handler. |
---|
| 152 | * |
---|
| 153 | * Returns: |
---|
| 154 | * {Boolean} Successfully activated the control and feature handler. |
---|
| 155 | */ |
---|
| 156 | activate: function() { |
---|
| 157 | return (this.handlers.feature.activate() && |
---|
| 158 | OpenLayers.Control.prototype.activate.apply(this, arguments)); |
---|
| 159 | }, |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * APIMethod: deactivate |
---|
| 163 | * Deactivate the control and all handlers. |
---|
| 164 | * |
---|
| 165 | * Returns: |
---|
| 166 | * {Boolean} Successfully deactivated the control. |
---|
| 167 | */ |
---|
| 168 | deactivate: function() { |
---|
| 169 | // the return from the handlers is unimportant in this case |
---|
| 170 | this.handlers.drag.deactivate(); |
---|
| 171 | this.handlers.feature.deactivate(); |
---|
| 172 | this.feature = null; |
---|
| 173 | this.dragging = false; |
---|
| 174 | this.lastPixel = null; |
---|
| 175 | OpenLayers.Element.removeClass( |
---|
| 176 | this.map.viewPortDiv, this.displayClass + "Over" |
---|
| 177 | ); |
---|
| 178 | return OpenLayers.Control.prototype.deactivate.apply(this, arguments); |
---|
| 179 | }, |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Method: overFeature |
---|
| 183 | * Called when the feature handler detects a mouse-over on a feature. |
---|
| 184 | * This activates the drag handler. |
---|
| 185 | * |
---|
| 186 | * Parameters: |
---|
| 187 | * feature - {<OpenLayers.Feature.Vector>} The selected feature. |
---|
| 188 | */ |
---|
| 189 | overFeature: function(feature) { |
---|
| 190 | if(!this.handlers.drag.dragging) { |
---|
| 191 | this.feature = feature; |
---|
| 192 | this.handlers.drag.activate(); |
---|
| 193 | this.over = true; |
---|
| 194 | OpenLayers.Element.addClass(this.map.viewPortDiv, this.displayClass + "Over"); |
---|
| 195 | } else { |
---|
| 196 | if(this.feature.id == feature.id) { |
---|
| 197 | this.over = true; |
---|
| 198 | } else { |
---|
| 199 | this.over = false; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | }, |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * Method: downFeature |
---|
| 206 | * Called when the drag handler detects a mouse-down. |
---|
| 207 | * |
---|
| 208 | * Parameters: |
---|
| 209 | * pixel - {<OpenLayers.Pixel>} Location of the mouse event. |
---|
| 210 | */ |
---|
| 211 | downFeature: function(pixel) { |
---|
| 212 | this.lastPixel = pixel; |
---|
| 213 | this.onStart(this.feature, pixel); |
---|
| 214 | }, |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | * Method: moveFeature |
---|
| 218 | * Called when the drag handler detects a mouse-move. Also calls the |
---|
| 219 | * optional onDrag method. |
---|
| 220 | * |
---|
| 221 | * Parameters: |
---|
| 222 | * pixel - {<OpenLayers.Pixel>} Location of the mouse event. |
---|
| 223 | */ |
---|
| 224 | moveFeature: function(pixel) { |
---|
| 225 | var res = this.map.getResolution(); |
---|
| 226 | this.feature.geometry.move(res * (pixel.x - this.lastPixel.x), |
---|
| 227 | res * (this.lastPixel.y - pixel.y)); |
---|
| 228 | this.layer.drawFeature(this.feature); |
---|
| 229 | this.lastPixel = pixel; |
---|
| 230 | this.onDrag(this.feature, pixel); |
---|
| 231 | }, |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * Method: upFeature |
---|
| 235 | * Called when the drag handler detects a mouse-up. |
---|
| 236 | * |
---|
| 237 | * Parameters: |
---|
| 238 | * pixel - {<OpenLayers.Pixel>} Location of the mouse event. |
---|
| 239 | */ |
---|
| 240 | upFeature: function(pixel) { |
---|
| 241 | if(!this.over) { |
---|
| 242 | this.handlers.drag.deactivate(); |
---|
| 243 | } |
---|
| 244 | }, |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | * Method: doneDragging |
---|
| 248 | * Called when the drag handler is done dragging. |
---|
| 249 | * |
---|
| 250 | * Parameters: |
---|
| 251 | * pixel - {<OpenLayers.Pixel>} The last event pixel location. If this event |
---|
| 252 | * came from a mouseout, this may not be in the map viewport. |
---|
| 253 | */ |
---|
| 254 | doneDragging: function(pixel) { |
---|
| 255 | this.onComplete(this.feature, pixel); |
---|
| 256 | }, |
---|
| 257 | |
---|
| 258 | /** |
---|
| 259 | * Method: outFeature |
---|
| 260 | * Called when the feature handler detects a mouse-out on a feature. |
---|
| 261 | * |
---|
| 262 | * Parameters: |
---|
| 263 | * feature - {<OpenLayers.Feature.Vector>} The feature that the mouse left. |
---|
| 264 | */ |
---|
| 265 | outFeature: function(feature) { |
---|
| 266 | if(!this.handlers.drag.dragging) { |
---|
| 267 | this.over = false; |
---|
| 268 | this.handlers.drag.deactivate(); |
---|
| 269 | OpenLayers.Element.removeClass( |
---|
| 270 | this.map.viewPortDiv, this.displayClass + "Over" |
---|
| 271 | ); |
---|
| 272 | this.feature = null; |
---|
| 273 | } else { |
---|
| 274 | if(this.feature.id == feature.id) { |
---|
| 275 | this.over = false; |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | }, |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Method: cancel |
---|
| 282 | * Called when the drag handler detects a mouse-out (from the map viewport). |
---|
| 283 | */ |
---|
| 284 | cancel: function() { |
---|
| 285 | this.handlers.drag.deactivate(); |
---|
| 286 | this.over = false; |
---|
| 287 | }, |
---|
| 288 | |
---|
| 289 | /** |
---|
| 290 | * Method: setMap |
---|
| 291 | * Set the map property for the control and all handlers. |
---|
| 292 | * |
---|
| 293 | * Parameters: |
---|
| 294 | * map - {<OpenLayers.Map>} The control's map. |
---|
| 295 | */ |
---|
| 296 | setMap: function(map) { |
---|
| 297 | this.handlers.drag.setMap(map); |
---|
| 298 | this.handlers.feature.setMap(map); |
---|
| 299 | OpenLayers.Control.prototype.setMap.apply(this, arguments); |
---|
| 300 | }, |
---|
| 301 | |
---|
| 302 | CLASS_NAME: "OpenLayers.Control.DragFeature" |
---|
| 303 | }); |
---|