Files
openlayers/lib/OpenLayers/Control/Attribution.js
Tim Schaub d1cecf3be1 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
2009-10-06 18:46:06 +00:00

103 lines
3.0 KiB
JavaScript

/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
*/
/**
* Class: OpenLayers.Control.Attribution
* The attribution control adds attribution from layers to the map display.
* It uses 'attribution' property of each layer.
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.Attribution =
OpenLayers.Class(OpenLayers.Control, {
/**
* APIProperty: seperator
* {String} String used to seperate layers.
*/
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
*
* Parameters:
* options - {Object} Options for control.
*/
initialize: function(options) {
OpenLayers.Control.prototype.initialize.apply(this, arguments);
},
/**
* Method: destroy
* Destroy control.
*/
destroy: function() {
this.map.events.un({
"removelayer": this.updateAttribution,
"addlayer": this.updateAttribution,
"changelayer": this.updateAttribution,
"changebaselayer": this.updateAttribution,
scope: this
});
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
/**
* Method: draw
* Initialize control.
*
* Returns:
* {DOMElement} A reference to the DIV DOMElement containing the control
*/
draw: function() {
OpenLayers.Control.prototype.draw.apply(this, arguments);
this.map.events.on({
'changebaselayer': this.updateAttribution,
'changelayer': this.updateAttribution,
'addlayer': this.updateAttribution,
'removelayer': this.updateAttribution,
scope: this
});
this.updateAttribution();
return this.div;
},
/**
* Method: updateAttribution
* Update attribution string.
*/
updateAttribution: function() {
var attributions = [];
if (this.map && this.map.layers) {
for(var i=0, len=this.map.layers.length; i<len; i++) {
var layer = this.map.layers[i];
if (layer.attribution && layer.getVisibility()) {
// 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);
}
},
CLASS_NAME: "OpenLayers.Control.Attribution"
});