From 42d2adb1cf58122513f55c3c02f7082d7acf0e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Peru=C4=8Di=C4=87?= Date: Fri, 27 Mar 2015 12:48:52 +0100 Subject: [PATCH] fix: repeating sort function + js annotation Sort function is now bind to this, and it uses a pixelCoordinate_ member. ol.interaction.Snap.handleEvent is removed js annotations are modified --- externs/olx.js | 5 +-- src/ol/interaction/snapinteraction.js | 45 ++++++++++++++------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 9b42facd4b..f7096817a3 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2684,7 +2684,7 @@ olx.interaction.SnapOptions; /** - * Snap to this features + * Snap to these features. Either this option or source should be provided. * @type {Array.|ol.Collection.|undefined} * @api */ @@ -2701,8 +2701,9 @@ olx.interaction.SnapOptions.prototype.pixelTolerance; /** - * Snap to features from this source + * Snap to features from this source. Either this option or features should be provided * @type {ol.source.Vector|undefined} + * @api */ olx.interaction.SnapOptions.prototype.source; diff --git a/src/ol/interaction/snapinteraction.js b/src/ol/interaction/snapinteraction.js index e43a8249d1..5e9a7e400b 100644 --- a/src/ol/interaction/snapinteraction.js +++ b/src/ol/interaction/snapinteraction.js @@ -48,7 +48,7 @@ goog.require('ol.structs.RBush'); ol.interaction.Snap = function(opt_options) { goog.base(this, { - handleEvent: ol.interaction.Snap.handleEvent, + handleEvent: this.handleEvent_, handleDownEvent: goog.functions.TRUE, handleUpEvent: ol.interaction.Snap.handleUpEvent }); @@ -91,6 +91,13 @@ ol.interaction.Snap = function(opt_options) { */ this.indexedFeaturesExtents_ = {}; + /** + * Used for distance sorting in sortByDistance_ + * @type {ol.Coordinate} + * @private + */ + this.pixelCoordinate_ = null; + /** * @type {number} * @private @@ -98,6 +105,12 @@ ol.interaction.Snap = function(opt_options) { this.pixelTolerance_ = goog.isDef(options.pixelTolerance) ? options.pixelTolerance : 10; + /** + * @type {Function} + * @private + */ + this.sortByDistance_ = goog.bind(ol.interaction.Snap.sortByDistance, this); + /** * Segment RTree for each layer @@ -212,7 +225,7 @@ ol.interaction.Snap.prototype.handleEvent_ = function(evt) { evt.coordinate = result.vertex; evt.pixel = result.vertexPixel; } - return true; + return ol.interaction.Pointer.handleEvent.call(this, evt); }; @@ -360,8 +373,8 @@ ol.interaction.Snap.prototype.snapTo = function(pixel, pixelCoordinate, map) { var vertex = null; var vertexPixel = null; if (segments.length > 0) { - segments.sort(goog.partial( - ol.interaction.Snap.sortByDistance, pixelCoordinate)); + this.pixelCoordinate_ = pixelCoordinate; + segments.sort(this.sortByDistance_); var closestSegment = segments[0].segment; vertex = (ol.coordinate.closestOnSegment(pixelCoordinate, closestSegment)); @@ -396,7 +409,6 @@ ol.interaction.Snap.prototype.snapTo = function(pixel, pixelCoordinate, map) { * @private */ ol.interaction.Snap.prototype.updateFeature_ = function(feature) { - console.log(3); this.removeFeature(feature, false); this.addFeature(feature, false); }; @@ -568,7 +580,6 @@ ol.interaction.Snap.SegmentDataType; * @param {ol.MapBrowserPointerEvent} evt Event. * @return {boolean} Stop drag sequence? * @this {ol.interaction.Snap} - * @api */ ol.interaction.Snap.handleUpEvent = function(evt) { goog.array.forEach(goog.object.getValues(this.pendingFeatures_), @@ -578,26 +589,16 @@ ol.interaction.Snap.handleUpEvent = function(evt) { }; -/** - * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event. - * @return {boolean} `false` to stop event propagation. - * @this {ol.interaction.Snap} - * @api - */ -ol.interaction.Snap.handleEvent = function(mapBrowserEvent) { - return ol.interaction.Pointer.handleEvent.call(this, mapBrowserEvent) && - this.handleEvent_(mapBrowserEvent); -}; - - /** * Sort segments by distance, helper function - * @param {ol.Coordinate} pixelCoordinate Coordinate to determine distance * @param {ol.interaction.Snap.SegmentDataType} a * @param {ol.interaction.Snap.SegmentDataType} b * @return {number} + * @this {ol.interaction.Snap} */ -ol.interaction.Snap.sortByDistance = function(pixelCoordinate, a, b) { - return ol.coordinate.squaredDistanceToSegment(pixelCoordinate, a.segment) - - ol.coordinate.squaredDistanceToSegment(pixelCoordinate, b.segment); +ol.interaction.Snap.sortByDistance = function(a, b) { + return ol.coordinate.squaredDistanceToSegment( + this.pixelCoordinate_, a.segment) - + ol.coordinate.squaredDistanceToSegment( + this.pixelCoordinate_, b.segment); };