on master help sign in
Add mouse support in the `Touch` library
9f29649evancz
libraries/Native/Signal/Touch.js+43−3
@@ -28,6 +28,10 @@ Elm.Native.Touch = function(elm) {
28 this.values.splice(i,1);
28 this.values.splice(i,1);
29 return t;
29 return t;
30 };
30 };
31+ this.clear = function() {
32+ this.keys = [];
33+ this.values = [];
34+ };
31 }
35 }
32
36
33 var root = Signal.constant([]),
37 var root = Signal.constant([]),
@@ -48,6 +52,8 @@ Elm.Native.Touch = function(elm) {
48 };
52 };
49 }
53 }
50
54
55+ var node = elm.node === document.body ? document : elm.node;
56+
51 function start(e) {
57 function start(e) {
52 dict.insert(e.identifier,{x:e.pageX,y:e.pageY,t:Date.now()});
58 dict.insert(e.identifier,{x:e.pageX,y:e.pageY,t:Date.now()});
53 }
59 }
@@ -65,10 +71,10 @@ Elm.Native.Touch = function(elm) {
65 var ts = new Array(e.touches.length);
71 var ts = new Array(e.touches.length);
66 for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
72 for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
67 var hasListener = elm.notify(root.id, ts);
73 var hasListener = elm.notify(root.id, ts);
68− if (!hasListener) return elm.node.removeEventListener(name, update);
74+ if (!hasListener) return node.removeEventListener(name, update);
69 e.preventDefault();
75 e.preventDefault();
70 }
76 }
71− elm.node.addEventListener(name, update);
77+ node.addEventListener(name, update);
72 }
78 }
73
79
74 listen("touchstart", start);
80 listen("touchstart", start);
@@ -77,6 +83,40 @@ Elm.Native.Touch = function(elm) {
77 listen("touchcancel", end);
83 listen("touchcancel", end);
78 listen("touchleave", end);
84 listen("touchleave", end);
79
85
86+ function move(e) {
87+ for (var i = root.value.length; i--; ) {
88+ if (root.value[i].id === 0) {
89+ root.value[i].x = e.pageX;
90+ root.value[i].y = e.pageY;
91+ elm.notify(root.id, root.value);
92+ break;
93+ }
94+ }
95+ }
96+ node.addEventListener("mousedown", function(e) {
97+ node.addEventListener("mousemove", move);
98+ e.identifier = 0;
99+ root.value.push(touch(e));
100+ start(e);
101+ elm.notify(root.id, root.value);
102+ });
103+ node.addEventListener("mouseup", function(e) {
104+ node.removeEventListener("mousemove", move);
105+ e.identifier = 0;
106+ end(e);
107+ for (var i = root.value.length; i--; ) {
108+ if (root.value[i].id === 0) {
109+ root.value.splice(i, 1);
110+ break;
111+ }
112+ }
113+ elm.notify(root.id, root.value);
114+ });
115+ node.addEventListener("blur", function() {
116+ node.removeEventListener("mousemove", move);
117+ dict.clear();
118+ });
119+
80 function dependency(f) {
120 function dependency(f) {
81 var sig = A2( Signal.lift, f, root );
121 var sig = A2( Signal.lift, f, root );
82 root.defaultNumberOfKids += 1;
122 root.defaultNumberOfKids += 1;
@@ -84,7 +124,7 @@ Elm.Native.Touch = function(elm) {
84 return sig;
124 return sig;
85 }
125 }
86
126
87− var touches = dependency(JS.fromList);
127+ var touches = dependency(JS.toList);
88
128
89 var taps = function() {
129 var taps = function() {
90 var sig = dependency(function(_) { return tap; });
130 var sig = dependency(function(_) { return tap; });
libraries/Touch.elm+1−1
@@ -4,7 +4,7 @@ module Touch where
4
4
5import Native.Touch as T
5import Native.Touch as T
6
6
7−type Touch = { x:Int, y:Int, id:Int, x0:Int, y0:Int, t0:Time }
7+-- type Touch = { x:Int, y:Int, id:Int, x0:Int, y0:Int, t0:Time }
8
8
9-- A list of touches. Each ongoing touch is represented by a set of
9-- A list of touches. Each ongoing touch is represented by a set of
10-- coordinates and an identifier id that allows you to distinguish
10-- coordinates and an identifier id that allows you to distinguish