From 10e2510f0f119e69fa78a62096453d648a5708d0 Mon Sep 17 00:00:00 2001 From: "Philip \"digitalfox\" Kovac" Date: Wed, 16 Dec 2015 15:24:58 -0500 Subject: [PATCH] Added boxEndCondition to DragBoxOptions to replace the hardcoded check against DRAG_BOX_HYSTERESIS_PIXELS_SQUARED. --- externs/olx.js | 22 ++++++++++++- src/ol/interaction/dragboxinteraction.js | 41 +++++++++++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index b527195d2f..bec4bc0800 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -2361,7 +2361,8 @@ olx.interaction.DragAndDropOptions.prototype.projection; /** * @typedef {{className: (string|undefined), - * condition: (ol.events.ConditionType|undefined)}} + * condition: (ol.events.ConditionType|undefined), + * boxEndCondition: (ol.interaction.DragBoxEndConditionType|undefined)}} * @api */ olx.interaction.DragBoxOptions; @@ -2385,6 +2386,25 @@ olx.interaction.DragBoxOptions.prototype.className; olx.interaction.DragBoxOptions.prototype.condition; +/** + * A function that takes a {@link ol.MapBrowserEvent} and two + * {@link ol.Pixel}s to indicate whether a boxend event should be fired. + * Default is: + * ```js + * function(mapBrowserEvent, + * startPixel, endPixel) { + * var width = endPixel[0] - startPixel[0]; + * var height = endPixel[1] - startPixel[1]; + * return width * width + height * height >= + * ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED; + * } + * ``` + * @type {ol.interaction.DragBoxEndConditionType|undefined} + * @api + */ +olx.interaction.DragBoxOptions.prototype.boxEndCondition; + + /** * @typedef {{kinetic: (ol.Kinetic|undefined)}} * @api diff --git a/src/ol/interaction/dragboxinteraction.js b/src/ol/interaction/dragboxinteraction.js index ca044f1067..0c2988cd16 100644 --- a/src/ol/interaction/dragboxinteraction.js +++ b/src/ol/interaction/dragboxinteraction.js @@ -65,6 +65,16 @@ ol.DragBoxEvent = function(type, coordinate) { goog.inherits(ol.DragBoxEvent, goog.events.Event); +/** + * A function that takes a {@link ol.MapBrowserEvent} and two + * {@link ol.Pixel}s and returns a `{boolean}`. If the condition is met, + * true should be returned. + * @typedef {function(ol.MapBrowserEvent, ol.Pixel, ol.Pixel):boolean} + * @api + */ +ol.interaction.DragBoxEndConditionType; + + /** * @classdesc @@ -112,10 +122,34 @@ ol.interaction.DragBox = function(opt_options) { this.condition_ = options.condition ? options.condition : ol.events.condition.always; + /** + * @private + * @type {ol.interaction.DragBoxEndConditionType} + */ + this.boxEndCondition_ = options.boxEndCondition ? + options.boxEndCondition : ol.interaction.DragBox.defaultBoxEndCondition; }; goog.inherits(ol.interaction.DragBox, ol.interaction.Pointer); +/** + * The default condition for determining whether the boxend event + * should fire. + * @param {ol.MapBrowserEvent} mapBrowserEvent The originating MapBrowserEvent + * leading to the box end. + * @param {ol.Pixel} startPixel The starting pixel of the box. + * @param {ol.Pixel} endPixel The end pixel of the box. + * @return {boolean} Whether or not the boxend condition should be fired. + */ +ol.interaction.DragBox.defaultBoxEndCondition = function(mapBrowserEvent, + startPixel, endPixel) { + var width = endPixel[0] - startPixel[0]; + var height = endPixel[1] - startPixel[1]; + return width * width + height * height >= + ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED; +}; + + /** * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event. * @this {ol.interaction.DragBox} @@ -162,11 +196,8 @@ ol.interaction.DragBox.handleUpEvent_ = function(mapBrowserEvent) { this.box_.setMap(null); - var deltaX = mapBrowserEvent.pixel[0] - this.startPixel_[0]; - var deltaY = mapBrowserEvent.pixel[1] - this.startPixel_[1]; - - if (deltaX * deltaX + deltaY * deltaY >= - ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED) { + if (this.boxEndCondition_(mapBrowserEvent, + this.startPixel_, mapBrowserEvent.pixel)) { this.onBoxEnd(mapBrowserEvent); this.dispatchEvent(new ol.DragBoxEvent(ol.DragBoxEventType.BOXEND, mapBrowserEvent.coordinate));