@@ -1,149 +1,149 @@
11Elm.Native.Touch = {};
22Elm.Native.Touch.make = function(elm) {
33
4− elm.Native = elm.Native || {};
5− elm.Native.Touch = elm.Native.Touch || {};
6− if (elm.Native.Touch.values) return elm.Native.Touch.values;
4+ elm.Native = elm.Native || {};
5+ elm.Native.Touch = elm.Native.Touch || {};
6+ if (elm.Native.Touch.values) return elm.Native.Touch.values;
77
8− var Signal = Elm.Signal.make(elm);
9− var JS = Elm.JavaScript.make(elm);
10− var _ = Elm.Native.Utils.make(elm);
8+ var Signal = Elm.Signal.make(elm);
9+ var JS = Elm.JavaScript.make(elm);
10+ var _ = Elm.Native.Utils.make(elm);
1111
12− function Dict() {
13− this.keys = [];
14− this.values = [];
15−
16− this.insert = function(key,value) {
17− this.keys.push(key);
18− this.values.push(value);
19− };
20− this.lookup = function(key) {
21− var i = this.keys.indexOf(key)
22− return i >= 0 ? this.values[i] : {x:0,y:0,t:0};
23− };
24− this.remove = function(key) {
25− var i = this.keys.indexOf(key);
26− if (i < 0) return;
27− var t = this.values[i];
28− this.keys.splice(i,1);
29− this.values.splice(i,1);
30− return t;
31− };
32− this.clear = function() {
12+ function Dict() {
3313 this.keys = [];
3414 this.values = [];
35− };
36− }
37−
38− var root = Signal.constant([]),
39− tapTime = 500,
40− hasTap = false,
41− tap = {_:{},x:0,y:0},
42− dict = new Dict();
4315
44− function touch(t) {
45− var r = dict.lookup(t.identifier);
46− return {_ : {},
47− id: t.identifier,
48− x : t.pageX - elm.node.offsetX,
49− y : t.pageY - elm.node.offsetY,
50− x0: r.x,
51− y0: r.y,
52− t0: r.t
53− };
54− }
16+ this.insert = function(key,value) {
17+ this.keys.push(key);
18+ this.values.push(value);
19+ };
20+ this.lookup = function(key) {
21+ var i = this.keys.indexOf(key)
22+ return i >= 0 ? this.values[i] : {x:0,y:0,t:0};
23+ };
24+ this.remove = function(key) {
25+ var i = this.keys.indexOf(key);
26+ if (i < 0) return;
27+ var t = this.values[i];
28+ this.keys.splice(i,1);
29+ this.values.splice(i,1);
30+ return t;
31+ };
32+ this.clear = function() {
33+ this.keys = [];
34+ this.values = [];
35+ };
36+ }
37+
38+ var root = Signal.constant([]),
39+ tapTime = 500,
40+ hasTap = false,
41+ tap = {_:{},x:0,y:0},
42+ dict = new Dict();
43+
44+ function touch(t) {
45+ var r = dict.lookup(t.identifier);
46+ return {_ : {},
47+ id: t.identifier,
48+ x : t.pageX - elm.node.offsetX,
49+ y : t.pageY - elm.node.offsetY,
50+ x0: r.x,
51+ y0: r.y,
52+ t0: r.t
53+ };
54+ }
5555
56− var node = elm.display === ElmRuntime.Display.FULLSCREEN ? document : elm.node;
56+ var node = elm.display === ElmRuntime.Display.FULLSCREEN ? document : elm.node;
5757
58− function start(e) {
59− dict.insert(e.identifier,
60− {x: e.pageX - elm.node.offsetX,
61− y: e.pageY - elm.node.offsetY,
62− t: Date.now()});
63− }
64− function end(e) {
65− var t = dict.remove(e.identifier);
66− if (Date.now() - t.t < tapTime) {
67− hasTap = true;
68− tap = {_:{}, x:t.x, y:t.y};
58+ function start(e) {
59+ dict.insert(e.identifier,
60+ {x: e.pageX - elm.node.offsetX,
61+ y: e.pageY - elm.node.offsetY,
62+ t: Date.now()});
63+ }
64+ function end(e) {
65+ var t = dict.remove(e.identifier);
66+ if (Date.now() - t.t < tapTime) {
67+ hasTap = true;
68+ tap = {_:{}, x:t.x, y:t.y};
69+ }
6970 }
70− }
7171
72− function listen(name, f) {
73− function update(e) {
74− for (var i = e.changedTouches.length; i--; ) { f(e.changedTouches[i]); }
75− var ts = new Array(e.touches.length);
76− for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
77− elm.notify(root.id, ts);
78− e.preventDefault();
72+ function listen(name, f) {
73+ function update(e) {
74+ for (var i = e.changedTouches.length; i--; ) { f(e.changedTouches[i]); }
75+ var ts = new Array(e.touches.length);
76+ for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
77+ elm.notify(root.id, ts);
78+ e.preventDefault();
79+ }
80+ elm.addListener([root.id], node, name, update);
7981 }
80− elm.addListener([root.id], node, name, update);
81− }
8282
83− listen("touchstart", start);
84− listen("touchmove", function(_){});
85− listen("touchend", end);
86− listen("touchcancel", end);
87− listen("touchleave", end);
83+ listen("touchstart", start);
84+ listen("touchmove", function(_){});
85+ listen("touchend", end);
86+ listen("touchcancel", end);
87+ listen("touchleave", end);
8888
89− var mouseID = -1;
90− function move(e) {
91− for (var i = root.value.length; i--; ) {
92− if (root.value[i].id === mouseID) {
93− root.value[i].x = e.pageX - elm.node.offsetX;
94− root.value[i].y = e.pageY - elm.node.offsetY;
95− elm.notify(root.id, root.value);
96− break;
97− }
98− }
99− }
100− elm.addListener([root.id], node, "mousedown", function down(e) {
101− node.addEventListener("mousemove", move);
102− e.identifier = mouseID;
103− start(e);
104− root.value.push(touch(e));
105− elm.notify(root.id, root.value);
106− });
107− elm.addListener([root.id], node, "mouseup", function up(e) {
108− node.removeEventListener("mousemove", move);
109− e.identifier = mouseID;
110− end(e);
111− for (var i = root.value.length; i--; ) {
112− if (root.value[i].id === mouseID) {
113− root.value.splice(i, 1);
114− --mouseID;
115− break;
116− }
117− }
118− elm.notify(root.id, root.value);
119− });
120− elm.addListener([root.id], node, "blur", function blur(e) {
121− node.removeEventListener("mousemove", move);
122− if (root.value.length > 0) {
123− elm.notify(root.id, []);
124− --mouseID;
125− }
126− dict.clear();
127− });
89+ var mouseID = -1;
90+ function move(e) {
91+ for (var i = root.value.length; i--; ) {
92+ if (root.value[i].id === mouseID) {
93+ root.value[i].x = e.pageX - elm.node.offsetX;
94+ root.value[i].y = e.pageY - elm.node.offsetY;
95+ elm.notify(root.id, root.value);
96+ break;
97+ }
98+ }
99+ }
100+ elm.addListener([root.id], node, "mousedown", function down(e) {
101+ node.addEventListener("mousemove", move);
102+ e.identifier = mouseID;
103+ start(e);
104+ root.value.push(touch(e));
105+ elm.notify(root.id, root.value);
106+ });
107+ elm.addListener([root.id], node, "mouseup", function up(e) {
108+ node.removeEventListener("mousemove", move);
109+ e.identifier = mouseID;
110+ end(e);
111+ for (var i = root.value.length; i--; ) {
112+ if (root.value[i].id === mouseID) {
113+ root.value.splice(i, 1);
114+ --mouseID;
115+ break;
116+ }
117+ }
118+ elm.notify(root.id, root.value);
119+ });
120+ elm.addListener([root.id], node, "blur", function blur(e) {
121+ node.removeEventListener("mousemove", move);
122+ if (root.value.length > 0) {
123+ elm.notify(root.id, []);
124+ --mouseID;
125+ }
126+ dict.clear();
127+ });
128128
129− function dependency(f) {
130− var sig = A2( Signal.lift, f, root );
131− root.defaultNumberOfKids += 1;
132− sig.defaultNumberOfKids = 0;
133− return sig;
134− }
129+ function dependency(f) {
130+ var sig = A2( Signal.lift, f, root );
131+ root.defaultNumberOfKids += 1;
132+ sig.defaultNumberOfKids = 0;
133+ return sig;
134+ }
135135
136− var touches = dependency(JS.toList);
136+ var touches = dependency(JS.toList);
137137
138− var taps = function() {
139− var sig = dependency(function(_) { return tap; });
140− sig.defaultNumberOfKids = 1;
141− function pred(_) { var b = hasTap; hasTap = false; return b; }
142− var sig2 = A3( Signal.keepIf, pred, {_:{},x:0,y:0}, sig);
143− sig2.defaultNumberOfKids = 0;
144− return sig2;
145− }();
138+ var taps = function() {
139+ var sig = dependency(function(_) { return tap; });
140+ sig.defaultNumberOfKids = 1;
141+ function pred(_) { var b = hasTap; hasTap = false; return b; }
142+ var sig2 = A3( Signal.keepIf, pred, {_:{},x:0,y:0}, sig);
143+ sig2.defaultNumberOfKids = 0;
144+ return sig2;
145+ }();
146146
147− return elm.Native.Touch.values = { touches: touches, taps: taps };
147+ return elm.Native.Touch.values = { touches: touches, taps: taps };
148148
149149};