diff --git a/externs/olx.js b/externs/olx.js index f72424f7aa..c38031e27d 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 b73893481a..f3531ab18b 100644 --- a/src/ol/interaction/dragboxinteraction.js +++ b/src/ol/interaction/dragboxinteraction.js @@ -73,6 +73,16 @@ ol.DragBoxEvent = function(type, coordinate, mapBrowserEvent) { 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 @@ -120,10 +130,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} @@ -170,11 +204,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, mapBrowserEvent));