4Elm.Mouse = function() {
7 /** position : Signal (Int,Int)
8 The current mouse position.
10 var position = Elm.Signal.constant(Value.Tuple(0,0));
11 position.defaultNumberOfKids = 2;
14 The current x-coordinate of the mouse.
16 var x = Elm.Signal.lift(function(p){return p[1];})(position);
17 x.defaultNumberOfKids = 0;
20 The current y-coordinate of the mouse.
22 var y = Elm.Signal.lift(function(p){return p[2];})(position);
23 y.defaultNumberOfKids = 0;
27 /** isDown : Signal Bool
28 The current state of the left mouse-button.
29 True when the button is down, and false otherwise.
31 var isDown = Elm.Signal.constant(false);
33 /** isClicked : Signal Bool
34 True immediately after the left mouse-button has been clicked,
37 var isClicked = Elm.Signal.constant(false);
39 /** clicks : Signal ()
40 Always equal to unit. Event triggers on every mouse click.
42 var clicks = Elm.Signal.constant(Value.Tuple());
47 if (!e) e = window.event;
48 if (e.pageX || e.pageY) {
51 } else if (e.clientX || e.clientY) {
52 posx = e.clientX + document.body.scrollLeft +
53 document.documentElement.scrollLeft;
54 posy = e.clientY + document.body.scrollTop +
55 document.documentElement.scrollTop;
57 return Value.Tuple(posx, posy);
60 Value.addListener(document, 'click', function(e) {
61 var hasListener1 = Dispatcher.notify(isClicked.id, true);
62 var hasListener2 = Dispatcher.notify(clicks.id, Value.Tuple());
63 Dispatcher.notify(isClicked.id, false);
64 if (!hasListener1 && !hasListener2)
65 this.removeEventListener('click',arguments.callee,false);
67 Value.addListener(document, 'mousedown', function(e) {
68 var hasListener = Dispatcher.notify(isDown.id, true);
70 this.removeEventListener('mousedown',arguments.callee,false);
72 Value.addListener(document, 'mouseup', function(e) {
73 var hasListener = Dispatcher.notify(isDown.id, false);
75 this.removeEventListener('mouseup',arguments.callee,false);
77 Value.addListener(document, 'mousemove', function(e) {
78 var hasListener = Dispatcher.notify(position.id, getXY(e));
80 this.removeEventListener('mousemove',arguments.callee,false);
83 /** isClickedOn : Element -> (Element, Signal Bool)
84 Determine whether an element has been clicked. The resulting pair
85 is a signal of booleans that is true when its paired element has
86 been clicked. The signal is True immediately after the left
87 mouse-button has been clicked, and false otherwise.
89 var clickedOn = function(elem) {
90 var node = Render.render(elem);
91 var click = Elm.Signal.constant(false);
92 Value.addListener(node, 'click', function(e) {
93 Dispatcher.notify(click.id, true);
94 Dispatcher.notify(click.id, false);
96 return Value.Tuple(Value.wrap(node), click);
98 return {position: position,
101 isClicked: isClicked,
104 isClickedOn: clickedOn
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list