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 | /** api: (define) |
---|
10 | * module = GeoExt.grid |
---|
11 | * class = FeatureSelectionModel |
---|
12 | * base_link = `Ext.grid.RowSelectionModel <http://dev.sencha.com/deploy/dev/docs/?class=Ext.grid.RowSelectionModel>`_ |
---|
13 | */ |
---|
14 | |
---|
15 | Ext.namespace('GeoExt.grid'); |
---|
16 | |
---|
17 | /** api: constructor |
---|
18 | * .. class:: FeatureSelectionModel |
---|
19 | * |
---|
20 | * A row selection model which enables automatic selection of features |
---|
21 | * in the map when rows are selected in the grid and vice-versa. |
---|
22 | */ |
---|
23 | |
---|
24 | /** api: example |
---|
25 | * Sample code to create a feature grid with a feature selection model: |
---|
26 | * |
---|
27 | * .. code-block:: javascript |
---|
28 | * |
---|
29 | * var gridPanel = new Ext.grid.GridPanel({ |
---|
30 | * title: "Feature Grid", |
---|
31 | * region: "east", |
---|
32 | * store: store, |
---|
33 | * width: 320, |
---|
34 | * columns: [{ |
---|
35 | * header: "Name", |
---|
36 | * width: 200, |
---|
37 | * dataIndex: "name" |
---|
38 | * }, { |
---|
39 | * header: "Elevation", |
---|
40 | * width: 100, |
---|
41 | * dataIndex: "elevation" |
---|
42 | * }], |
---|
43 | * sm: new GeoExt.grid.FeatureSelectionModel() |
---|
44 | * }); |
---|
45 | */ |
---|
46 | |
---|
47 | GeoExt.grid.FeatureSelectionModelMixin = function() { |
---|
48 | return { |
---|
49 | /** api: config[autoActivateControl] |
---|
50 | * ``Boolean`` If true the select feature control is activated and |
---|
51 | * deactivated when binding and unbinding. Defaults to true. |
---|
52 | */ |
---|
53 | autoActivateControl: true, |
---|
54 | |
---|
55 | /** api: config[layerFromStore] |
---|
56 | * ``Boolean`` If true, and if the constructor is passed neither a |
---|
57 | * layer nor a select feature control, a select feature control is |
---|
58 | * created using the layer found in the grid's store. Set it to |
---|
59 | * false if you want to manually bind the selection model to a |
---|
60 | * layer. Defaults to true. |
---|
61 | */ |
---|
62 | layerFromStore: true, |
---|
63 | |
---|
64 | /** api: config[selectControl] |
---|
65 | * |
---|
66 | * ``OpenLayers.Control.SelectFeature`` A select feature control. If not |
---|
67 | * provided one will be created. If provided any "layer" config option |
---|
68 | * will be ignored, and its "multiple" option will be used to configure |
---|
69 | * the selectionModel. If an ``Object`` is provided here, it will be |
---|
70 | * passed as config to the SelectFeature constructor, and the "layer" |
---|
71 | * config option will be used for the layer. |
---|
72 | */ |
---|
73 | |
---|
74 | /** private: property[selectControl] |
---|
75 | * ``OpenLayers.Control.SelectFeature`` The select feature control |
---|
76 | * instance. |
---|
77 | */ |
---|
78 | selectControl: null, |
---|
79 | |
---|
80 | /** api: config[layer] |
---|
81 | * ``OpenLayers.Layer.Vector`` The vector layer used for the creation of |
---|
82 | * the select feature control, it must already be added to the map. If not |
---|
83 | * provided, the layer bound to the grid's store, if any, will be used. |
---|
84 | */ |
---|
85 | |
---|
86 | /** private: property[bound] |
---|
87 | * ``Boolean`` Flag indicating if the selection model is bound. |
---|
88 | */ |
---|
89 | bound: false, |
---|
90 | |
---|
91 | /** private: property[superclass] |
---|
92 | * ``Ext.grid.AbstractSelectionModel`` Our superclass. |
---|
93 | */ |
---|
94 | superclass: null, |
---|
95 | |
---|
96 | /** private */ |
---|
97 | constructor: function(config) { |
---|
98 | config = config || {}; |
---|
99 | if(config.selectControl instanceof OpenLayers.Control.SelectFeature) { |
---|
100 | if(!config.singleSelect) { |
---|
101 | var ctrl = config.selectControl; |
---|
102 | config.singleSelect = !(ctrl.multiple || !!ctrl.multipleKey); |
---|
103 | } |
---|
104 | } else if(config.layer instanceof OpenLayers.Layer.Vector) { |
---|
105 | this.selectControl = this.createSelectControl( |
---|
106 | config.layer, config.selectControl |
---|
107 | ); |
---|
108 | delete config.layer; |
---|
109 | delete config.selectControl; |
---|
110 | } |
---|
111 | this.superclass = arguments.callee.superclass; |
---|
112 | this.superclass.constructor.call(this, config); |
---|
113 | }, |
---|
114 | |
---|
115 | /** private: method[initEvents] |
---|
116 | * |
---|
117 | * Called after this.grid is defined |
---|
118 | */ |
---|
119 | initEvents: function() { |
---|
120 | this.superclass.initEvents.call(this); |
---|
121 | if(this.layerFromStore) { |
---|
122 | var layer = this.grid.getStore() && this.grid.getStore().layer; |
---|
123 | if(layer && |
---|
124 | !(this.selectControl instanceof OpenLayers.Control.SelectFeature)) { |
---|
125 | this.selectControl = this.createSelectControl( |
---|
126 | layer, this.selectControl |
---|
127 | ); |
---|
128 | } |
---|
129 | } |
---|
130 | if(this.selectControl) { |
---|
131 | this.bind(this.selectControl); |
---|
132 | } |
---|
133 | }, |
---|
134 | |
---|
135 | /** private: createSelectControl |
---|
136 | * :param layer: ``OpenLayers.Layer.Vector`` The vector layer. |
---|
137 | * :param config: ``Object`` The select feature control config. |
---|
138 | * |
---|
139 | * Create the select feature control. |
---|
140 | */ |
---|
141 | createSelectControl: function(layer, config) { |
---|
142 | config = config || {}; |
---|
143 | var singleSelect = config.singleSelect !== undefined ? |
---|
144 | config.singleSelect : this.singleSelect; |
---|
145 | config = OpenLayers.Util.extend({ |
---|
146 | toggle: true, |
---|
147 | multipleKey: singleSelect ? null : |
---|
148 | (Ext.isMac ? "metaKey" : "ctrlKey") |
---|
149 | }, config); |
---|
150 | var selectControl = new OpenLayers.Control.SelectFeature( |
---|
151 | layer, config |
---|
152 | ); |
---|
153 | layer.map.addControl(selectControl); |
---|
154 | return selectControl; |
---|
155 | }, |
---|
156 | |
---|
157 | /** api: method[bind] |
---|
158 | * |
---|
159 | * :param obj: ``OpenLayers.Layer.Vector`` or |
---|
160 | * ``OpenLayers.Control.SelectFeature`` The object this selection model |
---|
161 | * should be bound to, either a vector layer or a select feature |
---|
162 | * control. |
---|
163 | * :param options: ``Object`` An object with a "controlConfig" |
---|
164 | * property referencing the configuration object to pass to the |
---|
165 | * ``OpenLayers.Control.SelectFeature`` constructor. |
---|
166 | * :return: ``OpenLayers.Control.SelectFeature`` The select feature |
---|
167 | * control this selection model uses. |
---|
168 | * |
---|
169 | * Bind the selection model to a layer or a SelectFeature control. |
---|
170 | */ |
---|
171 | bind: function(obj, options) { |
---|
172 | if(!this.bound) { |
---|
173 | options = options || {}; |
---|
174 | this.selectControl = obj; |
---|
175 | if(obj instanceof OpenLayers.Layer.Vector) { |
---|
176 | this.selectControl = this.createSelectControl( |
---|
177 | obj, options.controlConfig |
---|
178 | ); |
---|
179 | } |
---|
180 | if(this.autoActivateControl) { |
---|
181 | this.selectControl.activate(); |
---|
182 | } |
---|
183 | var layers = this.getLayers(); |
---|
184 | for(var i = 0, len = layers.length; i < len; i++) { |
---|
185 | layers[i].events.on({ |
---|
186 | featureselected: this.featureSelected, |
---|
187 | featureunselected: this.featureUnselected, |
---|
188 | scope: this |
---|
189 | }); |
---|
190 | } |
---|
191 | this.on("rowselect", this.rowSelected, this); |
---|
192 | this.on("rowdeselect", this.rowDeselected, this); |
---|
193 | this.bound = true; |
---|
194 | } |
---|
195 | return this.selectControl; |
---|
196 | }, |
---|
197 | |
---|
198 | /** api: method[unbind] |
---|
199 | * :return: ``OpenLayers.Control.SelectFeature`` The select feature |
---|
200 | * control this selection model used. |
---|
201 | * |
---|
202 | * Unbind the selection model from the layer or SelectFeature control. |
---|
203 | */ |
---|
204 | unbind: function() { |
---|
205 | var selectControl = this.selectControl; |
---|
206 | if(this.bound) { |
---|
207 | var layers = this.getLayers(); |
---|
208 | for(var i = 0, len = layers.length; i < len; i++) { |
---|
209 | layers[i].events.un({ |
---|
210 | featureselected: this.featureSelected, |
---|
211 | featureunselected: this.featureUnselected, |
---|
212 | scope: this |
---|
213 | }); |
---|
214 | } |
---|
215 | this.un("rowselect", this.rowSelected, this); |
---|
216 | this.un("rowdeselect", this.rowDeselected, this); |
---|
217 | if(this.autoActivateControl) { |
---|
218 | selectControl.deactivate(); |
---|
219 | } |
---|
220 | this.selectControl = null; |
---|
221 | this.bound = false; |
---|
222 | } |
---|
223 | return selectControl; |
---|
224 | }, |
---|
225 | |
---|
226 | /** private: method[featureSelected] |
---|
227 | * :param evt: ``Object`` An object with a feature property referencing |
---|
228 | * the selected feature. |
---|
229 | */ |
---|
230 | featureSelected: function(evt) { |
---|
231 | if(!this._selecting) { |
---|
232 | var store = this.grid.store; |
---|
233 | var row = store.findBy(function(record, id) { |
---|
234 | return record.getFeature() == evt.feature; |
---|
235 | }); |
---|
236 | if(row != -1 && !this.isSelected(row)) { |
---|
237 | this._selecting = true; |
---|
238 | this.selectRow(row, !this.singleSelect); |
---|
239 | this._selecting = false; |
---|
240 | // focus the row in the grid to ensure it is visible |
---|
241 | this.grid.getView().focusRow(row); |
---|
242 | } |
---|
243 | } |
---|
244 | }, |
---|
245 | |
---|
246 | /** private: method[featureUnselected] |
---|
247 | * :param evt: ``Object`` An object with a feature property referencing |
---|
248 | * the unselected feature. |
---|
249 | */ |
---|
250 | featureUnselected: function(evt) { |
---|
251 | if(!this._selecting) { |
---|
252 | var store = this.grid.store; |
---|
253 | var row = store.findBy(function(record, id) { |
---|
254 | return record.getFeature() == evt.feature; |
---|
255 | }); |
---|
256 | if(row != -1 && this.isSelected(row)) { |
---|
257 | this._selecting = true; |
---|
258 | this.deselectRow(row); |
---|
259 | this._selecting = false; |
---|
260 | this.grid.getView().focusRow(row); |
---|
261 | } |
---|
262 | } |
---|
263 | }, |
---|
264 | |
---|
265 | /** private: method[rowSelected] |
---|
266 | * :param model: ``Ext.grid.RowSelectModel`` The row select model. |
---|
267 | * :param row: ``Integer`` The row index. |
---|
268 | * :param record: ``Ext.data.Record`` The record. |
---|
269 | */ |
---|
270 | rowSelected: function(model, row, record) { |
---|
271 | var feature = record.getFeature(); |
---|
272 | if(!this._selecting && feature) { |
---|
273 | var layers = this.getLayers(); |
---|
274 | for(var i = 0, len = layers.length; i < len; i++) { |
---|
275 | if(layers[i].selectedFeatures.indexOf(feature) == -1) { |
---|
276 | this._selecting = true; |
---|
277 | this.selectControl.select(feature); |
---|
278 | this._selecting = false; |
---|
279 | break; |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | }, |
---|
284 | |
---|
285 | /** private: method[rowDeselected] |
---|
286 | * :param model: ``Ext.grid.RowSelectModel`` The row select model. |
---|
287 | * :param row: ``Integer`` The row index. |
---|
288 | * :param record: ``Ext.data.Record`` The record. |
---|
289 | */ |
---|
290 | rowDeselected: function(model, row, record) { |
---|
291 | var feature = record.getFeature(); |
---|
292 | if(!this._selecting && feature) { |
---|
293 | var layers = this.getLayers(); |
---|
294 | for(var i = 0, len = layers.length; i < len; i++) { |
---|
295 | if(layers[i].selectedFeatures.indexOf(feature) != -1) { |
---|
296 | this._selecting = true; |
---|
297 | this.selectControl.unselect(feature); |
---|
298 | this._selecting = false; |
---|
299 | break; |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | }, |
---|
304 | |
---|
305 | /** private: method[getLayers] |
---|
306 | * Return the layers attached to the select feature control. |
---|
307 | */ |
---|
308 | getLayers: function() { |
---|
309 | return this.selectControl.layers || [this.selectControl.layer]; |
---|
310 | } |
---|
311 | }; |
---|
312 | }; |
---|
313 | |
---|
314 | GeoExt.grid.FeatureSelectionModel = Ext.extend( |
---|
315 | Ext.grid.RowSelectionModel, |
---|
316 | new GeoExt.grid.FeatureSelectionModelMixin |
---|
317 | ); |
---|