diff --git a/src/ol/interaction/interaction.js b/src/ol/interaction/interaction.js index ffb2e7bf70..4e5ef9d738 100644 --- a/src/ol/interaction/interaction.js +++ b/src/ol/interaction/interaction.js @@ -12,6 +12,22 @@ goog.require('ol.easing'); * @constructor */ ol.interaction.Interaction = function() { + + /** + * @private + * @type {ol.Map} + */ + this.map_ = null; + +}; + + +/** + * Get the map associated with this interaction. + * @return {ol.Map} Map. + */ +ol.interaction.Interaction.prototype.getMap = function() { + return this.map_; }; @@ -25,6 +41,17 @@ ol.interaction.Interaction.prototype.handleMapBrowserEvent = goog.abstractMethod; +/** + * Remove the interaction from its current map and attach it to the new map. + * Subclasses may set up event handlers to get notified about changes to + * the map here. + * @param {ol.Map} map Map. + */ +ol.interaction.Interaction.prototype.setMap = function(map) { + this.map_ = map; +}; + + /** * @param {ol.Map} map Map. * @param {ol.View2D} view View. diff --git a/test/spec/ol/interaction/interaction.test.js b/test/spec/ol/interaction/interaction.test.js new file mode 100644 index 0000000000..5f2613404a --- /dev/null +++ b/test/spec/ol/interaction/interaction.test.js @@ -0,0 +1,50 @@ +goog.provide('ol.test.interaction.Interaction'); + +describe('ol.interaction.Interaction', function() { + + describe('constructor', function() { + + it('creates a new interaction', function() { + var interaction = new ol.interaction.Interaction(); + expect(interaction).to.be.a(ol.interaction.Interaction); + }); + + }); + + describe('#getMap()', function() { + + it('retrieves the associated map', function() { + var map = new ol.Map({}); + var interaction = new ol.interaction.Interaction(); + interaction.setMap(map); + expect(interaction.getMap()).to.be(map); + }); + + it('returns null if no map', function() { + var interaction = new ol.interaction.Interaction(); + expect(interaction.getMap()).to.be(null); + }); + + }); + + describe('#setMap()', function() { + + it('allows a map to be set', function() { + var map = new ol.Map({}); + var interaction = new ol.interaction.Interaction(); + interaction.setMap(map); + expect(interaction.getMap()).to.be(map); + }); + + it('accepts null', function() { + var interaction = new ol.interaction.Interaction(); + interaction.setMap(null); + expect(interaction.getMap()).to.be(null); + }); + + }); + +}); + +goog.require('ol.Map'); +goog.require('ol.interaction.Interaction');