RootContainer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/Layer/Vector.js
  7. */
  8. /**
  9. * Class: OpenLayers.Layer.Vector.RootContainer
  10. * A special layer type to combine multiple vector layers inside a single
  11. * renderer root container. This class is not supposed to be instantiated
  12. * from user space, it is a helper class for controls that require event
  13. * processing for multiple vector layers.
  14. *
  15. * Inherits from:
  16. * - <OpenLayers.Layer.Vector>
  17. */
  18. OpenLayers.Layer.Vector.RootContainer = OpenLayers.Class(OpenLayers.Layer.Vector, {
  19. /**
  20. * Property: displayInLayerSwitcher
  21. * Set to false for this layer type
  22. */
  23. displayInLayerSwitcher: false,
  24. /**
  25. * APIProperty: layers
  26. * Layers that are attached to this container. Required config option.
  27. */
  28. layers: null,
  29. /**
  30. * Constructor: OpenLayers.Layer.Vector.RootContainer
  31. * Create a new root container for multiple vector layer. This constructor
  32. * is not supposed to be used from user space, it is only to be used by
  33. * controls that need feature selection across multiple vector layers.
  34. *
  35. * Parameters:
  36. * name - {String} A name for the layer
  37. * options - {Object} Optional object with non-default properties to set on
  38. * the layer.
  39. *
  40. * Required options properties:
  41. * layers - {Array(<OpenLayers.Layer.Vector>)} The layers managed by this
  42. * container
  43. *
  44. * Returns:
  45. * {<OpenLayers.Layer.Vector.RootContainer>} A new vector layer root
  46. * container
  47. */
  48. /**
  49. * Method: display
  50. */
  51. display: function() {},
  52. /**
  53. * Method: getFeatureFromEvent
  54. * walk through the layers to find the feature returned by the event
  55. *
  56. * Parameters:
  57. * evt - {Object} event object with a feature property
  58. *
  59. * Returns:
  60. * {<OpenLayers.Feature.Vector>}
  61. */
  62. getFeatureFromEvent: function(evt) {
  63. var layers = this.layers;
  64. var feature;
  65. for(var i=0; i<layers.length; i++) {
  66. feature = layers[i].getFeatureFromEvent(evt);
  67. if(feature) {
  68. return feature;
  69. }
  70. }
  71. },
  72. /**
  73. * Method: setMap
  74. *
  75. * Parameters:
  76. * map - {<OpenLayers.Map>}
  77. */
  78. setMap: function(map) {
  79. OpenLayers.Layer.Vector.prototype.setMap.apply(this, arguments);
  80. this.collectRoots();
  81. map.events.register("changelayer", this, this.handleChangeLayer);
  82. },
  83. /**
  84. * Method: removeMap
  85. *
  86. * Parameters:
  87. * map - {<OpenLayers.Map>}
  88. */
  89. removeMap: function(map) {
  90. map.events.unregister("changelayer", this, this.handleChangeLayer);
  91. this.resetRoots();
  92. OpenLayers.Layer.Vector.prototype.removeMap.apply(this, arguments);
  93. },
  94. /**
  95. * Method: collectRoots
  96. * Collects the root nodes of all layers this control is configured with
  97. * and moveswien the nodes to this control's layer
  98. */
  99. collectRoots: function() {
  100. var layer;
  101. // walk through all map layers, because we want to keep the order
  102. for(var i=0; i<this.map.layers.length; ++i) {
  103. layer = this.map.layers[i];
  104. if(OpenLayers.Util.indexOf(this.layers, layer) != -1) {
  105. layer.renderer.moveRoot(this.renderer);
  106. }
  107. }
  108. },
  109. /**
  110. * Method: resetRoots
  111. * Resets the root nodes back into the layers they belong to.
  112. */
  113. resetRoots: function() {
  114. var layer;
  115. for(var i=0; i<this.layers.length; ++i) {
  116. layer = this.layers[i];
  117. if(this.renderer && layer.renderer.getRenderLayerId() == this.id) {
  118. this.renderer.moveRoot(layer.renderer);
  119. }
  120. }
  121. },
  122. /**
  123. * Method: handleChangeLayer
  124. * Event handler for the map's changelayer event. We need to rebuild
  125. * this container's layer dom if order of one of its layers changes.
  126. * This handler is added with the setMap method, and removed with the
  127. * removeMap method.
  128. *
  129. * Parameters:
  130. * evt - {Object}
  131. */
  132. handleChangeLayer: function(evt) {
  133. var layer = evt.layer;
  134. if(evt.property == "order" &&
  135. OpenLayers.Util.indexOf(this.layers, layer) != -1) {
  136. this.resetRoots();
  137. this.collectRoots();
  138. }
  139. },
  140. CLASS_NAME: "OpenLayers.Layer.Vector.RootContainer"
  141. });