Merge pull request #2743 from elemoine/drawinteraction

Handle ol.interaction.Draw activation/deactivation
This commit is contained in:
Éric Lemoine
2014-10-08 16:01:10 +02:00
2 changed files with 137 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ goog.provide('ol.DrawEvent');
goog.provide('ol.interaction.Draw');
goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('ol.Collection');
goog.require('ol.Coordinate');
@@ -10,6 +11,7 @@ goog.require('ol.FeatureOverlay');
goog.require('ol.Map');
goog.require('ol.MapBrowserEvent');
goog.require('ol.MapBrowserEvent.EventType');
goog.require('ol.Object');
goog.require('ol.events.condition');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString');
@@ -18,6 +20,7 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.interaction.InteractionProperty');
goog.require('ol.interaction.Pointer');
goog.require('ol.source.Vector');
goog.require('ol.style.Style');
@@ -204,6 +207,10 @@ ol.interaction.Draw = function(options) {
this.condition_ = goog.isDef(options.condition) ?
options.condition : ol.events.condition.noModifierKeys;
goog.events.listen(this,
ol.Object.getChangeEventType(ol.interaction.InteractionProperty.ACTIVE),
this.updateState_, false, this);
};
goog.inherits(ol.interaction.Draw, ol.interaction.Pointer);
@@ -223,12 +230,8 @@ ol.interaction.Draw.getDefaultStyleFunction = function() {
* @inheritDoc
*/
ol.interaction.Draw.prototype.setMap = function(map) {
if (goog.isNull(map)) {
// removing from a map, clean up
this.abortDrawing_();
}
this.overlay_.setMap(map);
goog.base(this, 'setMap', map);
this.updateState_();
};
@@ -562,6 +565,19 @@ ol.interaction.Draw.prototype.updateSketchFeatures_ = function() {
};
/**
* @private
*/
ol.interaction.Draw.prototype.updateState_ = function() {
var map = this.getMap();
var active = this.getActive();
if (goog.isNull(map) || !active) {
this.abortDrawing_();
}
this.overlay_.setMap(active ? map : null);
};
/**
* Get the drawing mode. The mode for mult-part geometries is the same as for
* their single-part cousins.

View File

@@ -470,6 +470,122 @@ describe('ol.interaction.Draw', function() {
});
describe('#setActive()', function() {
var interaction;
beforeEach(function() {
interaction = new ol.interaction.Draw({
type: ol.geom.GeometryType.LINE_STRING
});
expect(interaction.getActive()).to.be(true);
map.addInteraction(interaction);
// first point
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
expect(interaction.sketchFeature_).not.to.be(null);
});
afterEach(function() {
map.removeInteraction(interaction);
});
describe('#setActive(false)', function() {
it('unsets the map from the feature overlay', function() {
interaction.setActive(false);
expect(interaction.overlay_.map_).to.be(null);
});
it('aborts the drawing', function() {
interaction.setActive(false);
expect(interaction.sketchFeature_).to.be(null);
});
it('fires change:active', function() {
listenerSpy = sinon.spy(function() {
// test that the interaction's change:active listener is called first
expect(interaction.overlay_.map_).to.be(null);
});
interaction.on('change:active', listenerSpy);
interaction.setActive(false);
expect(listenerSpy.callCount).to.be(1);
});
});
describe('#setActive(true)', function() {
beforeEach(function() {
interaction.setActive(false);
});
it('sets the map into the feature overlay', function() {
interaction.setActive(true);
expect(interaction.overlay_.map_).to.be(map);
});
it('fires change:active', function() {
listenerSpy = sinon.spy(function() {
// test that the interaction's change:active listener is called first
expect(interaction.overlay_.map_).not.to.be(null);
});
interaction.on('change:active', listenerSpy);
interaction.setActive(true);
expect(listenerSpy.callCount).to.be(1);
});
});
});
describe('#setMap()', function() {
var interaction;
beforeEach(function() {
interaction = new ol.interaction.Draw({
type: ol.geom.GeometryType.LINE_STRING
});
expect(interaction.getActive()).to.be(true);
});
describe('#setMap(null)', function() {
beforeEach(function() {
map.addInteraction(interaction);
// first point
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
expect(interaction.sketchFeature_).not.to.be(null);
});
afterEach(function() {
map.removeInteraction(interaction);
});
describe('#setMap(null) when interaction is active', function() {
it('unsets the map from the feature overlay', function() {
interaction.setMap(null);
expect(interaction.overlay_.map_).to.be(null);
});
it('aborts the drawing', function() {
interaction.setMap(null);
expect(interaction.sketchFeature_).to.be(null);
});
});
});
describe('#setMap(map)', function() {
describe('#setMap(map) when interaction is active', function() {
it('sets the map into the feature overlay', function() {
interaction.setMap(map);
expect(interaction.overlay_.map_).to.be(map);
});
});
describe('#setMap(map) when interaction is not active', function() {
it('does not set the map into the feature overlay', function() {
interaction.setActive(false);
interaction.setMap(map);
expect(interaction.overlay_.map_).to.be(null);
});
});
});
});
});
goog.require('goog.dispose');