source: sandbox/opengeo/geoexplorer/lib/GeoExt/data/LayerStore.js @ 264

Last change on this file since 264 was 264, checked in by tschaub, 9 years ago

merge r246:HEAD from trunk

File size: 4.9 KB
Line 
1/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation ¹
2 * Published under the BSD license.
3 * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
4 * of the license.
5 *
6 * ¹ pending approval */
7
8Ext.namespace("GeoExt.data");
9
10/**
11 * Class: LayerStoreMixin
12 * A store that synchronizes a layers array of an {OpenLayers.Map} with a
13 * layer store holding {<GeoExt.data.LayerRecord>} entries.
14 *
15 * This class can not be instantiated directly. Instead, it is meant to extend
16 * {Ext.data.Store} or a subclass of it:
17 * (start code)
18 * var store = new (Ext.extend(Ext.data.Store, GeoExt.data.LayerStoreMixin))({
19 *     map: myMap,
20 *     layers: myLayers
21 * });
22 * (end)
23 *
24 * For convenience, a {GeoExt.data.LayerStore} class is available as a
25 * shortcut to the Ext.extend sequence in the above code snippet. The above
26 * is equivalent to:
27 * (start code)
28 * var store = new GeoExt.data.LayerStore({
29 *     map: myMap,
30 *     layers: myLayers
31 * })
32 * (end)
33 */
34GeoExt.data.LayerStoreMixin = {
35    /**
36     * APIProperty: map
37     * {OpenLayers.Map} Map that this store will be in sync with.
38     */
39    map: null,
40
41    /**
42     * Property: reader
43     * {<GeoExt.data.LayerReader>} The reader used to get
44     *     <GeoExt.data.LayerRecord> objects from {OpenLayers.Layer}
45     *     objects.
46     */
47    reader: null,
48
49    /**
50     * Constructor: GeoExt.LayerStore
51     *
52     * Parameters:
53     * config - {Object}
54     *
55     * Valid config options:
56     * map - {OpenLayers.Map|<GeoExt.MapPanel>} map to sync the layer store
57     *     with.
58     * layers - {Array(OpenLayers.Layer)} Layers that will be added to the
59     *     layer store (and the map, because we are already syncing).
60     * recordType - {<GeoExt.data.LayerRecord>} If provided, a custom layer
61     *     record type with additional fields will be used. Default fields for
62     *     every layer record are {OpenLayers.Layer} layer and {String} title.
63     */
64    constructor: function(config) {
65        arguments.callee.superclass.constructor.apply(this, arguments);
66        config = config || {};
67        this.reader = new GeoExt.data.LayerReader({}, config.recordType);
68        var map = config.map instanceof GeoExt.MapPanel ?
69            config.map.map : config.map;
70        if(map) {
71            // create a snapshop of the map's layers
72            var layers = map.layers;
73            var layer;
74            // walk through the layers snapshot and add layers to the store
75            for(var i=0; i<layers.length; ++i) {
76                layer = layers[i];
77                this.add((this.reader.readRecords([layer])).records);
78            }
79
80            this.setMap(map);
81            config.layers && map.addLayers(config.layers);
82        }
83    },
84   
85    /**
86     * APIMethod: setMap
87     *
88     * Parameters:
89     * map - {OpenLayers.Map}
90     */
91    setMap: function(map) {
92        this.map = map;
93        map.events.on({
94            "addlayer": this.onAddLayer,
95            "removelayer": this.onRemoveLayer,
96            scope: this
97        });
98        this.on({
99            "add": this.onAdd,
100            "remove": this.onRemove,
101            scope: this
102        });
103    },
104   
105    /**
106     * Method: onAddLayer
107     * Handler for a map's addlayer event
108     *
109     * Parameters:
110     * evt - {Object}
111     */
112    onAddLayer: function(evt) {
113        var layer = evt.layer;
114        this._adding = true;
115        this.add((this.reader.readRecords([layer])).records);
116        delete this._adding;
117    },
118   
119    /**
120     * Method: onRemoveLayer
121     * Handler for a map's removelayer event
122     *
123     * Parameters:
124     * evt - {Object}
125     */
126    onRemoveLayer: function(evt){
127        var layer = evt.layer;
128        this._removing = true;
129        this.remove(this.getById(layer.id));
130        delete this._removing;
131    },
132   
133    /**
134     * Method: onAdd
135     * Handler for a store's add event
136     *
137     * Parameters:
138     * store - {<Ext.data.Store>}
139     * records - {Array(Ext.data.Record)}
140     * index - {Number}
141     */
142    onAdd: function(store, records, index) {
143        if(!this._adding) {
144            for(var i=0; i<records.length; ++i) {
145                this.map.addLayer(records[i].get("layer"));
146            }
147        }
148    },
149   
150    /**
151     * Method: onRemove
152     * Handler for a store's remove event
153     *
154     * Parameters:
155     * store - {<Ext.data.Store>}
156     * records - {Array(Ext.data.Record)}
157     * index - {Number}
158     */
159    onRemove: function(store, record, index){
160        if(!this._removing) {
161            this.map.removeLayer(record.get("layer"));
162        }
163    }
164};
165
166/**
167 * Class: GeoExt.data.LayerStore
168 * Default implementation of an {Ext.data.Store} enhanced with
169 * {<GeoExt.data.LayerStoreMixin>}
170 */
171GeoExt.data.LayerStore = Ext.extend(
172    Ext.data.Store,
173    GeoExt.data.LayerStoreMixin
174);
Note: See TracBrowser for help on using the repository browser.