-
Notifications
You must be signed in to change notification settings - Fork 0
/
rx.touch.js
127 lines (125 loc) · 4.13 KB
/
rx.touch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Generated by CoffeeScript 1.5.0
(function() {
(function($) {
var context;
Rx.Observable.prototype.normalizeTouch = function(preventDefault) {
return this.doAction(function(e) {
if (preventDefault) {
return e.preventDefault();
}
}).selectMany(function(e) {
return Rx.Observable.fromArray(e.originalEvent.changedTouches).select(function(touch) {
return {
preventDefault: e.preventDefault.bind(e),
identifier: touch.identifier,
pageX: touch.pageX,
pageY: touch.pageY
};
});
});
};
Rx.Observable.prototype.normalizeMouse = function(preventDefault) {
return this.doAction(function(e) {
if (preventDefault) {
return e.preventDefault();
}
}).select(function(mouse) {
return {
preventDefault: mouse.preventDefault.bind(mouse),
identifier: 0,
pageX: mouse.pageX,
pageY: mouse.pageY
};
});
};
$.fn.upAsObservable = function(preventDefault) {
var target;
if (preventDefault == null) {
preventDefault = true;
}
target = $(this);
return Rx.Observable.amb(target.onAsObservable('touchend').normalizeTouch(preventDefault), target.onAsObservable('mouseup').normalizeMouse(preventDefault));
};
$.fn.downAsObservable = function(preventDefault) {
var target;
if (preventDefault == null) {
preventDefault = true;
}
target = $(this);
return Rx.Observable.amb(target.onAsObservable('touchstart').normalizeTouch(preventDefault), target.onAsObservable('mousedown').normalizeMouse(preventDefault));
};
context = $(document);
$.fn.movesAsObservable = function(preventDefault) {
var down, move, moves, target, up;
if (preventDefault == null) {
preventDefault = true;
}
target = $(this);
up = context.upAsObservable(preventDefault);
down = target.downAsObservable(preventDefault);
move = Rx.Observable.amb(context.onAsObservable('touchmove').normalizeTouch(preventDefault), context.onAsObservable('mousemove').normalizeMouse(preventDefault));
moves = down.selectMany(function(e) {
return move.where(function(b) {
return b.identifier === e.identifier;
}).takeUntil(up.where(function(b) {
return b.identifier === e.identifier;
})).startWith(e);
});
return moves.groupByUntil(function(e) {
return e.identifier;
}, function(e) {
return e;
}, function(group) {
return up.where(function(e) {
return e.identifier === group.key;
});
}, function(key) {
return key;
});
};
return $.fn.tapAsObservable = function(preventDefault, radius) {
var gestures, inPageRange, target;
if (preventDefault == null) {
preventDefault = true;
}
if (radius == null) {
radius = 50;
}
target = $(this);
gestures = [];
inPageRange = function(e1, e2, r) {
return (Math.pow(e2.pageX - e1.pageX, 2) + Math.pow(e2.pageY - e1.pageY, 2)) < r * r;
};
return Rx.Observable.createWithDisposable(function(o) {
var groups;
groups = target.downAsObservable(preventDefault).groupByUntil(function(e) {
var g, _i, _len;
for (_i = 0, _len = gestures.length; _i < _len; _i++) {
g = gestures[_i];
if (inPageRange(e, g, radius)) {
return g;
}
}
return e;
}, function(x) {
return x;
}, function(x) {
return x.throttle(350).take(1);
}, function(e) {
return e.pageX + ":" + e.pageY;
});
return groups.subscribe(function(group) {
o.onNext(group);
return group.take(1).subscribe(function(e) {
gestures.push(e);
return group.takeLast(1).subscribe(function() {
var index;
index = gestures.indexOf(e);
return gestures.splice(index, 1);
});
});
});
});
};
})(jQuery);
}).call(this);