on 727d708 help sign in
66 lines 2.0 kB history raw
2Elm.Native.Mouse = function(elm) {
3 'use strict';
4 elm.Native = elm.Native || {};
5 if (elm.Native.Mouse) return elm.Native.Mouse;
7 var Signal = Elm.Signal(elm);
8 var Utils = Elm.Native.Utils(elm);
10 var position = Signal.constant(Utils.Tuple2(0,0));
11 position.defaultNumberOfKids = 2;
13 // do not move x and y into Elm. By setting their default number
14 // of kids, it is possible to detatch the mouse listeners if
15 // they are not needed.
16 var x = A2( Signal.lift, function(p){return p._0}, position);
17 x.defaultNumberOfKids = 0;
18 var y = A2( Signal.lift, function(p){return p._1}, position);
19 y.defaultNumberOfKids = 0;
21 var isDown = Signal.constant(false);
22 var isClicked = Signal.constant(false);
23 var clicks = Signal.constant(Utils.Tuple0);
25 function getXY(e) {
26 var posx = 0;
27 var posy = 0;
28 if (!e) e = window.event;
29 if (e.pageX || e.pageY) {
30 posx = e.pageX;
31 posy = e.pageY;
32 } else if (e.clientX || e.clientY) {
33 posx = e.clientX + document.body.scrollLeft +
34 document.documentElement.scrollLeft;
35 posy = e.clientY + document.body.scrollTop +
36 document.documentElement.scrollTop;
37 }
38 return Utils.Tuple2(posx-elm.node.offsetX, posy-elm.node.offsetY);
39 }
41 var node = elm.display === ElmRuntime.Display.FULLSCREEN ? document : elm.node;
43 elm.addListener([isClicked.id, clicks.id], node, 'click', function click() {
44 elm.notify(isClicked.id, true);
45 elm.notify(clicks.id, Utils.Tuple0);
46 elm.notify(isClicked.id, false);
47 });
48 elm.addListener([isDown.id], node, 'mousedown', function down() {
49 elm.notify(isDown.id, true);
50 });
51 elm.addListener([isDown.id], node, 'mouseup', function up() {
52 elm.notify(isDown.id, false);
53 });
54 elm.addListener([position.id], node, 'mousemove', function move(e) {
55 elm.notify(position.id, getXY(e));
56 });
58 return elm.Native.Mouse = {
59 position: position,
60 x:x,
61 y:y,
62 isClicked: isClicked,
63 isDown: isDown,
64 clicks: clicks
65 };
66};