From 6cb02724da0189d39013fd68bebf5cfd9dccdd8d Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 22 Jan 2014 16:46:26 +0100 Subject: [PATCH] Make ol.interaction.DragAndDrop fire an event instead of managing layers and sources --- src/objectliterals.jsdoc | 11 +- .../draganddropinteraction.exports | 3 + src/ol/interaction/draganddropinteraction.js | 126 ++++++++++-------- 3 files changed, 75 insertions(+), 65 deletions(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 6124101b82..6c1c23e5f1 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -282,17 +282,10 @@ /** * @typedef {Object} olx.interaction.DragAndDropOptions - * @property {boolean|undefined} fitView Fit view. Default is `true`. * @property {Array.|undefined} formatConstructors * Format constructors. - * @property {ol.source.Vector|undefined} source Source. If this is defined - * then features will be added to this source. - * @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. + * @property {ol.proj.ProjectionLike} reprojectTo Target projection. By + * default, the map's view's projection is used. */ /** diff --git a/src/ol/interaction/draganddropinteraction.exports b/src/ol/interaction/draganddropinteraction.exports index 50e9fef9e3..0e8bc5ea27 100644 --- a/src/ol/interaction/draganddropinteraction.exports +++ b/src/ol/interaction/draganddropinteraction.exports @@ -1 +1,4 @@ @exportSymbol ol.interaction.DragAndDrop + +@exportProperty ol.interaction.DragAndDropEvent.prototype.getFeatures +@exportProperty ol.interaction.DragAndDropEvent.prototype.getProjection diff --git a/src/ol/interaction/draganddropinteraction.js b/src/ol/interaction/draganddropinteraction.js index 329a3c5172..c9cf155fee 100644 --- a/src/ol/interaction/draganddropinteraction.js +++ b/src/ol/interaction/draganddropinteraction.js @@ -1,18 +1,18 @@ // FIXME should handle all geo-referenced data, not just vector data goog.provide('ol.interaction.DragAndDrop'); +goog.provide('ol.interaction.DragAndDropEvent'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.events'); +goog.require('goog.events.Event'); goog.require('goog.events.FileDropHandler'); goog.require('goog.events.FileDropHandler.EventType'); goog.require('goog.fs.FileReader'); goog.require('goog.functions'); goog.require('ol.interaction.Interaction'); -goog.require('ol.layer.Vector'); goog.require('ol.proj'); -goog.require('ol.source.Vector'); @@ -27,12 +27,6 @@ ol.interaction.DragAndDrop = function(opt_options) { goog.base(this); - /** - * @private - * @type {boolean} - */ - this.fitView_ = goog.isDef(options.fitView) ? options.fitView : true; - /** * @private * @type {Array.} @@ -42,21 +36,10 @@ ol.interaction.DragAndDrop = function(opt_options) { /** * @private - * @type {ol.source.Vector} + * @type {ol.proj.Projection} */ - this.source_ = goog.isDef(options.source) ? options.source : 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; + this.reprojectTo_ = goog.isDef(options.reprojectTo) ? + ol.proj.get(options.reprojectTo) : null; /** * @private @@ -108,16 +91,12 @@ ol.interaction.DragAndDrop.prototype.handleDrop_ = function(event) { ol.interaction.DragAndDrop.prototype.handleResult_ = function(result) { var map = this.getMap(); goog.asserts.assert(!goog.isNull(map)); - var view = map.getView(); - goog.asserts.assert(goog.isDef(view)); - var view2D = view.getView2D(); - var targetProjection; - if (!goog.isNull(this.source_)) { - targetProjection = this.source_.getProjection(); - } else if (!goog.isNull(this.layer_)) { - targetProjection = this.layer_.getSource().getProjection(); - } else { - targetProjection = view2D.getProjection(); + var projection = this.reprojectTo_; + if (goog.isNull(projection)) { + var view = map.getView(); + goog.asserts.assert(goog.isDef(view)); + projection = view.getView2D().getProjection(); + goog.asserts.assert(goog.isDef(projection)); } var formatConstructors = this.formatConstructors_; var features = []; @@ -128,7 +107,7 @@ ol.interaction.DragAndDrop.prototype.handleResult_ = function(result) { var readFeatures = this.tryReadFeatures_(format, result); if (!goog.isNull(readFeatures)) { var featureProjection = format.readProjection(result); - var transform = ol.proj.getTransform(featureProjection, targetProjection); + var transform = ol.proj.getTransform(featureProjection, projection); var j, jj; for (j = 0, jj = readFeatures.length; j < jj; ++j) { var feature = readFeatures[j]; @@ -140,29 +119,10 @@ ol.interaction.DragAndDrop.prototype.handleResult_ = function(result) { } } } - if (features.length > 0) { - var source; - if (!goog.isNull(this.source_)) { - source = this.source_; - } 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()); - } - } + this.dispatchEvent( + new ol.interaction.DragAndDropEvent( + ol.interaction.DragAndDropEventType.ADD_FEATURES, this, features, + projection)); }; @@ -209,3 +169,57 @@ ol.interaction.DragAndDrop.prototype.tryReadFeatures_ = function(format, text) { 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.=} 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.|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.|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_; +};