Register/unregister listeners in setActive
This commit is contained in:
@@ -65,17 +65,14 @@ ol.inherits(ol.interaction.DragAndDrop, ol.interaction.Interaction);
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.interaction.DragAndDrop.handleDrop_ = function(event) {
|
ol.interaction.DragAndDrop.handleDrop_ = function(event) {
|
||||||
var active = this.getActive();
|
var files = event.dataTransfer.files;
|
||||||
if (active) {
|
var i, ii, file;
|
||||||
var files = event.dataTransfer.files;
|
for (i = 0, ii = files.length; i < ii; ++i) {
|
||||||
var i, ii, file;
|
file = files.item(i);
|
||||||
for (i = 0, ii = files.length; i < ii; ++i) {
|
var reader = new FileReader();
|
||||||
file = files.item(i);
|
reader.addEventListener(ol.events.EventType.LOAD,
|
||||||
var reader = new FileReader();
|
this.handleResult_.bind(this, file));
|
||||||
reader.addEventListener(ol.events.EventType.LOAD,
|
reader.readAsText(file);
|
||||||
this.handleResult_.bind(this, file));
|
|
||||||
reader.readAsText(file);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -85,12 +82,9 @@ ol.interaction.DragAndDrop.handleDrop_ = function(event) {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.interaction.DragAndDrop.handleStop_ = function(event) {
|
ol.interaction.DragAndDrop.handleStop_ = function(event) {
|
||||||
var active = this.getActive();
|
event.stopPropagation();
|
||||||
if (active) {
|
event.preventDefault();
|
||||||
event.stopPropagation();
|
event.dataTransfer.dropEffect = 'copy';
|
||||||
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) {
|
ol.interaction.DragAndDrop.prototype.registerListeners_ = function() {
|
||||||
if (this.dropListenKeys_) {
|
var map = this.getMap();
|
||||||
this.dropListenKeys_.forEach(ol.events.unlistenByKey);
|
|
||||||
this.dropListenKeys_ = null;
|
|
||||||
}
|
|
||||||
ol.interaction.Interaction.prototype.setMap.call(this, map);
|
|
||||||
if (map) {
|
if (map) {
|
||||||
var dropArea = this.target ? this.target : map.getViewport();
|
var dropArea = this.target ? this.target : map.getViewport();
|
||||||
this.dropListenKeys_ = [
|
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 {ol.format.Feature} format Format.
|
||||||
* @param {string} text Text.
|
* @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}
|
* @enum {string}
|
||||||
* @private
|
* @private
|
||||||
|
|||||||
@@ -37,7 +37,20 @@ describe('ol.interaction.DragAndDrop', function() {
|
|||||||
expect(interaction.formatConstructors_).to.have.length(1);
|
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() {
|
describe('#setMap()', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user