refactoring, type annotations

This commit is contained in:
tsauerwein
2014-02-19 11:58:56 +01:00
parent d772c95ccf
commit 7fbd11154b
8 changed files with 250 additions and 56 deletions

View File

@@ -1,6 +1,8 @@
goog.provide('ol.pointer.EventSource');
goog.require('goog.events.BrowserEvent');
/**
@@ -25,7 +27,8 @@ ol.pointer.EventSource.prototype.getEvents = goog.abstractMethod;
/**
* Returns a mapping between the supported event types and
* the handlers that should handle an event.
* @return {Object.<string, function(Event)>} Event/Handler mapping
* @return {Object.<string, function(goog.events.BrowserEvent)>}
* Event/Handler mapping
*/
ol.pointer.EventSource.prototype.getMapping = goog.abstractMethod;
@@ -33,7 +36,7 @@ ol.pointer.EventSource.prototype.getMapping = goog.abstractMethod;
/**
* Returns the handler that should handle a given event type.
* @param {string} eventType
* @return {function(Event)} Handler
* @return {function(goog.events.BrowserEvent)} Handler
*/
ol.pointer.EventSource.prototype.getHandlerForEvent = function(eventType) {
return this.getMapping()[eventType];

View File

@@ -42,14 +42,36 @@ goog.require('ol.pointer.EventSource');
ol.pointer.MouseSource = function(dispatcher) {
goog.base(this, dispatcher);
/**
* @const
* @type {goog.structs.Map}
*/
this.pointerMap = dispatcher.pointerMap;
// radius around touchend that swallows mouse events
/**
* Radius around touchend that swallows mouse events.
*
* @const
* @type {number}
*/
this.DEDUP_DIST = 25;
/**
* @const
* @type {number}
*/
this.POINTER_ID = 1;
/**
* @const
* @type {string}
*/
this.POINTER_TYPE = 'mouse';
/**
* @const
* @type {Array.<string>}
*/
this.events = [
'mousedown',
'mousemove',
@@ -57,6 +79,11 @@ ol.pointer.MouseSource = function(dispatcher) {
'mouseover',
'mouseout'
];
/**
* @const
* @type {Object.<string, function(goog.events.BrowserEvent)>}
*/
this.mapping = {
'mousedown': this.mousedown,
'mousemove': this.mousemove,
@@ -65,6 +92,10 @@ ol.pointer.MouseSource = function(dispatcher) {
'mouseout': this.mouseout
};
/**
* @const
* @type {Array.<goog.math.Coordinate>}
*/
this.lastTouches = [];
};
goog.inherits(ol.pointer.MouseSource, ol.pointer.EventSource);

View File

@@ -42,10 +42,22 @@ goog.require('ol.pointer.EventSource');
ol.pointer.MsSource = function(dispatcher) {
goog.base(this, dispatcher);
/**
* @const
* @type {goog.structs.Map}
*/
this.pointerMap = dispatcher.pointerMap;
/**
* @const
* @type {boolean}
*/
this.HAS_BITMAP_TYPE = this.hasBitmapType();
/**
* @const
* @type {Array.<string>}
*/
this.events = [
'MSPointerDown',
'MSPointerMove',
@@ -56,6 +68,11 @@ ol.pointer.MsSource = function(dispatcher) {
'MSGotPointerCapture',
'MSLostPointerCapture'
];
/**
* @const
* @type {Object.<string, function(goog.events.BrowserEvent)>}
*/
this.mapping = {
'MSPointerDown': this.msPointerDown,
'MSPointerMove': this.msPointerMove,
@@ -67,6 +84,10 @@ ol.pointer.MsSource = function(dispatcher) {
'MSLostPointerCapture': this.msLostPointerCapture
};
/**
* @const
* @type {Array.<string>}
*/
this.POINTER_TYPES = [
'',
'unavailable',

View File

@@ -42,8 +42,16 @@ goog.require('ol.pointer.EventSource');
ol.pointer.NativeSource = function(dispatcher) {
goog.base(this, dispatcher);
/**
* @const
* @type {goog.structs.Map}
*/
this.pointerMap = dispatcher.pointerMap;
/**
* @const
* @type {Array.<string>}
*/
this.events = [
'pointerdown',
'pointermove',
@@ -54,6 +62,11 @@ ol.pointer.NativeSource = function(dispatcher) {
'gotpointercapture',
'lostpointercapture'
];
/**
* @const
* @type {Object.<string, function(goog.events.BrowserEvent)>}
*/
this.mapping = {
'pointerdown': this.pointerDown,
'pointermove': this.pointerMove,

View File

@@ -65,53 +65,130 @@ ol.pointer.PointerEvent = function(type, browserEvent, opt_eventDict) {
this.originalEvent = browserEvent.getBrowserEvent();
var eventDict = goog.isDef(opt_eventDict) ? opt_eventDict : {};
/**
* @type {number}
*/
this.buttons = this.getButtons_(eventDict);
/**
* @type {number}
*/
this.pressure = this.getPressure_(eventDict, this.buttons);
// MouseEvent related properties
/**
* @type {boolean}
*/
this.bubbles = this.getValue_('bubbles', eventDict);
/**
* @type {boolean}
*/
this.cancelable = this.getValue_('cancelable', eventDict);
/**
* @type {Object}
*/
this.view = this.getValue_('view', eventDict);
/**
* @type {number}
*/
this.detail = this.getValue_('detail', eventDict);
/**
* @type {number}
*/
this.screenX = this.getValue_('screenX', eventDict);
/**
* @type {number}
*/
this.screenY = this.getValue_('screenY', eventDict);
/**
* @type {number}
*/
this.clientX = this.getValue_('clientX', eventDict);
/**
* @type {number}
*/
this.clientY = this.getValue_('clientY', eventDict);
/**
* @type {boolean}
*/
this.ctrlKey = this.getValue_('ctrlKey', eventDict);
/**
* @type {boolean}
*/
this.altKey = this.getValue_('altKey', eventDict);
/**
* @type {boolean}
*/
this.shiftKey = this.getValue_('shiftKey', eventDict);
/**
* @type {boolean}
*/
this.metaKey = this.getValue_('metaKey', eventDict);
/**
* @type {number}
*/
this.button = this.getValue_('button', eventDict);
/**
* @type {Node}
*/
this.relatedTarget = this.getValue_('relatedTarget', eventDict);
// PointerEvent related properties
/**
* @const
* @type {number}
*/
this.pointerId = this.getValueOr_('pointerId', 0, eventDict);
/**
* @type {number}
*/
this.width = this.getValueOr_('width', 0, eventDict);
/**
* @type {number}
*/
this.height = this.getValueOr_('height', 0, eventDict);
/**
* @type {number}
*/
this.tiltX = this.getValueOr_('tiltX', 0, eventDict);
/**
* @type {number}
*/
this.tiltY = this.getValueOr_('tiltY', 0, eventDict);
/**
* @type {string}
*/
this.pointerType = this.getValueOr_('pointerType', '', eventDict);
/**
* @type {number}
*/
this.hwTimestamp = this.getValueOr_('hwTimestamp', 0, eventDict);
/**
* @type {boolean}
*/
this.isPrimary = this.getValueOr_('isPrimary', false, eventDict);
};
@@ -122,7 +199,7 @@ goog.inherits(ol.pointer.PointerEvent, goog.events.Event);
* @private
* @param {string} key
* @param {Object.<string, ?>} eventDict
* @return {*}
* @return {string|number|?}
*/
ol.pointer.PointerEvent.prototype.getValue_ = function(key, eventDict) {
return goog.isDefAndNotNull(eventDict[key]) ?
@@ -136,7 +213,7 @@ ol.pointer.PointerEvent.prototype.getValue_ = function(key, eventDict) {
* @param {string} key
* @param {*} defaultValue
* @param {Object.<string, ?>} eventDict
* @return {*}
* @return {string|number|?}
*/
ol.pointer.PointerEvent.prototype.getValueOr_ =
function(key, defaultValue, eventDict) {

View File

@@ -31,7 +31,7 @@
goog.provide('ol.pointer.PointerEventHandler');
goog.require('goog.debug.Console');
goog.require('goog.array');
goog.require('goog.events');
goog.require('goog.events.BrowserEvent');
goog.require('goog.events.Event');
@@ -67,15 +67,17 @@ ol.pointer.PointerEventHandler = function(element) {
*/
this.pointerMap = new goog.structs.Map();
/**
* @type {Object.<string, function(goog.events.BrowserEvent)>}
* @private
*/
this.eventMap_ = {};
this.eventMap = {};
// Scope objects for native events.
// This exists for ease of testing.
this.eventSources = {};
this.eventSourceList = [];
this.boundHandler_ = goog.bind(this.eventHandler_, this);
/**
* @type {Array.<ol.pointer.EventSource>}
* @private
*/
this.eventSourceList_ = [];
this.registerSources();
};
@@ -151,15 +153,14 @@ ol.pointer.PointerEventHandler.prototype.registerSource =
var newEvents = s.getEvents();
if (newEvents) {
newEvents.forEach(function(e) {
goog.array.forEach(newEvents, function(e) {
var handler = s.getHandlerForEvent(e);
if (handler) {
this.eventMap[e] = goog.bind(handler, s);
this.eventMap_[e] = goog.bind(handler, s);
}
}, this);
this.eventSources[name] = s;
this.eventSourceList.push(s);
this.eventSourceList_.push(s);
}
};
@@ -169,8 +170,8 @@ ol.pointer.PointerEventHandler.prototype.registerSource =
* @private
*/
ol.pointer.PointerEventHandler.prototype.register_ = function() {
var l = this.eventSourceList.length;
for (var i = 0, es; (i < l) && (es = this.eventSourceList[i]); i++) {
var l = this.eventSourceList_.length;
for (var i = 0, es; (i < l) && (es = this.eventSourceList_[i]); i++) {
this.addEvents_(es.getEvents());
}
};
@@ -181,8 +182,8 @@ ol.pointer.PointerEventHandler.prototype.register_ = function() {
* @private
*/
ol.pointer.PointerEventHandler.prototype.unregister_ = function() {
var l = this.eventSourceList.length;
for (var i = 0, es; (i < l) && (es = this.eventSourceList[i]); i++) {
var l = this.eventSourceList_.length;
for (var i = 0, es; (i < l) && (es = this.eventSourceList_[i]); i++) {
this.removeEvents_(es.getEvents());
}
};
@@ -195,7 +196,7 @@ ol.pointer.PointerEventHandler.prototype.unregister_ = function() {
*/
ol.pointer.PointerEventHandler.prototype.eventHandler_ = function(inEvent) {
var type = inEvent.type;
var handler = this.eventMap[type];
var handler = this.eventMap_[type];
if (handler) {
handler(inEvent);
}
@@ -222,10 +223,10 @@ ol.pointer.PointerEventHandler.prototype.eventHandler_ = function(inEvent) {
*/
ol.pointer.PointerEventHandler.prototype.listenOnDocument = function(
type, listener, opt_useCapture, opt_listenerScope) {
var l = this.eventSourceList.length;
var l = this.eventSourceList_.length;
var eventSource;
for (var i = 0; i < l; i++) {
eventSource = this.eventSourceList[i];
eventSource = this.eventSourceList_[i];
eventSource.listenOnDocument(type);
}
@@ -249,10 +250,10 @@ ol.pointer.PointerEventHandler.prototype.listenOnDocument = function(
*/
ol.pointer.PointerEventHandler.prototype.unlistenOnDocument = function(
type, listener, opt_useCapture, opt_listenerScope) {
var l = this.eventSourceList.length;
var l = this.eventSourceList_.length;
var eventSource;
for (var i = 0; i < l; i++) {
eventSource = this.eventSourceList[i];
eventSource = this.eventSourceList_[i];
eventSource.listenOnDocument(type);
}
@@ -267,9 +268,9 @@ ol.pointer.PointerEventHandler.prototype.unlistenOnDocument = function(
* @param {Array.<string>} events List of events.
*/
ol.pointer.PointerEventHandler.prototype.addEvents_ = function(events) {
events.forEach(function(eventName) {
goog.array.forEach(events, function(eventName) {
goog.events.listen(this.element_, eventName,
this.boundHandler_);
this.eventHandler_, false, this);
}, this);
};
@@ -283,7 +284,7 @@ ol.pointer.PointerEventHandler.prototype.addEvent = function(
eventName, opt_element) {
var element = goog.isDef(opt_element) ? opt_element : this.element_;
goog.events.listen(element, eventName,
this.boundHandler_);
this.eventHandler_, false, this);
};
@@ -293,9 +294,9 @@ ol.pointer.PointerEventHandler.prototype.addEvent = function(
* @param {Array.<string>} events List of events.
*/
ol.pointer.PointerEventHandler.prototype.removeEvents_ = function(events) {
events.forEach(function(e) {
goog.array.forEach(events, function(e) {
goog.events.unlisten(this.element_, e,
this.boundHandler_);
this.eventHandler_, false, this);
}, this);
};
@@ -309,7 +310,7 @@ ol.pointer.PointerEventHandler.prototype.removeEvent = function(
eventName, opt_element) {
var element = goog.isDef(opt_element) ? opt_element : this.element_;
goog.events.unlisten(element, eventName,
this.boundHandler_);
this.eventHandler_, false, this);
};
@@ -524,7 +525,8 @@ ol.pointer.PointerEventHandler.prototype.fireEvent =
/**
* Re-fires a native pointer event.
* Creates a pointer event from a native pointer event
* and dispatches this event.
* @param {goog.events.BrowserEvent} nativeEvent A platform event with a target.
*/
ol.pointer.PointerEventHandler.prototype.fireNativeEvent =

View File

@@ -31,6 +31,7 @@
goog.provide('ol.pointer.TouchSource');
goog.require('goog.array');
goog.require('goog.math.Coordinate');
goog.require('ol.pointer.EventSource');
@@ -56,20 +57,59 @@ ol.pointer.TouchSource = function(dispatcher, mouseSource) {
*/
this.mouseSource = mouseSource;
// This should be long enough to ignore compat mouse events made by touch
/**
* Mouse event timeout: This should be long enough to
* ignore compat mouse events made by touch.
* @const
* @type {number}
*/
this.DEDUP_TIMEOUT = 2500;
this.CLICK_COUNT_TIMEOUT = 200;
this.POINTER_TYPE = 'touch';
this.firstTouch = null;
this.clickCount = 0;
this.resetId = null;
/**
* @const
* @type {number}
*/
this.CLICK_COUNT_TIMEOUT = 200;
/**
* @const
* @type {string}
*/
this.POINTER_TYPE = 'touch';
/**
* @private
* @type {?number}
*/
this.firstTouchId_ = null;
/**
* @private
* @type {number}
*/
this.clickCount_ = 0;
/**
* @private
* @type {?number}
*/
this.resetId_ = null;
/**
* @const
* @type {Array.<string>}
*/
this.events = [
'touchstart',
'touchmove',
'touchend',
'touchcancel'
];
/**
* @const
* @type {Object.<string, function(goog.events.BrowserEvent)>}
*/
this.mapping = {
'touchstart': this.touchstart,
'touchmove': this.touchmove,
@@ -98,7 +138,7 @@ ol.pointer.TouchSource.prototype.getMapping = function() {
* @return {boolean} True, if this is the primary touch.
*/
ol.pointer.TouchSource.prototype.isPrimaryTouch_ = function(inTouch) {
return this.firstTouch === inTouch.identifier;
return this.firstTouchId_ === inTouch.identifier;
};
@@ -110,7 +150,7 @@ ol.pointer.TouchSource.prototype.isPrimaryTouch_ = function(inTouch) {
ol.pointer.TouchSource.prototype.setPrimaryTouch_ = function(inTouch) {
if (this.pointerMap.getCount() === 0 ||
(this.pointerMap.getCount() === 1 && this.pointerMap.containsKey(1))) {
this.firstTouch = inTouch.identifier;
this.firstTouchId_ = inTouch.identifier;
this.firstXY = {X: inTouch.clientX, Y: inTouch.clientY};
this.cancelResetClickCount_();
}
@@ -123,7 +163,7 @@ ol.pointer.TouchSource.prototype.setPrimaryTouch_ = function(inTouch) {
*/
ol.pointer.TouchSource.prototype.removePrimaryPointer_ = function(inPointer) {
if (inPointer.isPrimary) {
this.firstTouch = null;
this.firstTouchId_ = null;
this.firstXY = null;
this.resetClickCount_();
}
@@ -135,10 +175,11 @@ ol.pointer.TouchSource.prototype.removePrimaryPointer_ = function(inPointer) {
*/
ol.pointer.TouchSource.prototype.resetClickCount_ = function() {
var fn = function() {
this.clickCount = 0;
this.resetId = null;
this.clickCount_ = 0;
this.resetId_ = null;
};
this.resetId = setTimeout(goog.bind(fn, this), this.CLICK_COUNT_TIMEOUT);
this.resetId_ = goog.global.setTimeout(goog.bind(fn, this),
this.CLICK_COUNT_TIMEOUT);
};
@@ -146,8 +187,8 @@ ol.pointer.TouchSource.prototype.resetClickCount_ = function() {
* @private
*/
ol.pointer.TouchSource.prototype.cancelResetClickCount_ = function() {
if (this.resetId) {
clearTimeout(this.resetId);
if (this.resetId_) {
goog.global.clearTimeout(this.resetId_);
}
};
@@ -169,7 +210,7 @@ ol.pointer.TouchSource.prototype.touchToPointer_ =
//e.target = findTarget(e);
e.bubbles = true;
e.cancelable = true;
e.detail = this.clickCount;
e.detail = this.clickCount_;
e.button = 0;
e.buttons = 1;
e.width = inTouch['webkitRadiusX'] || inTouch['radiusX'] || 0;
@@ -193,13 +234,13 @@ ol.pointer.TouchSource.prototype.processTouches_ =
var pointers = goog.array.map(tl,
goog.partial(this.touchToPointer_, inEvent), this);
// forward touch preventDefaults
pointers.forEach(function(p) {
goog.array.forEach(pointers, function(p) {
p.preventDefault = function() {
this.firstXY = null;
inEvent.preventDefault();
};
}, this);
pointers.forEach(goog.partial(inFunction, inEvent), this);
goog.array.forEach(pointers, goog.partial(inFunction, inEvent), this);
};
@@ -245,7 +286,7 @@ ol.pointer.TouchSource.prototype.vacuumTouches_ = function(inEvent) {
d.push(this.touchToPointer_(p));
}
}, this);
d.forEach(goog.partial(this.cancelOut_, inEvent), this);
goog.array.forEach(d, goog.partial(this.cancelOut_, inEvent), this);
}
};
@@ -257,7 +298,7 @@ ol.pointer.TouchSource.prototype.vacuumTouches_ = function(inEvent) {
* @param {Object} thisArg
*/
ol.pointer.TouchSource.prototype.forEach_ = function(map, callback, thisArg) {
map.getValues().forEach(function(value) {
goog.array.forEach(map.getValues(), function(value) {
callback.call(thisArg, value, map.get(value));
});
};
@@ -273,7 +314,7 @@ ol.pointer.TouchSource.prototype.touchstart = function(inEvent) {
this.vacuumTouches_(inEvent);
this.setPrimaryTouch_(inEvent.getBrowserEvent().changedTouches[0]);
this.dedupSynthMouse_(inEvent);
this.clickCount++;
this.clickCount_++;
this.processTouches_(inEvent, this.overDown_);
};
@@ -414,7 +455,7 @@ ol.pointer.TouchSource.prototype.dedupSynthMouse_ = function(inEvent) {
// only the primary finger will synth mouse events
if (this.isPrimaryTouch_(t)) {
// remember x/y of last touch
var lt = {x: t.clientX, y: t.clientY};
var lt = new goog.math.Coordinate(t.clientX, t.clientY);
lts.push(lt);
var fn = goog.bind(function(lts, lt) {
var i = lts.indexOf(lt);
@@ -422,6 +463,6 @@ ol.pointer.TouchSource.prototype.dedupSynthMouse_ = function(inEvent) {
lts.splice(i, 1);
}
}, null, lts, lt);
setTimeout(fn, this.DEDUP_TIMEOUT);
goog.global.setTimeout(fn, this.DEDUP_TIMEOUT);
}
};