bebraw
3/22/2011 - 1:40 PM

Simple touch adapter proto for RightJS (not working atm)

Simple touch adapter proto for RightJS (not working atm)

// basic idea: http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript

var initiPad = function() {
    // little hack to use the solution on iPad only
    var isiPad = navigator.userAgent.match(/iPad/i) != null;

    if(isiPad) {
        // hack position. seems to give wrong coords otherwise
        Event.include({
            position: function() {
                var pos = this.$super.apply(this, arguments);

                var touchList = this._.touches;

                // XXX: figure out how to deal with multiple
                if(touchList.length > 0) {
                    var touch = this._.touches[0];

                    pos = {
                        x: touch.pageX - pos.x,
                        y: touch.pageY - pos.y
                    };
                }

                return pos;
            }
        });

        var $document = $(document);

        // not the most compact code. enough to get the point across :)
        // each of these handlers ~should~ map touch event to mouse one.
        // for some reason they don't seem to trigger (no bubbling?)
        $document.on('touchstart', function(e) {
            this.fire('mousedown', e);
        });

        $document.on('touchmove', function(e) {
            this.fire('mousemove', e);
        });

        $document.on('touchend', function(e) {
            this.fire('mouseup', e);
        });
    }
};