on master help sign in
Add mouse support in the `Touch` library
commit9f2964992ad406f8ad1d13fbd450d9059976008c authorevancz <info@elm-lang.org> dateSun 5 May 2013, 17:51 (14 years ago) parent82d753a
2 files changed+44−4
libraries/Native/Signal/Touch.js+43−3 libraries/Touch.elm+1−1
libraries/Native/Signal/Touch.js+43−3
@@ -28,6 +28,10 @@ Elm.Native.Touch = function(elm) {
2828 this.values.splice(i,1);
2929 return t;
3030 };
31+ this.clear = function() {
32+ this.keys = [];
33+ this.values = [];
34+ };
3135 }
3236
3337 var root = Signal.constant([]),
@@ -48,6 +52,8 @@ Elm.Native.Touch = function(elm) {
4852 };
4953 }
5054
55+ var node = elm.node === document.body ? document : elm.node;
56+
5157 function start(e) {
5258 dict.insert(e.identifier,{x:e.pageX,y:e.pageY,t:Date.now()});
5359 }
@@ -65,10 +71,10 @@ Elm.Native.Touch = function(elm) {
6571 var ts = new Array(e.touches.length);
6672 for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
6773 var hasListener = elm.notify(root.id, ts);
68− if (!hasListener) return elm.node.removeEventListener(name, update);
74+ if (!hasListener) return node.removeEventListener(name, update);
6975 e.preventDefault();
7076 }
71− elm.node.addEventListener(name, update);
77+ node.addEventListener(name, update);
7278 }
7379
7480 listen("touchstart", start);
@@ -77,6 +83,40 @@ Elm.Native.Touch = function(elm) {
7783 listen("touchcancel", end);
7884 listen("touchleave", end);
7985
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+
80120 function dependency(f) {
81121 var sig = A2( Signal.lift, f, root );
82122 root.defaultNumberOfKids += 1;
@@ -84,7 +124,7 @@ Elm.Native.Touch = function(elm) {
84124 return sig;
85125 }
86126
87− var touches = dependency(JS.fromList);
127+ var touches = dependency(JS.toList);
88128
89129 var taps = function() {
90130 var sig = dependency(function(_) { return tap; });
libraries/Touch.elm+1−1
@@ -4,7 +4,7 @@ module Touch where
44
55import Native.Touch as T
66
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 }
88
99-- A list of touches. Each ongoing touch is represented by a set of
1010-- coordinates and an identifier id that allows you to distinguish