Attribution.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Control.js
  7. */
  8. /**
  9. * Class: OpenLayers.Control.Attribution
  10. * The attribution control adds attribution from layers to the map display.
  11. * It uses 'attribution' property of each layer.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Control>
  15. */
  16. OpenLayers.Control.Attribution =
  17. OpenLayers.Class(OpenLayers.Control, {
  18. /**
  19. * APIProperty: seperator
  20. * {String} String used to seperate layers.
  21. */
  22. separator: ", ",
  23. /**
  24. * Constructor: OpenLayers.Control.Attribution
  25. *
  26. * Parameters:
  27. * options - {Object} Options for control.
  28. */
  29. /**
  30. * Method: destroy
  31. * Destroy control.
  32. */
  33. destroy: function() {
  34. this.map.events.un({
  35. "removelayer": this.updateAttribution,
  36. "addlayer": this.updateAttribution,
  37. "changelayer": this.updateAttribution,
  38. "changebaselayer": this.updateAttribution,
  39. scope: this
  40. });
  41. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  42. },
  43. /**
  44. * Method: draw
  45. * Initialize control.
  46. *
  47. * Returns:
  48. * {DOMElement} A reference to the DIV DOMElement containing the control
  49. */
  50. draw: function() {
  51. OpenLayers.Control.prototype.draw.apply(this, arguments);
  52. this.map.events.on({
  53. 'changebaselayer': this.updateAttribution,
  54. 'changelayer': this.updateAttribution,
  55. 'addlayer': this.updateAttribution,
  56. 'removelayer': this.updateAttribution,
  57. scope: this
  58. });
  59. this.updateAttribution();
  60. return this.div;
  61. },
  62. /**
  63. * Method: updateAttribution
  64. * Update attribution string.
  65. */
  66. updateAttribution: function() {
  67. var attributions = [];
  68. if (this.map && this.map.layers) {
  69. for(var i=0, len=this.map.layers.length; i<len; i++) {
  70. var layer = this.map.layers[i];
  71. if (layer.attribution && layer.getVisibility()) {
  72. // add attribution only if attribution text is unique
  73. if (OpenLayers.Util.indexOf(
  74. attributions, layer.attribution) === -1) {
  75. attributions.push( layer.attribution );
  76. }
  77. }
  78. }
  79. this.div.innerHTML = attributions.join(this.separator);
  80. }
  81. },
  82. CLASS_NAME: "OpenLayers.Control.Attribution"
  83. });