Bienvenue sur PostGIS.fr

Bienvenue sur PostGIS.fr , le site de la communauté des utilisateurs francophones de PostGIS.

PostGIS ajoute le support d'objets géographique à la base de données PostgreSQL. En effet, PostGIS "spatialise" le serverur PostgreSQL, ce qui permet de l'utiliser comme une base de données SIG.

Maintenu à jour, en fonction de nos disponibilités et des diverses sorties des outils que nous testons, nous vous proposons l'ensemble de nos travaux publiés en langue française.

source: trunk/workshop-routing-foss4g/web/GeoExt/lib/GeoExt/data/LayerStore.js @ 76

Revision 76, 15.2 KB checked in by djay, 12 years ago (diff)

Ajout du répertoire web

  • Property svn:executable set to *
Line 
1/**
2 * Copyright (c) 2008-2010 The Open Source Geospatial Foundation
3 *
4 * Published under the BSD license.
5 * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
6 * of the license.
7 */
8
9/**
10 * @include GeoExt/data/LayerReader.js
11 */
12
13/** api: (define)
14 *  module = GeoExt.data
15 *  class = LayerStore
16 *  base_link = `Ext.data.Store <http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.Store>`_
17 */
18Ext.namespace("GeoExt.data");
19
20/** private: constructor
21 *  .. class:: LayerStoreMixin
22 *      A store that synchronizes a layers array of an {OpenLayers.Map} with a
23 *      layer store holding {<GeoExt.data.LayerRecord>} entries.
24 *
25 *      This class can not be instantiated directly. Instead, it is meant to
26 *      extend ``Ext.data.Store`` or a subclass of it.
27 */
28
29/** private: example
30 *  Sample code to extend a store with the LayerStoreMixin.
31 *
32 *  .. code-block:: javascript
33 * 
34 *      var store = new (Ext.extend(Ext.data.Store, new GeoExt.data.LayerStoreMixin))({
35 *          map: myMap,
36 *          layers: myLayers
37 *      });
38 *
39 *  For convenience, a :class:`GeoExt.data.LayerStore` class is available as a
40 *  shortcut to the ``Ext.extend`` sequence in the above code snippet.
41 */
42
43GeoExt.data.LayerStoreMixin = function() {
44    return {
45        /** api: config[map]
46         *  ``OpenLayers.Map``
47         *  Map that this store will be in sync with. If not provided, the
48         *  store will not be bound to a map.
49         */
50       
51        /** api: property[map]
52         *  ``OpenLayers.Map``
53         *  Map that the store is synchronized with, if any.
54         */
55        map: null,
56       
57        /** api: config[layers]
58         *  ``Array(OpenLayers.Layer)``
59         *  Layers that will be added to the store (and the map, depending on the
60         *  value of the ``initDir`` option.
61         */
62       
63        /** api: config[initDir]
64         *  ``Number``
65         *  Bitfields specifying the direction to use for the initial sync between
66         *  the map and the store, if set to 0 then no initial sync is done.
67         *  Defaults to ``GeoExt.data.LayerStore.MAP_TO_STORE|GeoExt.data.LayerStore.STORE_TO_MAP``
68         */
69
70        /** api: config[fields]
71         *  ``Array``
72         *  If provided a custom layer record type with additional fields will be
73         *  used. Default fields for every layer record are `layer`
74         *  (``OpenLayers.Layer``) `title` (``String``). The value of this option is
75         *  either a field definition objects as passed to the
76         *  :meth:`GeoExt.data.LayerRecord.create` function or a
77         *  :class:`GeoExt.data.LayerRecord` constructor created using
78         *  :meth:`GeoExt.data.LayerRecord.create`.
79         */
80
81        /** api: config[reader]
82         *  ``Ext.data.DataReader`` The reader used to produce
83         *  :class:`GeoExt.data.LayerRecord` objects from ``OpenLayers.Layer``
84         *  objects.  If not provided, a :class:`GeoExt.data.LayerReader` will be
85         *  used.
86         */
87        reader: null,
88
89        /** private: method[constructor]
90         */
91        constructor: function(config) {
92            config = config || {};
93            config.reader = config.reader ||
94                            new GeoExt.data.LayerReader({}, config.fields);
95            delete config.fields;
96            // "map" option
97            var map = config.map instanceof GeoExt.MapPanel ?
98                      config.map.map : config.map;
99            delete config.map;
100            // "layers" option - is an alias to "data" option
101            if(config.layers) {
102                config.data = config.layers;
103            }
104            delete config.layers;
105            // "initDir" option
106            var options = {initDir: config.initDir};
107            delete config.initDir;
108            arguments.callee.superclass.constructor.call(this, config);
109            if(map) {
110                this.bind(map, options);
111            }
112        },
113
114        /** private: method[bind]
115         *  :param map: ``OpenLayers.Map`` The map instance.
116         *  :param options: ``Object``
117         * 
118         *  Bind this store to a map instance, once bound the store
119         *  is synchronized with the map and vice-versa.
120         */
121        bind: function(map, options) {
122            if(this.map) {
123                // already bound
124                return;
125            }
126            this.map = map;
127            options = options || {};
128
129            var initDir = options.initDir;
130            if(options.initDir == undefined) {
131                initDir = GeoExt.data.LayerStore.MAP_TO_STORE |
132                          GeoExt.data.LayerStore.STORE_TO_MAP;
133            }
134
135            // create a snapshot of the map's layers
136            var layers = map.layers.slice(0);
137
138            if(initDir & GeoExt.data.LayerStore.STORE_TO_MAP) {
139                this.each(function(record) {
140                    this.map.addLayer(record.getLayer());
141                }, this);
142            }
143            if(initDir & GeoExt.data.LayerStore.MAP_TO_STORE) {
144                this.loadData(layers, true);
145            }
146
147            map.events.on({
148                "changelayer": this.onChangeLayer,
149                "addlayer": this.onAddLayer,
150                "removelayer": this.onRemoveLayer,
151                scope: this
152            });
153            this.on({
154                "load": this.onLoad,
155                "clear": this.onClear,
156                "add": this.onAdd,
157                "remove": this.onRemove,
158                "update": this.onUpdate,
159                scope: this
160            });
161            this.data.on({
162                "replace" : this.onReplace,
163                scope: this
164            });
165        },
166
167        /** private: method[unbind]
168         *  Unbind this store from the map it is currently bound.
169         */
170        unbind: function() {
171            if(this.map) {
172                this.map.events.un({
173                    "changelayer": this.onChangeLayer,
174                    "addlayer": this.onAddLayer,
175                    "removelayer": this.onRemoveLayer,
176                    scope: this
177                });
178                this.un("load", this.onLoad, this);
179                this.un("clear", this.onClear, this);
180                this.un("add", this.onAdd, this);
181                this.un("remove", this.onRemove, this);
182
183                this.data.un("replace", this.onReplace, this);
184
185                this.map = null;
186            }
187        },
188       
189        /** private: method[onChangeLayer]
190         *  :param evt: ``Object``
191         *
192         *  Handler for layer changes.  When layer order changes, this moves the
193         *  appropriate record within the store.
194         */
195        onChangeLayer: function(evt) {
196            var layer = evt.layer;
197            var recordIndex = this.findBy(function(rec, id) {
198                return rec.getLayer() === layer;
199            });
200            if(recordIndex > -1) {
201                var record = this.getAt(recordIndex);
202                if(evt.property === "order") {
203                    if(!this._adding && !this._removing) {
204                        var layerIndex = this.map.getLayerIndex(layer);
205                        if(layerIndex !== recordIndex) {
206                            this._removing = true;
207                            this.remove(record);
208                            delete this._removing;
209                            this._adding = true;
210                            this.insert(layerIndex, [record]);
211                            delete this._adding;
212                        }
213                    }
214                } else if(evt.property === "name") {
215                    record.set("title", layer.name);
216                } else {
217                    this.fireEvent("update", this, record, Ext.data.Record.EDIT);
218                }
219            }
220        },
221       
222        /** private: method[onAddLayer]
223         *  :param evt: ``Object``
224         * 
225         *  Handler for a map's addlayer event
226         */
227        onAddLayer: function(evt) {
228            if(!this._adding) {
229                var layer = evt.layer;
230                this._adding = true;
231                this.loadData([layer], true);
232                delete this._adding;
233            }
234        },
235       
236        /** private: method[onRemoveLayer]
237         *  :param evt: ``Object``
238         *
239         *  Handler for a map's removelayer event
240         */
241        onRemoveLayer: function(evt){
242            //TODO replace the check for undloadDestroy with a listener for the
243            // map's beforedestroy event, doing unbind(). This can be done as soon
244            // as http://trac.openlayers.org/ticket/2136 is fixed.
245            if(this.map.unloadDestroy) {
246                if(!this._removing) {
247                    var layer = evt.layer;
248                    this._removing = true;
249                    this.remove(this.getById(layer.id));
250                    delete this._removing;
251                }
252            } else {
253                this.unbind();
254            }
255        },
256       
257        /** private: method[onLoad]
258         *  :param store: ``Ext.data.Store``
259         *  :param records: ``Array(Ext.data.Record)``
260         *  :param options: ``Object``
261         *
262         *  Handler for a store's load event
263         */
264        onLoad: function(store, records, options) {
265            if (!Ext.isArray(records)) {
266                records = [records];
267            }
268            if (options && !options.add) {
269                this._removing = true;
270                for (var i = this.map.layers.length - 1; i >= 0; i--) {
271                    this.map.removeLayer(this.map.layers[i]);
272                }
273                delete this._removing;
274
275                // layers has already been added to map on "add" event
276                var len = records.length;
277                if (len > 0) {
278                    var layers = new Array(len);
279                    for (var j = 0; j < len; j++) {
280                        layers[j] = records[j].getLayer();
281                    }
282                    this._adding = true;
283                    this.map.addLayers(layers);
284                    delete this._adding;
285                }
286            }
287        },
288       
289        /** private: method[onClear]
290         *  :param store: ``Ext.data.Store``
291         *
292         *  Handler for a store's clear event
293         */
294        onClear: function(store) {
295            this._removing = true;
296            for (var i = this.map.layers.length - 1; i >= 0; i--) {
297                this.map.removeLayer(this.map.layers[i]);
298            }
299            delete this._removing;
300        },
301       
302        /** private: method[onAdd]
303         *  :param store: ``Ext.data.Store``
304         *  :param records: ``Array(Ext.data.Record)``
305         *  :param index: ``Number``
306         *
307         *  Handler for a store's add event
308         */
309        onAdd: function(store, records, index) {
310            if(!this._adding) {
311                this._adding = true;
312                var layer;
313                for(var i=records.length-1; i>=0; --i) {
314                    layer = records[i].getLayer();
315                    this.map.addLayer(layer);
316                    if(index !== this.map.layers.length-1) {
317                        this.map.setLayerIndex(layer, index);
318                    }
319                }
320                delete this._adding;
321            }
322        },
323       
324        /** private: method[onRemove]
325         *  :param store: ``Ext.data.Store``
326         *  :param record: ``Ext.data.Record``
327         *  :param index: ``Number``
328         *
329         *  Handler for a store's remove event
330         */
331        onRemove: function(store, record, index){
332            if(!this._removing) {
333                var layer = record.getLayer();
334                if (this.map.getLayer(layer.id) != null) {
335                    this._removing = true;
336                    this.removeMapLayer(record);
337                    delete this._removing;
338                }
339            }
340        },
341       
342        /** private: method[onUpdate]
343         *  :param store: ``Ext.data.Store``
344         *  :param record: ``Ext.data.Record``
345         *  :param operation: ``Number``
346         *
347         *  Handler for a store's update event
348         */
349        onUpdate: function(store, record, operation) {
350            if(operation === Ext.data.Record.EDIT) {
351                if (record.modified && record.modified.title) {
352                    var layer = record.getLayer();
353                    var title = record.get("title");
354                    if(title !== layer.name) {
355                        layer.setName(title);
356                    }
357                }
358            }
359        },
360
361        /** private: method[removeMapLayer]
362         *  :param record: ``Ext.data.Record``
363         * 
364         *  Removes a record's layer from the bound map.
365         */
366        removeMapLayer: function(record){
367            this.map.removeLayer(record.getLayer());
368        },
369
370        /** private: method[onReplace]
371         *  :param key: ``String``
372         *  :param oldRecord: ``Object`` In this case, a record that has been
373         *      replaced.
374         *  :param newRecord: ``Object`` In this case, a record that is replacing
375         *      oldRecord.
376
377         *  Handler for a store's data collections' replace event
378         */
379        onReplace: function(key, oldRecord, newRecord){
380            this.removeMapLayer(oldRecord);
381        },
382       
383        /** public: method[getByLayer]
384         *  :param layer: ``OpenLayers.Layer``
385         *  :return: :class:`GeoExt.data.LayerRecord` or undefined if not found
386         * 
387         *  Get the record for the specified layer
388         */
389        getByLayer: function(layer) {
390            var index = this.findBy(function(r) {
391                return r.getLayer() === layer;
392            });
393            if(index > -1) {
394                return this.getAt(index);
395            }
396        },
397       
398        /** private: method[destroy]
399         */
400        destroy: function() {
401            this.unbind();
402            GeoExt.data.LayerStore.superclass.destroy.call(this);
403        }
404    };
405};
406
407/** api: example
408 *  Sample to create a new store containing a cache of
409 *  :class:`GeoExt.data.LayerRecord` instances derived from map layers.
410 *
411 *  .. code-block:: javascript
412 * 
413 *      var store = new GeoExt.data.LayerStore({
414 *          map: myMap,
415 *          layers: myLayers
416 *      });
417 */
418
419/** api: constructor
420 *  .. class:: LayerStore
421 *
422 *      A store that contains a cache of :class:`GeoExt.data.LayerRecord`
423 *      objects.
424 */
425GeoExt.data.LayerStore = Ext.extend(
426    Ext.data.Store,
427    new GeoExt.data.LayerStoreMixin
428);
429
430/**
431 * Constant: GeoExt.data.LayerStore.MAP_TO_STORE
432 * {Integer} Constant used to make the store be automatically updated
433 * when changes occur in the map.
434 */
435GeoExt.data.LayerStore.MAP_TO_STORE = 1;
436
437/**
438 * Constant: GeoExt.data.LayerStore.STORE_TO_MAP
439 * {Integer} Constant used to make the map be automatically updated
440 * when changes occur in the store.
441 */
442GeoExt.data.LayerStore.STORE_TO_MAP = 2;
Note: See TracBrowser for help on using the repository browser.