on master help sign in
Add `hoverable` and `hoverables`
commitafacd1b8584866a920a74300621587c0647ff34e authorevancz <info@elm-lang.org> dateSat 15 Jun 2013, 00:49 (13 years ago) parent316cd1d
6 files changed+40−4
changelog.txt+1 compiler/Language/Elm.hs−1 libraries/Graphics/Element.elm+3−2 libraries/Graphics/Input.elm+8 libraries/Native/Graphics/Input.js+16−1 runtime/Render/Element.js+12
changelog.txt+1
@@ -5,6 +5,7 @@ Release 0.8.1
55* Hot-swapping
66* Type annotations in let expressions (Andrew)
77* Pattern aliases with `as` (Andrew)
8+* Detect hovering over any Element
89* Opacity on arbitrary forms in collages
910
1011
compiler/Language/Elm.hs−1
@@ -56,4 +56,3 @@ runtime = getDataFileName "elm-runtime.js"
5656-- |The absolute path to Elm's core library documentation.
5757docs :: IO FilePath
5858docs = getDataFileName "docs.json"
59−
libraries/Graphics/Element.elm+3−2
@@ -25,7 +25,8 @@ type Properties = {
2525 opacity : Float,
2626 color : Maybe Color,
2727 href : JSString,
28− tag : JSString
28+ tag : JSString,
29+ hover : ()
2930 }
3031
3132type Element = { props : Properties, element : ElementPrim }
@@ -87,7 +88,7 @@ link href e = let p = e.props in
8788
8889emptyStr = JS.fromString ""
8990newElement w h e =
90− { props = Properties (guid ()) w h 1 Nothing emptyStr emptyStr, element = e }
91+ { props = Properties (guid ()) w h 1 Nothing emptyStr emptyStr (), element = e }
9192
9293data ElementPrim
9394 = Image ImageStyle Int Int JSString
libraries/Graphics/Input.elm+8
@@ -67,6 +67,14 @@ checkbox b =
6767 let cbs = N.checkboxes b
6868 in (lift (cbs.box id) cbs.events, cbs.events)
6969
70+hoverables : a -> { events : Signal a,
71+ hoverable : (Bool -> a) -> Element -> Element }
72+
73+hoverable : Element -> (Element, Signal Bool)
74+hoverable elem =
75+ let pool = N.hoverables False
76+ in (pool.hoverable id elem, pool.events)
77+
7078-- Represents the current state of a text field. The `string` represents the
7179-- characters filling the text field. The `selectionStart` and `selectionEnd`
7280-- values represent what the user has selected with their mouse or keyboard.
libraries/Native/Graphics/Input.js+16−1
@@ -12,7 +12,8 @@ Elm.Native.Graphics.Input = function(elm) {
1212 var Signal = Elm.Signal(elm);
1313 var newElement = Elm.Graphics.Element(elm).newElement;
1414 var JS = Elm.Native.JavaScript(elm);
15− var Tuple2 = Elm.Native.Utils(elm).Tuple2;
15+ var Utils = Elm.Native.Utils(elm);
16+ var Tuple2 = Utils.Tuple2;
1617
1718 function dropDown(values) {
1819 var entries = JS.fromList(values);
@@ -145,6 +146,19 @@ Elm.Native.Graphics.Input = function(elm) {
145146 }
146147
147148
149+ function hoverables(defaultValue) {
150+ var events = Signal.constant(defaultValue);
151+ function hoverable(handler, elem) {
152+ function onHover(bool) {
153+ elm.notify(events.id, handler(bool));
154+ }
155+ var props = Utils.replace([['hover',onHover]], elem.props);
156+ return { props:props, element:elem.element };
157+ }
158+ return { _:{}, hoverable:F2(hoverable), events:events };
159+ }
160+
161+
148162 function checkboxes(defaultValue) {
149163 var events = Signal.constant(defaultValue);
150164
@@ -271,6 +285,7 @@ Elm.Native.Graphics.Input = function(elm) {
271285 return elm.Native.Graphics.Input = {
272286 buttons:buttons,
273287 customButtons:customButtons,
288+ hoverables:hoverables,
274289 checkboxes:checkboxes,
275290 fields:mkTextPool('text'),
276291 emails:mkTextPool('email'),
runtime/Render/Element.js+12
@@ -21,6 +21,18 @@ function setProps(props, e) {
2121 a.appendChild(e);
2222 return a;
2323 }
24+ if (props.hover.ctor !== 'Tuple0') {
25+ var overCount = 0;
26+ e.addEventListener('mouseover', function() {
27+ if (overCount++ > 0) return;
28+ props.hover(true);
29+ });
30+ e.addEventListener('mouseout', function(evt) {
31+ if (e.contains(evt.toElement || evt.relatedTarget)) return;
32+ overCount = 0;
33+ props.hover(false);
34+ });
35+ }
2436 return e;
2537}
2638