/
ElmChatExample.elm
308 lines (256 loc) · 7.82 KB
/
ElmChatExample.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
----------------------------------------------------------------------
--
-- ElmChatExample.elm
-- Example of using the ElmChat module.
-- Copyright (c) 2017 Bill St. Clair <billstclair@gmail.com>
-- Some rights reserved.
-- Distributed under the MIT License
-- See LICENSE.txt
--
----------------------------------------------------------------------
module ElmChatExample exposing (Model, Msg(..), Settings, b, center, chatSend, chatSendInternal, copyright, defaultAttributes, delayedAction, init, main, nbsp, stringFromCode, update, view)
import Browser
import Char
import Debug exposing (log)
import ElmChat exposing (LineSpec(..), defaultExtraAttributes)
import Html
exposing
( Attribute
, Html
, a
, br
, button
, div
, h2
, input
, p
, table
, td
, text
, th
, tr
)
import Html.Attributes exposing (disabled, href, size, style, type_, value)
import Html.Events exposing (onClick, onInput)
import Task
import Time exposing (Posix, Zone)
main =
Browser.element
{ init = \() -> init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
type alias Settings =
ElmChat.Settings Msg
type alias Model =
{ settings : Settings
-- Your code won't need a duplicate settings property.
-- It's here only because this example has two input boxes.
, settings2 : ElmChat.Settings Msg
, time : Posix
, json : String
, error : Maybe String
}
type Msg
= ChatUpdate (ElmChat.Settings Msg) (Cmd Msg)
| ChatSend1 String String (ElmChat.Settings Msg)
-- Your code won't need these additional messages.
-- They're here only because this examples has two input boxes.
| ChatUpdate2 (ElmChat.Settings Msg) (Cmd Msg)
| ChatSend2 String String (ElmChat.Settings Msg)
| UpdateJson String
| Restore
| DelayedAction (Model -> ( Model, Cmd Msg )) Posix
| SetZone Zone
defaultAttributes =
{ defaultExtraAttributes
| textArea =
style "border-color" "blue"
:: defaultExtraAttributes.textArea
}
init : ( Model, Cmd Msg )
init =
let
settings =
ElmChat.makeSettings "id1" 14 True ChatUpdate
settings2 =
{ settings
| attributes = defaultAttributes
}
in
( { settings = settings2
, settings2 =
{ settings2
| id = "id2"
, updater = ElmChat.TheUpdater ChatUpdate2
}
, time = Time.millisToPosix 0
, json = ""
, error = Nothing
}
, Task.perform SetZone Time.here
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ChatUpdate settings cmd ->
( { model | settings = log "update" settings }
, cmd
)
ChatSend1 name line settings ->
chatSend name line settings model.settings2 model
ChatUpdate2 settings cmd ->
( { model | settings2 = settings }
, cmd
)
ChatSend2 name line settings2 ->
chatSend name line model.settings settings2 model
UpdateJson json ->
( { model | json = json }
, Cmd.none
)
Restore ->
case ElmChat.decodeSettings ChatUpdate model.json of
Err m ->
( { model | error = Just m }
, Cmd.none
)
Ok settings ->
let
settings2 =
{ settings
| zone = model.settings.zone
}
in
( { model
| settings = settings2
, error = Nothing
}
, ElmChat.restoreScroll settings2
)
DelayedAction updater time ->
updater { model | time = time }
SetZone zone ->
let
settings =
model.settings
in
( { model
| settings =
{ settings | zone = zone }
}
, Cmd.none
)
chatSend : String -> String -> Settings -> Settings -> Model -> ( Model, Cmd Msg )
chatSend name line settings1 settings2 model =
( model
, delayedAction <|
chatSendInternal name line settings1 settings2
)
chatSendInternal : String -> String -> Settings -> Settings -> Model -> ( Model, Cmd Msg )
chatSendInternal name line settings1 settings2 model =
let
( settings, cmd ) =
ElmChat.addLineSpec settings1 <|
ElmChat.makeLineSpec line (Just name) (Just model.time)
in
( { model
| settings = settings1
, settings2 = settings2
}
, cmd
)
delayedAction : (Model -> ( Model, Cmd Msg )) -> Cmd Msg
delayedAction updater =
Task.perform (DelayedAction updater) Time.now
b : String -> Html Msg
b string =
Html.b [] [ text string ]
center : List (Attribute msg) -> List (Html msg) -> Html msg
center attributes body =
Html.node "center" attributes body
view : Model -> Html Msg
view model =
center []
[ h2 [] [ text "ElmChat Example" ]
, p [] [ ElmChat.chat model.settings ]
, p []
[ table []
[ tr []
[ td [] [ b "Bill: " ]
, td []
[ ElmChat.inputBox
40
"Send"
(ChatSend1 "Bill")
model.settings
]
]
, tr []
[ td [] [ b "Mary: " ]
, td []
[ ElmChat.inputBox
40
"Send"
(ChatSend2 "Mary")
model.settings2
]
]
, tr []
[ td [] [ b "JSON: " ]
, td []
[ input
[ type_ "text"
, disabled True
, size 40
, value <| ElmChat.encodeSettings model.settings
]
[]
]
]
, tr []
[ td [] []
, td []
[ input
[ type_ "text"
, onInput UpdateJson
, size 40
, value model.json
]
[]
, text " "
, button [ onClick Restore ]
[ text "Restore" ]
]
]
]
]
, p [ style "color" "red" ]
[ case model.error of
Nothing ->
text nbsp
Just msg ->
text msg
]
, p []
[ text <| "Copyright " ++ copyright ++ " 2017-2018 Bill St. Clair"
, br [] []
, a [ href "https://gibgoygames.com/" ]
[ text "Gib Goy Games" ]
, text " "
, a [ href "https://github.com/billstclair/elm-chat" ]
[ text "GitHub" ]
]
]
stringFromCode : Int -> String
stringFromCode code =
String.fromList [ Char.fromCode code ]
nbsp : String
nbsp =
stringFromCode 160
-- \u00A0
copyright : String
copyright =
stringFromCode 169
-- \u00A9