forked from rtfeldman/elm-in-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoGroove.elm
308 lines (235 loc) · 7.38 KB
/
PhotoGroove.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
port module PhotoGroove exposing (..)
import Html exposing (..)
import Html.Events exposing (onClick, on)
import Array exposing (Array)
import Random
import Http
import Html.Attributes as Attr exposing (id, class, classList, src, name, type_, title)
import Json.Decode exposing (string, int, list, Decoder, at)
import Json.Decode.Pipeline exposing (decode, required, optional)
photoDecoder : Decoder Photo
photoDecoder =
decode buildPhoto
|> required "url" string
|> required "size" int
|> optional "title" string "(untitled)"
buildPhoto : String -> Int -> String -> Photo
buildPhoto url size title =
{ url = url, size = size, title = title }
urlPrefix : String
urlPrefix =
"http://elm-in-action.com/"
type ThumbnailSize
= Small
| Medium
| Large
view : Model -> Html Msg
view model =
div [ class "content" ]
[ h1 [] [ text "Photo Groove" ]
, button
[ onClick SurpriseMe ]
[ text "Surprise Me!" ]
, div [ class "status" ] [ text model.status ]
, div [ class "filters" ]
[ viewFilter "Hue" SetHue model.hue
, viewFilter "Ripple" SetRipple model.ripple
, viewFilter "Noise" SetNoise model.noise
]
, h3 [] [ text "Thumbnail Size:" ]
, div [ id "choose-size" ]
(List.map viewSizeChooser [ Small, Medium, Large ])
, div [ id "thumbnails", class (sizeToString model.chosenSize) ]
(List.map (viewThumbnail model.selectedUrl) model.photos)
, viewLarge model.selectedUrl
]
viewFilter : String -> (Int -> Msg) -> Int -> Html Msg
viewFilter name toMsg magnitude =
div [ class "filter-slider" ]
[ label [] [ text name ]
, paperSlider [ Attr.max "11", onImmediateValueChange toMsg ] []
, label [] [ text (toString magnitude) ]
]
viewLarge : Maybe String -> Html Msg
viewLarge maybeUrl =
case maybeUrl of
Nothing ->
text ""
Just url ->
canvas [ id "main-canvas", class "large" ] []
viewThumbnail : Maybe String -> Photo -> Html Msg
viewThumbnail selectedUrl thumbnail =
img
[ src (urlPrefix ++ thumbnail.url)
, title (thumbnail.title ++ " [" ++ toString thumbnail.size ++ " KB]")
, classList [ ( "selected", selectedUrl == Just thumbnail.url ) ]
, onClick (SelectByUrl thumbnail.url)
]
[]
viewSizeChooser : ThumbnailSize -> Html Msg
viewSizeChooser size =
label []
[ input [ type_ "radio", name "size", onClick (SetSize size) ] []
, text (sizeToString size)
]
sizeToString : ThumbnailSize -> String
sizeToString size =
case size of
Small ->
"small"
Medium ->
"med"
Large ->
"large"
port setFilters : FilterOptions -> Cmd msg
port statusChanges : (String -> msg) -> Sub msg
type alias FilterOptions =
{ url : String
, filters : List { name : String, amount : Float }
}
type alias Photo =
{ url : String
, size : Int
, title : String
}
type alias Model =
{ photos : List Photo
, status : String
, selectedUrl : Maybe String
, loadingError : Maybe String
, chosenSize : ThumbnailSize
, hue : Int
, ripple : Int
, noise : Int
}
initialModel : Model
initialModel =
{ photos = []
, status = ""
, selectedUrl = Nothing
, loadingError = Nothing
, chosenSize = Medium
, hue = 0
, ripple = 0
, noise = 0
}
photoArray : Array Photo
photoArray =
Array.fromList initialModel.photos
getPhotoUrl : Int -> Maybe String
getPhotoUrl index =
case Array.get index photoArray of
Just photo ->
Just photo.url
Nothing ->
Nothing
type Msg
= SelectByUrl String
| SelectByIndex Int
| SetStatus String
| SurpriseMe
| SetSize ThumbnailSize
| LoadPhotos (Result Http.Error (List Photo))
| SetHue Int
| SetRipple Int
| SetNoise Int
randomPhotoPicker : Random.Generator Int
randomPhotoPicker =
Random.int 0 (Array.length photoArray - 1)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SetStatus status ->
( { model | status = status }, Cmd.none )
SetHue hue ->
applyFilters { model | hue = hue }
SetRipple ripple ->
applyFilters { model | ripple = ripple }
SetNoise noise ->
applyFilters { model | noise = noise }
SelectByIndex index ->
let
newSelectedUrl : Maybe String
newSelectedUrl =
model.photos
|> Array.fromList
|> Array.get index
|> Maybe.map .url
in
applyFilters { model | selectedUrl = newSelectedUrl }
SelectByUrl selectedUrl ->
applyFilters { model | selectedUrl = Just selectedUrl }
SurpriseMe ->
let
randomPhotoPicker =
Random.int 0 (List.length model.photos - 1)
in
( model, Random.generate SelectByIndex randomPhotoPicker )
SetSize size ->
( { model | chosenSize = size }, Cmd.none )
LoadPhotos (Ok photos) ->
applyFilters
{ model
| photos = photos
, selectedUrl = Maybe.map .url (List.head photos)
}
LoadPhotos (Err _) ->
( { model
| loadingError = Just "Error! (Try turning it off and on again?)"
}
, Cmd.none
)
applyFilters : Model -> ( Model, Cmd Msg )
applyFilters model =
case model.selectedUrl of
Just selectedUrl ->
let
filters =
[ { name = "Hue", amount = toFloat model.hue / 11 }
, { name = "Ripple", amount = toFloat model.ripple / 11 }
, { name = "Noise", amount = toFloat model.noise / 11 }
]
url =
urlPrefix ++ "large/" ++ selectedUrl
in
( model, setFilters { url = url, filters = filters } )
Nothing ->
( model, Cmd.none )
initialCmd : Cmd Msg
initialCmd =
list photoDecoder
|> Http.get "http://elm-in-action.com/photos/list.json"
|> Http.send LoadPhotos
viewOrError : Model -> Html Msg
viewOrError model =
case model.loadingError of
Nothing ->
view model
Just errorMessage ->
div [ class "error-message" ]
[ h1 [] [ text "Photo Groove" ]
, p [] [ text errorMessage ]
]
main : Program Float Model Msg
main =
Html.programWithFlags
{ init = init
, view = viewOrError
, update = update
, subscriptions = \_ -> statusChanges SetStatus
}
init : Float -> ( Model, Cmd Msg )
init flags =
let
status =
"Initializing Pasta v" ++ toString flags
in
( { initialModel | status = status }, initialCmd )
paperSlider : List (Attribute msg) -> List (Html msg) -> Html msg
paperSlider =
node "paper-slider"
onImmediateValueChange : (Int -> msg) -> Attribute msg
onImmediateValueChange toMsg =
at [ "target", "immediateValue" ] int
|> Json.Decode.map toMsg
|> on "immediate-value-changed"