/
Main.elm
251 lines (214 loc) · 6.77 KB
/
Main.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
module Main exposing (..)
import Browser exposing (UrlRequest(..))
import Browser.Navigation as Navigation
import Css
import Css.Global as Global
import FamilyTree
import Html.Styled as Html exposing (Html)
import Html.Styled.Attributes as Attrs exposing (css)
import Url exposing (Url)
import Url.Parser as Parser exposing ((</>), Parser)
type alias Model =
{ key : Navigation.Key
, route : Route
, familyTree : FamilyTree.Model
}
type Route
= Index
| FamilyTree FamilyTree.Route
| NotFound
parseRoute : Url -> Route
parseRoute url =
url
|> Parser.parse
(Parser.oneOf
[ Parser.map Index Parser.top
, Parser.map FamilyTree (Parser.top </> Parser.s "family-tree" </> FamilyTree.routeParser)
]
)
|> Maybe.withDefault NotFound
pathFor : Route -> String
pathFor route =
case route of
Index ->
"/"
FamilyTree familyTreeRoute ->
"/family-tree" ++ FamilyTree.pathFor familyTreeRoute
NotFound ->
"/404"
type Msg
= UrlChanged Url
| UrlRequested UrlRequest
| FamilyTreeMsg FamilyTree.Msg
type alias Flags =
()
init : Flags -> Url -> Navigation.Key -> ( Model, Cmd Msg )
init _ url key =
let
( familyTree, familyTreeCmd ) =
FamilyTree.init
in
( { key = key
, route = parseRoute url
, familyTree = familyTree
}
, Cmd.map FamilyTreeMsg familyTreeCmd
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UrlChanged url ->
( { model | route = parseRoute url }
, Cmd.none
)
UrlRequested (Internal url) ->
( model
, Navigation.pushUrl model.key (Url.toString url)
)
UrlRequested (External url) ->
( model
, Navigation.load url
)
FamilyTreeMsg familyTreeMsg ->
let
( newFamilyTree, familyTreeEffect ) =
FamilyTree.update familyTreeMsg model.familyTree
in
( { model | familyTree = newFamilyTree }
, case familyTreeEffect of
Just (FamilyTree.Navigate route) ->
Navigation.pushUrl model.key (pathFor (FamilyTree route))
Nothing ->
Cmd.none
)
view : Model -> Browser.Document Msg
view model =
{ title =
case model.route of
Index ->
"Datalog Sample Apps"
FamilyTree _ ->
"Family Tree | Datalog Sample Apps"
NotFound ->
"Not Found | Datalog Sample Apps"
, body =
Html.div
[ css
[ Css.maxWidth (Css.px 1024)
, Css.minHeight (Css.vh 100)
, Css.margin2 Css.zero Css.auto
, Css.padding (Css.px 25)
, Css.boxShadow4 (Css.px 0) (Css.px 0) (Css.px 80) (Css.hex "B2EBF2")
, Css.backgroundColor (Css.hex "FFF")
]
]
[ Global.global
[ Global.body
[ Css.margin Css.zero
, Css.fontFamily Css.sansSerif
, Css.backgroundColor (Css.hex "E0F7FA")
, Css.color (Css.hex "37474F")
]
, Global.everything [ Css.boxSizing Css.borderBox ]
]
, Html.header
[ css
[ Css.displayFlex
, Css.marginBottom (Css.px 25)
]
]
[ Html.p
[ css
[ Css.marginRight (Css.px 10)
, Css.fontWeight Css.bold
]
]
[ Html.text "Datalog Sample Apps" ]
, Html.nav []
[ Html.ol
[ css
[ Css.listStyleType Css.none
, Css.margin Css.zero
, Css.padding Css.zero
, Css.displayFlex
]
]
[ viewHeaderLink Index "Index"
, viewHeaderLink (FamilyTree FamilyTree.Index) "Family Tree"
]
]
]
, Html.main_ []
[ case model.route of
Index ->
viewIndex
FamilyTree route ->
FamilyTree.view route model.familyTree
|> Html.map FamilyTreeMsg
NotFound ->
viewNotFound
]
, Html.footer
[ css [ Css.marginTop (Css.px 100) ] ]
[ Html.hr [] []
, Html.details []
[ Html.summary [] [ Html.text "Debug view of the whole model" ]
, Html.text (Debug.toString model)
]
]
]
|> Html.toUnstyled
|> List.singleton
}
viewHeaderLink : Route -> String -> Html msg
viewHeaderLink route caption =
Html.li
[ css
[ Css.paddingRight (Css.px 10)
]
]
[ Html.p []
[ Html.a
[ Attrs.href (pathFor route) ]
[ Html.text caption ]
]
]
viewIndex : Html Msg
viewIndex =
Html.div []
[ Html.h1 [] [ Html.text "Datalog Sample Apps" ]
, Html.p []
[ Html.text "These are sample apps for "
, Html.a
[ Attrs.href "https://git.bytes.zone/brian/bad-datalog" ]
[ Html.text "this yet-to-be-named datalog implementation in Elm" ]
, Html.text ". The source for these lives in the "
, Html.a
[ Attrs.href "https://git.bytes.zone/brian/bad-datalog/src/branch/main/sample-apps" ]
[ Html.code [] [ Html.text "sample-apps" ]
, Html.text " directory in the repo"
]
, Html.text "."
]
, Html.h2 [] [ Html.text "Apps" ]
, Html.ul []
[ Html.li []
[ Html.a
[ Attrs.href (pathFor (FamilyTree FamilyTree.Index)) ]
[ Html.text "Family Tree" ]
]
]
]
viewNotFound : Html Msg
viewNotFound =
Html.text "Not Found!"
main : Program Flags Model Msg
main =
Browser.application
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
, onUrlChange = UrlChanged
, onUrlRequest = UrlRequested
}