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

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Handler/Path.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/Handler/Point.js
 
8
 * @requires OpenLayers/Geometry/Point.js
 
9
 * @requires OpenLayers/Geometry/LineString.js
 
10
 */
 
11
 
 
12
/**
 
13
 * Class: OpenLayers.Handler.Path
 
14
 * Handler to draw a path on the map.  Path is displayed on mouse down,
 
15
 * moves on mouse move, and is finished on mouse up.
 
16
 *
 
17
 * Inherits from:
 
18
 *  - <OpenLayers.Handler.Point>
 
19
 */
 
20
OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
 
21
    
 
22
    /**
 
23
     * Property: line
 
24
     * {<OpenLayers.Feature.Vector>}
 
25
     */
 
26
    line: null,
 
27
    
 
28
    /**
 
29
     * Property: freehand
 
30
     * {Boolean} In freehand mode, the handler starts the path on mouse down,
 
31
     * adds a point for every mouse move, and finishes the path on mouse up.
 
32
     * Outside of freehand mode, a point is added to the path on every mouse
 
33
     * click and double-click finishes the path.
 
34
     */
 
35
    freehand: false,
 
36
    
 
37
    /**
 
38
     * Property: freehandToggle
 
39
     * {String} If set, freehandToggle is checked on mouse events and will set
 
40
     * the freehand mode to the opposite of this.freehand.  To disallow
 
41
     * toggling between freehand and non-freehand mode, set freehandToggle to
 
42
     * null.  Acceptable toggle values are 'shiftKey', 'ctrlKey', and 'altKey'.
 
43
     */
 
44
    freehandToggle: 'shiftKey',
 
45
 
 
46
    /**
 
47
     * Constructor: OpenLayers.Handler.Path
 
48
     * Create a new path hander
 
49
     *
 
50
     * Parameters:
 
51
     * control - {<OpenLayers.Control>} 
 
52
     * callbacks - {Object} An object with a 'done' property whos value is a
 
53
     *     function to be called when the path drawing is finished. The 
 
54
     *     callback should expect to recieve a single argument, the line 
 
55
     *     string geometry. If the callbacks object contains a 'point' 
 
56
     *     property, this function will be sent each point as they are added.  
 
57
     *     If the callbacks object contains a 'cancel' property, this function 
 
58
     *     will be called when the handler is deactivated while drawing. The 
 
59
     *     cancel should expect to receive a geometry.
 
60
     * options - {Object} An optional object with properties to be set on the
 
61
     *           handler
 
62
     */
 
63
    initialize: function(control, callbacks, options) {
 
64
        OpenLayers.Handler.Point.prototype.initialize.apply(this, arguments);
 
65
    },
 
66
        
 
67
    /**
 
68
     * Method: createFeature
 
69
     * Add temporary geometries
 
70
     */
 
71
    createFeature: function() {
 
72
        this.line = new OpenLayers.Feature.Vector(
 
73
                                        new OpenLayers.Geometry.LineString());
 
74
        this.point = new OpenLayers.Feature.Vector(
 
75
                                        new OpenLayers.Geometry.Point());
 
76
        this.layer.addFeatures([this.line, this.point], {silent: true});
 
77
    },
 
78
        
 
79
    /**
 
80
     * Method: destroyFeature
 
81
     * Destroy temporary geometries
 
82
     */
 
83
    destroyFeature: function() {
 
84
        OpenLayers.Handler.Point.prototype.destroyFeature.apply(this);
 
85
        this.line = null;
 
86
    },
 
87
 
 
88
    /**
 
89
     * Method: destroyPoint
 
90
     * Destroy the temporary point.
 
91
     */
 
92
    destroyPoint: function() {
 
93
        if(this.point) {
 
94
            this.layer.destroyFeatures([this.point]);
 
95
        }
 
96
    },
 
97
    
 
98
    /**
 
99
     * Method: addPoint
 
100
     * Add point to geometry.  Send the point index to override
 
101
     * the behavior of LinearRing that disregards adding duplicate points.
 
102
     */
 
103
    addPoint: function() {
 
104
        this.line.geometry.addComponent(this.point.geometry.clone(),
 
105
                                        this.line.geometry.components.length);
 
106
        this.callback("point", [this.point.geometry, this.getGeometry()]);
 
107
    },
 
108
    
 
109
    /**
 
110
     * Method: freehandMode
 
111
     * Determine whether to behave in freehand mode or not.
 
112
     *
 
113
     * Returns:
 
114
     * {Boolean}
 
115
     */
 
116
    freehandMode: function(evt) {
 
117
        return (this.freehandToggle && evt[this.freehandToggle]) ?
 
118
                    !this.freehand : this.freehand;
 
119
    },
 
120
 
 
121
    /**
 
122
     * Method: modifyFeature
 
123
     * Modify the existing geometry given the new point
 
124
     */
 
125
    modifyFeature: function() {
 
126
        var index = this.line.geometry.components.length - 1;
 
127
        this.line.geometry.components[index].x = this.point.geometry.x;
 
128
        this.line.geometry.components[index].y = this.point.geometry.y;
 
129
        this.line.geometry.components[index].clearBounds();
 
130
    },
 
