source: core/trunk/lib/GeoExt/data/LayerStore.js @ 238

Last change on this file since 238 was 238, checked in by ahocevar, 9 years ago

Added MapPanel, LayerRecord and LayerStore with examples and tests; also contains license.txt file. Big thanks to sbenthall and tcoulter for their original input, to elemoine for the good discussion today about the LayerStoreMixin and his patience despite my angryness, to tschaub for the constructive comments, and finally to everyone who participated in the discussions on the mailing list about what we want this component to be. r=elemoine,tschaub (closes #4)

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     * APIProperty: recordType
43     * {<GeoExt.data.LayerRecord>} type of the layer records used by this
44     *     store.
45     */
46    recordType: null,
47
48    /**
49     * Constructor: GeoExt.LayerStore
50     *
51     * Parameters:
52     * config - {Object}
53     *
54     * Valid config options:
55     * map - {OpenLayers.Map|<GeoExt.MapPanel>} map to sync the layer store
56     *     with.
57     * layers - {Array(OpenLayers.Layer)} Layers that will be added to the
58     *     layer store (and the map, because we are already syncing).
59     * recordType - {<GeoExt.data.LayerRecord>} If provided, a custom layer
60     *     record type with additional fields will be used. Default fields for
61     *     every layer record are {OpenLayers.Layer} layer and {String} title.
62     */
63    constructor: function(config) {
64        arguments.callee.superclass.constructor.apply(this, arguments);
65        config = config || {};
66        this.recordType = config.recordType || GeoExt.data.LayerRecord.create();
67        var map = config.map instanceof GeoExt.MapPanel ?
68            config.map.map : config.map;
69        if(map) {
70            // create a snapshop of the map's layers
71            var layers = map.layers;
72            var layer;
73            // walk through the layers snapshot and add layers to the store
74            for(var i=0; i<layers.length; ++i) {
75                layer = layers[i];
76                this.add(new this.recordType(layer));
77            }
78
79            this.setMap(map);
80            config.layers && map.addLayers(config.layers);
81        }
82    },
83   
84    /**
85     * APIMethod: setMap
86     *
87     * Parameters:
88     * map - {OpenLayers.Map}
89     */
90    setMap: function(map) {
91        this.map = map;
92        map.events.on({
93            "addlayer": this.onAddLayer,
94            "removelayer": this.onRemoveLayer,
95            scope: this
96        });
97        this.on({
98            "add": this.onAdd,
99            "remove": this.onRemove,
100            scope: this
101        });
102    },
103   
104    /**
105     * Method: onAddLayer
106     * Handler for a map's addlayer event
107     *
108     * Parameters:
109     * evt - {Object}
110     */
111    onAddLayer: function(evt) {
112        var layer = evt.layer;
113        this._adding = true;
114        this.add(new this.recordType(layer));
115        delete this._adding;
116    },
117   
118    /**
119     * Method: onRemoveLayer
120     * Handler for a map's removelayer event
121     *
122     * Parameters:
123     * evt - {Object}
124     */
125    onRemoveLayer: function(evt){
126        var layer = evt.layer;
127        this._removing = true;
128        this.remove(this.getById(layer.id));
129        delete this._removing;
130    },
131   
132    /**
133     * Method: onAdd
134     * Handler for a store's add event
135     *
136     * Parameters:
137     * store - {<Ext.data.Store>}
138     * records - {Array(Ext.data.Record)}
139     * index - {Number}
140     */
141    onAdd: function(store, records, index) {
142        if(!this._adding) {
143            for(var i=0; i<records.length; ++i) {
144                this.map.addLayer(records[i].get("layer"));
145            }
146        }
147    },
148   
149    /**
150     * Method: onRemove
151     * Handler for a store's remove event
152     *
153     * Parameters:
154     * store - {<Ext.data.Store>}
155     * records - {Array(Ext.data.Record)}
156     * index - {Number}
157     */
158    onRemove: function(store, record, index){
159        if(!this._removing) {
160            this.map.removeLayer(record.get("layer"));
161        }
162    }
163};
164
165/**
166 * Class: GeoExt.data.LayerStore
167 * Default implementation of an {Ext.data.Store} enhanced with
168 * {<GeoExt.data.LayerStoreMixin>}
169 */
170GeoExt.data.LayerStore = Ext.extend(
171    Ext.data.Store,
172    GeoExt.data.LayerStoreMixin
173);
Note: See TracBrowser for help on using the repository browser.