OpenLayers OpenLayers

Changeset 5896

Show
Ignore:
Timestamp:
01/25/08 18:13:57 (1 year ago)
Author:
ahocevar
Message:

SelectFeature and OpenLayers.Feature.Vector.styleselect?: changed Control.SelectFeature to inherit properties that are not set in selectStyle from feature.style. r=tschaub (closes #1260)

Files:

Legend:

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

    r5614 r5896  
    221221    select: function(feature) { 
    222222        // Store feature style for restoration later 
    223         if(feature.originalStyle == null) { 
     223        if(feature.originalStyle != feature.style) { 
    224224            feature.originalStyle = feature.style; 
    225225        } 
    226226        this.layer.selectedFeatures.push(feature); 
    227         feature.style = this.selectStyle; 
     227 
     228        var selectStyle = this.selectStyle; 
     229 
     230        if (feature.style.CLASS_NAME == "OpenLayers.Style") { 
     231            feature.style = feature.style.createStyle(feature); 
     232        } else { 
     233            feature.style = OpenLayers.Util.extend({}, feature.style); 
     234        } 
     235        if (selectStyle.CLASS_NAME == "OpenLayers.Style") { 
     236            selectStyle = selectStyle.createStyle(feature); 
     237        } 
     238        OpenLayers.Util.extend(feature.style, selectStyle); 
     239 
    228240        this.layer.drawFeature(feature); 
    229241        this.onSelect(feature); 
  • trunk/openlayers/tests/Control/test_SelectFeature.html

    r4232 r5896  
    3434     
    3535    function test_Control_SelectFeature_select(t) { 
    36         t.plan(2); 
     36        t.plan(7); 
    3737        var map = new OpenLayers.Map("map"); 
    3838        var layer = new OpenLayers.Layer.Vector(); 
     
    4646        control.unselect(feature); 
    4747        t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['default'].strokeColor, "feature style is set back to old style"); 
     48         
     49        // Don't ever overwrite my feature style with undefined properties from the selectStyle 
     50        feature.style = {externalGraphic: "foo.png", pointRadius: 39}; 
     51        control.selectStyle.pointRadius = undefined; 
     52        control.select(feature); 
     53        t.eq(feature.style.pointRadius, 39, "undefined style property inherited from original feature style"); 
     54        control.unselect(feature); 
     55         
     56        // Ok, that one went well. But I'm sure you cannot handle OL.Style. 
     57        feature.style = new OpenLayers.Style({externalGraphic: "foo.png", pointRadius: 39}); 
     58        control.select(feature); 
     59        t.eq(feature.style.pointRadius, 39, "undefined style property inherited from original feature style object"); 
     60        control.unselect(feature); 
     61         
     62        // Wow, but using OL.Style as selectStyle will break you. 
     63        control.selectStyle = new OpenLayers.Style({strokeColor: "green"}); 
     64        control.select(feature); 
     65        t.eq(feature.style.strokeColor, "green", "style correct if both feature.style and selectStyle are OL.Style"); 
     66        control.unselect(feature); 
     67         
     68        // Not bad, not bad. And what if I set feature.style back to a style hash? 
     69        feature.style = layer.style; 
     70        control.select(feature); 
     71        t.eq(feature.style.strokeColor, "green", "style still correct with only selectStyle being OL.Style"); 
     72        control.unselect(feature); 
     73        t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style["default"].strokeColor, "style set back to original correctly"); 
    4874    } 
    4975