diff --git a/src/ol/interaction/translate.js b/src/ol/interaction/translate.js index 54d911808b..57526896fa 100644 --- a/src/ol/interaction/translate.js +++ b/src/ol/interaction/translate.js @@ -2,6 +2,7 @@ goog.provide('ol.interaction.Translate'); goog.require('ol'); goog.require('ol.Collection'); +goog.require('ol.events'); goog.require('ol.events.Event'); goog.require('ol.functions'); goog.require('ol.array'); @@ -82,6 +83,11 @@ ol.interaction.Translate = function(opt_options) { * @private */ this.lastFeature_ = null; + + ol.events.listen(this, + ol.Object.getChangeEventType(ol.interaction.Property.ACTIVE), + this.handleActiveChanged_, this); + }; ol.inherits(ol.interaction.Translate, ol.interaction.Pointer); @@ -233,6 +239,43 @@ ol.interaction.Translate.prototype.setHitTolerance = function(hitTolerance) { }; +/** + * @inheritDoc + */ +ol.interaction.Translate.prototype.setMap = function(map) { + var oldMap = this.getMap(); + ol.interaction.Pointer.prototype.setMap.call(this, map); + this.updateState_(oldMap); +}; + + +/** + * @private + */ +ol.interaction.Translate.prototype.handleActiveChanged_ = function() { + this.updateState_(null); +}; + + +/** + * @param {ol.Map} oldMap Old map. + * @private + */ +ol.interaction.Translate.prototype.updateState_ = function(oldMap) { + var map = this.getMap(); + var active = this.getActive(); + if ((!map || !active) && this.previousCursor_ !== undefined) { + if (!map) { + map = oldMap; + } + + var elem = map.getTargetElement(); + elem.style.cursor = this.previousCursor_; + this.previousCursor_ = undefined; + } +}; + + /** * @classdesc * Events emitted by {@link ol.interaction.Translate} instances are instances of diff --git a/test/spec/ol/interaction/translate.test.js b/test/spec/ol/interaction/translate.test.js index 5cec115461..b79cbce77e 100644 --- a/test/spec/ol/interaction/translate.test.js +++ b/test/spec/ol/interaction/translate.test.js @@ -233,6 +233,50 @@ describe('ol.interaction.Translate', function() { expect(element.style.cursor).to.eql('pointer'); }); + it('resets css cursor when interaction is deactivated while pointer is on feature', function() { + simulateEvent('pointermove', 10, 20); + expect(element.style.cursor).to.match(/grab$/); + + translate.setActive(false); + + simulateEvent('pointermove', 0, 0); + expect(element.style.cursor).to.eql(''); + }); + + it('resets css cursor to existing cursor when interaction is deactivated while pointer is on feature', function() { + element.style.cursor = 'pointer'; + + simulateEvent('pointermove', 10, 20); + expect(element.style.cursor).to.match(/grab$/); + + translate.setActive(false); + + simulateEvent('pointermove', 0, 0); + expect(element.style.cursor).to.eql('pointer'); + }); + + it('resets css cursor interaction is removed while pointer is on feature', function() { + simulateEvent('pointermove', 10, 20); + expect(element.style.cursor).to.match(/grab$/); + + map.removeInteraction(translate); + + simulateEvent('pointermove', 0, 0); + expect(element.style.cursor).to.eql(''); + }); + + it('resets css cursor to existing cursor interaction is removed while pointer is on feature', function() { + element.style.cursor = 'pointer'; + + simulateEvent('pointermove', 10, 20); + expect(element.style.cursor).to.match(/grab$/); + + map.removeInteraction(translate); + + simulateEvent('pointermove', 0, 0); + expect(element.style.cursor).to.eql('pointer'); + }); + }); });