Skip to content

Commit

Permalink
Merge pull request openlayers#3573 from gberaudo/interaction_dispatch…
Browse files Browse the repository at this point in the history
…_order

Modify draw interaction dispatch order
  • Loading branch information
bartvde committed Apr 16, 2015
2 parents 14d546c + a7e4e20 commit 4306ecb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
6 changes: 5 additions & 1 deletion changelog/upgrade-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ size of a sub-rectangle in an image sprite.

### Support for non-square tiles

The return value of ´ol.tilegrid.TileGrid#getTileSize()` will now be an `ol.Size` array instead of a number if non-square tiles (i.e. an `ol.Size` array instead of a number as `tilsSize`) are used. To always get an `ol.Size`, the new `ol.size.toSize()` was added.
The return value of `ol.tilegrid.TileGrid#getTileSize()` will now be an `ol.Size` array instead of a number if non-square tiles (i.e. an `ol.Size` array instead of a number as `tilsSize`) are used. To always get an `ol.Size`, the new `ol.size.toSize()` was added.

### Change to `ol.interaction.Draw`

When finishing a draw, the `drawend` event is now dispatched before the feature is inserted to either the source or the collection. This change allows application code to finish setting up the feature.

### v3.4.0

Expand Down
7 changes: 6 additions & 1 deletion src/ol/interaction/drawinteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ ol.interaction.Draw.prototype.addToDrawing_ = function(event) {

/**
* Stop drawing and add the sketch feature to the target layer.
* The {@link ol.DrawEventType.DRAWEND} event is dispatched before inserting
* the feature.
* @api
*/
ol.interaction.Draw.prototype.finishDrawing = function() {
Expand Down Expand Up @@ -557,13 +559,16 @@ ol.interaction.Draw.prototype.finishDrawing = function() {
sketchFeature.setGeometry(new ol.geom.MultiPolygon([coordinates]));
}

// First dispatch event to allow full set up of feature
this.dispatchEvent(new ol.DrawEvent(ol.DrawEventType.DRAWEND, sketchFeature));

// Then insert feature
if (!goog.isNull(this.features_)) {
this.features_.push(sketchFeature);
}
if (!goog.isNull(this.source_)) {
this.source_.addFeature(sketchFeature);
}
this.dispatchEvent(new ol.DrawEvent(ol.DrawEventType.DRAWEND, sketchFeature));
};


Expand Down
24 changes: 24 additions & 0 deletions test/spec/ol/interaction/drawinteraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ describe('ol.interaction.Draw', function() {
expect(ds).to.be.called(2);
expect(de).to.be.called(1);
});

it('triggers drawend event before inserting the feature', function() {
var receivedEvents = {
end: 0,
addfeature: 0
};
goog.events.listen(draw, ol.DrawEventType.DRAWEND, function() {
expect(receivedEvents.end).to.be(0);
expect(receivedEvents.addfeature).to.be(0);
++receivedEvents.end;
});
source.on(ol.source.VectorEventType.ADDFEATURE, function() {
expect(receivedEvents.end).to.be(1);
expect(receivedEvents.addfeature).to.be(0);
receivedEvents.addfeature++;
});
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
simulateEvent('pointermove', 20, 20);
expect(receivedEvents.end).to.be(1);
expect(receivedEvents.addfeature).to.be(1);
});
});

describe('drawing multipoints', function() {
Expand Down Expand Up @@ -661,3 +684,4 @@ goog.require('ol.interaction.Interaction');
goog.require('ol.layer.Vector');
goog.require('ol.pointer.PointerEvent');
goog.require('ol.source.Vector');
goog.require('ol.source.VectorEventType');

0 comments on commit 4306ecb

Please sign in to comment.