/
Select.js
591 lines (542 loc) · 18.5 KB
/
Select.js
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* @module ol/interaction/Select
*/
import Collection from '../Collection.js';
import CollectionEventType from '../CollectionEventType.js';
import Event from '../events/Event.js';
import Feature from '../Feature.js';
import Interaction from './Interaction.js';
import VectorLayer from '../layer/Vector.js';
import {TRUE} from '../functions.js';
import {clear} from '../obj.js';
import {createEditingStyle} from '../style/Style.js';
import {extend} from '../array.js';
import {getUid} from '../util.js';
import {never, shiftKeyOnly, singleClick} from '../events/condition.js';
/**
* @enum {string}
*/
const SelectEventType = {
/**
* Triggered when feature(s) has been (de)selected.
* @event SelectEvent#select
* @api
*/
SELECT: 'select',
};
/**
* A function that takes an {@link module:ol/Feature~Feature} and returns `true` if the feature may be
* selected or `false` otherwise.
* @typedef {function(import("../Feature.js").default, import("../layer/Layer.js").default<import("../source/Source").default>):boolean} FilterFunction
*/
/**
* @typedef {Object} Options
* @property {import("../events/condition.js").Condition} [addCondition] A function
* that takes an {@link module:ol/MapBrowserEvent~MapBrowserEvent} and returns a
* boolean to indicate whether that event should be handled.
* By default, this is {@link module:ol/events/condition.never}. Use this if you
* want to use different events for add and remove instead of `toggle`.
* @property {import("../events/condition.js").Condition} [condition] A function that
* takes an {@link module:ol/MapBrowserEvent~MapBrowserEvent} and returns a
* boolean to indicate whether that event should be handled. This is the event
* for the selected features as a whole. By default, this is
* {@link module:ol/events/condition.singleClick}. Clicking on a feature selects that
* feature and removes any that were in the selection. Clicking outside any
* feature removes all from the selection.
* See `toggle`, `add`, `remove` options for adding/removing extra features to/
* from the selection.
* @property {Array<import("../layer/Layer.js").default>|function(import("../layer/Layer.js").default<import("../source/Source").default>): boolean} [layers]
* A list of layers from which features should be selected. Alternatively, a
* filter function can be provided. The function will be called for each layer
* in the map and should return `true` for layers that you want to be
* selectable. If the option is absent, all visible layers will be considered
* selectable.
* @property {import("../style/Style.js").StyleLike|null} [style]
* Style for the selected features. By default the default edit style is used
* (see {@link module:ol/style/Style~Style}). Set to `null` if this interaction should not apply
* any style changes for selected features.
* If set to a falsey value, the selected feature's style will not change.
* @property {import("../events/condition.js").Condition} [removeCondition] A function
* that takes an {@link module:ol/MapBrowserEvent~MapBrowserEvent} and returns a
* boolean to indicate whether that event should be handled.
* By default, this is {@link module:ol/events/condition.never}. Use this if you
* want to use different events for add and remove instead of `toggle`.
* @property {import("../events/condition.js").Condition} [toggleCondition] A function
* that takes an {@link module:ol/MapBrowserEvent~MapBrowserEvent} and returns a
* boolean to indicate whether that event should be handled. This is in addition
* to the `condition` event. By default,
* {@link module:ol/events/condition.shiftKeyOnly}, i.e. pressing `shift` as
* well as the `condition` event, adds that feature to the current selection if
* it is not currently selected, and removes it if it is. See `add` and `remove`
* if you want to use different events instead of a toggle.
* @property {boolean} [multi=false] A boolean that determines if the default
* behaviour should select only single features or all (overlapping) features at
* the clicked map position. The default of `false` means single select.
* @property {Collection<Feature>} [features]
* Collection where the interaction will place selected features. Optional. If
* not set the interaction will create a collection. In any case the collection
* used by the interaction is returned by
* {@link module:ol/interaction/Select~Select#getFeatures}.
* @property {FilterFunction} [filter] A function
* that takes an {@link module:ol/Feature~Feature} and an
* {@link module:ol/layer/Layer~Layer} and returns `true` if the feature may be
* selected or `false` otherwise.
* @property {number} [hitTolerance=0] Hit-detection tolerance. Pixels inside
* the radius around the given position will be checked for features.
*/
/**
* @classdesc
* Events emitted by {@link module:ol/interaction/Select~Select} instances are instances of
* this type.
*/
export class SelectEvent extends Event {
/**
* @param {SelectEventType} type The event type.
* @param {Array<import("../Feature.js").default>} selected Selected features.
* @param {Array<import("../Feature.js").default>} deselected Deselected features.
* @param {import("../MapBrowserEvent.js").default} mapBrowserEvent Associated
* {@link module:ol/MapBrowserEvent~MapBrowserEvent}.
*/
constructor(type, selected, deselected, mapBrowserEvent) {
super(type);
/**
* Selected features array.
* @type {Array<import("../Feature.js").default>}
* @api
*/
this.selected = selected;
/**
* Deselected features array.
* @type {Array<import("../Feature.js").default>}
* @api
*/
this.deselected = deselected;
/**
* Associated {@link module:ol/MapBrowserEvent~MapBrowserEvent}.
* @type {import("../MapBrowserEvent.js").default}
* @api
*/
this.mapBrowserEvent = mapBrowserEvent;
}
}
/**
* Original feature styles to reset to when features are no longer selected.
* @type {Object<number, import("../style/Style.js").default|Array<import("../style/Style.js").default>|import("../style/Style.js").StyleFunction>}
*/
const originalFeatureStyles = {};
/***
* @template Return
* @typedef {import("../Observable").OnSignature<import("../Observable").EventTypes, import("../events/Event.js").default, Return> &
* import("../Observable").OnSignature<import("../ObjectEventType").Types|
* 'change:active', import("../Object").ObjectEvent, Return> &
* import("../Observable").OnSignature<'select', SelectEvent, Return> &
* import("../Observable").CombinedOnSignature<import("../Observable").EventTypes|import("../ObjectEventType").Types|
* 'change:active'|'select', Return>} SelectOnSignature
*/
/**
* @classdesc
* Interaction for selecting vector features. By default, selected features are
* styled differently, so this interaction can be used for visual highlighting,
* as well as selecting features for other actions, such as modification or
* output. There are three ways of controlling which features are selected:
* using the browser event as defined by the `condition` and optionally the
* `toggle`, `add`/`remove`, and `multi` options; a `layers` filter; and a
* further feature filter using the `filter` option.
*
* @fires SelectEvent
* @api
*/
class Select extends Interaction {
/**
* @param {Options} [options] Options.
*/
constructor(options) {
super();
/***
* @type {SelectOnSignature<import("../events").EventsKey>}
*/
this.on;
/***
* @type {SelectOnSignature<import("../events").EventsKey>}
*/
this.once;
/***
* @type {SelectOnSignature<void>}
*/
this.un;
options = options ? options : {};
/**
* @private
*/
this.boundAddFeature_ = this.addFeature_.bind(this);
/**
* @private
*/
this.boundRemoveFeature_ = this.removeFeature_.bind(this);
/**
* @private
* @type {import("../events/condition.js").Condition}
*/
this.condition_ = options.condition ? options.condition : singleClick;
/**
* @private
* @type {import("../events/condition.js").Condition}
*/
this.addCondition_ = options.addCondition ? options.addCondition : never;
/**
* @private
* @type {import("../events/condition.js").Condition}
*/
this.removeCondition_ = options.removeCondition
? options.removeCondition
: never;
/**
* @private
* @type {import("../events/condition.js").Condition}
*/
this.toggleCondition_ = options.toggleCondition
? options.toggleCondition
: shiftKeyOnly;
/**
* @private
* @type {boolean}
*/
this.multi_ = options.multi ? options.multi : false;
/**
* @private
* @type {FilterFunction}
*/
this.filter_ = options.filter ? options.filter : TRUE;
/**
* @private
* @type {number}
*/
this.hitTolerance_ = options.hitTolerance ? options.hitTolerance : 0;
/**
* @private
* @type {import("../style/Style.js").default|Array<import("../style/Style.js").default>|import("../style/Style.js").StyleFunction|null}
*/
this.style_ =
options.style !== undefined ? options.style : getDefaultStyleFunction();
/**
* @private
* @type {Collection<Feature>}
*/
this.features_ = options.features || new Collection();
/** @type {function(import("../layer/Layer.js").default<import("../source/Source").default>): boolean} */
let layerFilter;
if (options.layers) {
if (typeof options.layers === 'function') {
layerFilter = options.layers;
} else {
const layers = options.layers;
layerFilter = function (layer) {
return layers.includes(layer);
};
}
} else {
layerFilter = TRUE;
}
/**
* @private
* @type {function(import("../layer/Layer.js").default<import("../source/Source").default>): boolean}
*/
this.layerFilter_ = layerFilter;
/**
* An association between selected feature (key)
* and layer (value)
* @private
* @type {Object<string, import("../layer/Layer.js").default>}
*/
this.featureLayerAssociation_ = {};
}
/**
* @param {import("../Feature.js").default} feature Feature.
* @param {import("../layer/Layer.js").default} layer Layer.
* @private
*/
addFeatureLayerAssociation_(feature, layer) {
this.featureLayerAssociation_[getUid(feature)] = layer;
}
/**
* Get the selected features.
* @return {Collection<Feature>} Features collection.
* @api
*/
getFeatures() {
return this.features_;
}
/**
* Returns the Hit-detection tolerance.
* @return {number} Hit tolerance in pixels.
* @api
*/
getHitTolerance() {
return this.hitTolerance_;
}
/**
* Returns the associated {@link module:ol/layer/Vector~VectorLayer vector layer} of
* a selected feature.
* @param {import("../Feature.js").default} feature Feature
* @return {import('../layer/Vector.js').default} Layer.
* @api
*/
getLayer(feature) {
return /** @type {import('../layer/Vector.js').default} */ (
this.featureLayerAssociation_[getUid(feature)]
);
}
/**
* Hit-detection tolerance. Pixels inside the radius around the given position
* will be checked for features.
* @param {number} hitTolerance Hit tolerance in pixels.
* @api
*/
setHitTolerance(hitTolerance) {
this.hitTolerance_ = hitTolerance;
}
/**
* 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 {import("../Map.js").default|null} map Map.
* @api
*/
setMap(map) {
const currentMap = this.getMap();
if (currentMap && this.style_) {
this.features_.forEach(this.restorePreviousStyle_.bind(this));
}
super.setMap(map);
if (map) {
this.features_.addEventListener(
CollectionEventType.ADD,
this.boundAddFeature_
);
this.features_.addEventListener(
CollectionEventType.REMOVE,
this.boundRemoveFeature_
);
if (this.style_) {
this.features_.forEach(this.applySelectedStyle_.bind(this));
}
} else {
this.features_.removeEventListener(
CollectionEventType.ADD,
this.boundAddFeature_
);
this.features_.removeEventListener(
CollectionEventType.REMOVE,
this.boundRemoveFeature_
);
}
}
/**
* @param {import("../Collection.js").CollectionEvent<Feature>} evt Event.
* @private
*/
addFeature_(evt) {
const feature = evt.element;
if (this.style_) {
this.applySelectedStyle_(feature);
}
if (!this.getLayer(feature)) {
const layer = /** @type {VectorLayer} */ (
this.getMap()
.getAllLayers()
.find(function (layer) {
if (
layer instanceof VectorLayer &&
layer.getSource() &&
layer.getSource().hasFeature(feature)
) {
return layer;
}
})
);
if (layer) {
this.addFeatureLayerAssociation_(feature, layer);
}
}
}
/**
* @param {import("../Collection.js").CollectionEvent<Feature>} evt Event.
* @private
*/
removeFeature_(evt) {
if (this.style_) {
this.restorePreviousStyle_(evt.element);
}
}
/**
* @return {import("../style/Style.js").StyleLike|null} Select style.
*/
getStyle() {
return this.style_;
}
/**
* @param {Feature} feature Feature
* @private
*/
applySelectedStyle_(feature) {
const key = getUid(feature);
if (!(key in originalFeatureStyles)) {
originalFeatureStyles[key] = feature.getStyle();
}
feature.setStyle(this.style_);
}
/**
* @param {Feature} feature Feature
* @private
*/
restorePreviousStyle_(feature) {
const interactions = this.getMap().getInteractions().getArray();
for (let i = interactions.length - 1; i >= 0; --i) {
const interaction = interactions[i];
if (
interaction !== this &&
interaction instanceof Select &&
interaction.getStyle() &&
interaction.getFeatures().getArray().lastIndexOf(feature) !== -1
) {
feature.setStyle(interaction.getStyle());
return;
}
}
const key = getUid(feature);
feature.setStyle(originalFeatureStyles[key]);
delete originalFeatureStyles[key];
}
/**
* @param {Feature} feature Feature.
* @private
*/
removeFeatureLayerAssociation_(feature) {
delete this.featureLayerAssociation_[getUid(feature)];
}
/**
* Handles the {@link module:ol/MapBrowserEvent~MapBrowserEvent map browser event} and may change the
* selected state of features.
* @param {import("../MapBrowserEvent.js").default} mapBrowserEvent Map browser event.
* @return {boolean} `false` to stop event propagation.
* @this {Select}
*/
handleEvent(mapBrowserEvent) {
if (!this.condition_(mapBrowserEvent)) {
return true;
}
const add = this.addCondition_(mapBrowserEvent);
const remove = this.removeCondition_(mapBrowserEvent);
const toggle = this.toggleCondition_(mapBrowserEvent);
const set = !add && !remove && !toggle;
const map = mapBrowserEvent.map;
const features = this.getFeatures();
/**
* @type {Array<Feature>}
*/
const deselected = [];
/**
* @type {Array<Feature>}
*/
const selected = [];
if (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.
clear(this.featureLayerAssociation_);
map.forEachFeatureAtPixel(
mapBrowserEvent.pixel,
/**
* @param {import("../Feature.js").FeatureLike} feature Feature.
* @param {import("../layer/Layer.js").default} layer Layer.
* @return {boolean|undefined} Continue to iterate over the features.
*/
function (feature, layer) {
if (!(feature instanceof Feature) || !this.filter_(feature, layer)) {
return;
}
this.addFeatureLayerAssociation_(feature, layer);
selected.push(feature);
return !this.multi_;
}.bind(this),
{
layerFilter: this.layerFilter_,
hitTolerance: this.hitTolerance_,
}
);
for (let i = features.getLength() - 1; i >= 0; --i) {
const feature = features.item(i);
const index = selected.indexOf(feature);
if (index > -1) {
// feature is already selected
selected.splice(index, 1);
} else {
features.remove(feature);
deselected.push(feature);
}
}
if (selected.length !== 0) {
features.extend(selected);
}
} else {
// Modify the currently selected feature(s).
map.forEachFeatureAtPixel(
mapBrowserEvent.pixel,
/**
* @param {import("../Feature.js").FeatureLike} feature Feature.
* @param {import("../layer/Layer.js").default} layer Layer.
* @return {boolean|undefined} Continue to iterate over the features.
*/
function (feature, layer) {
if (!(feature instanceof Feature) || !this.filter_(feature, layer)) {
return;
}
if ((add || toggle) && !features.getArray().includes(feature)) {
this.addFeatureLayerAssociation_(feature, layer);
selected.push(feature);
} else if (
(remove || toggle) &&
features.getArray().includes(feature)
) {
deselected.push(feature);
this.removeFeatureLayerAssociation_(feature);
}
return !this.multi_;
}.bind(this),
{
layerFilter: this.layerFilter_,
hitTolerance: this.hitTolerance_,
}
);
for (let j = deselected.length - 1; j >= 0; --j) {
features.remove(deselected[j]);
}
features.extend(selected);
}
if (selected.length > 0 || deselected.length > 0) {
this.dispatchEvent(
new SelectEvent(
SelectEventType.SELECT,
selected,
deselected,
mapBrowserEvent
)
);
}
return true;
}
}
/**
* @return {import("../style/Style.js").StyleFunction} Styles.
*/
function getDefaultStyleFunction() {
const styles = createEditingStyle();
extend(styles['Polygon'], styles['LineString']);
extend(styles['GeometryCollection'], styles['LineString']);
return function (feature) {
if (!feature.getGeometry()) {
return null;
}
return styles[feature.getGeometry().getType()];
};
}
export default Select;