Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository 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: onEnter
70
     * {Function} Define this function if you want to know when the mouse
71
     *     goes over a feature and thereby makes this feature a candidate
72
     *     for dragging.
73
     *
74
     * Parameters:
75
     * feature - {<OpenLayers.Feature.Vector>} The feature that is ready
76
     *     to be dragged.
77
     */
78
    onEnter: function(feature) {},
79

    
80
    /**
81
     * APIProperty: onLeave
82
     * {Function} Define this function if you want to know when the mouse
83
     *     goes out of the feature that was dragged.
84
     *
85
     * Parameters:
86
     * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged.
87
     */
88
    onLeave: function(feature) {},
89

    
90
    /**
91
     * APIProperty: documentDrag
92
     * {Boolean} If set to true, mouse dragging will continue even if the
93
     *     mouse cursor leaves the map viewport. Default is false.
94
     */
95
    documentDrag: false,
96
    
97
    /**
98
     * Property: layer
99
     * {<OpenLayers.Layer.Vector>}
100
     */
101
    layer: null,
102
    
103
    /**
104
     * Property: feature
105
     * {<OpenLayers.Feature.Vector>}
106
     */
107
    feature: null,
108

    
109
    /**
110
     * Property: dragCallbacks
111
     * {Object} The functions that are sent to the drag handler for callback.
112
     */
113
    dragCallbacks: {},
114

    
115
    /**
116
     * Property: featureCallbacks
117
     * {Object} The functions that are sent to the feature handler for callback.
118
     */
119
    featureCallbacks: {},
120
    
121
    /**
122
     * Property: lastPixel
123
     * {<OpenLayers.Pixel>}
124
     */
125
    lastPixel: null,
126

    
127
    /**
128
     * Constructor: OpenLayers.Control.DragFeature
129
     * Create a new control to drag features.
130
     *
131
     * Parameters:
132
     * layer - {<OpenLayers.Layer.Vector>} The layer containing features to be
133
     *     dragged.
134
     * options - {Object} Optional object whose properties will be set on the
135
     *     control.
136
     */
137
    initialize: function(layer, options) {
138
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
139
        this.layer = layer;
140
        this.handlers = {
141
            drag: new OpenLayers.Handler.Drag(
142
                this, OpenLayers.Util.extend({
143
                    down: this.downFeature,
144
                    move: this.moveFeature,
145
                    up: this.upFeature,
146
                    out: this.cancel,
147
                    done: this.doneDragging
148
                }, this.dragCallbacks), {
149
                    documentDrag: this.documentDrag
150
                }
151
            ),
152
            feature: new OpenLayers.Handler.Feature(
153
                this, this.layer, OpenLayers.Util.extend({
154
                    // 'click' and 'clickout' callback are for the mobile
155
                    // support: no 'over' or 'out' in touch based browsers.
156
                    click: this.clickFeature,
157
                    clickout: this.clickoutFeature,
158
                    over: this.overFeature,
159
                    out: this.outFeature
160
                }, this.featureCallbacks),
161
                {geometryTypes: this.geometryTypes}
162
            )
163
        };
164
    },
165

    
166
    /**
167
     * Method: clickFeature
168
     * Called when the feature handler detects a click-in on a feature.
169
     *
170
     * Parameters:
171
     * feature - {<OpenLayers.Feature.Vector>}
172
     */
173
    clickFeature: function(feature) {
174
        if (this.handlers.feature.touch && !this.over && this.overFeature(feature)) {
175
            this.handlers.drag.dragstart(this.handlers.feature.evt);
176
            // to let the events propagate to the feature handler (click callback)
177
            this.handlers.drag.stopDown = false;
178
        }
179
    },
180

    
181
    /**
182
     * Method: clickoutFeature
183
     * Called when the feature handler detects a click-out on a feature.
184
     *
185
     * Parameters:
186
     * feature - {<OpenLayers.Feature.Vector>}
187
     */
188
    clickoutFeature: function(feature) {
189
        if (this.handlers.feature.touch && this.over) {
190
            this.outFeature(feature);
191
            this.handlers.drag.stopDown = true;
192
        }
193
    },
194

    
195
    /**
196
     * APIMethod: destroy
197
     * Take care of things that are not handled in superclass
198
     */
199
    destroy: function() {
200
        this.layer = null;
201
        OpenLayers.Control.prototype.destroy.apply(this, []);
202
    },
203

    
204
    /**
205
     * APIMethod: activate
206
     * Activate the control and the feature handler.
207
     * 
208
     * Returns:
209
     * {Boolean} Successfully activated the control and feature handler.
210
     */
211
    activate: function() {
212
        return (this.handlers.feature.activate() &&
213
                OpenLayers.Control.prototype.activate.apply(this, arguments));
214
    },
215

    
216
    /**
217
     * APIMethod: deactivate
218
     * Deactivate the control and all handlers.
219
     * 
220
     * Returns:
221
     * {Boolean} Successfully deactivated the control.
222
     */
