Zoom.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * @requires OpenLayers/Events/buttonclick.js
  8. */
  9. /**
  10. * Class: OpenLayers.Control.Zoom
  11. * The Zoom control is a pair of +/- links for zooming in and out.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Control>
  15. */
  16. OpenLayers.Control.Zoom = OpenLayers.Class(OpenLayers.Control, {
  17. /**
  18. * APIProperty: zoomInText
  19. * {String}
  20. * Text for zoom-in link. Default is "+".
  21. */
  22. zoomInText: "+",
  23. /**
  24. * APIProperty: zoomInId
  25. * {String}
  26. * Instead of having the control create a zoom in link, you can provide
  27. * the identifier for an anchor element already added to the document.
  28. * By default, an element with id "olZoomInLink" will be searched for
  29. * and used if it exists.
  30. */
  31. zoomInId: "olZoomInLink",
  32. /**
  33. * APIProperty: zoomOutText
  34. * {String}
  35. * Text for zoom-out link. Default is "\u2212".
  36. */
  37. zoomOutText: "\u2212",
  38. /**
  39. * APIProperty: zoomOutId
  40. * {String}
  41. * Instead of having the control create a zoom out link, you can provide
  42. * the identifier for an anchor element already added to the document.
  43. * By default, an element with id "olZoomOutLink" will be searched for
  44. * and used if it exists.
  45. */
  46. zoomOutId: "olZoomOutLink",
  47. /**
  48. * Method: draw
  49. *
  50. * Returns:
  51. * {DOMElement} A reference to the DOMElement containing the zoom links.
  52. */
  53. draw: function() {
  54. var div = OpenLayers.Control.prototype.draw.apply(this),
  55. links = this.getOrCreateLinks(div),
  56. zoomIn = links.zoomIn,
  57. zoomOut = links.zoomOut,
  58. eventsInstance = this.map.events;
  59. if (zoomOut.parentNode !== div) {
  60. eventsInstance = this.events;
  61. eventsInstance.attachToElement(zoomOut.parentNode);
  62. }
  63. eventsInstance.register("buttonclick", this, this.onZoomClick);
  64. this.zoomInLink = zoomIn;
  65. this.zoomOutLink = zoomOut;
  66. return div;
  67. },
  68. /**
  69. * Method: getOrCreateLinks
  70. *
  71. * Parameters:
  72. * el - {DOMElement}
  73. *
  74. * Return:
  75. * {Object} Object with zoomIn and zoomOut properties referencing links.
  76. */
  77. getOrCreateLinks: function(el) {
  78. var zoomIn = document.getElementById(this.zoomInId),
  79. zoomOut = document.getElementById(this.zoomOutId);
  80. if (!zoomIn) {
  81. zoomIn = document.createElement("a");
  82. zoomIn.href = "#zoomIn";
  83. zoomIn.appendChild(document.createTextNode(this.zoomInText));
  84. zoomIn.className = "olControlZoomIn";
  85. el.appendChild(zoomIn);
  86. }
  87. OpenLayers.Element.addClass(zoomIn, "olButton");
  88. if (!zoomOut) {
  89. zoomOut = document.createElement("a");
  90. zoomOut.href = "#zoomOut";
  91. zoomOut.appendChild(document.createTextNode(this.zoomOutText));
  92. zoomOut.className = "olControlZoomOut";
  93. el.appendChild(zoomOut);
  94. }
  95. OpenLayers.Element.addClass(zoomOut, "olButton");
  96. return {
  97. zoomIn: zoomIn, zoomOut: zoomOut
  98. };
  99. },
  100. /**
  101. * Method: onZoomClick
  102. * Called when zoomin/out link is clicked.
  103. */
  104. onZoomClick: function(evt) {
  105. var button = evt.buttonElement;
  106. if (button === this.zoomInLink) {
  107. this.map.zoomIn();
  108. } else if (button === this.zoomOutLink) {
  109. this.map.zoomOut();
  110. }
  111. },
  112. /**
  113. * Method: destroy
  114. * Clean up.
  115. */
  116. destroy: function() {
  117. if (this.map) {
  118. this.map.events.unregister("buttonclick", this, this.onZoomClick);
  119. }
  120. delete this.zoomInLink;
  121. delete this.zoomOutLink;
  122. OpenLayers.Control.prototype.destroy.apply(this);
  123. },
  124. CLASS_NAME: "OpenLayers.Control.Zoom"
  125. });