~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Control/DragFeature.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

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