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

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/mapfish/widgets/data/GridRowFeatureMediator.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 OpenLayers/Control/SelectFeature.js
 
22
 */
 
23
 
 
24
Ext.namespace('mapfish.widgets', 'mapfish.widgets.data');
 
25
 
 
26
/**
 
27
 * Class: mapfish.widgets.data.GridRowFeatureMediator
 
28
 * A mediator for selecting feature on grid row selection and
 
29
 * vice-vera.
 
30
 *
 
31
 * Usage example:
 
32
 * (start code)
 
33
 * var mediator = mapfish.widgets.data.GridRowFeatureMediator({
 
34
 *     grid: grid,
 
35
 *     selectControl: selectFeatureControl
 
36
 * });
 
37
 * (end)
 
38
 */
 
39
 
 
40
/**
 
41
 * Constructor: mapfish.widgets.data.GridRowFeatureMediator
 
42
 * Create an instance of mapfish.widgets.data.GridRowFeatureMediator.
 
43
 *
 
44
 * Parameters:
 
45
 * config - {Object}
 
46
 *
 
47
 * Returns:
 
48
 * {<mapfish.widgets.data.GridRowFeatureMediator>}
 
49
 */
 
50
mapfish.widgets.data.GridRowFeatureMediator = function(config) {
 
51
    Ext.apply(this, config);
 
52
    if (!this.grid) {
 
53
        OpenLayers.Console.error(
 
54
            "no Ext.grid.GridPanel provided");
 
55
        return;
 
56
    }
 
57
    if (!this.selectControl ||
 
58
        this.selectControl.CLASS_NAME != "OpenLayers.Control.SelectFeature") {
 
59
        OpenLayers.Console.error(
 
60
            "no OpenLayers.Control.SelectFeature provided");
 
61
        return;
 
62
    }
 
63
    this.selectModel = this.grid.getSelectionModel();
 
64
    if (this.autoActivate) {
 
65
        this.activate();
 
66
    }
 
67
};
 
