OpenLayers OpenLayers

Ticket #1071: patch-1071-r7546-A0.diff

File patch-1071-r7546-A0.diff, 13.3 kB (added by pgiraud, 1 month ago)
  • theme/default/style.css

    old new  
    215215    background-position: 0px 1px; 
    216216} 
    217217 
    218 .olHandlerBoxZoomBox
     218.olHandlerBoxZoomBox, .olHandlerBoxFeatureSelect
    219219    border: 2px solid red; 
    220220    position: absolute; 
    221221    background-color: white; 
  • tests/BaseTypes/Bounds.html

    old new  
    196196 
    197197    } 
    198198     
     199     
     200    function test_Bounds_intersects(t){ 
     201        t.plan(2); 
     202         
     203        var bounds = new OpenLayers.Bounds(-20, -10, 20, 10); 
     204         
     205        // intersects 
     206        var geometry = new OpenLayers.Geometry.Point(0, 0); 
     207        t.ok( bounds.intersects(geometry), "(" + bounds.toBBOX() + ") correctly intersects (" + geometry + ")"); 
     208         
     209        // no intersect 
     210        geometry = new OpenLayers.Geometry.Point(-50, 0); 
     211        t.ok( !bounds.intersects(geometry), "(" + bounds.toBBOX() + ") doesn't intersect (" + geometry + ")"); 
     212    } 
     213     
    199214    function test_Bounds_containsBounds(t) { 
    200215        t.plan( 35 ); 
    201216        containerBounds = new OpenLayers.Bounds(10,10,40,40); 
  • tests/Control/SelectFeature.html

    old new  
    1818    } 
    1919     
    2020    function test_Control_SelectFeature_destroy(t) { 
    21         t.plan(1); 
     21        t.plan(2); 
    2222        var map = new OpenLayers.Map("map"); 
    2323        var layer = new OpenLayers.Layer.Vector(); 
    2424        map.addLayer(layer); 
    25         var control = new OpenLayers.Control.SelectFeature(layer); 
    26         control.handler.destroy = function() { 
     25        var control = new OpenLayers.Control.SelectFeature(layer, {box: true}); 
     26        control.featureHandler.deactivate = function() { 
    2727            t.ok(true, 
    28                  "control.destroy calls destroy on feature handler"); 
     28                 "control.deactivate calls deactivate on feature handler"); 
    2929        } 
     30        control.boxHandler.deactivate = function() { 
     31            t.ok(true, 
     32                 "control.deactivate calls deactivate on box handler"); 
     33        } 
    3034// should nullify the layer property here 
    3135        control.destroy(); 
    3236         
     
    6165        }; 
    6266        // mock up active control 
    6367        var control = new OpenLayers.Control.SelectFeature(layer); 
    64         control.handler = { 
     68        control.featureHandler = { 
    6569            evt: {} 
    6670        }; 
    6771        // mock up features 
     
    110114         
    111115    } 
    112116     
     117    function test_Control_SelectFeature_box(t) { 
     118        t.plan(3); 
     119        var map = new OpenLayers.Map("map"); 
     120        var layer = new OpenLayers.Layer.Vector(); 
     121        map.addLayer(layer); 
     122        var control = new OpenLayers.Control.SelectFeature(layer, {'multiple': true, box: true }); 
     123        var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); 
     124        var feature2 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,1)); 
     125        var feature3 = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-2,-2)); 
     126        layer.addFeatures([feature,feature2,feature3]); 
     127        control.setMap(map); 
     128        map.getLonLatFromPixel = function(arg) { 
     129            return new OpenLayers.LonLat(arg.x, arg.y); 
     130        }     
     131        control.selectBox(new OpenLayers.Bounds(-1, -1, 2, 2)); 
     132        t.eq(layer.selectedFeatures.length, 2, "box around all features selects all features."); 
     133         
     134        control.selectBox(new OpenLayers.Bounds(-3, -3, -1, -1)); 
     135        t.eq(layer.selectedFeatures.length, 3, "box around other features doesn't turn off already selected features.");         
     136         
     137        control.multipleSelect = function() { 
     138            return false; 
     139        }; 
     140        control.selectBox(new OpenLayers.Bounds(-3, -3, -1, -1)); 
     141        t.eq(layer.selectedFeatures.length, 1, "box around other features correctly turns off already selected features.") 
     142 
     143    } 
     144     
    113145    function test_Control_SelectFeature_activate(t) { 
    114         t.plan(2); 
     146        t.plan(4); 
    115147        var map = new OpenLayers.Map("map"); 
    116148        var layer = new OpenLayers.Layer.Vector(); 
    117149        map.addLayer(layer); 
    118         var control = new OpenLayers.Control.SelectFeature(layer); 
     150        var control = new OpenLayers.Control.SelectFeature(layer, {box: true}); 
    119151        map.addControl(control); 
    120         t.ok(!control.handler.active, 
     152        t.ok(!control.featureHandler.active, 
    121153             "feature handler is not active prior to activating control"); 
     154        t.ok(!control.boxHandler.active, 
     155             "box handler is not active prior to activating control"); 
    122156        control.activate(); 
    123         t.ok(control.handler.active, 
     157        t.ok(control.featureHandler.active, 
    124158             "feature handler is active after activating control"); 
     159        t.ok(control.boxHandler.active, 
     160             "box handler is active after activating control"); 
    125161    } 
    126162 
    127163    function test_Control_SelectFeature_deactivate(t) { 
    128         t.plan(1); 
     164        t.plan(2); 
    129165        var map = new OpenLayers.Map("map"); 
    130166        var layer = new OpenLayers.Layer.Vector(); 
    131167        map.addLayer(layer); 
    132         var control = new OpenLayers.Control.SelectFeature(layer); 
     168        var control = new OpenLayers.Control.SelectFeature(layer, {box: true}); 
    133169        map.addControl(control); 
    134170         
    135171        control.activate(); 
    136         control.handler.deactivate = function() { 
     172        control.featureHandler.deactivate = function() { 
    137173            t.ok(true, 
    138174                 "control.deactivate calls deactivate on feature handler"); 
    139175        } 
     176        control.boxHandler.deactivate = function() { 
     177            t.ok(true, 
     178                 "control.deactivate calls deactivate on box handler"); 
     179        } 
    140180        control.deactivate(); 
    141181    } 
    142182 
  • lib/OpenLayers/BaseTypes/Bounds.js

    old new  
    381381    }, 
    382382     
    383383    /** 
     384     * APIMethod: intersects 
     385     *    Converts the current bounds into a LinearRing to check for 
     386     *    intersection with the given geometry 
     387     *  
     388     * Parameters: 
     389     * geometry - {<OpenLayers.Geometry>} 
     390     *  
     391     * Returns: 
     392     * {Boolean} The passed-in geometry object intersects this bounds. 
     393     */ 
     394    intersects: function(geometry) { 
     395        return this.toGeometry().intersects(geometry); 
     396    }, 
     397     
     398    /** 
    384399     * APIMethod: containsBounds 
    385400     *  
    386401     * bounds - {<OpenLayers.Bounds>} 
  • lib/OpenLayers/Control/SelectFeature.js

    old new  
    6060    hover: false, 
    6161     
    6262    /** 
     63     * APIProperty: box 
     64     * {Boolean} Allow feature selection by drawing a box. 
     65     */ 
     66    box: false, 
     67     
     68    /** 
    6369     * APIProperty: onSelect  
    6470     * {Function} Optional function to be called when a feature is selected. 
    6571     * The function should expect to be called with a feature. 
     
    8894     
    8995    /** 
    9096     * APIProperty: callbacks 
    91      * {Object} The functions that are sent to the handler for callback 
     97     * {Object} The functions that are sent to the featureHandler for callback 
    9298     */ 
    9399    callbacks: null, 
    94100     
     
    106112    renderIntent: "select", 
    107113 
    108114    /** 
    109      * Property: handler 
     115     * Property: featureHandler 
    110116     * {<OpenLayers.Handler.Feature>} 
    111117     */ 
    112     handler: null, 
     118    featureHandler: null, 
    113119 
    114120    /** 
    115121     * Constructor: <OpenLayers.Control.SelectFeature> 
     
    127133                                                  over: this.overFeature, 
    128134                                                  out: this.outFeature 
    129135                                                }, this.callbacks); 
    130         var handlerOptions = { geometryTypes: this.geometryTypes}; 
    131         this.handler = new OpenLayers.Handler.Feature(this, layer, 
     136        var featureHandlerOptions = { geometryTypes: this.geometryTypes}; 
     137        this.featureHandler = new OpenLayers.Handler.Feature(this, layer, 
    132138                                                      this.callbacks, 
    133                                                       handlerOptions); 
     139                                                      featureHandlerOptions); 
     140                                                       
     141        if (this.box) { 
     142            this.boxHandler = new OpenLayers.Handler.Box( this,  
     143                {done: this.selectBox}, 
     144                {  
     145                    //keyMask: // we don't want to set any keyMask for box 
     146                    boxDivClassName: "olHandlerBoxFeatureSelect"  
     147                } 
     148            );  
     149        } 
    134150    }, 
    135151 
    136152    /** 
     
    188204     * {Boolean} Allow for multiple selected features. 
    189205     */ 
    190206    multipleSelect: function() { 
    191         return this.multiple || this.handler.evt[this.multipleKey]; 
     207        return this.multiple || this.featureHandler.evt[this.multipleKey]; 
    192208    }, 
    193209     
    194210    /** 
     
    200216     * {Boolean} Toggle the selected state of a feature. 
    201217     */ 
    202218    toggleSelect: function() { 
    203         return this.toggle || this.handler.evt[this.toggleKey]; 
     219        return this.toggle || this.featureHandler.evt[this.toggleKey]; 
    204220    }, 
    205221 
    206222    /** 
     
    279295        this.layer.events.triggerEvent("featureunselected", {feature: feature}); 
    280296        this.onUnselect(feature); 
    281297    }, 
     298     
     299    /** 
     300     * Method: activate 
     301     */ 
     302    activate: function() { 
     303        this.featureHandler.activate(); 
     304        if (this.boxHandler) { 
     305            this.boxHandler.activate(); 
     306        }     
     307        return OpenLayers.Control.prototype.activate.apply(this,arguments); 
     308    }, 
     309  
     310    /** 
     311     * Method: deactivate 
     312     */ 
     313    deactivate: function() { 
     314        if (this.boxHandler) { 
     315            this.boxHandler.deactivate(); 
     316        } 
     317        this.featureHandler.deactivate(); 
     318        return OpenLayers.Control.prototype.deactivate.apply(this,arguments); 
     319    }, 
    282320 
     321    /** 
     322     * Method: destroy 
     323     */ 
     324    destroy: function() { 
     325        this.featureHandler.destroy(); 
     326        if (this.boxHandler) { 
     327            this.boxHandler.destroy(); 
     328        }     
     329        return OpenLayers.Control.prototype.destroy.apply(this,arguments); 
     330    }, 
     331         
     332    /** 
     333     * Method: selectBox 
     334     * Callback from the boxHandler set up when multiple feature selection is on. 
     335     * depending on the inclusiveBox setting, will either select all features which 
     336     * intersect the given bounds, or toggle the feature selection status. 
     337     * If passed a bounds (shift-click) has no effect. 
     338     * 
     339     * Parameters: 
     340     * feature - {<OpenLayers.Bounds> || <OpenLayers.LonLat> }   
     341     */ 
     342    selectBox: function (position) { 
     343        if (position instanceof OpenLayers.Bounds) { 
     344            var minXY = this.map.getLonLatFromPixel( 
     345                            new OpenLayers.Pixel(position.left, position.bottom)); 
     346            var maxXY = this.map.getLonLatFromPixel( 
     347                            new OpenLayers.Pixel(position.right, position.top)); 
     348            var bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat, 
     349                                               maxXY.lon, maxXY.lat); 
     350             
     351            // if multiple is false, first deselect currently selected features 
     352            if (!this.multipleSelect()) { 
     353                this.unselectAll(); 
     354            } 
     355             
     356            // because we're using a box, we consider we want multiple selection 
     357            var prevMultiple = this.multiple; 
     358            this.multiple = true; 
     359            for(var i = 0; i < this.layer.features.length; i++) { 
     360                var feature = this.layer.features[i]; 
     361                if (bounds.intersects(feature.geometry)) { 
     362                    if (OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) == -1) { 
     363                        this.select(feature); 
     364                    } 
     365                } 
     366            } 
     367            this.multiple = prevMultiple; 
     368        } 
     369    }, 
     370 
    283371    /**  
    284372     * Method: setMap 
    285373     * Set the map property for the control.  
     
    288376     * map - {<OpenLayers.Map>}  
    289377     */ 
    290378    setMap: function(map) { 
    291         this.handler.setMap(map); 
     379        this.featureHandler.setMap(map); 
     380        if (this.box) { 
     381            this.boxHandler.setMap(map); 
     382        } 
    292383        OpenLayers.Control.prototype.setMap.apply(this, arguments); 
    293384    }, 
    294385 
  • examples/select-feature.html

    old new  
    4141                        clickout: false, toggle: false, 
    4242                        multiple: false, hover: false, 
    4343                        toggleKey: "ctrlKey", // ctrl key removes from selection 
    44                         multipleKey: "shiftKey" // shift key adds to selection 
     44                        multipleKey: "shiftKey", // shift key adds to selection 
     45                        box: true 
    4546                    } 
    4647                ) 
    4748            };