Created
April 9, 2010 15:45
-
-
Save kushal/361295 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @constructor | |
*/ | |
chartbeat.widgets.IDragger = function(target, handle, limits) { | |
goog.fx.Dragger.call(this, target, handle, limits); | |
if (goog.userAgent.MOBILE) { | |
target.addEventListener('touchstart', goog.bind(this.startDragTouch, this)); | |
} | |
}; | |
goog.inherits(chartbeat.widgets.IDragger, goog.fx.Dragger); | |
chartbeat.widgets.IDragger.prototype.startDragTouch = function(e) { | |
// Start tracking when the first finger comes down in this element | |
if (e.targetTouches.length != 1) | |
return false; | |
this.startX = e.targetTouches[0].clientX; | |
this.startY = e.targetTouches[0].clientY; | |
this.screenX = this.startX; | |
this.screenY = this.startY; | |
this.deltaX = this.target.offsetLeft; | |
this.deltaY = this.target.offsetTop; | |
this.pageScroll = goog.dom.getDomHelper(this.document_).getDocumentScroll(); | |
this.mouseDownTime_ = goog.now(); | |
var self = this; | |
this.handle.addEventListener('touchmove', function(e) { return self.onTouchMove(e) }, false); | |
this.handle.addEventListener('touchend', function(e) { return self.onTouchEnd(e) }, false); | |
return false; | |
}; | |
chartbeat.widgets.IDragger.prototype.onTouchMove = function(e) { | |
// Prevent the browser from doing its default thing (scroll, zoom) | |
e.preventDefault(); | |
// Don't track motion when multiple touches are down in this element (that's a gesture) | |
if (e.targetTouches.length != 1) | |
return false; | |
var dx = e.targetTouches[0].clientX - this.screenX; | |
var dy = e.targetTouches[0].clientY - this.screenY; | |
this.screenX = e.targetTouches[0].clientX; | |
this.screenY = e.targetTouches[0].clientY; | |
if (!this.dragging_) { | |
var diffX = this.startX - this.screenX; | |
var diffY = this.startY - this.screenY; | |
var distance = diffX * diffX + diffY * diffY; | |
if (distance > this.hysteresisDistanceSquared_) { | |
this.initializeDrag_(e); | |
if (!this.dragging_) { | |
// If the start drag is cancelled, stop trying to drag. | |
this.endDrag(e); | |
return; | |
} | |
} | |
} | |
var pos = this.calculatePosition_(dx, dy); | |
var x = pos.x; | |
var y = pos.y; | |
if (this.dragging_) { | |
var rv = this.dispatchEvent(new goog.fx.DragEvent( | |
goog.fx.Dragger.EventType.BEFOREDRAG, this, e.clientX, e.clientY, | |
e, x, y)); | |
// Only do the defaultAction and dispatch drag event if predrag didn't | |
// prevent default | |
if (rv !== false) { | |
this.doDrag(e, x, y, false); | |
e.preventDefault(); | |
} | |
} | |
}; | |
chartbeat.widgets.IDragger.prototype.onTouchEnd = function(e) { | |
// Prevent the browser from doing its default thing (scroll, zoom) | |
e.preventDefault(); | |
// Stop tracking when the last finger is removed from this element | |
if (e.targetTouches.length > 0) | |
return false; | |
this.handle.removeEventListener('touchmove', function(e) { return self.onTouchMove(e) }, false); | |
this.handle.removeEventListener('touchend', function(e) { return self.onTouchEnd(e) }, false); | |
this.endDrag(e); | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment