Simplified ISequence API; add object property also for browser events.
This commit is contained in:
+13
-43
@@ -1,5 +1,4 @@
|
|||||||
goog.provide('ol.event.Drag');
|
goog.provide('ol.event.Drag');
|
||||||
goog.provide('ol.event.DragEvent');
|
|
||||||
|
|
||||||
goog.require('ol.event.ISequence');
|
goog.require('ol.event.ISequence');
|
||||||
goog.require('ol.event.Events');
|
goog.require('ol.event.Events');
|
||||||
@@ -52,67 +51,38 @@ ol.event.Drag.prototype.dispatchEvent = function(e) {
|
|||||||
} else if (e.type === goog.fx.Dragger.EventType.END) {
|
} else if (e.type === goog.fx.Dragger.EventType.END) {
|
||||||
e.type = ol.event.Drag.EventType.DRAGEND;
|
e.type = ol.event.Drag.EventType.DRAGEND;
|
||||||
}
|
}
|
||||||
|
e.target = e.browserEvent.target;
|
||||||
|
e.dx = e.clientX - this.previousX_;
|
||||||
|
e.dy = e.clientY - this.previousY_;
|
||||||
}
|
}
|
||||||
this.target_.dispatchEvent(/** @type {Event} */ (e));
|
this.target_.triggerEvent(e.type, /** @type {Object} (e.type) */ (e));
|
||||||
return goog.base(this, 'dispatchEvent', e);
|
return goog.base(this, 'dispatchEvent', e);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
ol.event.Drag.prototype.startDrag = function(e) {
|
ol.event.Drag.prototype.startDrag = function(e) {
|
||||||
|
goog.base(this, 'startDrag', e);
|
||||||
this.previousX_ = e.clientX;
|
this.previousX_ = e.clientX;
|
||||||
this.previousY_ = e.clientY;
|
this.previousY_ = e.clientY;
|
||||||
goog.base(this, 'startDrag', e);
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
ol.event.Drag.prototype.doDrag = function(e, x, y, dragFromScroll) {
|
|
||||||
e.dx = e.clientX - this.previousX_;
|
|
||||||
e.dy = e.clientX - this.previousY_;
|
|
||||||
goog.base(this, 'doDrag', e, x, y, dragFromScroll);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @override */
|
/** @override */
|
||||||
|
ol.event.Drag.prototype.doDrag = function(e, x, y, dragFromScroll) {
|
||||||
|
goog.base(this, 'doDrag', e, x, y, dragFromScroll);
|
||||||
|
this.previousX_ = e.clientX;
|
||||||
|
this.previousY_ = e.clientY;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
ol.event.Drag.prototype.defaultAction = function(x, y) {};
|
ol.event.Drag.prototype.defaultAction = function(x, y) {};
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
ol.event.Drag.prototype.destroy = ol.event.Drag.prototype.dispose;
|
ol.event.Drag.prototype.destroy = ol.event.Drag.prototype.dispose;
|
||||||
|
|
||||||
|
|
||||||
ol.event.addSequenceProvider('drag', ol.event.Drag);
|
ol.event.addSequenceProvider('drag', ol.event.Drag);
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Object representing a drag event
|
|
||||||
*
|
|
||||||
* @param {string} type Event type.
|
|
||||||
* @param {goog.fx.Dragger} dragobj Drag object initiating event.
|
|
||||||
* @param {number} clientX X-coordinate relative to the viewport.
|
|
||||||
* @param {number} clientY Y-coordinate relative to the viewport.
|
|
||||||
* @param {goog.events.BrowserEvent} browserEvent Object representing the
|
|
||||||
* browser event that caused this drag event.
|
|
||||||
* @constructor
|
|
||||||
* @extends {goog.fx.DragEvent}
|
|
||||||
*/
|
|
||||||
ol.event.DragEvent = function(type, dragobj, clientX, clientY, browserEvent) {
|
|
||||||
|
|
||||||
goog.base(this, type, dragobj, clientX, clientY, browserEvent);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The move delta in X direction since the previous drag event
|
|
||||||
*
|
|
||||||
* @type {number}
|
|
||||||
*/
|
|
||||||
this.dx = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The move delta in Y direction since the previous drag event
|
|
||||||
*
|
|
||||||
* @type {number}
|
|
||||||
*/
|
|
||||||
this.dy = 0;
|
|
||||||
};
|
|
||||||
goog.inherits(ol.event.DragEvent, goog.fx.DragEvent);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Object.<string, string>}
|
* @type {Object.<string, string>}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ ol.event.Events.prototype.setElement = function(element) {
|
|||||||
for (t in types) {
|
for (t in types) {
|
||||||
// register the event cross-browser
|
// register the event cross-browser
|
||||||
goog.events.unlisten(
|
goog.events.unlisten(
|
||||||
this.element_, types[t], this.dispatchEvent, true, this
|
this.element_, types[t], this.handleBrowserEvent, true, this
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
this.destroySequences();
|
this.destroySequences();
|
||||||
@@ -148,7 +148,7 @@ ol.event.Events.prototype.setElement = function(element) {
|
|||||||
for (t in types) {
|
for (t in types) {
|
||||||
// register the event cross-browser
|
// register the event cross-browser
|
||||||
goog.events.listen(
|
goog.events.listen(
|
||||||
element, types[t], this.dispatchEvent, true, this
|
element, types[t], this.handleBrowserEvent, true, this
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -243,15 +243,14 @@ ol.event.Events.prototype.triggerEvent = function(type, opt_evt) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basically just a wrapper to the parent's dispatchEvent() function, but takes
|
* Prepares browser events before they are dispatched. This takes care to set a
|
||||||
* care to set a property 'xy' on the event with the current mouse position and
|
* property 'xy' on the event with the current pointer position (if
|
||||||
* normalize clientX and clientY for multi-touch events.
|
* {@code includeXY} is set to true), and normalizes clientX and clientY for
|
||||||
|
* multi-touch events.
|
||||||
*
|
*
|
||||||
* @param {Event} evt Event object.
|
* @param {Event} evt Event object.
|
||||||
* @return {boolean} If anyone called preventDefault on the event object (or
|
|
||||||
* if any of the handlers returns false this will also return false.
|
|
||||||
*/
|
*/
|
||||||
ol.event.Events.prototype.dispatchEvent = function(evt) {
|
ol.event.Events.prototype.handleBrowserEvent = function(evt) {
|
||||||
var type = evt.type,
|
var type = evt.type,
|
||||||
listeners = goog.events.getListeners(this.element_, type, false)
|
listeners = goog.events.getListeners(this.element_, type, false)
|
||||||
.concat(goog.events.getListeners(this.element_, type, true));
|
.concat(goog.events.getListeners(this.element_, type, true));
|
||||||
@@ -276,7 +275,7 @@ ol.event.Events.prototype.dispatchEvent = function(evt) {
|
|||||||
evt.xy = goog.style.getRelativePosition(evt, this.element_);
|
evt.xy = goog.style.getRelativePosition(evt, this.element_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return goog.base(this, 'dispatchEvent', evt);
|
this.triggerEvent(evt.type, evt);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ goog.provide('ol.event.ISequence');
|
|||||||
* Interface for event sequences. Event sequences map sequences of native
|
* Interface for event sequences. Event sequences map sequences of native
|
||||||
* browser events to high level events that the sequence provides.
|
* browser events to high level events that the sequence provides.
|
||||||
*
|
*
|
||||||
* Implementations are expected to call dispatchEvent on the {@code target} to
|
* Implementations are expected to call {@code triggerEvent} on the
|
||||||
* to fire their high level events.
|
* {@code target} to to fire their high level events.
|
||||||
*
|
*
|
||||||
* Implementations can expect the {@code target}'s {@code getElement()} method
|
* Implementations can expect the {@code target}'s {@code getElement} method
|
||||||
* to return an {Element} at construction time.
|
* to return an {Element} at construction time.
|
||||||
*
|
*
|
||||||
* @interface
|
* @interface
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ describe("ol.event.Events", function() {
|
|||||||
events.triggerEvent("bar", evt);
|
events.triggerEvent("bar", evt);
|
||||||
expect(log[0].scope).toBe(scope);
|
expect(log[0].scope).toBe(scope);
|
||||||
expect(log[0].evt).toBe(evt);
|
expect(log[0].evt).toBe(evt);
|
||||||
|
expect(log[0].evt.object).toBe("foo");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("respects event priority", function() {
|
it("respects event priority", function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user