Add a 'condition' option to ol.interaction.Modify

This commit is contained in:
Frederic Junod
2016-05-09 17:13:22 +02:00
parent 8c8c4c3df4
commit b49546d2af
4 changed files with 59 additions and 2 deletions

View File

@@ -2837,7 +2837,8 @@ olx.interaction.KeyboardZoomOptions.prototype.delta;
/**
* @typedef {{deleteCondition: (ol.events.ConditionType|undefined),
* @typedef {{condition: (ol.events.ConditionType|undefined),
* deleteCondition: (ol.events.ConditionType|undefined),
* pixelTolerance: (number|undefined),
* style: (ol.style.Style|Array.<ol.style.Style>|ol.style.StyleFunction|undefined),
* features: ol.Collection.<ol.Feature>,
@@ -2847,6 +2848,17 @@ olx.interaction.KeyboardZoomOptions.prototype.delta;
olx.interaction.ModifyOptions;
/**
* A function that takes an {@link ol.MapBrowserEvent} and returns a boolean
* to indicate whether that event will be considered to add or move a vertex
* to the sketch.
* Default is {@link ol.events.condition.primaryAction}.
* @type {ol.events.ConditionType|undefined}
* @api
*/
olx.interaction.ModifyOptions.prototype.condition;
/**
* A function that takes an {@link ol.MapBrowserEvent} and returns a boolean
* to indicate whether that event should be handled.

View File

@@ -220,3 +220,18 @@ ol.events.condition.mouseOnly = function(mapBrowserEvent) {
// see http://www.w3.org/TR/pointerevents/#widl-PointerEvent-pointerType
return mapBrowserEvent.pointerEvent.pointerType == 'mouse';
};
/**
* Return `true` if the event originates from a primary pointer in
* contact with the surface or if the left mouse button is pressed.
* @see http://www.w3.org/TR/pointerevents/#button-states
*
* @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
* @return {boolean} True if the event originates from a primary pointer.
* @api
*/
ol.events.condition.primaryAction = function(mapBrowserEvent) {
var pointerEvent = mapBrowserEvent.pointerEvent;
return pointerEvent.isPrimary && pointerEvent.button === 0;
};

View File

@@ -112,6 +112,14 @@ ol.interaction.Modify = function(options) {
handleUpEvent: ol.interaction.Modify.handleUpEvent_
});
/**
* @private
* @type {ol.events.ConditionType}
*/
this.condition_ = options.condition ?
options.condition : ol.events.condition.primaryAction;
/**
* @private
* @param {ol.MapBrowserEvent} mapBrowserEvent Browser event.
@@ -553,6 +561,9 @@ ol.interaction.Modify.compareIndexes_ = function(a, b) {
* @private
*/
ol.interaction.Modify.handleDownEvent_ = function(evt) {
if (!this.condition_(evt)) {
return false;
}
this.handlePointerAtPixel_(evt.pixel, evt.map);
this.dragSegments_.length = 0;
this.modified_ = false;

View File

@@ -68,10 +68,12 @@ describe('ol.interaction.Modify', function() {
var shiftKey = opt_shiftKey !== undefined ? opt_shiftKey : false;
var pointerEvent = new ol.pointer.PointerEvent(type, {
type: type,
button: button,
clientX: position.left + x + width / 2,
clientY: position.top + y + height / 2,
shiftKey: shiftKey
}, {
button: button,
isPrimary: true
});
var event = new ol.MapBrowserPointerEvent(type, map, pointerEvent);
event.pointerEvent.pointerId = 1;
@@ -376,6 +378,23 @@ describe('ol.interaction.Modify', function() {
validateEvents(events, [feature]);
});
it('clicking with right button should not add a vertex', function() {
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
simulateEvent('pointermove', 40, -20, false, 0);
// right click
simulateEvent('pointerdown', 40, -20, false, 1);
simulateEvent('pointermove', 30, -20, false, 1);
simulateEvent('pointerdrag', 30, -20, false, 1);
simulateEvent('pointerup', 30, -20, false, 1);
expect(feature.getGeometry().getRevision()).to.equal(1);
expect(feature.getGeometry().getCoordinates()[0]).to.have.length(5);
expect(events).to.have.length(0);
});
});
describe('double click deleteCondition', function() {