From 45d5fdfd0697224fe809d31efe2bfda08789c8f8 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Fri, 25 Jan 2008 23:13:57 +0000 Subject: [PATCH] SelectFeature and OpenLayers.Feature.Vector.style["select"]: changed Control.SelectFeature to inherit properties that are not set in selectStyle from feature.style. r=tschaub (closes #1260) git-svn-id: http://svn.openlayers.org/trunk/openlayers@5896 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control/SelectFeature.js | 16 ++++++++++++-- tests/Control/test_SelectFeature.html | 28 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/OpenLayers/Control/SelectFeature.js b/lib/OpenLayers/Control/SelectFeature.js index 57eb6c0264..6540402ac2 100644 --- a/lib/OpenLayers/Control/SelectFeature.js +++ b/lib/OpenLayers/Control/SelectFeature.js @@ -220,11 +220,23 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, { */ select: function(feature) { // Store feature style for restoration later - if(feature.originalStyle == null) { + if(feature.originalStyle != feature.style) { feature.originalStyle = feature.style; } this.layer.selectedFeatures.push(feature); - feature.style = this.selectStyle; + + var selectStyle = this.selectStyle; + + if (feature.style.CLASS_NAME == "OpenLayers.Style") { + feature.style = feature.style.createStyle(feature); + } else { + feature.style = OpenLayers.Util.extend({}, feature.style); + } + if (selectStyle.CLASS_NAME == "OpenLayers.Style") { + selectStyle = selectStyle.createStyle(feature); + } + OpenLayers.Util.extend(feature.style, selectStyle); + this.layer.drawFeature(feature); this.onSelect(feature); }, diff --git a/tests/Control/test_SelectFeature.html b/tests/Control/test_SelectFeature.html index c6b5c5258a..94a0cdd1c4 100644 --- a/tests/Control/test_SelectFeature.html +++ b/tests/Control/test_SelectFeature.html @@ -33,7 +33,7 @@ } function test_Control_SelectFeature_select(t) { - t.plan(2); + t.plan(7); var map = new OpenLayers.Map("map"); var layer = new OpenLayers.Layer.Vector(); map.addLayer(layer); @@ -45,6 +45,32 @@ t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['select'].strokeColor, "feature style is set to select style"); control.unselect(feature); t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style['default'].strokeColor, "feature style is set back to old style"); + + // Don't ever overwrite my feature style with undefined properties from the selectStyle + feature.style = {externalGraphic: "foo.png", pointRadius: 39}; + control.selectStyle.pointRadius = undefined; + control.select(feature); + t.eq(feature.style.pointRadius, 39, "undefined style property inherited from original feature style"); + control.unselect(feature); + + // Ok, that one went well. But I'm sure you cannot handle OL.Style. + feature.style = new OpenLayers.Style({externalGraphic: "foo.png", pointRadius: 39}); + control.select(feature); + t.eq(feature.style.pointRadius, 39, "undefined style property inherited from original feature style object"); + control.unselect(feature); + + // Wow, but using OL.Style as selectStyle will break you. + control.selectStyle = new OpenLayers.Style({strokeColor: "green"}); + control.select(feature); + t.eq(feature.style.strokeColor, "green", "style correct if both feature.style and selectStyle are OL.Style"); + control.unselect(feature); + + // Not bad, not bad. And what if I set feature.style back to a style hash? + feature.style = layer.style; + control.select(feature); + t.eq(feature.style.strokeColor, "green", "style still correct with only selectStyle being OL.Style"); + control.unselect(feature); + t.eq(feature.style.strokeColor, OpenLayers.Feature.Vector.style["default"].strokeColor, "style set back to original correctly"); } function test_Control_SelectFeature_activate(t) {