-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.elm
129 lines (91 loc) · 2.54 KB
/
Library.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
module Library (init, update, view) where
import Effects exposing (Effects, Never)
import Html exposing (..)
import Http
import Json.Decode as Json exposing ((:=))
import List as List
import Task
-- MODEL
type alias Library =
{ modules : List Module
, primitives : List Primitive
}
type alias Module =
{ name : String
, functions : List NamedFunction
}
type alias NamedFunction =
{ id : Int
, name : String
}
type alias Primitive =
{ id : Int
, name : String
}
init : (Library, Effects Action)
init =
( Library [ ] [ ]
, fetchLibrary
)
-- UPDATE
type Action =
LoadLibrary (Maybe Library)
update : Action -> Library -> (Library, Effects Action)
update action model =
case action of
LoadLibrary maybeLibrary ->
( Maybe.withDefault model maybeLibrary
, Effects.none
)
-- VIEW
view : Signal.Address Action -> Library -> Html
view address library =
div
[ ]
[ h2 [ ] [ text "Library" ]
, h3 [ ] [ text "Primitives" ]
, ul [ ] ( List.map ( primitiveView address ) library.primitives )
, h3 [ ] [ text "Functions"]
, ul [ ] ( List.map ( moduleView address ) library.modules )
]
primitiveView : Signal.Address Action -> Primitive -> Html
primitiveView address primitive =
li [ ] [ text primitive.name ]
moduleView : Signal.Address Action -> Module -> Html
moduleView address libraryModule =
li
[ ]
[ h4 [ ] [ text libraryModule.name ]
, ul [ ] ( List.map ( functionView address ) libraryModule.functions )
]
functionView : Signal.Address Action -> NamedFunction -> Html
functionView address namedFunction =
li [ ] [ text namedFunction.name ]
-- EFFECTS
fetchLibrary : Effects Action
fetchLibrary =
Http.get libraryDecoder "http://demo5895613.mockable.io/library/js/1"
|> Task.toMaybe
|> Task.map LoadLibrary
|> Effects.task
apply : Json.Decoder (a -> b) -> Json.Decoder a -> Json.Decoder b
apply func value =
Json.object2 (<|) func value
libraryDecoder : Json.Decoder Library
libraryDecoder =
Json.object2
Library
("modules" := (Json.list moduleDecoder))
("primitives" := (Json.list primitiveDecoder))
moduleDecoder : Json.Decoder Module
moduleDecoder =
Json.object2
Module
("name" := Json.string)
("functions" := (Json.list namedFunctionDecoder))
namedFunctionDecoder : Json.Decoder NamedFunction
namedFunctionDecoder =
Json.object2 NamedFunction ("id" := Json.int) ("name" := Json.string)
primitiveDecoder : Json.Decoder Primitive
primitiveDecoder =
Json.object2 Primitive ("id" := Json.int) ("name" := Json.string)