Attribution.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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: separator
  20. * {String} String used to separate layers.
  21. */
  22. separator: ", ",
  23. /**
  24. * APIProperty: template
  25. * {String} Template for the attribution. This has to include the substring
  26. * "${layers}", which will be replaced by the layer specific
  27. * attributions, separated by <separator>. The default is "${layers}".
  28. */
  29. template: "${layers}",
  30. /**
  31. * Constructor: OpenLayers.Control.Attribution
  32. *
  33. * Parameters:
  34. * options - {Object} Options for control.
  35. */
  36. /**
  37. * Method: destroy
  38. * Destroy control.
  39. */
  40. destroy: function() {
  41. this.map.events.un({
  42. "removelayer": this.updateAttribution,
  43. "addlayer": this.updateAttribution,
  44. "changelayer": this.updateAttribution,
  45. "changebaselayer": this.updateAttribution,
  46. scope: this
  47. });
  48. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  49. },
  50. /**
  51. * Method: draw
  52. * Initialize control.
  53. *
  54. * Returns:
  55. * {DOMElement} A reference to the DIV DOMElement containing the control
  56. */
  57. draw: function() {
  58. OpenLayers.Control.prototype.draw.apply(this, arguments);
  59. this.map.events.on({
  60. 'changebaselayer': this.updateAttribution,
  61. 'changelayer': this.updateAttribution,
  62. 'addlayer': this.updateAttribution,
  63. 'removelayer': this.updateAttribution,
  64. scope: this
  65. });
  66. this.updateAttribution();
  67. return this.div;
  68. },
  69. /**
  70. * Method: updateAttribution
  71. * Update attribution string.
  72. */
  73. updateAttribution: function() {
  74. var attributions = [];
  75. if (this.map && this.map.layers) {
  76. for(var i=0, len=this.map.layers.length; i<len; i++) {
  77. var layer = this.map.layers[i];
  78. if (layer.attribution && layer.getVisibility()) {
  79. // add attribution only if attribution text is unique
  80. if (OpenLayers.Util.indexOf(
  81. attributions, layer.attribution) === -1) {
  82. attributions.push( layer.attribution );
  83. }
  84. }
  85. }
  86. this.div.innerHTML = OpenLayers.String.format(this.template, {
  87. layers: attributions.join(this.separator)
  88. });
  89. }
  90. },
  91. CLASS_NAME: "OpenLayers.Control.Attribution"
  92. });