1module Graphics.Element where
2{-| Graphical elements that snap together to build complex widgets and layouts.
3Each Element is a rectangle with a known width and height, making them easy to
7@docs image, fittedImage, croppedImage, tiledImage
10@docs width, height, size, color, opacity, link, tag
13@docs widthOf, heightOf, sizeOf
16@docs flow, up, down, left, right, inward, outward
19There are also some convenience functions for working
20with `flow` in specific cases:
22@docs layers, above, below, beside
25@docs spacer, container
29To create a `Position` you can use any of the built-in positions
30which cover nine common positions.
31@docs middle, midTop, midBottom, midLeft, midRight, topLeft, topRight,
32 bottomLeft, bottomRight
34If you need more precision, you can create custom positions.
36@docs absolute, relative, middleAt, midTopAt, midBottomAt, midLeftAt,
37 midRightAt, topLeftAt, topRightAt, bottomLeftAt, bottomRightAt
42import JavaScript as JS
43import JavaScript (JSString)
46import Maybe (Maybe, Just, Nothing)
59type Element = { props : Properties, element : ElementPrim }
61{-| Get the width of an Element -}
62widthOf : Element -> Int
63widthOf e = e.props.width
65{-| Get the height of an Element -}
66heightOf : Element -> Int
67heightOf e = e.props.height
69{-| Get the width and height of an Element -}
70sizeOf : Element -> (Int,Int)
71sizeOf e = (e.props.width, e.props.height)
73{-| Create an `Element` with a given width. -}
74width : Int -> Element -> Element
77 props = case e.element of
78 Image _ w h _ -> {p| height <- round (toFloat h / toFloat w * toFloat nw) }
79 RawHtml -> {p| height <- snd (Native.Utils.htmlHeight nw e.element) }
81 in { element=e.element, props={ props | width <- nw } }
83{-| Create an `Element` with a given height. -}
84height : Int -> Element -> Element
87 props = case e.element of
88 Image _ w h _ -> {p| width <- round (toFloat w / toFloat h * toFloat nh) }
90 in { element=e.element, props={ p | height <- nh} }
92{-| Create an `Element` with a new width and height. -}
93size : Int -> Int -> Element -> Element
94size w h e = height h (width w e)
96{-| Create an `Element` with a given opacity. Opacity is a number between 0 and 1
97where 0 means totally clear.
99opacity : Float -> Element -> Element
100opacity o e = let p = e.props in { element=e.element, props={p| opacity <- o} }
102{-| Create an `Element` with a given background color. -}
103color : Color -> Element -> Element
104color c e = let p = e.props in
105 { element = e.element
106 , props = { p | color <- Just c}
109{-| Create an `Element` with a tag. This lets you link directly to it.
110The element `(tag "all-about-badgers" thirdParagraph)` can be reached
111with a link lik this: `/facts-about-animals.elm#all-about-badgers`
113tag : String -> Element -> Element
114tag name e = let p = e.props in
115 { element=e.element, props={p | tag <- JS.fromString name} }
117{-| Create an `Element` that is a hyper-link. -}
118link : String -> Element -> Element
119link href e = let p = e.props in
120 { element=e.element, props={p | href <- JS.fromString href} }
122emptyStr = JS.fromString ""
124 { props = Properties (Native.Utils.guid ()) w h 1 Nothing emptyStr emptyStr (), element = e }
127 = Image ImageStyle Int Int JSString
128 | Container Position Element
129 | Flow Direction [Element]
132 | Custom -- for custom Elements implemented in JS, see collage for example
134data ImageStyle = Plain | Fitted | Cropped (Int,Int) | Tiled
136{-| Create an image given a width, height, and image source. -}
137image : Int -> Int -> String -> Element
138image w h src = newElement w h (Image Plain w h (JS.fromString src))
140{-| Create a fitted image given a width, height, and image source.
141This will crop the picture to best fill the given dimensions.
143fittedImage : Int -> Int -> String -> Element
144fittedImage w h src = newElement w h (Image Fitted w h (JS.fromString src))
146{-| Create a cropped image. Take a rectangle out of the picture starting
147at the given top left coordinate. If you have a 140-by-140 image,
148the following will cut a 100-by-100 square out of the middle of it.
150 croppedImage (20,20) 100 100 "yogi.jpg"
152croppedImage : (Int,Int) -> Int -> Int -> String -> Element
153croppedImage pos w h src =
154 newElement w h (Image (Cropped pos) w h (JS.fromString src))
156tiledImage : Int -> Int -> String -> Element
158 newElement w h (Image Tiled w h (JS.fromString src))
160data Three = P | Z | N
161data Pos = Absolute Int | Relative Float
162type Position = { horizontal : Three, vertical : Three, x : Pos, y : Pos }
164{-| Put an element in a container. This lets you position the element really
165easily, and there are tons of ways to set the `Position`.
166To center `element` exactly in a 300-by-300 square you would say:
168 container 300 300 middle element
170By setting the color of the container, you can create borders.
172container : Int -> Int -> Position -> Element -> Element
173container w h pos e = newElement w h (Container pos e)
175{-| Create an empty box. This is useful for getting your spacing right and
178spacer : Int -> Int -> Element
179spacer w h = newElement w h Spacer
181data Direction = DUp | DDown | DLeft | DRight | DIn | DOut
183{-| Have a list of elements flow in a particular direction.
184The `Direction` starts from the first element in the list.
192flow : Direction -> [Element] -> Element
194 let ws = List.map widthOf es
195 hs = List.map heightOf es
196 newFlow w h = newElement w h (Flow dir es)
198 if es == [] then spacer 0 0 else
200 DUp -> newFlow (List.maximum ws) (List.sum hs)
201 DDown -> newFlow (List.maximum ws) (List.sum hs)
202 DLeft -> newFlow (List.sum ws) (List.maximum hs)
203 DRight -> newFlow (List.sum ws) (List.maximum hs)
204 DIn -> newFlow (List.maximum ws) (List.maximum hs)
205 DOut -> newFlow (List.maximum ws) (List.maximum hs)
207{-| Stack elements vertically.
208To put `a` above `b` you would say: ``a `above` b``
210above : Element -> Element -> Element
212 newElement (max (widthOf hi) (widthOf lo))
213 (heightOf hi + heightOf lo)
216{-| Stack elements vertically.
217To put `a` below `b` you would say: ``a `below` b``
219below : Element -> Element -> Element
221 newElement (max (widthOf hi) (widthOf lo))
222 (heightOf hi + heightOf lo)
225{-| Put elements beside each other horizontally.
226To put `a` beside `b` you would say: ``a `beside` b``
228beside : Element -> Element -> Element
230 newElement (widthOf lft + widthOf rht)
231 (max (heightOf lft) (heightOf rht))
232 (Flow right [lft,rht])
234{-| Layer elements on top of each other, starting from the bottom:
235`layers == flow outward`
237layers : [Element] -> Element
239 let ws = List.map widthOf es
240 hs = List.map heightOf es
241 in newElement (List.maximum ws) (List.maximum hs) (Flow DOut es)
244-- Repetitive things --
248relative : Float -> Pos
252middle = { horizontal=Z, vertical=Z, x=Relative 0.5, y=Relative 0.5 }
254topLeft = { horizontal=N, vertical=P, x=Absolute 0, y=Absolute 0 }
256topRight = { topLeft | horizontal <- P }
258bottomLeft = { topLeft | vertical <- N }
259bottomRight : Position
260bottomRight = { bottomLeft | horizontal <- P }
262midLeft = { middle | horizontal <- N, x <- Absolute 0 }
264midRight = { midLeft | horizontal <- P }
266midTop = { middle | vertical <- P, y <- Absolute 0 }
268midBottom = { midTop | vertical <- N }
270middleAt : Pos -> Pos -> Position
271middleAt x y = { horizontal = Z, vertical = Z, x = x, y = y }
272topLeftAt : Pos -> Pos -> Position
273topLeftAt x y = { horizontal = N, vertical = P, x = x, y = y }
274topRightAt : Pos -> Pos -> Position
275topRightAt x y = { horizontal = P, vertical = P, x = x, y = y }
276bottomLeftAt : Pos -> Pos -> Position
277bottomLeftAt x y = { horizontal = N, vertical = N, x = x, y = y }
278bottomRightAt : Pos -> Pos -> Position
279bottomRightAt x y = { horizontal = P, vertical = N, x = x, y = y }
280midLeftAt : Pos -> Pos -> Position
281midLeftAt x y = { horizontal = N, vertical = Z, x = x, y = y }
282midRightAt : Pos -> Pos -> Position
283midRightAt x y = { horizontal = P, vertical = Z, x = x, y = y }
284midTopAt : Pos -> Pos -> Position
285midTopAt x y = { horizontal = Z, vertical = P, x = x, y = y }
286midBottomAt : Pos -> Pos -> Position
287midBottomAt x y = { horizontal = Z, vertical = N, x = x, y = y }
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