Merge pull request #1173 from tschaub/map-interaction
Give interactions a reference to their map.
This commit is contained in:
@@ -12,6 +12,22 @@ goog.require('ol.easing');
|
|||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
ol.interaction.Interaction = function() {
|
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;
|
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.Map} map Map.
|
||||||
* @param {ol.View2D} view View.
|
* @param {ol.View2D} view View.
|
||||||
|
|||||||
@@ -356,6 +356,14 @@ ol.Map = function(options) {
|
|||||||
control.setMap(this);
|
control.setMap(this);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
this.interactions_.forEach(
|
||||||
|
/**
|
||||||
|
* @param {ol.interaction.Interaction} interaction Interaction.
|
||||||
|
*/
|
||||||
|
function(interaction) {
|
||||||
|
interaction.setMap(this);
|
||||||
|
}, this);
|
||||||
|
|
||||||
this.overlays_.forEach(
|
this.overlays_.forEach(
|
||||||
/**
|
/**
|
||||||
* @param {ol.Overlay} overlay Overlay.
|
* @param {ol.Overlay} overlay Overlay.
|
||||||
@@ -381,6 +389,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.
|
* Adds the given layer to the top of this map.
|
||||||
* @param {ol.layer.Base} layer Layer.
|
* @param {ol.layer.Base} layer Layer.
|
||||||
@@ -959,6 +979,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.
|
* Removes the given layer from the map.
|
||||||
* @param {ol.layer.Base} layer Layer.
|
* @param {ol.layer.Base} layer Layer.
|
||||||
|
|||||||
@@ -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');
|
||||||
@@ -51,6 +51,52 @@ describe('ol.RendererHints', function() {
|
|||||||
|
|
||||||
describe('ol.Map', 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() {
|
describe('dispose', function() {
|
||||||
var map;
|
var map;
|
||||||
|
|
||||||
@@ -128,5 +174,6 @@ goog.require('ol.Map');
|
|||||||
goog.require('ol.RendererHint');
|
goog.require('ol.RendererHint');
|
||||||
goog.require('ol.RendererHints');
|
goog.require('ol.RendererHints');
|
||||||
goog.require('ol.interaction');
|
goog.require('ol.interaction');
|
||||||
|
goog.require('ol.interaction.Interaction');
|
||||||
goog.require('ol.interaction.DoubleClickZoom');
|
goog.require('ol.interaction.DoubleClickZoom');
|
||||||
goog.require('ol.interaction.MouseWheelZoom');
|
goog.require('ol.interaction.MouseWheelZoom');
|
||||||
|
|||||||
Reference in New Issue
Block a user