@@ -1,15 +1,69 @@
2Elm.Touch = function() {
2Elm.Touch = function() {
4+ function log(id,msg) {
5+ var e = document.getElementById('logger' + id);
7+ e = document.createElement('div');
9+ document.body.appendChild(e);
18+ this.insert = function(key,value) {
20+ this.values.push(value);
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};
26+ this.remove = function(key) {
27+ var i = this.keys.indexOf(key);
29+ var t = this.values[i];
30+ this.keys.splice(i,1);
31+ this.values.splice(i,1);
3− var touches = Elm.Signal.constant(["Nil"]);
36+ var root = Elm.Signal.constant([]),
39+ tap = {_:[true],x:[0],y:[0]},
5 function touch(t) {
42 function touch(t) {
43+ var r = dict.lookup(t.identifier);
6− return {_ : [true], x: [t.pageX], y: [t.pageY], id: [t.identifier] };
44+ return {_ : [true], id: [t.identifier],
45+ x: [t.pageX], y: [t.pageY],
46+ x0: [r.x], y0: [r.y],
51+ dict.insert(e.identifier,{x:e.pageX,y:e.pageY,t:Date.now()});
54+ var t = dict.remove(e.identifier);
55+ if (Date.now() - t.t < tapTime) {
57+ tap = {_:[true], x:[t.x], y:[t.y]};
9− function listen(name) {
61+ function listen(name, f) {
10 function update(e) {
62 function update(e) {
11− var ts = Elm.JavaScript.castJSArrayToList(e.touches);
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]); }
12− var hasListener = Dispatcher.notify(touches.id, Elm.List.map(touch)(ts));
66+ var hasListener = Dispatcher.notify(root.id, ts);
13 if (!hasListener)
67 if (!hasListener)
14 return this.removeEventListener(name,arguments.callee,false);
68 return this.removeEventListener(name,arguments.callee,false);
15 e.preventDefault();
69 e.preventDefault();
@@ -17,11 +71,27 @@ Elm.Touch = function() {
17 Value.addListener(document, name, update);
71 Value.addListener(document, name, update);
20− listen("touchstart");
74+ listen("touchstart", start);
21− listen("touchmove");
75+ listen("touchmove", function(_){});
22− listen("touchend");
76+ listen("touchend", end);
23− listen("touchcancel");
77+ listen("touchcancel", end);
24− listen("touchleave");
78+ listen("touchleave", end);
80+ function dependency(f) {
81+ var signal = Elm.Signal.lift(f)(root);
82+ root.defaultNumberOfKids += 1;
83+ signal.defaultNumberOfKids = 0;
87+ var touches = dependency(function(ts) {
88+ return Elm.JavaScript.castJSArrayToList(ts);
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);
26− return { touches: touches };
96+ return { touches: touches, taps: taps };