Make ol.interaction.DragAndDrop fire an event instead of managing layers and sources

This commit is contained in:
Tom Payne
2014-01-22 16:46:26 +01:00
parent 6df6a26764
commit 6cb02724da
3 changed files with 75 additions and 65 deletions

View File

@@ -282,17 +282,10 @@
/** /**
* @typedef {Object} olx.interaction.DragAndDropOptions * @typedef {Object} olx.interaction.DragAndDropOptions
* @property {boolean|undefined} fitView Fit view. Default is `true`.
* @property {Array.<function(new: ol.format.Format)>|undefined} formatConstructors * @property {Array.<function(new: ol.format.Format)>|undefined} formatConstructors
* Format constructors. * Format constructors.
* @property {ol.source.Vector|undefined} source Source. If this is defined * @property {ol.proj.ProjectionLike} reprojectTo Target projection. By
* then features will be added to this source. * default, the map's view's projection is used.
* @property {ol.layer.Vector|undefined} layer Layer. If this is defined then
* features will be added to this layer's source. If neither `source` nor
* `layer` are defined then the dropped features will be added as a new
* layer.
* @property {ol.feature.StyleFunction|undefined} styleFunction Style function.
* This is used to configure any new layers created.
*/ */
/** /**

View File

@@ -1 +1,4 @@
@exportSymbol ol.interaction.DragAndDrop @exportSymbol ol.interaction.DragAndDrop
@exportProperty ol.interaction.DragAndDropEvent.prototype.getFeatures
@exportProperty ol.interaction.DragAndDropEvent.prototype.getProjection

View File

@@ -1,18 +1,18 @@
// FIXME should handle all geo-referenced data, not just vector data // FIXME should handle all geo-referenced data, not just vector data
goog.provide('ol.interaction.DragAndDrop'); goog.provide('ol.interaction.DragAndDrop');
goog.provide('ol.interaction.DragAndDropEvent');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.events.FileDropHandler'); goog.require('goog.events.FileDropHandler');
goog.require('goog.events.FileDropHandler.EventType'); goog.require('goog.events.FileDropHandler.EventType');
goog.require('goog.fs.FileReader'); goog.require('goog.fs.FileReader');
goog.require('goog.functions'); goog.require('goog.functions');
goog.require('ol.interaction.Interaction'); goog.require('ol.interaction.Interaction');
goog.require('ol.layer.Vector');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.Vector');
@@ -27,12 +27,6 @@ ol.interaction.DragAndDrop = function(opt_options) {
goog.base(this); goog.base(this);
/**
* @private
* @type {boolean}
*/
this.fitView_ = goog.isDef(options.fitView) ? options.fitView : true;
/** /**
* @private * @private
* @type {Array.<function(new: ol.format.Format)>} * @type {Array.<function(new: ol.format.Format)>}
@@ -42,21 +36,10 @@ ol.interaction.DragAndDrop = function(opt_options) {
/** /**
* @private * @private
* @type {ol.source.Vector} * @type {ol.proj.Projection}
*/ */
this.source_ = goog.isDef(options.source) ? options.source : null; this.reprojectTo_ = goog.isDef(options.reprojectTo) ?
ol.proj.get(options.reprojectTo) : null;
/**
* @private
* @type {ol.layer.Vector}
*/
this.layer_ = goog.isDef(options.layer) ? options.layer : null;
/**
* @private
* @type {ol.feature.StyleFunction|undefined}
*/
this.styleFunction_ = options.styleFunction;
/** /**
* @private * @private
@@ -108,16 +91,12 @@ ol.interaction.DragAndDrop.prototype.handleDrop_ = function(event) {
ol.interaction.DragAndDrop.prototype.handleResult_ = function(result) { ol.interaction.DragAndDrop.prototype.handleResult_ = function(result) {
var map = this.getMap(); var map = this.getMap();
goog.asserts.assert(!goog.isNull(map)); goog.asserts.assert(!goog.isNull(map));
var view = map.getView(); var projection = this.reprojectTo_;
goog.asserts.assert(goog.isDef(view)); if (goog.isNull(projection)) {
var view2D = view.getView2D(); var view = map.getView();
var targetProjection; goog.asserts.assert(goog.isDef(view));
if (!goog.isNull(this.source_)) { projection = view.getView2D().getProjection();
targetProjection = this.source_.getProjection(); goog.asserts.assert(goog.isDef(projection));
} else if (!goog.isNull(this.layer_)) {
targetProjection = this.layer_.getSource().getProjection();
} else {
targetProjection = view2D.getProjection();
} }
var formatConstructors = this.formatConstructors_; var formatConstructors = this.formatConstructors_;
var features = []; var features = [];
@@ -128,7 +107,7 @@ ol.interaction.DragAndDrop.prototype.handleResult_ = function(result) {
var readFeatures = this.tryReadFeatures_(format, result); var readFeatures = this.tryReadFeatures_(format, result);
if (!goog.isNull(readFeatures)) { if (!goog.isNull(readFeatures)) {
var featureProjection = format.readProjection(result); var featureProjection = format.readProjection(result);
var transform = ol.proj.getTransform(featureProjection, targetProjection); var transform = ol.proj.getTransform(featureProjection, projection);
var j, jj; var j, jj;
for (j = 0, jj = readFeatures.length; j < jj; ++j) { for (j = 0, jj = readFeatures.length; j < jj; ++j) {
var feature = readFeatures[j]; var feature = readFeatures[j];
@@ -140,29 +119,10 @@ ol.interaction.DragAndDrop.prototype.handleResult_ = function(result) {
} }
} }
} }
if (features.length > 0) { this.dispatchEvent(
var source; new ol.interaction.DragAndDropEvent(
if (!goog.isNull(this.source_)) { ol.interaction.DragAndDropEventType.ADD_FEATURES, this, features,
source = this.source_; projection));
} else if (!goog.isNull(this.layer_)) {
source = this.layer_.getSource();
goog.asserts.assertInstanceof(source, ol.source.Vector);
} else {
source = new ol.source.Vector();
}
for (i = 0, ii = features.length; i < ii; ++i) {
source.addFeature(features[i]);
}
if (goog.isNull(this.layer_)) {
map.getLayers().push(new ol.layer.Vector({
styleFunction: this.styleFunction_,
source: source
}));
}
if (this.fitView_) {
view2D.fitExtent(source.getExtent(), map.getSize());
}
}
}; };
@@ -209,3 +169,57 @@ ol.interaction.DragAndDrop.prototype.tryReadFeatures_ = function(format, text) {
return null; return null;
} }
}; };
/**
* @enum {string}
*/
ol.interaction.DragAndDropEventType = {
ADD_FEATURES: 'addfeatures'
};
/**
* @constructor
* @extends {goog.events.Event}
* @param {ol.interaction.DragAndDropEventType} type Type.
* @param {Object=} opt_target Target.
* @param {Array.<ol.Feature>=} opt_features Features.
* @param {ol.proj.Projection=} opt_projection Projection.
*/
ol.interaction.DragAndDropEvent =
function(type, opt_target, opt_features, opt_projection) {
goog.base(this, type, opt_target);
/**
* @private
* @type {Array.<ol.Feature>|undefined}
*/
this.features_ = opt_features;
/**
* @private
* @type {ol.proj.Projection|undefined}
*/
this.projection_ = opt_projection;
};
goog.inherits(ol.interaction.DragAndDropEvent, goog.events.Event);
/**
* @return {Array.<ol.Feature>|undefined} Features.
*/
ol.interaction.DragAndDropEvent.prototype.getFeatures = function() {
return this.features_;
};
/**
* @return {ol.proj.Projection|undefined} Projection.
*/
ol.interaction.DragAndDropEvent.prototype.getProjection = function() {
return this.projection_;
};