| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 | 1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
7
7
7
1
1
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
1
1
5
1
7
7
7
7
7
7
7
7
7
7
7
7
14
11
11
7
7
7
7
7
7
7
1
14
14
14
4
14
14
14
9
1
10
10
10
10
1
11
11
11
11
11
1
| goog.provide('ol.interaction.Select');
goog.provide('ol.interaction.SelectFilterFunction');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.functions');
goog.require('ol.CollectionEventType');
goog.require('ol.Feature');
goog.require('ol.FeatureOverlay');
goog.require('ol.events.condition');
goog.require('ol.geom.GeometryType');
goog.require('ol.interaction.Interaction');
goog.require('ol.style.Style');
/**
* @enum {string}
*/
ol.SelectEventType = {
/**
* Triggered when feature(s) has been (de)selected.
* @event ol.SelectEvent#select
* @api
*/
SELECT: 'select'
};
/**
* A function that takes an {@link ol.Feature} and an {@link ol.layer.Layer}
* and returns `true` if the feature may be selected or `false` otherwise.
* @typedef {function(ol.Feature, ol.layer.Layer): boolean}
* @api
*/
ol.interaction.SelectFilterFunction;
/**
* @classdesc
* Events emitted by {@link ol.interaction.Select} instances are instances of
* this type.
*
* @param {string} type The event type.
* @param {Array.<ol.Feature>} selected Selected features.
* @param {Array.<ol.Feature>} deselected Deselected features.
* @implements {oli.SelectEvent}
* @extends {goog.events.Event}
* @constructor
*/
ol.SelectEvent = function(type, selected, deselected) {
goog.base(this, type);
/**
* Selected features array.
* @type {Array.<ol.Feature>}
* @api
*/
this.selected = selected;
/**
* Deselected features array.
* @type {Array.<ol.Feature>}
* @api
*/
this.deselected = deselected;
};
goog.inherits(ol.SelectEvent, goog.events.Event);
/**
* @classdesc
* Handles selection of vector data. A {@link ol.FeatureOverlay} is maintained
* internally to store the selected feature(s). Which features are selected is
* determined by the `condition` option, and optionally the `toggle` or
* `add`/`remove` options.
*
* @constructor
* @extends {ol.interaction.Interaction}
* @param {olx.interaction.SelectOptions=} opt_options Options.
* @api stable
*/
ol.interaction.Select = function(opt_options) {
goog.base(this, {
handleEvent: ol.interaction.Select.handleEvent
});
var options = goog.isDef(opt_options) ? opt_options : {};
/**
* @private
* @type {ol.events.ConditionType}
*/
this.condition_ = goog.isDef(options.condition) ?
options.condition : ol.events.condition.singleClick;
/**
* @private
* @type {ol.events.ConditionType}
*/
this.addCondition_ = goog.isDef(options.addCondition) ?
options.addCondition : ol.events.condition.never;
/**
* @private
* @type {ol.events.ConditionType}
*/
this.removeCondition_ = goog.isDef(options.removeCondition) ?
options.removeCondition : ol.events.condition.never;
/**
* @private
* @type {ol.events.ConditionType}
*/
this.toggleCondition_ = goog.isDef(options.toggleCondition) ?
options.toggleCondition : ol.events.condition.shiftKeyOnly;
/**
* @private
* @type {boolean}
*/
this.multi_ = goog.isDef(options.multi) ? options.multi : false;
/**
* @private
* @type {ol.interaction.SelectFilterFunction}
*/
this.filter_ = goog.isDef(options.filter) ? options.filter :
goog.functions.TRUE;
var layerFilter;
Iif (goog.isDef(options.layers)) {
if (goog.isFunction(options.layers)) {
layerFilter = options.layers;
} else {
var layers = options.layers;
layerFilter =
/**
* @param {ol.layer.Layer} layer Layer.
* @return {boolean} Include.
*/
function(layer) {
return goog.array.contains(layers, layer);
};
}
} else {
layerFilter = goog.functions.TRUE;
}
/**
* @private
* @type {function(ol.layer.Layer): boolean}
*/
this.layerFilter_ = layerFilter;
/**
* @private
* @type {ol.FeatureOverlay}
*/
this.featureOverlay_ = new ol.FeatureOverlay({
style: goog.isDef(options.style) ? options.style :
ol.interaction.Select.getDefaultStyleFunction()
});
var features = this.featureOverlay_.getFeatures();
goog.events.listen(features, ol.CollectionEventType.ADD,
this.addFeature_, false, this);
goog.events.listen(features, ol.CollectionEventType.REMOVE,
this.removeFeature_, false, this);
};
goog.inherits(ol.interaction.Select, ol.interaction.Interaction);
/**
* Get the selected features.
* @return {ol.Collection.<ol.Feature>} Features collection.
* @api stable
*/
ol.interaction.Select.prototype.getFeatures = function() {
return this.featureOverlay_.getFeatures();
};
/**
* Handles the {@link ol.MapBrowserEvent map browser event} and may change the
* selected state of features.
* @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
* @return {boolean} `false` to stop event propagation.
* @this {ol.interaction.Select}
* @api
*/
ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
Iif (!this.condition_(mapBrowserEvent)) {
return true;
}
var add = this.addCondition_(mapBrowserEvent);
var remove = this.removeCondition_(mapBrowserEvent);
var toggle = this.toggleCondition_(mapBrowserEvent);
var set = !add && !remove && !toggle;
var map = mapBrowserEvent.map;
var features = this.featureOverlay_.getFeatures();
var /** @type {Array.<ol.Feature>} */ deselected = [];
var /** @type {Array.<ol.Feature>} */ selected = [];
var change = false;
Eif (set) {
// Replace the currently selected feature(s) with the feature(s) at the
// pixel, or clear the selected feature(s) if there is no feature at
// the pixel.
map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
/**
* @param {ol.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer.
*/
function(feature, layer) {
if (this.filter_(feature, layer)) {
selected.push(feature);
return !this.multi_;
}
}, this, this.layerFilter_);
Iif (selected.length > 0 && features.getLength() == 1 &&
features.item(0) == selected[0]) {
// No change
} else {
change = true;
Iif (features.getLength() !== 0) {
deselected = Array.prototype.concat(features.getArray());
features.clear();
}
features.extend(selected);
}
} else {
// Modify the currently selected feature(s).
map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
/**
* @param {ol.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer.
*/
function(feature, layer) {
var index = goog.array.indexOf(features.getArray(), feature);
if (index == -1) {
if (add || toggle) {
if (this.filter_(feature, layer)) {
selected.push(feature);
}
}
} else {
if (remove || toggle) {
deselected.push(feature);
}
}
}, this, this.layerFilter_);
var i;
for (i = deselected.length - 1; i >= 0; --i) {
features.remove(deselected[i]);
}
features.extend(selected);
if (selected.length > 0 || deselected.length > 0) {
change = true;
}
}
Eif (change) {
this.dispatchEvent(
new ol.SelectEvent(ol.SelectEventType.SELECT, selected, deselected));
}
return ol.events.condition.pointerMove(mapBrowserEvent);
};
/**
* Remove the interaction from its current map, if any, and attach it to a new
* map, if any. Pass `null` to just remove the interaction from the current map.
* @param {ol.Map} map Map.
* @api stable
*/
ol.interaction.Select.prototype.setMap = function(map) {
var currentMap = this.getMap();
var selectedFeatures = this.featureOverlay_.getFeatures();
if (!goog.isNull(currentMap)) {
selectedFeatures.forEach(currentMap.unskipFeature, currentMap);
}
goog.base(this, 'setMap', map);
this.featureOverlay_.setMap(map);
if (!goog.isNull(map)) {
selectedFeatures.forEach(map.skipFeature, map);
}
};
/**
* @return {ol.style.StyleFunction} Styles.
*/
ol.interaction.Select.getDefaultStyleFunction = function() {
var styles = ol.style.createDefaultEditingStyles();
goog.array.extend(styles[ol.geom.GeometryType.POLYGON],
styles[ol.geom.GeometryType.LINE_STRING]);
goog.array.extend(styles[ol.geom.GeometryType.GEOMETRY_COLLECTION],
styles[ol.geom.GeometryType.LINE_STRING]);
return function(feature, resolution) {
return styles[feature.getGeometry().getType()];
};
};
/**
* @param {ol.CollectionEvent} evt Event.
* @private
*/
ol.interaction.Select.prototype.addFeature_ = function(evt) {
var feature = evt.element;
var map = this.getMap();
goog.asserts.assertInstanceof(feature, ol.Feature,
'feature should be an ol.Feature');
Eif (!goog.isNull(map)) {
map.skipFeature(feature);
}
};
/**
* @param {ol.CollectionEvent} evt Event.
* @private
*/
ol.interaction.Select.prototype.removeFeature_ = function(evt) {
var feature = evt.element;
var map = this.getMap();
goog.asserts.assertInstanceof(feature, ol.Feature,
'feature should be an ol.Feature');
if (!goog.isNull(map)) {
map.unskipFeature(feature);
}
};
|