Register/unregister listeners in setActive

This commit is contained in:
Andreas Hocevar
2017-05-24 14:30:47 +02:00
parent 0e4d2b50b4
commit 278d3a0313
2 changed files with 63 additions and 24 deletions

View File

@@ -65,8 +65,6 @@ ol.inherits(ol.interaction.DragAndDrop, ol.interaction.Interaction);
* @private
*/
ol.interaction.DragAndDrop.handleDrop_ = function(event) {
var active = this.getActive();
if (active) {
var files = event.dataTransfer.files;
var i, ii, file;
for (i = 0, ii = files.length; i < ii; ++i) {
@@ -76,7 +74,6 @@ ol.interaction.DragAndDrop.handleDrop_ = function(event) {
this.handleResult_.bind(this, file));
reader.readAsText(file);
}
}
};
@@ -85,12 +82,9 @@ ol.interaction.DragAndDrop.handleDrop_ = function(event) {
* @private
*/
ol.interaction.DragAndDrop.handleStop_ = function(event) {
var active = this.getActive();
if (active) {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
}
};
@@ -147,14 +141,10 @@ ol.interaction.DragAndDrop.handleEvent = ol.functions.TRUE;
/**
* @inheritDoc
* @private
*/
ol.interaction.DragAndDrop.prototype.setMap = function(map) {
if (this.dropListenKeys_) {
this.dropListenKeys_.forEach(ol.events.unlistenByKey);
this.dropListenKeys_ = null;
}
ol.interaction.Interaction.prototype.setMap.call(this, map);
ol.interaction.DragAndDrop.prototype.registerListeners_ = function() {
var map = this.getMap();
if (map) {
var dropArea = this.target ? this.target : map.getViewport();
this.dropListenKeys_ = [
@@ -171,6 +161,31 @@ ol.interaction.DragAndDrop.prototype.setMap = function(map) {
};
/**
* @inheritDoc
*/
ol.interaction.DragAndDrop.prototype.setActive = function(active) {
ol.interaction.Interaction.prototype.setActive.call(this, active);
if (active) {
this.registerListeners_();
} else {
this.unregisterListeners_();
}
};
/**
* @inheritDoc
*/
ol.interaction.DragAndDrop.prototype.setMap = function(map) {
this.unregisterListeners_();
ol.interaction.Interaction.prototype.setMap.call(this, map);
if (this.getActive()) {
this.registerListeners_();
}
};
/**
* @param {ol.format.Feature} format Format.
* @param {string} text Text.
@@ -187,6 +202,17 @@ ol.interaction.DragAndDrop.prototype.tryReadFeatures_ = function(format, text, o
};
/**
* @private
*/
ol.interaction.DragAndDrop.prototype.unregisterListeners_ = function() {
if (this.dropListenKeys_) {
this.dropListenKeys_.forEach(ol.events.unlistenByKey);
this.dropListenKeys_ = null;
}
};
/**
* @enum {string}
* @private

View File

@@ -37,7 +37,20 @@ describe('ol.interaction.DragAndDrop', function() {
expect(interaction.formatConstructors_).to.have.length(1);
});
});
describe('#setActive()', function() {
it('registers and unregisters listeners', function() {
interaction.setMap(map);
interaction.setActive(true);
expect(viewport.hasListener('dragenter')).to.be(true);
expect(viewport.hasListener('dragover')).to.be(true);
expect(viewport.hasListener('drop')).to.be(true);
interaction.setActive(false);
expect(viewport.hasListener('dragenter')).to.be(false);
expect(viewport.hasListener('dragover')).to.be(false);
expect(viewport.hasListener('drop')).to.be(false);
});
});
describe('#setMap()', function() {