Created
October 13, 2014 16:41
-
-
Save joseph/a405924fe7f56631a38c to your computer and use it in GitHub Desktop.
TouchHound translates touch events back into iframes, for iOS8 home screen app workaround
This file contains hidden or 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
define(function (require) { | |
var Gala = require('gala'); | |
var C = require('common'); | |
var TouchHound = function (element) { | |
this._ = {}; | |
this._.element = element; | |
var handle = this._handleEvent.bind(this); | |
this._.handler = new Gala.ContactHandler( | |
this._.element, | |
{ start: handle, move: handle, end: handle, cancel: handle }, | |
{ cancelOnOwned: false } | |
).listen(); | |
}, PROTO = TouchHound.prototype; | |
// ## PROTECTED METHODS | |
PROTO._handleEvent = function (evt) { | |
var originalTouch; | |
if (evt.type == 'touchstart' || evt.type == 'touchmove') { | |
originalTouch = evt.touches[0]; | |
} else if (evt.type == 'touchend' || evt.type == 'touchcancel') { | |
originalTouch = evt.changedTouches[0]; | |
} else { | |
return; | |
} | |
var syntheticTouch = this._synthesizeTouch(originalTouch); | |
this._dispatchSyntheticEvent(evt, syntheticTouch); | |
} | |
PROTO._synthesizeTouch = function (originalTouch) { | |
var out = this._realElementFromPoint( | |
document, | |
originalTouch.clientX, | |
originalTouch.clientY | |
); | |
var target = out[0], pointX = out[1], pointY = out[2]; | |
var doc = target.ownerDocument; | |
return doc.createTouch( | |
doc.defaultView, | |
target, | |
originalTouch.identifier - 1000, | |
pointX, | |
pointY, | |
originalTouch.screenX, | |
originalTouch.screenY, | |
pointX, | |
pointY | |
); | |
} | |
PROTO._dispatchSyntheticEvent = function (originalEvent, touch) { | |
var target = touch.target; | |
var doc = target.ownerDocument; | |
var touchList = doc.createTouchList(touch); | |
var touchEvent = doc.createEvent('TouchEvent'); | |
touchEvent.initTouchEvent( | |
originalEvent.type, | |
true, | |
true, | |
doc.defaultView, | |
999, | |
touch.screenX, | |
touch.screenY, | |
touch.clientX, | |
touch.clientY, | |
false, | |
false, | |
false, | |
false, | |
touchList, | |
touchList, | |
touchList, | |
0, | |
0 | |
); | |
target.dispatchEvent(touchEvent); | |
} | |
PROTO._realElementFromPoint = function (doc, x, y) { | |
var elem = doc.elementFromPoint(x, y); | |
if (elem && typeof elem.contentDocument != 'undefined') { | |
var rect = elem.getBoundingClientRect(); | |
var pt = this._scalePoint(elem, { x: x - rect.left, y: y - rect.top }); | |
return this._realElementFromPoint(elem.contentDocument, pt.x, pt.y); | |
} | |
return [elem, x, y]; | |
} | |
PROTO._scalePoint = function (node, point) { | |
var scale = C.scaleFactor(node); | |
point.x = Math.round(point.x / scale.x); | |
point.y = Math.round(point.y / scale.y); | |
return point; | |
} | |
return TouchHound; | |
}); |
Am I right supposing that this snippet code is using requireJS library? Come on some help around here is needed for it seems Apple don't give a damn about this big bug and this is the only attempt so far to offer a solution!
hello Joseph!
I need some help with your code. A little example! You could be a hero out there!! ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Agreed. An example would be awesome! 😄