Allow drag and drop interaction to be configured with a source

This commit is contained in:
Tim Schaub
2017-08-10 07:10:13 -06:00
parent 23405b80a2
commit 17b6088a79
3 changed files with 67 additions and 1 deletions

View File

@@ -6,7 +6,7 @@ goog.require('ol.events.Event');
goog.require('ol.events.EventTarget');
goog.require('ol.format.GeoJSON');
goog.require('ol.interaction.DragAndDrop');
goog.require('ol.source.Vector');
where('FileReader').describe('ol.interaction.DragAndDrop', function() {
var viewport, map, interaction;
@@ -37,6 +37,14 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
expect(interaction.formatConstructors_).to.have.length(1);
});
it('accepts a source option', function() {
var source = new ol.source.Vector();
var drop = new ol.interaction.DragAndDrop({
formatConstructors: [ol.format.GeoJSON],
source: source
});
expect(drop.source_).to.equal(source);
});
});
describe('#setActive()', function() {
@@ -128,6 +136,41 @@ where('FileReader').describe('ol.interaction.DragAndDrop', function() {
expect(event.dataTransfer.dropEffect).to.be('copy');
expect(event.propagationStopped).to.be(true);
});
it('adds dropped features to a source', function(done) {
var source = new ol.source.Vector();
var drop = new ol.interaction.DragAndDrop({
formatConstructors: [ol.format.GeoJSON],
source: source
});
drop.setMap(map);
drop.on('addfeatures', function(evt) {
var features = source.getFeatures();
expect(features.length).to.be(1);
done();
});
var event = new ol.events.Event();
event.dataTransfer = {};
event.type = 'dragenter';
viewport.dispatchEvent(event);
event.type = 'dragover';
viewport.dispatchEvent(event);
event.type = 'drop';
event.dataTransfer.files = {
length: 1,
item: function() {
return JSON.stringify({
type: 'FeatureCollection',
features: [{type: 'Feature', id: '1'}]
});
}
};
viewport.dispatchEvent(event);
expect(event.dataTransfer.dropEffect).to.be('copy');
expect(event.propagationStopped).to.be(true);
});
});
});