Skip to content

Commit 901350c

Browse files
committed
starting work on events
1 parent 4ed52c1 commit 901350c

File tree

8 files changed

+41
-36
lines changed

8 files changed

+41
-36
lines changed

API.md

+11-21
Original file line numberDiff line numberDiff line change
@@ -184,43 +184,32 @@ The three draw modes work identically. They do not take an options argument.
184184

185185
Draw fires off a number of events on draw and select actions. All of these events are name spaced `draw` inside of the mapboxgl event emitter.
186186

187-
### draw.change_mode
187+
### draw.modechange
188188

189-
This event is fired just after the current mode is stopped and just before the next mode is started.If the `new` object provided is changed, the changed values will be used to start the next mode.
189+
This event is fired just after the current mode is stopped and just before the next mode is started. A render will not happen until after all event handlers have been triggered. This means you can force a mode redirect by calling `changeMode` inside of a `draw.modechange` handler.
190190

191191
This is not fired when the first mode is started.
192192

193193
Here is an example payload.
194194

195195
```js
196196
{
197-
old: {
198-
mode: `default`,
199-
options: []
200-
},
201-
new: {
202-
mode: `direct_select`
203-
options: '123123123'
204-
}
197+
mode: `direct_select`
198+
opts: '123123123'
205199
}
206200
```
207201

208-
### draw.delete
202+
### draw.deleted
209203

210204
This is fired every time a feature is deleted inside of `mapbox-gl-draw`. The payload is the GeoJSON feature just before it was deleted.
211205

212-
### draw.active
206+
### draw.mode.default.selected.start
213207

214-
This is fired every time a feature is set to active or inactive. If a feature was active and is then set to active again, this is not fired. Same goes for inactive. This is only fired for features. Not for vertices.
208+
This is fired every time a feature is selected in the default mode. The payload is an array of feature ids being selected. This is **NOT** fired when the mode starts as this information is in the `draw.modechange` event.
215209

216-
Here is an example payload.
210+
### draw.mode.default.selected.end
217211

218-
```js
219-
{
220-
active: false,
221-
featureId: '123123123'
222-
}
223-
```
212+
This is fired every time a feature is unselected in the default mode. The payload is an array of feature ids being unselected. This is **NOT** fired when the mode stops, as this can be assumed via the `draw.modechange` event.
224213

225214
## Styling Draw
226215

@@ -286,5 +275,6 @@ property | values | function
286275
id | string | only available when `meta` is `feature`
287276
parent | string | only avaible when `meta` is not `feature`
288277
coord_path | string | a `.` seporated path to one [lon, lat] entity in the parents coordinates
289-
lon | number | the longitude value of a handle. Only available when `meta` is ` midpoint`.
278+
lon | number | the longitude value of a handle. Only available when `meta` is `midpoint`.
279+
lat | number | the latitude value of a handle. Only available when `meta` is `midpoint`.
290280
bbox | array | the bounding box of the hidden features. Only available when `meta` is `too-small`.