223
    deactivate: function() {
224
        // the return from the handlers is unimportant in this case
225
        this.handlers.drag.deactivate();
226
        this.handlers.feature.deactivate();
227
        this.feature = null;
228
        this.dragging = false;
229
        this.lastPixel = null;
230
        OpenLayers.Element.removeClass(
231
            this.map.viewPortDiv, this.displayClass + "Over"
232
        );
233
        return OpenLayers.Control.prototype.deactivate.apply(this, arguments);
234
    },
235

    
236
    /**
237
     * Method: overFeature
238
     * Called when the feature handler detects a mouse-over on a feature.
239
     *     This activates the drag handler.
240
     *
241
     * Parameters:
242
     * feature - {<OpenLayers.Feature.Vector>} The selected feature.
243
     *
244
     * Returns:
245
     * {Boolean} Successfully activated the drag handler.
246
     */
247
    overFeature: function(feature) {
248
        var activated = false;
249
        if(!this.handlers.drag.dragging) {
250
            this.feature = feature;
251
            this.handlers.drag.activate();
252
            activated = true;
253
            this.over = true;
254
            OpenLayers.Element.addClass(this.map.viewPortDiv, this.displayClass + "Over");
255
            this.onEnter(feature);
256
        } else {
257
            if(this.feature.id == feature.id) {
258
                this.over = true;
259
            } else {
260
                this.over = false;
261
            }
262
        }
263
        return activated;
264
    },
265

    
266
    /**
267
     * Method: downFeature
268
     * Called when the drag handler detects a mouse-down.
269
     *
270
     * Parameters:
271
     * pixel - {<OpenLayers.Pixel>} Location of the mouse event.
272
     */
273
    downFeature: function(pixel) {
274
        this.lastPixel = pixel;
275
        this.onStart(this.feature, pixel);
276
    },
277

    
278
    /**
279
     * Method: moveFeature
280
     * Called when the drag handler detects a mouse-move.  Also calls the
281
     *     optional onDrag method.
282
     * 
283
     * Parameters:
284
     * pixel - {<OpenLayers.Pixel>} Location of the mouse event.
285
     */
286
    moveFeature: function(pixel) {
287
        var res = this.map.getResolution();
288
        this.feature.geometry.move(res * (pixel.x - this.lastPixel.x),
289
                                   res * (this.lastPixel.y - pixel.y));
290
        this.layer.drawFeature(this.feature);
291
        this.lastPixel = pixel;
292
        this.onDrag(this.feature, pixel);
293
    },
294

    
295
    /**
296
     * Method: upFeature
297
     * Called when the drag handler detects a mouse-up.
298
     * 
299
     * Parameters:
300
     * pixel - {<OpenLayers.Pixel>} Location of the mouse event.
301
     */
302
    upFeature: function(pixel) {
303
        if(!this.over) {
304
            this.handlers.drag.deactivate();
305
        }
306
    },
307

    
308
    /**
309
     * Method: doneDragging
310
     * Called when the drag handler is done dragging.
311
     *
312
     * Parameters:
313
     * pixel - {<OpenLayers.Pixel>} The last event pixel location.  If this event
314
     *     came from a mouseout, this may not be in the map viewport.
315
     */
316
    doneDragging: function(pixel) {
317
        this.onComplete(this.feature, pixel);
318
    },
319

    
320
    /**
321
     * Method: outFeature
322
     * Called when the feature handler detects a mouse-out on a feature.
323
     *
324
     * Parameters:
325
     * feature - {<OpenLayers.Feature.Vector>} The feature that the mouse left.
326
     */
327
    outFeature: function(feature) {
328
        if(!this.handlers.drag.dragging) {
329
            this.over = false;
330
            this.handlers.drag.deactivate();
331
            OpenLayers.Element.removeClass(
332
                this.map.viewPortDiv, this.displayClass + "Over"
333
            );
334
            this.onLeave(feature);
335
            this.feature = null;
336
        } else {
337
            if(this.feature.id == feature.id) {
338
                this.over = false;
339
            }
340
        }
341
    },
342
        
343
    /**
344
     * Method: cancel
345
     * Called when the drag handler detects a mouse-out (from the map viewport).
346
     */
347
    cancel: function() {
348
        this.handlers.drag.deactivate();
349
        this.over = false;
350
    },
351

    
352
    /**
353
     * Method: setMap
354
     * Set the map property for the control and all handlers.
355
     *
356
     * Parameters: 
357
     * map - {<OpenLayers.Map>} The control's map.
358
     */
359
    setMap: function(map) {
360
        this.handlers.drag.setMap(map);
361
        this.handlers.feature.setMap(map);
362
        OpenLayers.Control.prototype.setMap.apply(this, arguments);
363
    },
364

    
365
    CLASS_NAME: "OpenLayers.Control.DragFeature"
366
});
(6-6/45)