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