Merge pull request #4574 from WeaveTeam/dragbox-config-pr

Added boxEndCondition to DragBoxOptions to replace the hardcoded chec…
This commit is contained in:
Andreas Hocevar
2016-01-06 11:47:56 +01:00
2 changed files with 57 additions and 6 deletions

View File

@@ -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

View File

@@ -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));