OpenLayers OpenLayers

Ticket #1698: sketch.patch

File sketch.patch, 9.6 kB (added by tschaub, 5 months ago)

update to sketch handlers

  • lib/OpenLayers/Handler/Point.js

    old new  
    3333    layer: null, 
    3434     
    3535    /** 
     36     * Property: multi 
     37     * {Boolean} Cast features to multi-part geometries before passing to the 
     38     *     layer.  Default is false. 
     39     */ 
     40    multi: false, 
     41     
     42    /** 
    3643     * Property: drawing  
    3744     * {Boolean} A point is being drawn 
    3845     */ 
     
    5764    lastUp: null, 
    5865 
    5966    /** 
     67     * APIProperty: persist 
     68     * {Boolean} Leave the feature rendered until destroyFeature is called. 
     69     *     Default is false.  If set to true, the feature remains rendered until 
     70     *     destroyFeature is called, typically by deactivating the handler or 
     71     *     starting another drawing. 
     72     */ 
     73    persist: false, 
     74 
     75    /** 
    6076     * Constructor: OpenLayers.Handler.Point 
    6177     * Create a new point handler. 
    6278     * 
     
    89105        } 
    90106        // create temporary vector layer for rendering geometry sketch 
    91107        // TBD: this could be moved to initialize/destroy - setting visibility here 
    92         var options =
     108        var options = OpenLayers.Util.extend(
    93109            displayInLayerSwitcher: false, 
    94110            // indicate that the temp vector layer will never be out of range 
    95111            // without this, resolution properties must be specified at the 
    96112            // map-level for this temporary layer to init its resolutions 
    97113            // correctly 
    98114            calculateInRange: function() { return true; } 
    99         }
     115        }, this.layerOptions)
    100116        this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options); 
    101117        this.map.addLayer(this.layer); 
    102118        return true; 
     
    108124     */ 
    109125    createFeature: function() { 
    110126        this.point = new OpenLayers.Feature.Vector( 
    111                                               new OpenLayers.Geometry.Point()); 
     127            new OpenLayers.Geometry.Point() 
     128        ); 
     129        this.layer.addFeatures([this.point]); 
    112130    }, 
    113131 
    114132    /** 
     
    123141        if(this.drawing) { 
    124142            this.cancel(); 
    125143        } 
     144        this.destroyFeature(); 
    126145        // If a layer's map property is set to null, it means that that layer 
    127146        // isn't added to the map. Since we ourself added the layer to the map 
    128147        // in activate(), we can assume that if this.layer.map is null it means 
     
    140159     * Destroy the temporary geometries 
    141160     */ 
    142161    destroyFeature: function() { 
    143         if(this.point) { 
    144             this.point.destroy(); 
    145         } 
     162        this.layer.destroyFeatures(); 
    146163        this.point = null; 
    147164    }, 
    148165 
    149166    /** 
    150167     * Method: finalize 
    151168     * Finish the geometry and call the "done" callback. 
     169     * 
     170     * Parameters: 
     171     * cancel - {Boolean} Call cancel instead of done callback.  Default is 
     172     *     false. 
    152173     */ 
    153     finalize: function() { 
    154         this.layer.renderer.clear()
     174    finalize: function(cancel) { 
     175        var key = cancel ? "cancel" : "done"
    155176        this.drawing = false; 
    156177        this.mouseDown = false; 
    157178        this.lastDown = null; 
    158179        this.lastUp = null; 
    159         this.callback("done", [this.geometryClone()]); 
    160         this.destroyFeature(); 
     180        this.callback(key, [this.geometryClone()]); 
     181        if(cancel || !this.persist) { 
     182            this.destroyFeature(); 
     183        } 
    161184    }, 
    162185 
    163186    /** 
     
    165188     * Finish the geometry and call the "cancel" callback. 
    166189     */ 
    167190    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(); 
     191        this.finalize(true); 
    175192    }, 
    176193 
    177194    /** 
     
    215232    }, 
    216233     
    217234    /** 
     235     * Method: getGeometry 
     236     * Return the sketch geometry.  If <multi> is true, this will return 
     237     *     a multi-part geometry. 
     238     * 
     239     * Returns: 
     240     * {<OpenLayers.Geometry.Point>} 
     241     */ 
     242    getGeometry: function() { 
     243        var geometry = this.point.geometry; 
     244        if(this.multi) { 
     245            geometry = new OpenLayers.Geometry.MultiPoint([geometry]); 
     246        } 
     247        return geometry; 
     248    }, 
     249 
     250    /** 
    218251     * Method: geometryClone 
    219252     * Return a clone of the relevant geometry. 
    220253     * 
    221254     * Returns: 
    222      * {<OpenLayers.Geometry.Point>} 
     255     * {<OpenLayers.Geometry>} 
    223256     */ 
    224257    geometryClone: function() { 
    225         return this.point.geometry.clone(); 
     258        return this.getGeometry().clone(); 
    226259    }, 
    227260   
    228261    /** 
     
    246279            return true; 
    247280        } 
    248281        if(this.lastDown == null) { 
     282            if(this.persist) { 
     283                this.destroyFeature(); 
     284            } 
    249285            this.createFeature(); 
    250286        } 
    251287        this.lastDown = evt.xy; 
  • lib/OpenLayers/Handler/Path.js

    old new  
    7373                                        new OpenLayers.Geometry.LineString()); 
    7474        this.point = new OpenLayers.Feature.Vector( 
    7575                                        new OpenLayers.Geometry.Point()); 
     76        this.layer.addFeatures([this.line, this.point]); 
    7677    }, 
    7778         
    7879    /** 
     
    8182     */ 
    8283    destroyFeature: function() { 
    8384        OpenLayers.Handler.Point.prototype.destroyFeature.apply(this); 
    84         if(this.line) { 
    85             this.line.destroy(); 
    86         } 
    8785        this.line = null; 
    8886    }, 
     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    }, 
    8997     
    9098    /** 
    9199     * Method: addPoint 
     
    95103    addPoint: function() { 
    96104        this.line.geometry.addComponent(this.point.geometry.clone(), 
    97105                                        this.line.geometry.components.length); 
    98         this.callback("point", [this.point.geometry]); 
     106        this.callback("point", [this.point.geometry, this.getGeometry()]); 
    99107    }, 
    100108     
    101109    /** 
     
    131139    }, 
    132140 
    133141    /** 
    134      * Method: geometryClone 
    135      * Return a clone of the relevant geometry. 
     142     * Method: getGeometry 
     143     * Return the sketch geometry.  If <multi> is true, this will return 
     144     *     a multi-part geometry. 
    136145     * 
    137146     * Returns: 
    138147     * {<OpenLayers.Geometry.LineString>} 
    139148     */ 
    140     geometryClone: function() { 
    141         return this.line.geometry.clone(); 
     149    getGeometry: function() { 
     150        var geometry = this.line.geometry; 
     151        if(this.multi) { 
     152            geometry = new OpenLayers.Geometry.MultiLineString([geometry]); 
     153        } 
     154        return geometry; 
    142155    }, 
    143156 
    144157    /** 
     
    158171            return false; 
    159172        } 
    160173        if(this.lastDown == null) { 
     174            if(this.persist) { 
     175                this.destroyFeature(); 
     176            } 
    161177            this.createFeature(); 
    162178        } 
    163179        this.mouseDown = true; 
     
    214230        this.mouseDown = false; 
    215231        if(this.drawing) { 
    216232            if(this.freehandMode(evt)) { 
     233                if(this.persist) { 
     234                    this.destroyPoint(); 
     235                } 
    217236                this.finalize(); 
    218237            } else { 
    219238                if(this.lastUp == null) { 
     
    241260        if(!this.freehandMode(evt)) { 
    242261            var index = this.line.geometry.components.length - 1; 
    243262            this.line.geometry.removeComponent(this.line.geometry.components[index]); 
     263            if(this.persist) { 
     264                this.destroyPoint(); 
     265            } 
    244266            this.finalize(); 
    245267        } 
    246268        return false; 
  • lib/OpenLayers/Handler/Polygon.js

    old new  
    5959        this.polygon.geometry.addComponent(this.line.geometry); 
    6060        this.point = new OpenLayers.Feature.Vector( 
    6161                                        new OpenLayers.Geometry.Point()); 
     62        this.layer.addFeatures([this.polygon, this.point]); 
    6263    }, 
    6364 
    6465    /** 
     
    6768     */ 
    6869    destroyFeature: function() { 
    6970        OpenLayers.Handler.Path.prototype.destroyFeature.apply(this); 
    70         if(this.polygon) { 
    71             this.polygon.destroy(); 
    72         } 
    7371        this.polygon = null; 
    7472    }, 
    7573 
     
    9391        this.layer.drawFeature(this.polygon, this.style); 
    9492        this.layer.drawFeature(this.point, this.style); 
    9593    }, 
    96  
     94     
    9795    /** 
    98      * Method: geometryClone 
    99      * Return a clone of the relevant geometry. 
     96     * Method: getGeometry 
     97     * Return the sketch geometry.  If <multi> is true, this will return 
     98     *     a multi-part geometry. 
    10099     * 
    101100     * Returns: 
    102101     * {<OpenLayers.Geometry.Polygon>} 
    103102     */ 
    104     geometryClone: function() { 
    105         return this.polygon.geometry.clone(); 
     103    getGeometry: function() { 
     104        var geometry = this.polygon.geometry; 
     105        if(this.multi) { 
     106            geometry = new OpenLayers.Geometry.MultiPolygon([geometry]); 
     107        } 
     108        return geometry; 
    106109    }, 
    107110 
    108111    /** 
     
    118121            // remove the penultimate point 
    119122            var index = this.line.geometry.components.length - 2; 
    120123            this.line.geometry.removeComponent(this.line.geometry.components[index]); 
     124            if(this.persist) { 
     125                this.destroyPoint(); 
     126            } 
    121127            this.finalize(); 
    122128        } 
    123129        return false;