Adding an option to avoid duplicate attribution.
Thanks Marc Jansen for this nice contribution. Without wanting to gush, I think this should be a model contribution because: 1. it is a simple change 2. with a nice example that clearly demonstrates the benefit 3. it includes tests! 4. and Marc wrote a note to the dev list with links to the ticket r = me (closes #2266) git-svn-id: http://svn.openlayers.org/trunk/openlayers@9720 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -0,0 +1,78 @@
|
|||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<title>OpenLayers Attribution Example (attribute eliminateDuplicates)</title>
|
||||||
|
<link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
|
||||||
|
<link rel="stylesheet" href="style.css" type="text/css" />
|
||||||
|
<script src="../lib/OpenLayers.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
// making this a global variable so that it is accessible for
|
||||||
|
// debugging/inspecting in Firebug
|
||||||
|
var map = null;
|
||||||
|
|
||||||
|
function init(){
|
||||||
|
|
||||||
|
map = new OpenLayers.Map('map', {controls: []});
|
||||||
|
map2 = new OpenLayers.Map('map2', {controls: []});
|
||||||
|
|
||||||
|
var ol_wms = new OpenLayers.Layer.WMS(
|
||||||
|
"OpenLayers WMS",
|
||||||
|
"http://labs.metacarta.com/wms/vmap0",
|
||||||
|
{layers: 'basic'},
|
||||||
|
{attribution: '© MetaCarta'}
|
||||||
|
);
|
||||||
|
var ol_wms2 = new OpenLayers.Layer.WMS(
|
||||||
|
"OpenLayers WMS",
|
||||||
|
"http://labs.metacarta.com/wms/vmap0",
|
||||||
|
{layers: 'basic', transparent: 'true'},
|
||||||
|
{attribution: '© MetaCarta'}
|
||||||
|
);
|
||||||
|
var ol_wms3 = new OpenLayers.Layer.WMS(
|
||||||
|
"OpenLayers WMS",
|
||||||
|
"http://labs.metacarta.com/wms/vmap0",
|
||||||
|
{layers: 'basic', transparent: 'true'},
|
||||||
|
{attribution: '© other company'}
|
||||||
|
);
|
||||||
|
var ol_wms4 = new OpenLayers.Layer.WMS(
|
||||||
|
"OpenLayers WMS",
|
||||||
|
"http://labs.metacarta.com/wms/vmap0",
|
||||||
|
{layers: 'basic', transparent: 'true'},
|
||||||
|
{attribution: '© other company'}
|
||||||
|
);
|
||||||
|
var ol_wms5 = new OpenLayers.Layer.WMS(
|
||||||
|
"OpenLayers WMS",
|
||||||
|
"http://labs.metacarta.com/wms/vmap0",
|
||||||
|
{layers: 'basic', transparent: 'true'},
|
||||||
|
{attribution: '© MetaCarta'}
|
||||||
|
);
|
||||||
|
|
||||||
|
map.addLayers([ol_wms, ol_wms2, ol_wms3, ol_wms4, ol_wms5]);
|
||||||
|
map2.addLayers([ol_wms.clone(), ol_wms2.clone(), ol_wms3.clone(), ol_wms4.clone(), ol_wms5.clone()]);
|
||||||
|
|
||||||
|
var attrWithDuplicates = new OpenLayers.Control.Attribution();
|
||||||
|
var attrWithoutDuplicates = new OpenLayers.Control.Attribution( {
|
||||||
|
eliminateDuplicates: true
|
||||||
|
} );
|
||||||
|
|
||||||
|
map.addControl(attrWithDuplicates);
|
||||||
|
map.addControl(new OpenLayers.Control.LayerSwitcher());
|
||||||
|
map.zoomToMaxExtent();
|
||||||
|
|
||||||
|
map2.addControl(attrWithoutDuplicates);
|
||||||
|
map2.addControl(new OpenLayers.Control.LayerSwitcher());
|
||||||
|
map2.zoomToMaxExtent();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body onload="init()">
|
||||||
|
<h1 id="title">OpenLayers Attribution Example (attribute <code>eliminateDuplicates</code>)</h1>
|
||||||
|
<div id="tags"></div>
|
||||||
|
<p id="shortdesc">
|
||||||
|
Demonstrates the use of the attribute <code>eliminateDuplicates</code> for Attribution-controls
|
||||||
|
</p>
|
||||||
|
<h2>Standard Usage of the control</h2>
|
||||||
|
<div id="map" class="smallmap"></div>
|
||||||
|
<h2>Use the attribute <code>eliminateDuplicates</code> to get rid of redundant Information</h2>
|
||||||
|
<div id="map2" class="smallmap"></div>
|
||||||
|
<div id="docs"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -23,6 +23,12 @@ OpenLayers.Control.Attribution =
|
|||||||
*/
|
*/
|
||||||
separator: ", ",
|
separator: ", ",
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIProperty: eliminateDuplicates
|
||||||
|
* {Boolean} shall already present attribution strings be eliminated when other layers have the same attribution string?
|
||||||
|
*/
|
||||||
|
eliminateDuplicates: false,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor: OpenLayers.Control.Attribution
|
* Constructor: OpenLayers.Control.Attribution
|
||||||
*
|
*
|
||||||
@@ -81,7 +87,11 @@ OpenLayers.Control.Attribution =
|
|||||||
for(var i=0, len=this.map.layers.length; i<len; i++) {
|
for(var i=0, len=this.map.layers.length; i<len; i++) {
|
||||||
var layer = this.map.layers[i];
|
var layer = this.map.layers[i];
|
||||||
if (layer.attribution && layer.getVisibility()) {
|
if (layer.attribution && layer.getVisibility()) {
|
||||||
attributions.push( layer.attribution );
|
// add attribution if duplicates shall be ignored generally
|
||||||
|
// or if the current attribution string is unique
|
||||||
|
if (!this.eliminateDuplicates || OpenLayers.Util.indexOf(attributions, layer.attribution) === -1) {
|
||||||
|
attributions.push( layer.attribution );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.div.innerHTML = attributions.join(this.separator);
|
this.div.innerHTML = attributions.join(this.separator);
|
||||||
|
|||||||
@@ -35,6 +35,26 @@
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_Control_Attribution_Propertry_eliminateDuplicates(t) {
|
||||||
|
t.plan(3);
|
||||||
|
|
||||||
|
control = new OpenLayers.Control.Attribution();
|
||||||
|
map = new OpenLayers.Map("map");
|
||||||
|
map.addControl(control);
|
||||||
|
map.addLayer(new OpenLayers.Layer("Company A: 1",{'attribution':'company A'}));
|
||||||
|
map.addLayer(new OpenLayers.Layer("Company A: 2",{'attribution':'company A'}));
|
||||||
|
t.eq(control.div.innerHTML, 'company A, company A', "Attribution correct with two layers and eliminateDuplicates = false.");
|
||||||
|
|
||||||
|
control.destroy();
|
||||||
|
control = new OpenLayers.Control.Attribution({eliminateDuplicates:true});
|
||||||
|
map.addControl(control);
|
||||||
|
t.eq(control.div.innerHTML, 'company A', "Attribution correct with two layers and eliminateDuplicates = true.");
|
||||||
|
|
||||||
|
map.addLayer(new OpenLayers.Layer("Company B: 1",{'attribution':'company B'}));
|
||||||
|
map.addLayer(new OpenLayers.Layer("Company A: 3",{'attribution':'company A'}));
|
||||||
|
t.eq(control.div.innerHTML, 'company A, company B', "Attribution correct with four layers (3 with same attribution) and eliminateDuplicates = true.");
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user