Set opacity on layer div, not on tiles and markers. r=fredj (closes #3431)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@12185 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2011-07-25 11:42:43 +00:00
parent c3850a9fb9
commit fe720b259e
9 changed files with 17 additions and 178 deletions

View File

@@ -349,6 +349,11 @@ OpenLayers.Layer = OpenLayers.Class({
this.div.style.width = "100%";
this.div.style.height = "100%";
this.div.dir = "ltr";
if (this.opacity != null) {
OpenLayers.Util.modifyDOMElement(
this.div, null, null, null, null, null, null, this.opacity
);
}
this.events = new OpenLayers.Events(this, this.div,
this.EVENT_TYPES);
@@ -1269,11 +1274,8 @@ OpenLayers.Layer = OpenLayers.Class({
setOpacity: function(opacity) {
if (opacity != this.opacity) {
this.opacity = opacity;
for(var i=0, len=this.div.childNodes.length; i<len; ++i) {
var element = this.div.childNodes[i].firstChild;
OpenLayers.Util.modifyDOMElement(element, null, null, null,
null, null, null, opacity);
}
OpenLayers.Util.modifyDOMElement(this.div, null, null, null,
null, null, null, opacity);
if (this.map != null) {
this.map.events.triggerEvent("changelayer", {
layer: this,

View File

@@ -60,22 +60,6 @@ OpenLayers.Layer.Markers = OpenLayers.Class(OpenLayers.Layer, {
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
},
/**
* APIMethod: setOpacity
* Sets the opacity for all the markers.
*
* Parameter:
* opacity - {Float}
*/
setOpacity: function(opacity) {
if (opacity != this.opacity) {
this.opacity = opacity;
for (var i=0, len=this.markers.length; i<len; i++) {
this.markers[i].setOpacity(this.opacity);
}
}
},
/**
* Method: moveTo
*

View File

@@ -590,21 +590,5 @@ OpenLayers.Layer.WFS = OpenLayers.Class(
return extent;
},
/**
* APIMethod: setOpacity
* Call the setOpacity method of the appropriate parent class to set the
* opacity.
*
* Parameter:
* opacity - {Float}
*/
setOpacity: function (opacity) {
if (this.vectorMode) {
OpenLayers.Layer.Vector.prototype.setOpacity.apply(this, [opacity]);
} else {
OpenLayers.Layer.Markers.prototype.setOpacity.apply(this, [opacity]);
}
},
CLASS_NAME: "OpenLayers.Layer.WFS"
});

View File

@@ -786,14 +786,15 @@
}
function test_setOpacity(t) {
t.plan(5);
t.plan(8);
var map, layer, log;
map = new OpenLayers.Map("map");
layer = new OpenLayers.Layer("");
layer = new OpenLayers.Layer("", {opacity: 0.56});
map.addLayer(layer);
t.eq(layer.div.style.opacity, "0.56", "initial opacity correctly applied as layer.div style");
log = [];
map.events.register('changelayer', t, function(event) {
log.push({layer: event.layer, property: event.property});
@@ -801,6 +802,7 @@
layer.setOpacity(0.42);
t.eq(layer.opacity, 0.42,
"setOpacity() set layer.opacity to correct value");
t.eq(layer.div.style.opacity, "0.42", "opacity correctly applied as layer.div style");
t.eq(log.length, 1,
"setOpacity() triggers changelayer once");
t.ok(log[0].layer == layer,
@@ -813,6 +815,12 @@
layer.setOpacity(0.42);
t.eq(log.length, 0,
"setOpacity() does not trigger changelayer if the opacity value is the same");
// 0 as initial opacity
layer.destroy();
layer = new OpenLayers.Layer("", {opacity: 0});
map.addLayer(layer);
t.eq(layer.div.style.opacity, "0", "initial opacity correctly applied as layer.div style");
}

View File

@@ -195,31 +195,6 @@
}
function test_Layer_AGS93_setOpacity (t) {
var params = {layers: "show:0,2"};
t.plan( 5 );
var map = new OpenLayers.Map('map');
map.projection = "xx";
tParams = { layers: 'show:0,2',
format: 'png'};
tOptions = { 'opacity': '0.5' };
var tLayer = new OpenLayers.Layer.ArcGIS93Rest(name, url, tParams, tOptions);
map.addLayer(tLayer);
map.zoomToMaxExtent();
t.eq(tLayer.opacity, "0.5", "Opacity is set correctly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.5, "Opacity on tile is correct");
tLayer.setOpacity("0.6");
t.eq(tLayer.opacity, "0.6", "setOpacity works properly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.6, "Opacity on tile is changed correctly");
var pixel = new OpenLayers.Pixel(5,6);
var tile = tLayer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
tile.draw();
t.eq(parseFloat(tile.imgDiv.style.opacity), 0.6, "Tile opacity is set correctly");
map.destroy();
}
function test_Layer_AGS93_noGutters (t) {
t.plan(2);
var map = new OpenLayers.Map('map');

View File

@@ -196,31 +196,6 @@
}
function test_Layer_MapServer_setOpacity (t) {
t.plan( 5 );
var map = new OpenLayers.Map('map');
map.projection = "xx";
tUrl = "http://labs.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
format: 'image/png'};
tOptions = { 'opacity': '0.5' };
var tLayer = new OpenLayers.Layer.MapServer(name, tUrl, tParams, tOptions);
map.addLayer(tLayer);
map.zoomToMaxExtent();
t.eq(tLayer.opacity, "0.5", "Opacity is set correctly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.5, "Opacity on tile is correct");
tLayer.setOpacity("0.6");
t.eq(tLayer.opacity, "0.6", "setOpacity works properly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.6, "Opacity on tile is changed correctly");
var pixel = new OpenLayers.Pixel(5,6);
var tile = tLayer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
tile.draw();
t.eq(parseFloat(tile.imgDiv.style.opacity), 0.6, "Tile opacity is set correctly");
map.destroy();
}
function test_Layer_MapServer_Reproject (t) {
var validkey = (window.location.protocol == "file:") ||
(window.location.host == "localhost") ||
@@ -411,27 +386,6 @@
}
function test_Layer_MapServer_Untiled_setOpacity (t) {
t.plan( 4 );
var map = new OpenLayers.Map('map');
map.projection = "xx";
tUrl = "http://labs.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
format: 'image/png'};
tOptions = { 'opacity': '0.5' };
var tLayer = new OpenLayers.Layer.MapServer.Untiled(name, tUrl, tParams, tOptions);
map.addLayer(tLayer);
map.zoomToMaxExtent();
t.eq(tLayer.opacity, "0.5", "Opacity is set correctly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.5, "Opacity on tile is correct");
tLayer.setOpacity("0.6");
t.eq(tLayer.opacity, "0.6", "setOpacity works properly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.6, "Opacity on tile is changed correctly");
map.destroy();
}
// DEPRECATED -- REMOVE IN 3.0
function test_Layer_Untiled_MapServer(t) {
t.plan(1);

View File

@@ -121,33 +121,6 @@
}
function test_setOpacity(t) {
t.plan(1);
layer = new OpenLayers.Layer.Markers('Test Layer');
var opacity = 0.1234;
for (var i = 0; i < 12; i++) {
layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), new OpenLayers.Icon()));
}
layer.setOpacity(opacity);
for (var i = 0; i < 4; i++) {
layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), new OpenLayers.Icon()));
}
var itWorks = false;
for (var i = 0; i < layer.markers.length; i++) {
itWorks = parseFloat(layer.markers[i].icon.imageDiv.style.opacity) == opacity;
if (!itWorks) {
break;
}
}
t.ok(itWorks, "setOpacity change markers opacity");
}
</script>
</head>
<body>

View File

@@ -25,22 +25,6 @@
}
function test_Layer_WFS_setOpacity(t) {
t.plan(3);
var layer = new OpenLayers.Layer.WFS(name, "url", {});
layer.setOpacity(.5);
t.eq(layer.opacity, 0.5, "vector setOpacity didn't fail");
var layer = new OpenLayers.Layer.WFS(name, "url", {}, {'featureClass': OpenLayers.Feature.WFS});
var marker = new OpenLayers.Marker(new OpenLayers.LonLat(0,0));
marker.setOpacity = function() {
t.ok(true, "Marker setOpacity was called");
}
layer.addMarker(marker);
layer.setOpacity(.6);
t.eq(layer.opacity, 0.6, "setOpacity didn't fail on markers");
}
function test_Layer_WFS_destroy(t) {
t.plan(13);

View File

@@ -281,31 +281,6 @@
}
function test_Layer_WMS_setOpacity (t) {
t.plan( 5 );
var map = new OpenLayers.Map('map');
map.projection = "xx";
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
format: 'image/png'};
tOptions = { 'opacity': '0.5' };
var tLayer = new OpenLayers.Layer.WMS(name, tUrl, tParams, tOptions);
map.addLayer(tLayer);
map.zoomToMaxExtent();
t.eq(tLayer.opacity, "0.5", "Opacity is set correctly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.5, "Opacity on tile is correct");
tLayer.setOpacity("0.6");
t.eq(tLayer.opacity, "0.6", "setOpacity works properly");
t.eq(parseFloat(tLayer.div.firstChild.firstChild.style.opacity), 0.6, "Opacity on tile is changed correctly");
var pixel = new OpenLayers.Pixel(5,6);
var tile = tLayer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
tile.draw();
t.eq(parseFloat(tile.imgDiv.style.opacity), 0.6, "Tile opacity is set correctly");
map.destroy();
}
function test_Layer_WMS_Reproject (t) {
var validkey = (window.location.protocol == "file:") ||
(window.location.host == "localhost") ||