Changeset 6791


Ignore:
Timestamp:
Apr 3, 2008 9:20:21 AM (8 years ago)
Author:
sbenthall
Message:

Swapping the OL trunk Point handler back into our sandbox; swapping our custom one out into a subclass. Some hacks around to get things to work; still some bugs though.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sandbox/topp/vespucci/lib/OpenLayers/Handler/Point.js

    r6233 r6791  
    1111/**
    1212 * Class: OpenLayers.Handler.Point
    13  * Handler to draw a point on the map.  Point is displayed on activation,
    14  * moves on mouse move, and is finished on a click.  The handler triggers
     13 * Handler to draw a point on the map.  Point is displayed on mouse down,
     14 * moves on mouse move, and is finished on mouse up.  The handler triggers
    1515 * callbacks for 'done' and 'cancel'.  Create a new instance with the
    1616 * <OpenLayers.Handler.Point> constructor.
     
    3838     */
    3939    drawing: false,
    40 
    41     /**
    42      * Property: useTemporaryLayer
    43      * {Boolean} True if the Point handler created the layer.
    44      */
    45     useTemporaryLayer: true,
    4640   
    4741    /**
     
    6256     */
    6357    lastUp: null,
    64    
    65     /**
    66      * Property: isInFront
    67      * Whether the currently drawn point has been broght to the front.
    68      */
    69     isInFront: false,
    7058
    7159    /**
     
    8775    initialize: function(control, callbacks, options) {
    8876        // TBD: deal with style
    89         if (!options || !options.style) {
    90             options.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});
    91         }
    92        
     77        this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});
     78
    9379        OpenLayers.Handler.prototype.initialize.apply(this, arguments);
    94        
    95         this.defaultStyle = this.style;
    96        
    97         if (options && options.layer) {
    98             this.layer = options.layer;
    99             this.useTemporaryLayer = false;
    100         }
    10180    },
    10281   
     
    10988            return false;
    11089        }
    111 
    112         if (this.useTemporaryLayer) {
    113             // create temporary vector layer for rendering geometry sketch
    114             // TBD: this could be moved to initialize/destroy - setting visibility here
    115             var options = {
    116                 displayInLayerSwitcher: false,
    117                 // indicate that the temp vector layer will never be out of range
    118                 // without this, resolution properties must be specified at the
    119                 // map-level for this temporary layer to init its resolutions
    120                 // correctly
    121                 calculateInRange: function() { return true; }
    122             };
    123             this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options);
    124             this.map.addLayer(this.layer);
    125         }
    126        
    127         this.createFeature();
    128 
     90        // create temporary vector layer for rendering geometry sketch
     91        // TBD: this could be moved to initialize/destroy - setting visibility here
     92        var options = {
     93            displayInLayerSwitcher: false,
     94            // indicate that the temp vector layer will never be out of range
     95            // without this, resolution properties must be specified at the
     96            // map-level for this temporary layer to init its resolutions
     97            // correctly
     98            calculateInRange: function() { return true; }
     99        };
     100        this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options);
     101        this.map.addLayer(this.layer);
    129102        return true;
    130103    },
     
    137110        this.point = new OpenLayers.Feature.Vector(
    138111                                              new OpenLayers.Geometry.Point());
    139                                              
    140         this.point.style = OpenLayers.Util.extend({}, this.style);
    141         this.isInFront = false;
    142112    },
    143113
     
    154124            this.cancel();
    155125        }
    156      
    157         if (this.useTemporaryLayer) {
    158             this.map.removeLayer(this.layer);
    159             this.layer = null;
    160         }
    161        
     126        // If a layer's map property is set to null, it means that that layer
     127        // isn't added to the map. Since we ourself added the layer to the map
     128        // in activate(), we can assume that if this.layer.map is null it means
     129        // that the layer has been destroyed (as a result of map.destroy() for
     130        // example.
     131        if (this.layer.map != null) {
     132            this.layer.destroy(false);
     133        }
     134        this.layer = null;
    162135        return true;
    163136    },
     
    172145        }
    173146        this.point = null;
    174         this.isInFront = false;
    175147    },
    176148
     
    180152     */
    181153    finalize: function() {
    182         this.cleanup("done");
    183     },
    184 
    185     /**
    186      * APIMethod: cancel
    187      * Finish the geometry and call the "cancel" callback.
    188      */
    189     cancel: function() {
    190         this.cleanup("cancel");
    191     },
    192    
    193     cleanup: function(callback) {
    194         this.layer.eraseFeatures(this.point);
     154        this.layer.renderer.clear();
    195155        this.drawing = false;
    196156        this.mouseDown = false;
    197157        this.lastDown = null;
    198158        this.lastUp = null;
    199         this.callback(callback, [this.geometryClone()]);
     159        this.callback("done", [this.geometryClone()]);
    200160        this.destroyFeature();
    201161    },
    202162
    203163    /**
     164     * APIMethod: cancel
     165     * Finish the geometry and call the "cancel" callback.
     166     */
     167    cancel: function() {
     168        this.layer.renderer.clear();
     169        this.drawing = false;
     170        this.mouseDown = false;
     171        this.lastDown = null;
     172        this.lastUp = null;
     173        this.callback("cancel", [this.geometryClone()]);
     174        this.destroyFeature();
     175    },
     176
     177    /**
    204178     * Method: click
    205      * Handle click. Send the latest point in the geometry to the control.
    206      *
    207      * Parameters:
    208      * evt - {Event} The browser event
    209      *
    210      * Returns:
    211      * {Boolean} Disallow event propagation
     179     * Handle clicks.  Clicks are stopped from propagating to other listeners
     180     *     on map.events or other dom elements.
     181     *
     182     * Parameters:
     183     * evt - {Event} The browser event
     184     *
     185     * Returns:
     186     * {Boolean} Allow event propagation
    212187     */
    213188    click: function(evt) {
    214         this.finalize();
     189        OpenLayers.Event.stop(evt);
    215190        return false;
    216191    },
     
    237212     */
    238213    drawFeature: function() {
    239         this.layer.bringToFront(this.point);
     214        this.layer.drawFeature(this.point, this.style);
    240215    },
    241216   
     
    263238     */
    264239    mousedown: function(evt) {
    265         this.layer.bringToFront(this.point);
     240        // check keyboard modifiers
     241        if(!this.checkModifiers(evt)) {
     242            return true;
     243        }
     244        // ignore double-clicks
     245        if(this.lastDown && this.lastDown.equals(evt.xy)) {
     246            return true;
     247        }
     248        if(this.lastDown == null) {
     249            this.createFeature();
     250        }
     251        this.lastDown = evt.xy;
     252        this.drawing = true;
     253        var lonlat = this.map.getLonLatFromPixel(evt.xy);
     254        this.point.geometry.x = lonlat.lon;
     255        this.point.geometry.y = lonlat.lat;
     256        this.drawFeature();
    266257        return false;
    267258    },
     
    301292     */
    302293    mouseup: function (evt) {
    303        
    304     },
    305    
    306     //sets to default if style == null
    307     //otherwise extends default style with style
    308     setStyle: function(style) {
    309         this.style = OpenLayers.Util.extend({},this.defaultStyle);
    310         if (style) {
    311             OpenLayers.Util.extend(this.style, style);
    312         }
    313        
    314         if(this.point){
    315             this.point.style = OpenLayers.Util.extend({}, this.style);
    316         }
    317     },
    318    
    319     /*
    320      * Set drawing to true,
    321      * default to true
    322      */
    323     setDrawing: function(drawing){
    324        if(drawing !== false){
    325            drawing = true;
    326        }
    327        
    328        this.drawing = drawing;
    329     },
    330    
     294        if(this.drawing) {
     295            this.finalize();
     296            return false;
     297        } else {
     298            return true;
     299        }
     300    },
    331301
    332302    CLASS_NAME: "OpenLayers.Handler.Point"
Note: See TracChangeset for help on using the changeset viewer.