src/events.js

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ module.exports = function(ctx) {
106106
currentModeName = modename;
107107
var mode = modebuilder(ctx, opts);
108108
currentMode = ModeHandler(mode, ctx);
109+
110+
ctx.map.fire('draw.modechange', {
111+
mode: modename,
112+
opts: opts
113+
});
114+
109115
ctx.store.render();
110116
},
111117
fire: function(name, event) {

src/lib/to_midpoint.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function(parent, startVertex, endVertex, map) {
1313
parent: parent,
1414
lng: mid.lng,
1515
lat: mid.lat,
16-
path: endVertex.properties.path
16+
coord_path: endVertex.properties.coord_path
1717
},
1818
geometry: {
1919
type: 'Point',

src/lib/to_vertex.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = function(parent, coord, path, selected) {
55
properties: {
66
meta: 'vertex',
77
parent: parent,
8-
path: path,
8+
coord_path: path,
99
active: `${selected}`
1010
},
1111
geometry: {

src/modes/default.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
var {noFeature, isShiftDown, isFeature} = require('../lib/common_selectors');
22

3-
module.exports = function(ctx) {
3+
module.exports = function(ctx, startingSelectedFeatureIds) {
44

55
var selectedFeaturesById = {};
6+
(startingSelectedFeatureIds || []).forEach(id => {
7+
selectedFeaturesById[id] = ctx.store.get(id);
8+
});
69

710
var startPos = null;
811
var dragging = false;

src/modes/direct_select.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ module.exports = function(ctx, featureId) {
1717
dragging = true;
1818
startPos = e.lngLat;
1919
var about = e.featureTarget.properties;
20-
var selectedIndex = selectedCoordPaths.indexOf(about.path);
20+
var selectedIndex = selectedCoordPaths.indexOf(about.coord_path);
2121
if (!isShiftDown(e) && selectedIndex === -1) {
22-
selectedCoordPaths = [about.path];
22+
selectedCoordPaths = [about.coord_path];
2323
}
2424
else if (isShiftDown(e) && selectedIndex === -1) {
25-
selectedCoordPaths.push(about.path);
25+
selectedCoordPaths.push(about.coord_path);
2626
}
2727
};
2828

2929
var onMidpoint = function(e) {
3030
dragging = true;
3131
startPos = e.lngLat;
3232
var about = e.featureTarget.properties;
33-
feature.addCoordinate(about.path, about.lng, about.lat);
34-
selectedCoordPaths = [about.path];
33+
feature.addCoordinate(about.coord_path, about.lng, about.lat);
34+
selectedCoordPaths = [about.coord_path];
3535
};
3636

3737
var setupCoordPos = function() {
38-
coordPos = selectedCoordPaths.map(path => feature.getCoordinate(path));
38+
coordPos = selectedCoordPaths.map(coord_path => feature.getCoordinate(coord_path));
3939
numCoords = coordPos.length;
4040
};
4141

@@ -55,11 +55,11 @@ module.exports = function(ctx, featureId) {
5555
var latChange = e.lngLat.lat - startPos.lat;
5656

5757
for (var i = 0; i < numCoords; i++) {
58-
var path = selectedCoordPaths[i];
58+
var coord_path = selectedCoordPaths[i];
5959
var pos = coordPos[i];
6060
var lng = pos[0] + lngChange;
6161
var lat = pos[1] + latChange;
62-
feature.updateCoordinate(path, lng, lat);
62+
feature.updateCoordinate(coord_path, lng, lat);
6363
}
6464
});
6565
this.on('mouseup', () => true, function() {
@@ -100,9 +100,9 @@ module.exports = function(ctx, featureId) {
100100
let ring = geojson.geometry.coordinates[i];
101101
for (let j = 0; j < ring.length - 1; j++) {
102102
let coord = ring[j];
103-
let path = `${i}.${j}`;
103+
let coord_path = `${i}.${j}`;
104104

105-
vertices.push(toVertex(feature.id, coord, path, selectedCoordPaths.indexOf(path) > -1));
105+
vertices.push(toVertex(feature.id, coord, coord_path, selectedCoordPaths.indexOf(coord_path) > -1));
106106

107107
if (j > 0) {
108108
midpoints.push(toMidpoint(feature.id, vertices[j - 1], vertices[j], ctx.map));
@@ -112,8 +112,8 @@ module.exports = function(ctx, featureId) {
112112
}
113113
else {
114114
let coord = geojson.geometry.coordinates[i];
115-
let path = `${i}`;
116-
vertices.push(toVertex(feature.id, coord, path, selectedCoordPaths.indexOf(path) > -1));
115+
let coord_path = `${i}`;
116+
vertices.push(toVertex(feature.id, coord, coord_path, selectedCoordPaths.indexOf(coord_path) > -1));
117117
if (i > 0) {
118118
midpoints.push(toMidpoint(feature.id, vertices[i - 1], vertices[i], ctx.map));
119119
}

src/modes/draw_line_string.js

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ module.exports = function(ctx) {
5858
stop: function() {
5959
ctx.ui.setButtonInactive(types.LINE);
6060
ctx.ui.clearClass();
61+
if (!feature.isValid()) {
62+
ctx.store.delete(feature.id);
63+
}
6164
},
6265
render: function(geojson) {
6366
geojson.properties.active = geojson.properties.id === feature.id ? 'true' : 'false';

src/modes/draw_polygon.js

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ module.exports = function(ctx) {
5454
stop: function() {
5555
ctx.ui.setButtonInactive(types.POLYGON);
5656
ctx.ui.clearClass();
57+
if (!feature.isValid()) {
58+
ctx.store.delete(feature.id);
59+
}
5760
},
5861
render: function(geojson) {
5962
geojson.properties.active = geojson.properties.id === feature.id ? 'true' : 'false';

0 commit comments

Comments
 (0)