on 39b3fcb help sign in
elk/core/Graphics/Element.elm
87 lines 2.6 kB history raw
2module Grapics.Element where
4import Native.Guid (guid)
5import JavaScript as JS
6import List as List
7import Graphics.FillStyle
9type Properties = {
10 id : Int,
11 width : Int,
12 height : Int,
13 opacity : Float,
14 filling : FillStyle,
15 href : JSString,
16 tag : JSString
17 }
19data Element = Element Properties ElementPrim
21getProp get e = let (Element props _) = e in get props
22widthOf e = getProp .width e
23heightOf e = getProp .height e
24sizeOf e = getProp (\p -> (p.width, p.height)) e
26width w e =
27 case e of
28 Element props prim -> Element {props| width <- w} prim
29height h e =
30 case e of
31 Element props prim -> Element {props| height <- h} prim
32opacity o e =
33 case e of
34 Element props prim -> Element {props| opacity <- o} prim
35color c e =
36 case e of
37 Element props prim -> Element {props| filling <- c} prim
38filling f e =
39 case e of
40 Element props prim -> Element {props| filling <- f} prim
41tag name e =
42 case e of
43 Element props prim -> Element {props| tag <- JS.fromString name } prim
44link href e =
45 case e of
46 Element props prim -> Element {props| href <- JS.fromString href } prim
48emptyStr = JS.fromString ""
49newElement w h e =
50 Element (Properties (guid ()) w h 1 NoFill emptyStr emptyStr) e
52data ElementPrim
53 = Image ImageStyle String
54 | Container Position Element
55 | Flow Direction [Element]
56 | Spacer
57 | RawHtml JSString
58 | DomNode JSElement
60data ImageStyle = Plain | Fitted | Cropped (Int,Int) Int Int
61data Direction = DUp | DDown | DLeft | DRight | DIn | DOut
63image w h src = newElement w h (Image Plain src)
64fittedImage w h src = newElement w h (Image Fitted src)
65croppedImage w h pos src = newElement w h (Image (Cropped pos w h) src)
67container w h pos e = newElement w h (Container pos e)
68spacer w h = newElement w h Spacer
70flow dir es =
71 let ws = map widthOf es
72 hs = map heightOf es
73 newFlow w h = newElement w h (Flow dir es)
74 in
75 case dir of
76 DUp -> newFlow (List.maximum ws) (List.sum hs)
77 DDown -> newFlow (List.maximum ws) (List.sum hs)
78 DLeft -> newFlow (List.sum ws) (List.maximum hs)
79 DRight -> newFlow (List.sum ws) (List.maximum hs)
80 DIn -> newFlow (List.maximum ws) (List.maximum hs)
81 DOut -> newFlow (List.maximum ws) (List.maximum hs)
83above hi lo = newElement (max (widthOf hi) (widthOf lo)) (heightOf hi + heightOf lo) (Flow DDown [hi,lo])
84below lo hi = newElement (max (widthOf hi) (widthOf lo)) (heightOf hi + heightOf lo) (Flow DDown [hi,lo])
85beside lft rht = newElement (widthOf lft + widthOf rht) (max (heightOf lft) (heightOf rht)) (Flow right [lft,rht])
87text : Text -> Element