~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/mapfish/widgets/data/LayerStoreMediator.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007-2008  Camptocamp
 
3
 *
 
4
 * This file is part of MapFish Client
 
5
 *
 
6
 * MapFish Client is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * MapFish Client is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with MapFish Client.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
/**
 
21
 * @requires widgets/data/FeatureStoreMediator.js
 
22
 * @requires OpenLayers/Layer/Vector.js
 
23
 */
 
24
 
 
25
Ext.namespace('mapfish.widgets', 'mapfish.widgets.data');
 
26
 
 
27
/**
 
28
 * Class: mapfish.widgets.data.LayerStoreMediator
 
29
 *
 
30
 * This class is to be used when one wants to insert, remove, and
 
31
 * update features in a grid as a result of features being inserted,
 
32
 * removed, modified in a vector layer.
 
33
 *
 
34
 * Usage example:
 
35
 * (start code)
 
36
 * var layer = new OpenLayers.Layer.Vector("vector");
 
37
 * var store = new Ext.data.Store({
 
38
 *     reader: new mapfish.widgets.data.FeatureReader(
 
39
 *         {}, [{name: "name", type: "string"}]
 
40
 *     )
 
41
 * });
 
42
 * var mediator = new mapfish.widgets.data.LayerStoreMediator({
 
43
 *     store: store,
 
44
 *     layer: layer,
 
45
 *     filter: function(feature) {
 
46
 *         return feature.state != OpenLayers.State.UNKNOWN;
 
47
 *     }
 
48
 * });
 
49
 * (end)
 
50
 */
 
51
 
 
52
/**
 
53
 * Constructor: mapfish.widgets.data.LayerStoreMediator
 
54
 * Create an instance of mapfish.widgets.data.LayerStoreMediator.
 
55
 *
 
56
 * Parameters:
 
57
 * config - {Object} A config object used to set the layer
 
58
 *     store mediator's properties (see below for the list
 
59
 *     of supported properties), and configure it with the
 
60
 *     Ext store; see the usage example above.
 
61
 *
 
62
 * Returns:
 
63
 * {<mapfish.widgets.data.LayerStoreMediator>}
 
64
 */
 
65
mapfish.widgets.data.LayerStoreMediator = function(config){
 
66
    var store = config.store;
 
67
    // no need to place the store in the instance
 
68
    delete config.store;
 
69
    Ext.apply(this, config);
 
70
    if (!this.layer) {
 
71
        OpenLayers.Console.error(
 
72
            "layer is missing in config");
 
73
    }
 
74
    this.featureStoreMediator = new mapfish.widgets.data.FeatureStoreMediator({
 
75
        store: store
 
76
    });
 
77
    if (this.autoActivate) {
 
78
        this.activate();
 
79
    }
 
80
};
 
81
 
 
82
mapfish.widgets.data.LayerStoreMediator.prototype = {
 
83
    /**
 
84
     * APIProperty: layer
 
85
     * {<OpenLayers.Layer.Vector>} The vector layer.
 
86
     */
 
87
    layer: null,
 
88
 
 
89
    /**
 
90
     * APIProperty: filter
 
91
     * {Function} a filter function called for each feature to be
 
92
     * inserted, the feature is passed as an argument to the function,
 
93
     * if it returns true the feature is inserted into the store,
 
94
     * otherwise the feature is not inserted.
 
95
     */
 
96
    filter: null,
 
97
 
 
98
    /**
 
99
     * APIProperty: autoActivate
 
100
     * {Boolean} True if the mediator must be activated as part of
 
101
     * its creation, false otherwise; if false then the mediator must
 
102
     * be explicitely activate using the activate method; defaults
 
103
     * to true.
 
104
     */
 
105
    autoActivate: true,
 
106
 
 
107
    /**
 
108
     * Property: active
 
109
     * {Boolean}
 
110
     */
 
111
    active: false,
 
112
 
 
113
    /**
 
114
     * Property: featureStoreMediator
 
115
     * {<mapfish.widgets.data.featureStoreMediator>} An internal
 
116
     * feature store mediator for manually adding features to the
 
117
     * Ext store.
 
118
     */
 
119
    featureStoreMediator: null,
 
120
 
 
121
    /**
 
122
     * APIMethod: activate
 
123
     * Activate the mediator.
 
124
     *
 
125
     * Returns:
 
126
     * {Boolean} - False if the mediator was already active, true
 
127
     * otherwise.
 
128
     */
 
129
    activate: function() {
 
130
        if (!this.active) {
 
131
            this.layer.events.on({
 
132
                featuresadded: this.update,
 
133
                featuresremoved: this.update,
 
134
                featuremodified: this.update,
 
135
                scope: this
 
136
            });
 
137
            this.active = true;
 
138
            return true;
 
139
        }
 
140
        return false;
 
141
    },
 
142
 
 
143
    /**
 
144
     * APIMethod: deactivate
 
145
     * Deactivate the mediator.
 
146
     *
 
147
     * Returns:
 
148
     * {Boolean} - False if the mediator was already deactive, true
 
149
     * otherwise.
 
150
     */
 
151
    deactivate: function() {
 
152
        if (this.active) {
 
153
            this.layer.events.un({
 
154
                featuresadded: this.update,
 
155
                featuresremoved: this.update,
 
156
                featuremodified: this.update,
 
157
                scope: this
 
158
            });
 
159
            return true;
 
160
        }
 
161
        return false;
 
162
    },
 
163
 
 
164
    /**
 
165
     * Method: update
 
166
     *      Called when features are added, removed or modified. This
 
167
     *      function empties the store, loops over the features in
 
168
     *      the layer, and for each feature calls the user-defined
 
169
     *      filter function, if the return value of the filter function
 
170
     *      evaluates to true the feature is added to the store.
 
171
     */
 
172
    update: function() {
 
173
        this.featureStoreMediator.addFeatures(
 
174
            this.layer.features,
 
175
            {append: false, filter: this.filter});
 
176
    }
 
177
};