OpenLayers OpenLayers

Changeset 5959

Show
Ignore:
Timestamp:
02/01/08 11:42:33 (1 year ago)
Author:
tschaub
Message:

Taming the select feature control a bit. Previously, onUnselect was called twice for every unselection. r=elemoine (closes #1234)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Control/SelectFeature.js

    r5896 r5959  
    128128    /** 
    129129     * Method: unselectAll 
    130      * Unselect all selected features. 
    131      */ 
    132     unselectAll: function() { 
     130     * Unselect all selected features.  To unselect all except for a single 
     131     *     feature, set the options.except property to the feature. 
     132     * 
     133     * Parameters: 
     134     * options - {Object} Optional configuration object. 
     135     */ 
     136    unselectAll: function(options) { 
    133137        // we'll want an option to supress notification here 
    134         while (this.layer.selectedFeatures.length > 0) { 
    135             this.unselect(this.layer.selectedFeatures[0]); 
     138        var feature; 
     139        for(var i=this.layer.selectedFeatures.length-1; i>=0; --i) { 
     140            feature = this.layer.selectedFeatures[i]; 
     141            if(!options || options.except != feature) { 
     142                this.unselect(feature); 
     143            } 
    136144        } 
    137145    }, 
     
    146154     */ 
    147155    clickFeature: function(feature) { 
    148         if(this.hover) { 
    149             return; 
    150         } 
    151         var selected = (OpenLayers.Util.indexOf(this.layer.selectedFeatures, 
    152                                                 feature) > -1); 
    153         if(!this.multiple && !this.handler.evt[this.multipleKey]) { 
    154             // perhaps an "except" argument 
    155             this.unselectAll(); 
    156         } 
    157         if(selected) { 
    158             if(this.toggle || this.handler.evt[this.toggleKey]) { 
    159                 // notify here 
    160                 this.unselect(feature); 
     156        if(!this.hover) { 
     157            var selected = (OpenLayers.Util.indexOf(this.layer.selectedFeatures, 
     158                                                    feature) > -1); 
     159            if(selected) { 
     160                if(this.toggleSelect()) { 
     161                    this.unselect(feature); 
     162                } else if(!this.multipleSelect()) { 
     163                    this.unselectAll({except: feature}); 
     164                } 
    161165            } else { 
    162                 // don't notify here - could be removed if unselectAll is modified 
     166                if(!this.multipleSelect()) { 
     167                    this.unselectAll({except: feature}); 
     168                } 
    163169                this.select(feature); 
    164170            } 
    165         } else { 
    166             this.select(feature); 
    167         } 
     171        } 
     172    }, 
     173 
     174    /** 
     175     * Method: multipleSelect 
     176     * Allow for multiple selected features based on <multiple> property and 
     177     *     <multipleKey> event modifier. 
     178     * 
     179     * Returns: 
     180     * {Boolean} Allow for multiple selected features. 
     181     */ 
     182    multipleSelect: function() { 
     183        return this.multiple || this.handler.evt[this.multipleKey]; 
     184    }, 
     185     
     186    /** 
     187     * Method: toggleSelect 
     188     * Event should toggle the selected state of a feature based on <toggle> 
     189     *     property and <toggleKey> event modifier. 
     190     * 
     191     * Returns: 
     192     * {Boolean} Toggle the selected state of a feature. 
     193     */ 
     194    toggleSelect: function() { 
     195        return this.toggle || this.handler.evt[this.toggleKey]; 
    168196    }, 
    169197 
  • trunk/openlayers/tests/Control/test_SelectFeature.html

    r5896 r5959  
    7474    } 
    7575     
     76    function test_Control_SelectFeature_clickFeature(t) { 
     77        t.plan(4); 
     78        // mock up layer 
     79        var layer = { 
     80            selectedFeatures: [], 
     81            drawFeature: function() {} 
     82        }; 
     83        // mock up active control 
     84        var control = new OpenLayers.Control.SelectFeature(layer); 
     85        control.handler = { 
     86            evt: {} 
     87        }; 
     88        // mock up features 
     89        var features = new Array(4); 
     90        for(var i=0; i<features.length; ++i) { 
     91            features[i] = { 
     92                id: Math.random(), 
     93                tested: 0, 
     94                style: "" 
     95            }; 
     96        } 
     97         
     98        // test that onSelect gets called properly 
     99        control.onSelect = function(feature) { 
     100            feature.tested += 1; 
     101            t.eq(feature, features[feature.index], 
     102                 "onSelect called with proper feature (" + feature.index + ")"); 
     103            t.eq(feature.tested, feature.test, 
     104                 "onSelect called only once for feature (" + feature.index + ")"); 
     105        } 
     106 
     107        // test that onUnselect gets called properly 
     108        control.onUnselect = function(feature) { 
     109            feature.tested += 1; 
     110            t.eq(feature, features[feature.index], 
     111                 "onUnselect called with proper feature (" + feature.index + ")"); 
     112            t.eq(feature.tested, feature.test,  
     113                 "onUnselect called only once for feature (" + feature.index + ")"); 
     114        } 
     115         
     116        // mock up first click on first feature (runs 2 tests from onSelect) 
     117        var feature = features[0]; 
     118        feature.index = 0; 
     119        feature.test = 1; 
     120        control.clickFeature(feature); 
     121 
     122        // mock up second click on first feature (runs no tests - already selected) 
     123        control.toggle = false; 
     124        control.clickFeature(feature); 
     125 
     126        // mock up second click on first feature (runs 2 tests from onUnselect) 
     127        control.toggle = true; 
     128        feature.test = 2; 
     129        control.clickFeature(feature); 
     130 
     131         
     132    } 
     133     
    76134    function test_Control_SelectFeature_activate(t) { 
    77135        t.plan(2);