Convert ol.MapBrowserEvent#get* methods into properties

This commit is contained in:
Tom Payne
2014-01-27 16:07:34 +01:00
parent 390b61762e
commit 318fc68159
26 changed files with 89 additions and 87 deletions

View File

@@ -151,6 +151,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var pixel = evt.getPixel();
displayFeatureInfo(pixel);
displayFeatureInfo(evt.pixel);
});

View File

@@ -148,6 +148,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var pixel = evt.getPixel();
displayFeatureInfo(pixel);
displayFeatureInfo(evt.pixel);
});

View File

@@ -89,6 +89,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var pixel = evt.getPixel();
displayFeatureInfo(pixel);
displayFeatureInfo(evt.pixel);
});

View File

@@ -70,7 +70,7 @@ map.addOverlay(popup);
// display popup on click
map.on('singleclick', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.getPixel(),
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature, layer) {
return feature;
});

View File

@@ -111,8 +111,7 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var coordinate = evt.getCoordinate();
displaySnap(coordinate);
displaySnap(evt.coordinate);
});
var imageStyle = new ol.style.Circle({

View File

@@ -95,6 +95,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var pixel = evt.getPixel();
displayFeatureInfo(pixel);
displayFeatureInfo(evt.pixel);
});

View File

@@ -91,5 +91,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
displayFeatureInfo(evt.getPixel());
displayFeatureInfo(evt.pixel);
});

View File

@@ -97,5 +97,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
displayFeatureInfo(evt.getPixel());
displayFeatureInfo(evt.pixel);
});

View File

@@ -55,6 +55,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var pixel = evt.getPixel();
displayFeatureInfo(pixel);
displayFeatureInfo(evt.pixel);
});

View File

@@ -49,7 +49,7 @@ map.addOverlay(popup);
map.on('singleclick', function(evt) {
var element = popup.getElement();
var coordinate = evt.getCoordinate();
var coordinate = evt.coordinate;
var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
coordinate, 'EPSG:3857', 'EPSG:4326'));

View File

