on c19ccfe help sign in
149 lines 4.1 kB history raw
2Elm.Native.Touch = function(elm) {
3 'use strict';
5 elm.Native = elm.Native || {};
6 if (elm.Native.Touch) return elm.Native.Touch;
8 var Signal = Elm.Signal(elm);
9 var JS = Elm.JavaScript(elm);
10 var _ = Elm.Native.Utils(elm);
12 function Dict() {
13 this.keys = [];
14 this.values = [];
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 }
38 var root = Signal.constant([]),
39 tapTime = 500,
40 hasTap = false,
41 tap = {_:{},x:0,y:0},
42 dict = new Dict();
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 }
56 var node = elm.display === ElmRuntime.Display.FULLSCREEN ? document : elm.node;
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 }
70 }
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);
81 }
83 listen("touchstart", start);
84 listen("touchmove", function(_){});
85 listen("touchend", end);
86 listen("touchcancel", end);
87 listen("touchleave", end);
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.values.length > 0) {
123 elm.notify(root.id, []);
124 --mouseID;
125 }
126 dict.clear();
127 });
129 function dependency(f) {
130 var sig = A2( Signal.lift, f, root );
131 root.defaultNumberOfKids += 1;
132 sig.defaultNumberOfKids = 0;
133 return sig;
134 }
136 var touches = dependency(JS.toList);
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 }();
147 return elm.Native.Touch = { touches: touches, taps: taps };
149};