131
    
 
132
    /**
 
133
     * Method: drawFeature
 
134
     * Render geometries on the temporary layer.
 
135
     */
 
136
    drawFeature: function() {
 
137
        this.layer.drawFeature(this.line, this.style);
 
138
        this.layer.drawFeature(this.point, this.style);
 
139
    },
 
140
 
 
141
    /**
 
142
     * Method: getGeometry
 
143
     * Return the sketch geometry.  If <multi> is true, this will return
 
144
     *     a multi-part geometry.
 
145
     *
 
146
     * Returns:
 
147
     * {<OpenLayers.Geometry.LineString>}
 
148
     */
 
149
    getGeometry: function() {
 
150
        var geometry = this.line.geometry;
 
151
        if(this.multi) {
 
152
            geometry = new OpenLayers.Geometry.MultiLineString([geometry]);
 
153
        }
 
154
        return geometry;
 
155
    },
 
156
 
 
157
    /**
 
158
     * Method: mousedown
 
159
     * Handle mouse down.  Add a new point to the geometry and
 
160
     * render it. Return determines whether to propagate the event on the map.
 
161
     * 
 
162
     * Parameters:
 
163
     * evt - {Event} The browser event
 
164
     *
 
165
     * Returns: 
 
166
     * {Boolean} Allow event propagation
 
167
     */
 
168
    mousedown: function(evt) {
 
169
        // ignore double-clicks
 
170
        if (this.lastDown && this.lastDown.equals(evt.xy)) {
 
171
            return false;
 
172
        }
 
173
        if(this.lastDown == null) {
 
174
            if(this.persist) {
 
175
                this.destroyFeature();
 
176
            }
 
177
            this.createFeature();
 
178
        }
 
179
        this.mouseDown = true;
 
180
        this.lastDown = evt.xy;
 
181
        var lonlat = this.control.map.getLonLatFromPixel(evt.xy);
 
182
        this.point.geometry.x = lonlat.lon;
 
183
        this.point.geometry.y = lonlat.lat;
 
184
        this.point.geometry.clearBounds();
 
185
        if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
 
186
            this.addPoint();
 
187
        }
 
188
        this.drawFeature();
 
189
        this.drawing = true;
 
190
        return false;
 
191
    },
 
192
 
 
193
    /**
 
194
     * Method: mousemove
 
195
     * Handle mouse move.  Adjust the geometry and redraw.
 
196
     * Return determines whether to propagate the event on the map.
 
197
     * 
 
198
     * Parameters:
 
199
     * evt - {Event} The browser event
 
200
     *
 
201
     * Returns: 
 
202
     * {Boolean} Allow event propagation
 
203
     */
 
204
    mousemove: function (evt) {
 
205
        if(this.drawing) { 
 
206
            var lonlat = this.map.getLonLatFromPixel(evt.xy);
 
207
            this.point.geometry.x = lonlat.lon;
 
208
            this.point.geometry.y = lonlat.lat;
 
209
            this.point.geometry.clearBounds();
 
210
            if(this.mouseDown && this.freehandMode(evt)) {
 
211
                this.addPoint();
 
212
            } else {
 
213
                this.modifyFeature();
 
214
            }
 
215
            this.drawFeature();
 
216
        }
 
217
        return true;
 
218
    },
 
219
    
 
220
    /**
 
221
     * Method: mouseup
 
222
     * Handle mouse up.  Send the latest point in the geometry to
 
223
     * the control. Return determines whether to propagate the event on the map.
 
224
     * 
 
225
     * Parameters:
 
226
     * evt - {Event} The browser event
 
227
     *
 
228
     * Returns: 
 
229
     * {Boolean} Allow event propagation
 
230
     */
 
231
    mouseup: function (evt) {
 
232
        this.mouseDown = false;
 
233
        if(this.drawing) {
 
234
            if(this.freehandMode(evt)) {
 
235
                if(this.persist) {
 
236
                    this.destroyPoint();
 
237
                }
 
238
                this.finalize();
 
239
            } else {
 
240
                if(this.lastUp == null) {
 
241
                   this.addPoint();
 
242
                }
 
243
                this.lastUp = evt.xy;
 
244
            }
 
245
            return false;
 
246
        }
 
247
        return true;
 
248
    },
 
249
  
 
250
    /**
 
251
     * Method: dblclick 
 
252
     * Handle double-clicks.  Finish the geometry and send it back
 
253
     * to the control.
 
254
     * 
 
255
     * Parameters:
 
256
     * evt - {Event} The browser event
 
257
     *
 
258
     * Returns: 
 
259
     * {Boolean} Allow event propagation
 
260
     */
 
261
    dblclick: function(evt) {
 
262
        if(!this.freehandMode(evt)) {
 
263
            var index = this.line.geometry.components.length - 1;
 
264
            this.line.geometry.removeComponent(this.line.geometry.components[index]);
 
265
            if(this.persist) {
 
266
                this.destroyPoint();
 
267
            }
 
268
            this.finalize();
 
269
        }
 
270
        return false;
 
271
    },
 
272
 
 
273
    CLASS_NAME: "OpenLayers.Handler.Path"
 
274
});