-
Notifications
You must be signed in to change notification settings - Fork 592
/
simple_select.js
389 lines (324 loc) · 12.2 KB
/
simple_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
import * as CommonSelectors from '../lib/common_selectors.js';
import mouseEventPoint from '../lib/mouse_event_point.js';
import createSupplementaryPoints from '../lib/create_supplementary_points.js';
import StringSet from '../lib/string_set.js';
import doubleClickZoom from '../lib/double_click_zoom.js';
import moveFeatures from '../lib/move_features.js';
import * as Constants from '../constants.js';
const SimpleSelect = {};
SimpleSelect.onSetup = function(opts) {
// turn the opts into state.
const state = {
dragMoveLocation: null,
boxSelectStartLocation: null,
boxSelectElement: undefined,
boxSelecting: false,
canBoxSelect: false,
dragMoving: false,
canDragMove: false,
initialDragPanState: this.map.dragPan.isEnabled(),
initiallySelectedFeatureIds: opts.featureIds || []
};
this.setSelected(state.initiallySelectedFeatureIds.filter(id => this.getFeature(id) !== undefined));
this.fireActionable();
this.setActionableState({
combineFeatures: true,
uncombineFeatures: true,
trash: true
});
return state;
};
SimpleSelect.fireUpdate = function() {
this.fire(Constants.events.UPDATE, {
action: Constants.updateActions.MOVE,
features: this.getSelected().map(f => f.toGeoJSON())
});
};
SimpleSelect.fireActionable = function() {
const selectedFeatures = this.getSelected();
const multiFeatures = selectedFeatures.filter(
feature => this.isInstanceOf('MultiFeature', feature)
);
let combineFeatures = false;
if (selectedFeatures.length > 1) {
combineFeatures = true;
const featureType = selectedFeatures[0].type.replace('Multi', '');
selectedFeatures.forEach((feature) => {
if (feature.type.replace('Multi', '') !== featureType) {
combineFeatures = false;
}
});
}
const uncombineFeatures = multiFeatures.length > 0;
const trash = selectedFeatures.length > 0;
this.setActionableState({
combineFeatures, uncombineFeatures, trash
});
};
SimpleSelect.getUniqueIds = function(allFeatures) {
if (!allFeatures.length) return [];
const ids = allFeatures.map(s => s.properties.id)
.filter(id => id !== undefined)
.reduce((memo, id) => {
memo.add(id);
return memo;
}, new StringSet());
return ids.values();
};
SimpleSelect.stopExtendedInteractions = function(state) {
if (state.boxSelectElement) {
if (state.boxSelectElement.parentNode) state.boxSelectElement.parentNode.removeChild(state.boxSelectElement);
state.boxSelectElement = null;
}
if ((state.canDragMove || state.canBoxSelect) && state.initialDragPanState === true) {
this.map.dragPan.enable();
}
state.boxSelecting = false;
state.canBoxSelect = false;
state.dragMoving = false;
state.canDragMove = false;
};
SimpleSelect.onStop = function() {
doubleClickZoom.enable(this);
};
SimpleSelect.onMouseMove = function(state, e) {
const isFeature = CommonSelectors.isFeature(e);
if (isFeature && state.dragMoving) this.fireUpdate();
// On mousemove that is not a drag, stop extended interactions.
// This is useful if you drag off the canvas, release the button,
// then move the mouse back over the canvas --- we don't allow the
// interaction to continue then, but we do let it continue if you held
// the mouse button that whole time
this.stopExtendedInteractions(state);
// Skip render
return true;
};
SimpleSelect.onMouseOut = function(state) {
// As soon as you mouse leaves the canvas, update the feature
if (state.dragMoving) return this.fireUpdate();
// Skip render
return true;
};
SimpleSelect.onTap = SimpleSelect.onClick = function(state, e) {
// Click (with or without shift) on no feature
if (CommonSelectors.noTarget(e)) return this.clickAnywhere(state, e); // also tap
if (CommonSelectors.isOfMetaType(Constants.meta.VERTEX)(e)) return this.clickOnVertex(state, e); //tap
if (CommonSelectors.isFeature(e)) return this.clickOnFeature(state, e);
};
SimpleSelect.clickAnywhere = function (state) {
// Clear the re-render selection
const wasSelected = this.getSelectedIds();
if (wasSelected.length) {
this.clearSelectedFeatures();
wasSelected.forEach(id => this.doRender(id));
}
doubleClickZoom.enable(this);
this.stopExtendedInteractions(state);
};
SimpleSelect.clickOnVertex = function(state, e) {
// Enter direct select mode
this.changeMode(Constants.modes.DIRECT_SELECT, {
featureId: e.featureTarget.properties.parent,
coordPath: e.featureTarget.properties.coord_path,
startPos: e.lngLat
});
this.updateUIClasses({ mouse: Constants.cursors.MOVE });
};
SimpleSelect.startOnActiveFeature = function(state, e) {
// Stop any already-underway extended interactions
this.stopExtendedInteractions(state);
// Disable map.dragPan immediately so it can't start
this.map.dragPan.disable();
// Re-render it and enable drag move
this.doRender(e.featureTarget.properties.id);
// Set up the state for drag moving
state.canDragMove = true;
state.dragMoveLocation = e.lngLat;
};
SimpleSelect.clickOnFeature = function(state, e) {
// Stop everything
doubleClickZoom.disable(this);
this.stopExtendedInteractions(state);
const isShiftClick = CommonSelectors.isShiftDown(e);
const selectedFeatureIds = this.getSelectedIds();
const featureId = e.featureTarget.properties.id;
const isFeatureSelected = this.isSelected(featureId);
// Click (without shift) on any selected feature but a point
if (!isShiftClick && isFeatureSelected && this.getFeature(featureId).type !== Constants.geojsonTypes.POINT) {
// Enter direct select mode
return this.changeMode(Constants.modes.DIRECT_SELECT, {
featureId
});
}
// Shift-click on a selected feature
if (isFeatureSelected && isShiftClick) {
// Deselect it
this.deselect(featureId);
this.updateUIClasses({ mouse: Constants.cursors.POINTER });
if (selectedFeatureIds.length === 1) {
doubleClickZoom.enable(this);
}
// Shift-click on an unselected feature
} else if (!isFeatureSelected && isShiftClick) {
// Add it to the selection
this.select(featureId);
this.updateUIClasses({ mouse: Constants.cursors.MOVE });
// Click (without shift) on an unselected feature
} else if (!isFeatureSelected && !isShiftClick) {
// Make it the only selected feature
selectedFeatureIds.forEach(id => this.doRender(id));
this.setSelected(featureId);
this.updateUIClasses({ mouse: Constants.cursors.MOVE });
}
// No matter what, re-render the clicked feature
this.doRender(featureId);
};
SimpleSelect.onMouseDown = function(state, e) {
state.initialDragPanState = this.map.dragPan.isEnabled();
if (CommonSelectors.isActiveFeature(e)) return this.startOnActiveFeature(state, e);
if (this.drawConfig.boxSelect && CommonSelectors.isShiftMousedown(e)) return this.startBoxSelect(state, e);
};
SimpleSelect.startBoxSelect = function(state, e) {
this.stopExtendedInteractions(state);
this.map.dragPan.disable();
// Enable box select
state.boxSelectStartLocation = mouseEventPoint(e.originalEvent, this.map.getContainer());
state.canBoxSelect = true;
};
SimpleSelect.onTouchStart = function(state, e) {
if (CommonSelectors.isActiveFeature(e)) return this.startOnActiveFeature(state, e);
};
SimpleSelect.onDrag = function(state, e) {
if (state.canDragMove) return this.dragMove(state, e);
if (this.drawConfig.boxSelect && state.canBoxSelect) return this.whileBoxSelect(state, e);
};
SimpleSelect.whileBoxSelect = function(state, e) {
state.boxSelecting = true;
this.updateUIClasses({ mouse: Constants.cursors.ADD });
// Create the box node if it doesn't exist
if (!state.boxSelectElement) {
state.boxSelectElement = document.createElement('div');
state.boxSelectElement.classList.add(Constants.classes.BOX_SELECT);
this.map.getContainer().appendChild(state.boxSelectElement);
}
// Adjust the box node's width and xy position
const current = mouseEventPoint(e.originalEvent, this.map.getContainer());
const minX = Math.min(state.boxSelectStartLocation.x, current.x);
const maxX = Math.max(state.boxSelectStartLocation.x, current.x);
const minY = Math.min(state.boxSelectStartLocation.y, current.y);
const maxY = Math.max(state.boxSelectStartLocation.y, current.y);
const translateValue = `translate(${minX}px, ${minY}px)`;
state.boxSelectElement.style.transform = translateValue;
state.boxSelectElement.style.WebkitTransform = translateValue;
state.boxSelectElement.style.width = `${maxX - minX}px`;
state.boxSelectElement.style.height = `${maxY - minY}px`;
};
SimpleSelect.dragMove = function(state, e) {
// Dragging when drag move is enabled
state.dragMoving = true;
e.originalEvent.stopPropagation();
const delta = {
lng: e.lngLat.lng - state.dragMoveLocation.lng,
lat: e.lngLat.lat - state.dragMoveLocation.lat
};
moveFeatures(this.getSelected(), delta);
state.dragMoveLocation = e.lngLat;
};
SimpleSelect.onTouchEnd = SimpleSelect.onMouseUp = function(state, e) {
// End any extended interactions
if (state.dragMoving) {
this.fireUpdate();
} else if (state.boxSelecting) {
const bbox = [
state.boxSelectStartLocation,
mouseEventPoint(e.originalEvent, this.map.getContainer())
];
const featuresInBox = this.featuresAt(null, bbox, 'click');
const idsToSelect = this.getUniqueIds(featuresInBox)
.filter(id => !this.isSelected(id));
if (idsToSelect.length) {
this.select(idsToSelect);
idsToSelect.forEach(id => this.doRender(id));
this.updateUIClasses({ mouse: Constants.cursors.MOVE });
}
}
this.stopExtendedInteractions(state);
};
SimpleSelect.toDisplayFeatures = function(state, geojson, display) {
geojson.properties.active = (this.isSelected(geojson.properties.id)) ?
Constants.activeStates.ACTIVE : Constants.activeStates.INACTIVE;
display(geojson);
this.fireActionable();
if (geojson.properties.active !== Constants.activeStates.ACTIVE ||
geojson.geometry.type === Constants.geojsonTypes.POINT) return;
createSupplementaryPoints(geojson).forEach(display);
};
SimpleSelect.onTrash = function() {
this.deleteFeature(this.getSelectedIds());
this.fireActionable();
};
SimpleSelect.onCombineFeatures = function() {
const selectedFeatures = this.getSelected();
if (selectedFeatures.length === 0 || selectedFeatures.length < 2) return;
const coordinates = [], featuresCombined = [];
const featureType = selectedFeatures[0].type.replace('Multi', '');
for (let i = 0; i < selectedFeatures.length; i++) {
const feature = selectedFeatures[i];
if (feature.type.replace('Multi', '') !== featureType) {
return;
}
if (feature.type.includes('Multi')) {
feature.getCoordinates().forEach((subcoords) => {
coordinates.push(subcoords);
});
} else {
coordinates.push(feature.getCoordinates());
}
featuresCombined.push(feature.toGeoJSON());
}
if (featuresCombined.length > 1) {
const multiFeature = this.newFeature({
type: Constants.geojsonTypes.FEATURE,
properties: featuresCombined[0].properties,
geometry: {
type: `Multi${featureType}`,
coordinates
}
});
this.addFeature(multiFeature);
this.deleteFeature(this.getSelectedIds(), { silent: true });
this.setSelected([multiFeature.id]);
this.fire(Constants.events.COMBINE_FEATURES, {
createdFeatures: [multiFeature.toGeoJSON()],
deletedFeatures: featuresCombined
});
}
this.fireActionable();
};
SimpleSelect.onUncombineFeatures = function() {
const selectedFeatures = this.getSelected();
if (selectedFeatures.length === 0) return;
const createdFeatures = [];
const featuresUncombined = [];
for (let i = 0; i < selectedFeatures.length; i++) {
const feature = selectedFeatures[i];
if (this.isInstanceOf('MultiFeature', feature)) {
feature.getFeatures().forEach((subFeature) => {
this.addFeature(subFeature);
subFeature.properties = feature.properties;
createdFeatures.push(subFeature.toGeoJSON());
this.select([subFeature.id]);
});
this.deleteFeature(feature.id, { silent: true });
featuresUncombined.push(feature.toGeoJSON());
}
}
if (createdFeatures.length > 1) {
this.fire(Constants.events.UNCOMBINE_FEATURES, {
createdFeatures,
deletedFeatures: featuresUncombined
});
}
this.fireActionable();
};
export default SimpleSelect;