on 63243ca help sign in
51 lines 1.7 kB history raw
1Elm.Native.Mouse = {};
2Elm.Native.Mouse.make = function(elm) {
4 elm.Native = elm.Native || {};
5 elm.Native.Mouse = elm.Native.Mouse || {};
6 if (elm.Native.Mouse.values) return elm.Native.Mouse.values;
8 var Signal = Elm.Signal.make(elm);
9 var Utils = Elm.Native.Utils.make(elm);
11 var position = Signal.constant(Utils.Tuple2(0,0));
12 position.defaultNumberOfKids = 2;
14 // do not move x and y into Elm. By setting their default number
15 // of kids, it is possible to detatch the mouse listeners if
16 // they are not needed.
17 var x = A2( Signal.lift, function(p){return p._0}, position);
18 x.defaultNumberOfKids = 0;
19 var y = A2( Signal.lift, function(p){return p._1}, position);
20 y.defaultNumberOfKids = 0;
22 var isDown = Signal.constant(false);
23 var isClicked = Signal.constant(false);
24 var clicks = Signal.constant(Utils.Tuple0);
26 var node = elm.display === ElmRuntime.Display.FULLSCREEN ? document : elm.node;
28 elm.addListener([isClicked.id, clicks.id], node, 'click', function click() {
29 elm.notify(isClicked.id, true);
30 elm.notify(clicks.id, Utils.Tuple0);
31 elm.notify(isClicked.id, false);
32 });
33 elm.addListener([isDown.id], node, 'mousedown', function down() {
34 elm.notify(isDown.id, true);
35 });
36 elm.addListener([isDown.id], node, 'mouseup', function up() {
37 elm.notify(isDown.id, false);
38 });
39 elm.addListener([position.id], node, 'mousemove', function move(e) {
40 elm.notify(position.id, Utils.getXY(e));
41 });
43 return elm.Native.Mouse.values = {
44 position: position,
45 x:x,
46 y:y,
47 isClicked: isClicked,
48 isDown: isDown,
49 clicks: clicks
50 };
51};