Box.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/Marker.js
  7. */
  8. /**
  9. * Class: OpenLayers.Marker.Box
  10. *
  11. * Inherits from:
  12. * - <OpenLayers.Marker>
  13. */
  14. OpenLayers.Marker.Box = OpenLayers.Class(OpenLayers.Marker, {
  15. /**
  16. * Property: bounds
  17. * {<OpenLayers.Bounds>}
  18. */
  19. bounds: null,
  20. /**
  21. * Property: div
  22. * {DOMElement}
  23. */
  24. div: null,
  25. /**
  26. * Constructor: OpenLayers.Marker.Box
  27. *
  28. * Parameters:
  29. * bounds - {<OpenLayers.Bounds>}
  30. * borderColor - {String}
  31. * borderWidth - {int}
  32. */
  33. initialize: function(bounds, borderColor, borderWidth) {
  34. this.bounds = bounds;
  35. this.div = OpenLayers.Util.createDiv();
  36. this.div.style.overflow = 'hidden';
  37. this.events = new OpenLayers.Events(this, this.div);
  38. this.setBorder(borderColor, borderWidth);
  39. },
  40. /**
  41. * Method: destroy
  42. */
  43. destroy: function() {
  44. this.bounds = null;
  45. this.div = null;
  46. OpenLayers.Marker.prototype.destroy.apply(this, arguments);
  47. },
  48. /**
  49. * Method: setBorder
  50. * Allow the user to change the box's color and border width
  51. *
  52. * Parameters:
  53. * color - {String} Default is "red"
  54. * width - {int} Default is 2
  55. */
  56. setBorder: function (color, width) {
  57. if (!color) {
  58. color = "red";
  59. }
  60. if (!width) {
  61. width = 2;
  62. }
  63. this.div.style.border = width + "px solid " + color;
  64. },
  65. /**
  66. * Method: draw
  67. *
  68. * Parameters:
  69. * px - {<OpenLayers.Pixel>}
  70. * sz - {<OpenLayers.Size>}
  71. *
  72. * Returns:
  73. * {DOMElement} A new DOM Image with this marker's icon set at the
  74. * location passed-in
  75. */
  76. draw: function(px, sz) {
  77. OpenLayers.Util.modifyDOMElement(this.div, null, px, sz);
  78. return this.div;
  79. },
  80. /**
  81. * Method: onScreen
  82. *
  83. * Rreturn:
  84. * {Boolean} Whether or not the marker is currently visible on screen.
  85. */
  86. onScreen:function() {
  87. var onScreen = false;
  88. if (this.map) {
  89. var screenBounds = this.map.getExtent();
  90. onScreen = screenBounds.containsBounds(this.bounds, true, true);
  91. }
  92. return onScreen;
  93. },
  94. /**
  95. * Method: display
  96. * Hide or show the icon
  97. *
  98. * Parameters:
  99. * display - {Boolean}
  100. */
  101. display: function(display) {
  102. this.div.style.display = (display) ? "" : "none";
  103. },
  104. CLASS_NAME: "OpenLayers.Marker.Box"
  105. });