Add addInteraction and removeInteraction methods to the map

This is in line with how we add/remove controls and overlays.
This commit is contained in:
Tim Schaub
2013-10-25 12:24:57 -06:00
parent 7d2d68c011
commit 7fb56579c5
2 changed files with 85 additions and 0 deletions

View File

@@ -350,6 +350,14 @@ ol.Map = function(options) {
control.setMap(this);
}, this);
this.interactions_.forEach(
/**
* @param {ol.interaction.Interaction} interaction Interaction.
*/
function(interaction) {
interaction.setMap(this);
}, this);
this.overlays_.forEach(
/**
* @param {ol.Overlay} overlay Overlay.
@@ -375,6 +383,18 @@ ol.Map.prototype.addControl = function(control) {
};
/**
* Add the given interaction to the map.
* @param {ol.interaction.Interaction} interaction Interaction to add.
*/
ol.Map.prototype.addInteraction = function(interaction) {
var interactions = this.getInteractions();
goog.asserts.assert(goog.isDef(interactions));
interactions.push(interaction);
interaction.setMap(this);
};
/**
* Adds the given layer to the top of this map.
* @param {ol.layer.Base} layer Layer.
@@ -953,6 +973,24 @@ ol.Map.prototype.removeControl = function(control) {
};
/**
* Remove the given interaction from the map.
* @param {ol.interaction.Interaction} interaction Interaction to remove.
* @return {ol.interaction.Interaction|undefined} The removed interaction (or
* undefined if the interaction was not found).
*/
ol.Map.prototype.removeInteraction = function(interaction) {
var removed;
var interactions = this.getInteractions();
goog.asserts.assert(goog.isDef(interactions));
if (goog.isDef(interactions.remove(interaction))) {
interaction.setMap(null);
removed = interaction;
}
return removed;
};
/**
* Removes the given layer from the map.
* @param {ol.layer.Base} layer Layer.

View File

@@ -51,6 +51,52 @@ describe('ol.RendererHints', function() {
describe('ol.Map', function() {
describe('contstructor', function() {
it('creates a new map', function() {
var map = new ol.Map({});
expect(map).to.be.a(ol.Map);
});
it('creates a set of default interactions', function() {
var map = new ol.Map({});
var interactions = map.getInteractions();
var length = interactions.getLength();
expect(length).to.be.greaterThan(0);
for (var i = 0; i < length; ++i) {
expect(interactions.getAt(i).getMap()).to.be(map);
}
});
});
describe('#addInteraction()', function() {
it('adds an interaction to the map', function() {
var map = new ol.Map({});
var interaction = new ol.interaction.Interaction();
var before = map.getInteractions().getLength();
map.addInteraction(interaction);
var after = map.getInteractions().getLength();
expect(after).to.be(before + 1);
expect(interaction.getMap()).to.be(map);
});
});
describe('#removeInteraction()', function() {
it('removes an interaction from the map', function() {
var map = new ol.Map({});
var interaction = new ol.interaction.Interaction();
var before = map.getInteractions().getLength();
map.addInteraction(interaction);
map.removeInteraction(interaction);
expect(map.getInteractions().getLength()).to.be(before);
expect(interaction.getMap()).to.be(null);
});
});
describe('dispose', function() {
var map;
@@ -128,5 +174,6 @@ goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.RendererHints');
goog.require('ol.interaction');
goog.require('ol.interaction.Interaction');
goog.require('ol.interaction.DoubleClickZoom');
goog.require('ol.interaction.MouseWheelZoom');