on 1fcbec6 help sign in
elk/libraries/Graphics/Element.elm
73 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 = case e of Element props prim -> Element {props| width <- w} prim
27height h e = case e of Element props prim -> Element {props| height <- h} prim
28opacity o e = case e of Element props prim -> Element {props| opacity <- o} prim
29color c e = case e of Element props prim -> Element {props| filling <- c} prim
30filling f e = case e of Element props prim -> Element {props| filling <- f} prim
31tag name e = case e of Element props prim ->
32 Element {props| tag <- JS.fromString name } prim
33link href e = case e of Element props prim ->
34 Element {props| href <- JS.fromString href } prim
36emptyStr = JS.fromString ""
37newElement w h e =
38 Element (Properties (guid ()) w h 1 NoFill emptyStr emptyStr) e
40data ElementPrim
41 = Image ImageStyle String
42 | Container Position Element
43 | Flow Direction [Element]
44 | Spacer
45 | RawHtml JSString
46 | DomNode JSElement
48data ImageStyle = Plain | Fitted | Cropped (Int,Int) Int Int
49data Direction = DUp | DDown | DLeft | DRight | DIn | DOut
51image w h src = newElement w h (Image Plain src)
52fittedImage w h src = newElement w h (Image Fitted src)
53croppedImage w h pos src = newElement w h (Image (Cropped pos w h) src)
55container w h pos e = newElement w h (Container pos e)
56spacer w h = newElement w h Spacer
58flow dir es =
59 let ws = map widthOf es
60 hs = map heightOf es
61 newFlow w h = newElement w h (Flow dir es)
62 in
63 case dir of
64 DUp -> newFlow (List.maximum ws) (List.sum hs)
65 DDown -> newFlow (List.maximum ws) (List.sum hs)
66 DLeft -> newFlow (List.sum ws) (List.maximum hs)
67 DRight -> newFlow (List.sum ws) (List.maximum hs)
68 DIn -> newFlow (List.maximum ws) (List.maximum hs)
69 DOut -> newFlow (List.maximum ws) (List.maximum hs)
71above hi lo = newElement (max (widthOf hi) (widthOf lo)) (heightOf hi + heightOf lo) (Flow DDown [hi,lo])
72below lo hi = newElement (max (widthOf hi) (widthOf lo)) (heightOf hi + heightOf lo) (Flow DDown [hi,lo])
73beside lft rht = newElement (widthOf lft + widthOf rht) (max (heightOf lft) (heightOf rht)) (Flow right [lft,rht])