@@ -1,15 +1,69 @@
11
22Elm.Touch = function() {
3− var touches = Elm.Signal.constant(["Nil"]);
3+
4+ function log(id,msg) {
5+ var e = document.getElementById('logger' + id);
6+ if (!e) {
7+ e = document.createElement('div');
8+ e.id = 'logger' + id;
9+ document.body.appendChild(e);
10+ }
11+ e.innerHTML = msg;
12+ }
13+
14+ function Dict() {
15+ this.keys = [];
16+ this.values = [];
17+
18+ this.insert = function(key,value) {
19+ this.keys.push(key);
20+ this.values.push(value);
21+ }
22+ this.lookup = function(key) {
23+ var i = this.keys.indexOf(key)
24+ return i >= 0 ? this.values[i] : {x:0,y:0,t:0};
25+ }
26+ this.remove = function(key) {
27+ var i = this.keys.indexOf(key);
28+ if (i < 0) return;
29+ var t = this.values[i];
30+ this.keys.splice(i,1);
31+ this.values.splice(i,1);
32+ return t;
33+ }
34+ }
35+
36+ var root = Elm.Signal.constant([]),
37+ tapTime = 500,
38+ hasTap = false,
39+ tap = {_:[true],x:[0],y:[0]},
40+ dict = new Dict();
441
542 function touch(t) {
6− return {_ : [true], x: [t.pageX], y: [t.pageY], id: [t.identifier] };
43+ var r = dict.lookup(t.identifier);
44+ return {_ : [true], id: [t.identifier],
45+ x: [t.pageX], y: [t.pageY],
46+ x0: [r.x], y0: [r.y],
47+ t0: [r.t] };
48+ }
49+
50+ function start(e) {
51+ dict.insert(e.identifier,{x:e.pageX,y:e.pageY,t:Date.now()});
52+ }
53+ function end(e) {
54+ var t = dict.remove(e.identifier);
55+ if (Date.now() - t.t < tapTime) {
56+ hasTap = true;
57+ tap = {_:[true], x:[t.x], y:[t.y]};
58+ }
759 }
860
9− function listen(name) {
61+ function listen(name, f) {
1062 function update(e) {
11− var ts = Elm.JavaScript.castJSArrayToList(e.touches);
12− var hasListener = Dispatcher.notify(touches.id, Elm.List.map(touch)(ts));
63+ for (var i = e.changedTouches.length; i--; ) { f(e.changedTouches[i]); }
64+ var ts = new Array(e.touches.length);
65+ for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
66+ var hasListener = Dispatcher.notify(root.id, ts);
1367 if (!hasListener)
1468 return this.removeEventListener(name,arguments.callee,false);
1569 e.preventDefault();
@@ -17,11 +71,27 @@ Elm.Touch = function() {
1771 Value.addListener(document, name, update);
1872 }
1973
20− listen("touchstart");
21− listen("touchmove");
22− listen("touchend");
23− listen("touchcancel");
24− listen("touchleave");
74+ listen("touchstart", start);
75+ listen("touchmove", function(_){});
76+ listen("touchend", end);
77+ listen("touchcancel", end);
78+ listen("touchleave", end);
79+
80+ function dependency(f) {
81+ var signal = Elm.Signal.lift(f)(root);
82+ root.defaultNumberOfKids += 1;
83+ signal.defaultNumberOfKids = 0;
84+ return signal;
85+ }
86+
87+ var touches = dependency(function(ts) {
88+ return Elm.JavaScript.castJSArrayToList(ts);
89+ });
90+ var taps = function() {
91+ function pred(_) { var b = hasTap; hasTap = false; return b; }
92+ var sig = dependency(function(_) { return tap; });
93+ return Elm.Signal.keepIf(pred)({_:[true],x:[0],y:[0]})(sig);
94+ }();
2595
26− return { touches: touches };
96+ return { touches: touches, taps: taps };
2797}();