@@ -62,7 +62,7 @@ var map = new ol.Map({
* Add a click handler to the map to render the popup.
*/
map.on('singleclick', function(evt) {
var coordinate = evt.getCoordinate();
var coordinate = evt.coordinate;
var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
coordinate, 'EPSG:3857', 'EPSG:4326'));

View File

@@ -97,8 +97,7 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var coordinate = evt.getCoordinate();
displaySnap(coordinate);
displaySnap(evt.coordinate);
});
var imageStyle = new ol.style.Circle({

View File

@@ -94,6 +94,5 @@ $(map.getViewport()).on('mousemove', function(evt) {
});
map.on('singleclick', function(evt) {
var pixel = evt.getPixel();
displayFeatureInfo(pixel);
displayFeatureInfo(evt.pixel);
});

View File

@@ -36,6 +36,23 @@ oli.ObjectEvent.prototype.key;
/** @interface */
oli.MapBrowserEvent;
/** @type {ol.Coordinate} */
oli.MapBrowserEvent.prototype.coordinate;
/** @type {Event} */
oli.MapBrowserEvent.prototype.originalEvent;
/** @type {ol.Pixel} */
oli.MapBrowserEvent.prototype.pixel;
/**
* @interface
*/

View File

@@ -48,7 +48,7 @@ ol.interaction.DoubleClickZoom.prototype.handleMapBrowserEvent =
var browserEvent = mapBrowserEvent.browserEvent;
if (mapBrowserEvent.type == ol.MapBrowserEvent.EventType.DBLCLICK) {
var map = mapBrowserEvent.map;
var anchor = mapBrowserEvent.getCoordinate();
var anchor = mapBrowserEvent.coordinate;
var delta = browserEvent.shiftKey ? -this.delta_ : this.delta_;
// FIXME works for View2D only
var view = map.getView().getView2D();

View File

@@ -114,7 +114,7 @@ goog.inherits(ol.interaction.DragBox, ol.interaction.Drag);
* @inheritDoc
*/
ol.interaction.DragBox.prototype.handleDrag = function(mapBrowserEvent) {
this.box_.setPixels(this.startPixel_, mapBrowserEvent.getPixel());
this.box_.setPixels(this.startPixel_, mapBrowserEvent.pixel);
};
@@ -144,7 +144,7 @@ ol.interaction.DragBox.prototype.handleDragEnd =
ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED) {
this.onBoxEnd(mapBrowserEvent);
this.dispatchEvent(new ol.DragBoxEvent(ol.DragBoxEventType.BOXEND,
mapBrowserEvent.getCoordinate()));
mapBrowserEvent.coordinate));
}
};
@@ -156,11 +156,11 @@ ol.interaction.DragBox.prototype.handleDragStart =
function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
if (browserEvent.isMouseActionButton() && this.condition_(mapBrowserEvent)) {
this.startPixel_ = mapBrowserEvent.getPixel();
this.startPixel_ = mapBrowserEvent.pixel;
this.box_.setMap(mapBrowserEvent.map);
this.box_.setPixels(this.startPixel_, this.startPixel_);
this.dispatchEvent(new ol.DragBoxEvent(ol.DragBoxEventType.BOXSTART,
mapBrowserEvent.getCoordinate()));
mapBrowserEvent.coordinate));
return true;
} else {
return false;

View File

@@ -133,8 +133,7 @@ ol.interaction.Drag.prototype.handleMapBrowserEvent =
this.deltaX = 0;
this.deltaY = 0;
this.startCenter = view2DState.center;
this.startCoordinate = /** @type {ol.Coordinate} */
(mapBrowserEvent.getCoordinate());
this.startCoordinate = mapBrowserEvent.coordinate;
var handled = this.handleDragStart(mapBrowserEvent);
if (handled) {
this.dragging_ = true;

View File

@@ -71,7 +71,7 @@ ol.interaction.DragRotateAndZoom.prototype.handleDrag =
function(mapBrowserEvent) {
var map = mapBrowserEvent.map;
var size = map.getSize();
var offset = mapBrowserEvent.getPixel();
var offset = mapBrowserEvent.pixel;
var delta = new goog.math.Vec2(
offset[0] - size[0] / 2,
size[1] / 2 - offset[1]);

View File

@@ -52,7 +52,7 @@ goog.inherits(ol.interaction.DragRotate, ol.interaction.Drag);
ol.interaction.DragRotate.prototype.handleDrag = function(mapBrowserEvent) {
var map = mapBrowserEvent.map;
var size = map.getSize();
var offset = mapBrowserEvent.getPixel();
var offset = mapBrowserEvent.pixel;
var theta =
Math.atan2(size[1] / 2 - offset[1], offset[0] - size[0] / 2);
if (goog.isDef(this.lastAngle_)) {

View File

@@ -253,7 +253,7 @@ ol.interaction.Draw.prototype.handleMapBrowserEvent = function(event) {
*/
ol.interaction.Draw.prototype.handleClick_ = function(event) {
var downPx = event.map.getEventPixel(event.target.getDown());
var clickPx = event.getPixel();
var clickPx = event.pixel;
var dx = downPx[0] - clickPx[0];
var dy = downPx[1] - clickPx[1];
var squaredDistance = dx * dx + dy * dy;
@@ -316,7 +316,7 @@ ol.interaction.Draw.prototype.atFinish_ = function(event) {
for (var i = 0, ii = potentiallyFinishCoordinates.length; i < ii; i++) {
var finishCoordinate = potentiallyFinishCoordinates[i];
var finishPixel = map.getPixelFromCoordinate(finishCoordinate);
var pixel = event.getPixel();
var pixel = event.pixel;
var dx = pixel[0] - finishPixel[0];
var dy = pixel[1] - finishPixel[1];
at = Math.sqrt(dx * dx + dy * dy) <= this.snapTolerance_;
@@ -337,7 +337,7 @@ ol.interaction.Draw.prototype.atFinish_ = function(event) {
* @private
*/
ol.interaction.Draw.prototype.startDrawing_ = function(event) {
var start = event.getCoordinate();
var start = event.coordinate;
this.finishCoordinate_ = start;
var geometry;
if (this.mode_ === ol.interaction.DrawMode.POINT) {
@@ -368,7 +368,7 @@ ol.interaction.Draw.prototype.startDrawing_ = function(event) {
* @private
*/
ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
var coordinate = event.getCoordinate();
var coordinate = event.coordinate;
var geometry = this.sketchFeature_.getGeometry();
var coordinates, last;
if (this.mode_ === ol.interaction.DrawMode.POINT) {
@@ -416,7 +416,7 @@ ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
* @private
*/
ol.interaction.Draw.prototype.addToDrawing_ = function(event) {
var coordinate = event.getCoordinate();
var coordinate = event.coordinate;
var geometry = this.sketchFeature_.getGeometry();
var coordinates, last;
if (this.mode_ === ol.interaction.DrawMode.LINE_STRING) {

View File

@@ -79,11 +79,10 @@ ol.interaction.MouseWheelZoom.prototype.handleMapBrowserEvent =
if (mapBrowserEvent.type ==
goog.events.MouseWheelHandler.EventType.MOUSEWHEEL) {
var map = mapBrowserEvent.map;
var mouseWheelEvent = /** @type {goog.events.MouseWheelEvent} */
(mapBrowserEvent.browserEvent);
var mouseWheelEvent = mapBrowserEvent.browserEvent;
goog.asserts.assertInstanceof(mouseWheelEvent, goog.events.MouseWheelEvent);
this.lastAnchor_ = mapBrowserEvent.getCoordinate();
this.lastAnchor_ = mapBrowserEvent.coordinate;
this.delta_ += mouseWheelEvent.deltaY / 3;
if (!goog.isDef(this.startTime_)) {

View File

@@ -81,7 +81,7 @@ ol.interaction.Touch.isTouchEvent_ = function(mapBrowserEvent) {
ol.interaction.Touch.prototype.updateTrackedTouches_ =
function(mapBrowserEvent) {
if (ol.interaction.Touch.isTouchEvent_(mapBrowserEvent)) {
var event = mapBrowserEvent.browserEvent.getBrowserEvent();
var event = mapBrowserEvent.browserEvent;
if (goog.isDef(event.targetTouches)) {
// W3C touch events
this.targetTouches = event.targetTouches;

View File

@@ -777,7 +777,7 @@ ol.Map.prototype.handleMapBrowserEvent = function(mapBrowserEvent) {
// coordinates so interactions cannot be used.
return;
}
this.focus_ = mapBrowserEvent.getCoordinate();
this.focus_ = mapBrowserEvent.coordinate;
mapBrowserEvent.frameState = this.frameState_;
var interactions = this.getInteractions();
var interactionsArray = /** @type {Array.<ol.interaction.Interaction>} */

View File

@@ -1,5 +1,2 @@
@exportProperty ol.MapBrowserEvent.prototype.getBrowserEvent
@exportProperty ol.MapBrowserEvent.prototype.getCoordinate
@exportProperty ol.MapBrowserEvent.prototype.getPixel
@exportProperty ol.MapBrowserEvent.prototype.preventDefault
@exportProperty ol.MapBrowserEvent.prototype.stopPropagation

View File

@@ -18,6 +18,7 @@ goog.require('ol.Pixel');
/**
* @constructor
* @extends {ol.MapEvent}
* @implements {oli.MapBrowserEvent}
* @param {string} type Event type.
* @param {ol.Map} map Map.
* @param {goog.events.BrowserEvent} browserEvent Browser event.
@@ -35,55 +36,25 @@ ol.MapBrowserEvent = function(type, map, browserEvent, opt_frameState) {
this.browserEvent = browserEvent;
/**
* @private
* @type {ol.Coordinate}
* @const
* @type {Event}
*/
this.coordinate_ = null;
this.originalEvent = browserEvent.getBrowserEvent();
/**
* @type {ol.Coordinate}
*/
this.coordinate = map.getEventCoordinate(this.originalEvent);
/**
* @private
* @type {ol.Pixel}
*/
this.pixel_ = null;
this.pixel = map.getEventPixel(this.originalEvent);
};
goog.inherits(ol.MapBrowserEvent, ol.MapEvent);
/**
* @return {Event} The underlying browser event object.
*/
ol.MapBrowserEvent.prototype.getBrowserEvent = function() {
return this.browserEvent.getBrowserEvent();
};
/**
* @return {ol.Coordinate} Coordinate.
* @todo stability experimental
*/
ol.MapBrowserEvent.prototype.getCoordinate = function() {
if (goog.isNull(this.coordinate_)) {
this.coordinate_ = this.map.getEventCoordinate(
this.browserEvent.getBrowserEvent());
}
return this.coordinate_;
};
/**
* Get pixel offset of the event from the top-left corner of the map viewport.
* @return {ol.Pixel} Pixel offset.
* @todo stability experimental
*/
ol.MapBrowserEvent.prototype.getPixel = function() {
if (goog.isNull(this.pixel_)) {
this.pixel_ = this.map.getEventPixel(this.browserEvent.getBrowserEvent());
}
return this.pixel_;
};
/**
* Prevents the default browser action.
* @see https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault

View File

@@ -6,16 +6,21 @@ describe('ol.MapBrowserEventHandler', function() {
var handler;
var singleclickSpy;
var dblclickSpy;
var target;
beforeEach(function() {
clock = sinon.useFakeTimers();
handler = new ol.MapBrowserEventHandler(new ol.Map({}));
target = goog.dom.createElement(goog.dom.TagName.DIV);
handler = new ol.MapBrowserEventHandler(new ol.Map({
target: target
}));
singleclickSpy = sinon.spy();
goog.events.listen(handler, 'singleclick', singleclickSpy);
dblclickSpy = sinon.spy();
goog.events.listen(handler, 'dblclick', dblclickSpy);
});
afterEach(function() {
@@ -23,7 +28,12 @@ describe('ol.MapBrowserEventHandler', function() {
});
it('emulates click', function() {
handler.emulateClick_();
handler.emulateClick_(new goog.events.BrowserEvent({
type: 'mousedown',
target: target,
clientX: 0,
clientY: 0
}));
expect(singleclickSpy.called).to.not.be.ok();
expect(dblclickSpy.called).to.not.be.ok();
@@ -31,17 +41,32 @@ describe('ol.MapBrowserEventHandler', function() {
expect(singleclickSpy.calledOnce).to.be.ok();
expect(dblclickSpy.called).to.not.be.ok();
handler.emulateClick_();
handler.emulateClick_(new goog.events.BrowserEvent({
type: 'mousedown',
target: target,
clientX: 0,
clientY: 0
}));
expect(singleclickSpy.calledOnce).to.be.ok();
expect(dblclickSpy.called).to.not.be.ok();
});
it('emulates dblclick', function() {
handler.emulateClick_();
handler.emulateClick_({
type: 'mousedown',
target: target,
clientX: 0,
clientY: 0
});
expect(singleclickSpy.called).to.not.be.ok();
expect(dblclickSpy.called).to.not.be.ok();
handler.emulateClick_();
handler.emulateClick_(new goog.events.BrowserEvent({
type: 'mousedown',
target: target,
clientX: 0,
clientY: 0
}));
expect(singleclickSpy.called).to.not.be.ok();
expect(dblclickSpy.calledOnce).to.be.ok();
@@ -84,6 +109,8 @@ describe('ol.MapBrowserEventHandler', function() {
});
});
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.events.BrowserEvent');
goog.require('ol.Map');