@@ -1,27 +1,27 @@
1−/*
2−import Signal
3−import Native.Misc
4−*/
51
6−(function() {
2+Elm.Native.Signal.Mouse = function(elm) {
73 'use strict';
4+ elm.Native = elm.Native || {};
5+ elm.Native.Signal = elm.Native.Signal || {};
6+ if (elm.Native.Signal.Mouse) return elm.Native.Signal.Mouse;
87
9− var Misc = Elm.Native.Misc;
8+ var Signal = Elm.Signal(elm);
9+ var Misc = Elm.Native.Misc(elm);
1010
11− var position = Elm.Signal.constant(Misc.Tuple(0,0));
11+ var position = Signal.constant(Misc.Tuple(0,0));
1212 position.defaultNumberOfKids = 2;
1313
1414 // do not get rid of x and y. By setting their default number
1515 // of kids, it is possible to detatch the mouse listeners if
1616 // they are not needed.
17− var x = Elm.Signal.lift(function(p){return p._0})(position);
17+ var x = Signal.lift(function(p){return p._0})(position);
1818 x.defaultNumberOfKids = 0;
19− var y = Elm.Signal.lift(function(p){return p._1})(position);
19+ var y = Signal.lift(function(p){return p._1})(position);
2020 y.defaultNumberOfKids = 0;
2121
22− var isDown = Elm.Signal.constant(false);
23− var isClicked = Elm.Signal.constant(false);
24− var clicks = Elm.Signal.constant(Misc.Tuple());
22+ var isDown = Signal.constant(false);
23+ var isClicked = Signal.constant(false);
24+ var clicks = Signal.constant(Misc.Tuple0);
2525
2626 function getXY(e) {
2727 var posx = 0;
@@ -36,46 +36,43 @@ import Native.Misc
3636 posy = e.clientY + document.body.scrollTop +
3737 document.documentElement.scrollTop;
3838 }
39− return Misc.Tuple(posx, posy);
39+ return Misc.Tuple2(posx, posy);
4040 }
4141
42− Misc.addListener(document, 'click', function(e) {
43− var hasListener1 = Dispatcher.notify(isClicked.id, true);
44− var hasListener2 = Dispatcher.notify(clicks.id, Misc.Tuple());
45− Dispatcher.notify(isClicked.id, false);
46− if (!hasListener1 && !hasListener2)
47− this.removeEventListener('click',arguments.callee,false);
48− });
49− Misc.addListener(document, 'mousedown', function(e) {
50− var hasListener = Dispatcher.notify(isDown.id, true);
51− if (!hasListener)
52− this.removeEventListener('mousedown',arguments.callee,false);
53− });
54− Misc.addListener(document, 'mouseup', function(e) {
55− var hasListener = Dispatcher.notify(isDown.id, false);
56− if (!hasListener)
57− this.removeEventListener('mouseup',arguments.callee,false);
58− });
59− Misc.addListener(document, 'mousemove', function(e) {
60− var hasListener = Dispatcher.notify(position.id, getXY(e));
61− if (!hasListener)
62− this.removeEventListener('mousemove',arguments.callee,false);
63− });
42+ function click(e) {
43+ var hasListener1 = elm.notify(isClicked.id, true);
44+ var hasListener2 = elm.notify(clicks.id, Misc.Tuple0);
45+ elm.notify(isClicked.id, false);
46+ if (!hasListener1 && !hasListener2)
47+ this.removeEventListener('click',arguments.callee,false);
48+ }
49+
50+ function down(e) {
51+ var hasListener = elm.notify(isDown.id, true);
52+ if (!hasListener) this.removeEventListener('mousedown',arguments.callee,false);
53+ }
54+
55+ function up(e) {
56+ var hasListener = elm.notify(isDown.id, false);
57+ if (!hasListener) this.removeEventListener('mouseup',arguments.callee,false);
58+ }
59+
60+ function move(e) {
61+ var hasListener = elm.notify(position.id, getXY(e));
62+ if (!hasListener) this.removeEventListener('mousemove',arguments.callee,false);
63+ }
64+
65+ Misc.addListener(elm.node, 'click' , click);
66+ Misc.addListener(elm.node, 'mousedown', down);
67+ Misc.addListener(elm.node, 'mouseup' , up);
68+ Misc.addListener(elm.node, 'mousemove', move);
6469
65− var clickedOn = function(elem) {
66− var node = Render.render(elem);
67− var click = Elm.Signal.constant(false);
68− Misc.addListener(node, 'click', function(e) {
69− Dispatcher.notify(click.id, true);
70− Dispatcher.notify(click.id, false);
71− });
72− return Misc.Tuple(Misc.wrap(node), click);
70+ return elm.Native.Signal.Mouse = {
71+ position: position,
72+ x:x,
73+ y:y,
74+ isClicked: isClicked,
75+ isDown: isDown,
76+ clicks: clicks
7377 };
74− Elm.Native.Mouse = {position: position,
75− x:x,
76− y:y,
77− isClicked: isClicked,
78− isDown: isDown,
79− clicks: clicks,
80− isClickedOn: clickedOn};
81−}());
78+};