-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiffy.elm
144 lines (115 loc) · 3.74 KB
/
Giffy.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
module Giffy (Model, Action, init, update, view, angularActions) where
import Effects exposing (Effects, Never)
import Html exposing (..)
import Html.Attributes exposing (style,attribute,class,type')
import Html.Events exposing (onClick)
import Http
import Json.Decode as JsonD exposing (Decoder,(:=))
import Json.Encode as JsonE
import Task
import Elmgular.Action as A exposing (Angular)
import Elmgular.Html as A
-- MODEL
type alias Slide =
{ image: String
, text: String
, active: Bool
}
type alias Model =
{ topic : String
, slides : List Slide
, single : Bool
}
init : String -> (Model, Effects Action)
init topic =
( Model topic [Slide "assets/waiting.gif" "" False] True
, getRandomGif topic
)
-- UPDATE
type Action
= RequestMore
| NewGif (Maybe String)
| SetSingle Bool
angularActions : Angular Action
angularActions = A.merge [ A.make "RequestMore" RequestMore
, A.make1 "NewGif" NewGif (A.maybe A.argumentString)
, A.make1 "SetSingle" SetSingle (A.argumentBool)
]
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
RequestMore ->
(model, getRandomGif model.topic)
NewGif maybeUrl ->
let newSlides = case (maybeUrl, model.single) of
(Nothing, _) -> model.slides
(Just url, True) -> [Slide url "" False]
(Just url, False) -> Slide url "" False :: model.slides
in ({model | slides <- newSlides}, Effects.none)
SetSingle b -> ({model | single <- b}, Effects.none)
-- VIEW
(=>) = (,)
view : Signal.Address Action -> Model -> Html
view address model =
div [ style [ "width" => "200px" ] ]
[ h2 [headerStyle] [text model.topic]
, div [imgStyle (List.head model.slides |> Maybe.withDefault (Slide "" "" False) |> .image)] []
, button [ onClick address RequestMore ] [ text "More Please!" ]
, A.embed
[ node "carousel" [ attribute "interval" "5000"
, attribute "no-wrap" "false"]
[ node "slide" [ attribute "ng-repeat" "slide in elmModel.slides"
, attribute "active" "slide.active"
]
[ img [ attribute "ng-src" "{{slide.image}}"
, style ["margin"=>"auto"]
] []
, div [class "carousel-caption"]
[ h4 [] [text "Slide {{$index}}"]
, p [] [text "{{slide.text}}"]
]
]
]
]
, A.embed
[ pre [] [ text "{{elmModel.single + ' - ' + elmModel.slides}}"]
, button [ type' "button"
, class "btn btn-primary"
, attribute "ng-model" "elmModel.single"
, attribute "btn-checkbox" ""
] [ text "Toggle" ]
, A.react "elmModel.single" "elmActions.SetSingle"
]
]
headerStyle : Attribute
headerStyle =
style
[ "width" => "200px"
, "text-align" => "center"
]
imgStyle : String -> Attribute
imgStyle url =
style
[ "display" => "inline-block"
, "width" => "200px"
, "height" => "200px"
, "background-position" => "center center"
, "background-size" => "cover"
, "background-image" => ("url('" ++ url ++ "')")
]
-- EFFECTS
getRandomGif : String -> Effects Action
getRandomGif topic =
Http.get decodeUrl (randomUrl topic)
|> Task.toMaybe
|> Task.map NewGif
|> Effects.task
randomUrl : String -> String
randomUrl topic =
Http.url "http://api.giphy.com/v1/gifs/random"
[ "api_key" => "dc6zaTOxFJmzC"
, "tag" => topic
]
decodeUrl : JsonD.Decoder String
decodeUrl =
JsonD.at ["data", "image_url"] JsonD.string