OpenLayers OpenLayers

Ticket #1071: selectfeature.patch

File selectfeature.patch, 7.8 kB (added by crschmidt, 1 year ago)

patch requires #1070 for styling support

  • tests/Control/test_SelectFeature.html

    old new  
    2323        var layer = new OpenLayers.Layer.Vector(); 
    2424        map.addLayer(layer); 
    2525        var control = new OpenLayers.Control.SelectFeature(layer); 
    26         control.handler.destroy = function() { 
     26        control.featureHandler.destroy = function() { 
    2727            t.ok(true, 
    2828                 "control.destroy calls destroy on feature handler"); 
    2929        } 
     
    4747        t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['default'].strokeColor, "feature style is set back to old style"); 
    4848    } 
    4949     
     50    function test_Control_SelectFeature_multiple(t) { 
     51        t.plan(5); 
     52        var map = new OpenLayers.Map("map"); 
     53        var layer = new OpenLayers.Layer.Vector(); 
     54        map.addLayer(layer); 
     55        var control = new OpenLayers.Control.SelectFeature(layer, {'multiple': true, inclusiveBox: false }); 
     56        var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); 
     57        var feature2 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,1)); 
     58        layer.addFeatures([feature,feature2]); 
     59        layer.drawFeature = function() { } 
     60        control.setMap(map); 
     61        control.clickFeature(feature); 
     62        t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['select'].strokeColor, "feature style is set to select style"); 
     63        control.clickFeature(feature); 
     64        t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['default'].strokeColor, "feature style is set back to old style"); 
     65        map.getLonLatFromPixel = function(arg) { 
     66            return new OpenLayers.LonLat(arg.x, arg.y); 
     67        }     
     68        control.selectBox(new OpenLayers.Bounds(-1, -1, 2, 2)); 
     69        t.eq(layer.selectedFeatures.length, 2, "box around all features selects all features.") 
     70        control.selectBox(new OpenLayers.Bounds(-1, -1, .5, .5)); 
     71        t.eq(layer.selectedFeatures.length, 1, "box around one feature turns that feature off with inclusiveBox false") 
     72        control.inclusiveBox = true; 
     73        control.selectBox(new OpenLayers.Bounds(-1, -1, 2, 2)); 
     74        t.eq(layer.selectedFeatures.length, 2, "inclusiveBox lets all features turn on even when one is on already.") 
     75 
     76    } 
     77     
    5078    function test_Control_SelectFeature_activate(t) { 
    5179        t.plan(2); 
    5280        var map = new OpenLayers.Map("map"); 
     
    5482        map.addLayer(layer); 
    5583        var control = new OpenLayers.Control.SelectFeature(layer); 
    5684        map.addControl(control); 
    57         t.ok(!control.handler.active, 
     85        t.ok(!control.featureHandler.active, 
    5886             "feature handler is not active prior to activating control"); 
    5987        control.activate(); 
    60         t.ok(control.handler.active, 
     88        t.ok(control.featureHandler.active, 
    6189             "feature handler is active after activating control"); 
    6290    } 
    6391 
     
    7098        map.addControl(control); 
    7199         
    72100        control.activate(); 
    73         control.handler.deactivate = function() { 
     101        control.featureHandler.deactivate = function() { 
    74102            t.ok(true, 
    75103                 "control.deactivate calls deactivate on feature handler"); 
    76104        } 
  • lib/OpenLayers/Control/SelectFeature.js

    old new  
    2020     * {Boolean} Allow selection of multiple geometries 
    2121     */ 
    2222    multiple: false,  
     23     
     24    /** 
     25     * APIProperty: inclusiveBox 
     26     * {Boolean} If this is true, then when a box is drawn (in multiple feature 
     27     * selection mode), all intersected features will be selected, even if they  
     28     * were already selected. Otherwise, the box will switch the state of any 
     29     * features. Default is true. 
     30     */ 
     31    inclusiveBox: true,  
    2332 
     33 
    2434    /** 
    2535     * APIProperty: hover 
    2636     * {Boolean} Select on mouse over and deselect on mouse out.  If true, this 
     
    8999                                                  out: this.outFeature 
    90100                                                }, this.callbacks); 
    91101        var handlerOptions = {geometryTypes: this.geometryTypes}; 
    92         this.handler = new OpenLayers.Handler.Feature(this, layer, 
     102        this.featureHandler = new OpenLayers.Handler.Feature(this, layer, 
    93103                                                      this.callbacks, 
    94104                                                      handlerOptions); 
     105        if (this.multiple) { 
     106            this.boxHandler = new OpenLayers.Handler.Box( this, 
     107                  {done: this.selectBox}, { 
     108                      keyMask: OpenLayers.Handler.MOD_SHIFT, 
     109                      boxDivClassName: "olControlSelectFeatureSelectBox" 
     110                  } ); 
     111        }                     
    95112    }, 
     113     
     114    /** 
     115     * Method: activate 
     116     */ 
     117    activate: function() { 
     118        this.featureHandler.activate(); 
     119        if (this.boxHandler) { 
     120            this.boxHandler.activate(); 
     121        }     
     122        return OpenLayers.Control.prototype.activate.apply(this,arguments); 
     123    }, 
    96124 
    97125    /** 
     126     * Method: deactivate 
     127     */ 
     128    deactivate: function() { 
     129        if (this.boxHandler) { 
     130            this.boxHandler.deactivate(); 
     131        } 
     132        this.featureHandler.deactivate(); 
     133        return OpenLayers.Control.prototype.deactivate.apply(this,arguments); 
     134    }, 
     135 
     136    /** 
     137     * Method: destroy 
     138     */ 
     139    destroy: function() { 
     140        this.featureHandler.destroy(); 
     141        if (this.boxHandler) { 
     142            this.boxHandler.destroy(); 
     143        }     
     144        return OpenLayers.Control.prototype.destroy.apply(this,arguments); 
     145    }, 
     146         
     147    /** 
     148     * Method: selectBox 
     149     * Callback from the boxHandler set up when multiple feature selection is on. 
     150     * depending on the inclusiveBox setting, will either select all features which 
     151     * intersect the given bounds, or toggle the feature selection status. 
     152     * If passed a bounds (shift-click) has no effect. 
     153     * 
     154     * Parameters: 
     155     * feature - {<OpenLayers.Bounds> || <OpenLayers.LonLat> }   
     156     */ 
     157    selectBox: function (position) { 
     158        if (position instanceof OpenLayers.Bounds) { 
     159            var minXY = this.map.getLonLatFromPixel( 
     160                            new OpenLayers.Pixel(position.left, position.bottom)); 
     161            var maxXY = this.map.getLonLatFromPixel( 
     162                            new OpenLayers.Pixel(position.right, position.top)); 
     163            var bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat, 
     164                                               maxXY.lon, maxXY.lat); 
     165            for(var i = 0; i < this.layer.features.length; i++) { 
     166                var feature = this.layer.features[i];  
     167                if (bounds.intersectsBounds(feature.geometry.getBounds())) { 
     168                    if (this.inclusiveBox) { 
     169                        if(OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) == -1) { 
     170                            this.select(feature); 
     171                        }     
     172                    } else { 
     173                        this.clickFeature(feature) 
     174                    }     
     175                } 
     176            }     
     177        } 
     178    }, 
     179 
     180    /** 
    98181     * Method: clickFeature 
    99182     * Called when the feature handler detects a click on a feature 
    100183     * 
     
    202285     * map - {<OpenLayers.Map>}  
    203286     */ 
    204287    setMap: function(map) { 
    205         this.handler.setMap(map); 
     288        this.featureHandler.setMap(map); 
     289        if (this.multiple) { 
     290            this.boxHandler.setMap(map); 
     291        } 
    206292        OpenLayers.Control.prototype.setMap.apply(this, arguments); 
    207293    }, 
    208294