68
 
 
69
mapfish.widgets.data.GridRowFeatureMediator.prototype = {
 
70
    /**
 
71
     * APIProperty: autoActivate
 
72
     * {Boolean} The instance is activated at creation time, defaults
 
73
     *     to true.
 
74
     */
 
75
    autoActivate: true,
 
76
 
 
77
    /**
 
78
     * APIProperty: selectControl
 
79
     * {<OpenLayers.Control.SelectFeature>} The select feature control.
 
80
     */
 
81
    selectControl: null,
 
82
 
 
83
    /**
 
84
     * APIProperty: grid
 
85
     * {Ext.grid.GridPanel} The grid panel.
 
86
     */
 
87
    grid: null,
 
88
 
 
89
    /**
 
90
     * Property: selectModel
 
91
     * {Ext.grid.RowSelectionModel} The row selection model attached to
 
92
     *     the grid panel.
 
93
     */
 
94
    selectModel: null,
 
95
 
 
96
    /**
 
97
     * Property: active
 
98
     * {Boolean}
 
99
     */
 
100
    active: false,
 
101
 
 
102
    /**
 
103
     * APIMethod: activate
 
104
     * Activate the mediator.
 
105
     *
 
106
     * Returns:
 
107
     * {Boolean} - False if the mediator was already active, true
 
108
     * otherwise.
 
109
     */
 
110
    activate: function() {
 
111
        if (!this.active) {
 
112
            this.featureEventsOn();
 
113
            this.rowEventsOn();
 
114
            this.active = true;
 
115
            return true;
 
116
        }
 
117
        return false;
 
118
    },
 
119
 
 
120
    /**
 
121
     * APIMethod: deactivate
 
122
     * Deactivate the mediator.
 
123
     *
 
124
     * Returns:
 
125
     * {Boolean} - False if the mediator was already deactive, true
 
126
     * otherwise.
 
127
     */
 
128
    deactivate: function() {
 
129
        if (this.active) {
 
130
            this.featureEventsOff();
 
131
            this.rowEventsOff();
 
132
            this.active = false;
 
133
            return true;
 
134
        }
 
135
        return false;
 
136
    },
 
137
 
 
138
    /**
 
139
     * Method: featureSelected
 
140
     *
 
141
     * Parameters:
 
142
     * o - {Object} An object with a feature property referencing
 
143
     *     the selected feature.
 
144
     */
 
145
    featureSelected: function(o) {
 
146
        var r = this.grid.store.getById(o.feature.id);
 
147
        if (r) {
 
148
            this.rowEventsOff();
 
149
            this.selectModel.selectRecords([r]);
 
150
            this.rowEventsOn();
 
151
        }
 
152
    },
 
153
 
 
154
    /**
 
155
     * Method: featureUnselected
 
156
     *
 
157
     * Parameters:
 
158
     * o - {Object} An object with a feature property referencing
 
159
     *     the selected feature.
 
160
     */
 
161
    featureUnselected: function(o) {
 
162
        var r = this.grid.store.getById(o.feature.id);
 
163
        if (r) {
 
164
            this.rowEventsOff();
 
165
            this.selectModel.deselectRow(this.grid.store.indexOf(r));
 
166
            this.rowEventsOn();
 
167
        }
 
168
    },
 
169
 
 
170
    /**
 
171
     * Method: rowSelected
 
172
     *
 
173
     * Parameters:
 
174
     * s - {Ext.grid.RowSelectModel} The row select model.
 
175
     * i - {Number} The row index.
 
176
     * r - {Ext.data.Record} The record.
 
177
     */
 
178
    rowSelected: function(s, i, r) {
 
179
        var f = this.selectControl.layer.getFeatureById(r.id);
 
180
        if (f) {
 
181
            this.featureEventsOff();
 
182
            this.selectControl.select(f);
 
183
            this.featureEventsOn();
 
184
        }
 
185
    },
 
186
 
 
187
    /**
 
188
     * Method: rowDeselected
 
189
     *
 
190
     * Parameters:
 
191
     * s - {Ext.grid.RowSelectModel} The row select model.
 
192
     * i - {Number} The row index.
 
193
     * r - {Ext.data.Record} The record.
 
194
     */
 
195
    rowDeselected: function(s, i, r) {
 
196
        var f = this.selectControl.layer.getFeatureById(r.id);
 
197
        if (f) {
 
198
            this.featureEventsOff();
 
199
            this.selectControl.unselect(f);
 
200
            this.featureEventsOn();
 
201
        }
 
202
    },
 
203
 
 
204
    /**
 
205
     * Method: rowEventsOff
 
206
     * Turn off the row events.
 
207
     */
 
208
    rowEventsOff: function() {
 
209
        this.selectModel.un("rowselect", this.rowSelected, this);
 
210
        this.selectModel.un("rowdeselect", this.rowDeselected, this);
 
211
    },
 
212
 
 
213
    /**
 
214
     * Method: rowEventsOn
 
215
     * Turn on the row events.
 
216
     */
 
217
    rowEventsOn: function() {
 
218
        this.selectModel.on("rowselect", this.rowSelected, this);
 
219
        this.selectModel.on("rowdeselect", this.rowDeselected, this);
 
220
    },
 
221
 
 
222
    /**
 
223
     * Method: featureEventsOff
 
224
     * Turn off the feature events.
 
225
     */
 
226
    featureEventsOff: function() {
 
227
        this.selectControl.layer.events.un({
 
228
            featureselected: this.featureSelected,
 
229
            featureunselected: this.featureUnselected,
 
230
            scope: this
 
231
        });
 
232
    },
 
233
 
 
234
    /**
 
235
     * Method: featureEventsOn
 
236
     * Turn on the feature events.
 
237
     */
 
238
    featureEventsOn: function() {
 
239
        this.selectControl.layer.events.on({
 
240
            featureselected: this.featureSelected,
 
241
            featureunselected: this.featureUnselected,
 
242
            scope: this
 
243
        });
 
244
    }
 
245
};