Add map.getNumLayers(), map.getLayerIndex(), map.setLayerIndex(), map.raiseLayer() to support moving layers up and down in the map stack.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@1604 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Schuyler Erle
2006-10-05 19:07:19 +00:00
parent 8782ee3e15
commit fee4d6f973
2 changed files with 108 additions and 4 deletions

View File

@@ -255,14 +255,23 @@ OpenLayers.Map.prototype = {
return foundLayer;
},
/**
* @param {OpenLayers.Layer} layer
* @param {int} zIdx
* @private
*/
setLayerZIndex: function (layer, zIdx) {
layer.div.style.zIndex =
this.Z_INDEX_BASE[layer.isBaseLayer ? 'BaseLayer' : 'Overlay']
+ zIdx * 5;
},
/**
* @param {OpenLayers.Layer} layer
*/
addLayer: function (layer) {
layer.div.style.overflow = "";
layer.div.style.zIndex =
this.Z_INDEX_BASE[layer.isBaseLayer ? 'BaseLayer' : 'Overlay']
+ this.layers.length * 5;
this.setLayerZIndex(layer, this.layers.length);
if (layer.isFixed) {
this.viewPortDiv.appendChild(layer.div);
@@ -347,6 +356,62 @@ OpenLayers.Map.prototype = {
this.events.triggerEvent("removelayer");
},
/**
* @returns The number of layers attached to the map.
* @type int
*/
getNumLayers: function () {
return this.layers.length;
},
/**
* @returns The current (zero-based) index of the given layer in the map's
* layer stack. Returns -1 if the layer isn't on the map.
*
* @param {OpenLayers.Layer} layer
* @type int
*/
getLayerIndex: function (layer) {
return OpenLayers.Util.indexOf(this.layers, layer);
},
/** Move the given layer to the specified (zero-based) index in the layer
* list, changing its z-index in the map display. Use
* map.getLayerIndex() to find out the current index of a layer. Note
* that this cannot (or at least should not) be effectively used to
* raise base layers above overlays.
*
* @param {OpenLayers.Layer} layer
* @param {int} idx
*/
setLayerIndex: function (layer, idx) {
var base = this.getLayerIndex(layer);
if (idx < 0)
idx = 0;
else if (idx > this.layers.length)
idx = this.layers.length;
if (base != idx) {
this.layers.splice(base, 1);
this.layers.splice(idx, 0, layer);
for (var i = 0; i < this.layers.length; i++)
this.setLayerZIndex(this.layers[i], i);
this.events.triggerEvent("changelayer");
}
},
/** Change the index of the given layer by delta. If delta is positive,
* the layer is moved up the map's layer stack; if delta is negative,
* the layer is moved down. Again, note that this cannot (or at least
* should not) be effectively used to raise base layers above overlays.
*
* @param {OpenLayers.Layer} layer
* @param {int} idx
*/
raiseLayer: function (layer, delta) {
var idx = this.getLayerIndex(layer) + delta;
this.setLayerIndex(layer, idx);
},
/** Allows user to specify one of the currently-loaded layers as the Map's
* new base layer.
*

View File

@@ -249,7 +249,46 @@
}
function test_088_Map_setCenter(t) {
function test_12_Map_moveLayer (t) {
t.plan(10);
var ct = 0;
map = new OpenLayers.Map($('map'));
var wmslayer = new OpenLayers.Layer.WMS('Test Layer',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
var wmslayer2 = new OpenLayers.Layer.WMS('Test Layer2',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
var wmslayer3 = new OpenLayers.Layer.WMS('Test Layer2',
"http://octo.metacarta.com/cgi-bin/mapserv",
{map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'},
{maxExtent: new OpenLayers.Bounds(33861, 717605, 330846, 1019656), maxResolution: 296985/1024, projection:"EPSG:2805" } );
map.addLayers([wmslayer, wmslayer2, wmslayer3]);
map.events.register("changelayer", map, function (e) { ct++; });
t.eq( map.getNumLayers(), 3, "getNumLayers returns the number of layers" );
t.eq( map.getLayerIndex(wmslayer3), 2, "getLayerIndex returns the right index" );
map.raiseLayer(wmslayer3, 1);
t.eq( map.getLayerIndex(wmslayer3), 2, "can't moveLayer up past the top of the stack" );
map.raiseLayer(wmslayer, -1);
t.eq( map.getLayerIndex(wmslayer), 0, "can't moveLayer down past the bottom of the stack" );
map.raiseLayer(wmslayer3, -1);
t.eq( map.getLayerIndex(wmslayer3), 1, "can moveLayer down from the top" );
t.eq( parseInt(wmslayer3.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'] + 5,
"layer div has the right zIndex after moving down" );
map.raiseLayer(wmslayer, 2);
t.eq( map.getLayerIndex(wmslayer), 2, "can moveLayer up from the bottom" );
t.eq( parseInt(wmslayer.div.style.zIndex), map.Z_INDEX_BASE['BaseLayer'] + 2 * 5,
"layer div has the right zIndex after moving up" );
t.eq( map.getLayerIndex(wmslayer3), 0, "top layer is now on the bottom" );
t.eq( ct, 3, "raiseLayer triggered changelayer the right # of times" );
}
function test_08_Map_setCenter(t) {
t.plan(1);
map = new OpenLayers.Map($('map'));
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",