-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.elm
272 lines (214 loc) · 5.75 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
--https://github.com/evancz/elm-todomvc/tree/master
--http://package.elm-lang.org/packages/elm-lang/core/3.0.0
--https://github.com/deadfoxygrandpa/Elm.tmLanguage/issues/92
--Ports/effects/tasks
--https://gist.github.com/kami-/fd2427418628ada52ab1
--http://elm-lang.org/guide/interop
--http://elm-lang.org/guide/reactivity
module Main (..) where
import Html exposing (Html, Attribute, text, toElement, div, input, button)
import Html.Events exposing (on, targetValue)
import Html.Attributes
import String
import Signal
import List
import List.Extra
import Signal.Time
import Http
type alias Params =
List Param
type alias Param =
( String, String )
type alias Model =
{ queryParams : Params
, focus : Maybe ( Int, String )
}
type Action
= UpdateParam { index : Int, isKey : Bool, param : Param }
| Add
| None
encodeParam : Param -> Param
encodeParam ( key, value ) =
( Http.uriEncode key, Http.uriEncode value )
decodeParam : Param -> Param
decodeParam ( key, value ) =
( Http.uriDecode key, Http.uriDecode value )
port inputQueryParamsStr : String
initialModel : Model
initialModel =
{ queryParams =
inputQueryParamsStr
|>
String.split "&"
|>
List.map (String.split "=")
|>
List.map
(\ls ->
( List.head ls |> Maybe.withDefault ""
, List.Extra.getAt ls 1 |> Maybe.withDefault ""
)
)
|>
List.map decodeParam
--Filter qualified
|>
List.filter isKeyNotEmpty
, focus = Nothing
}
updateAtIndex : List a -> Int -> a -> List a
updateAtIndex ls indexToReplace newValue =
ls
|> List.indexedMap (,)
|> List.Extra.replaceIf
(\( index, tuple ) -> index == indexToReplace)
( indexToReplace
, newValue
)
|> List.map (\( _, tuple ) -> tuple)
actions : Signal.Mailbox Action
actions =
Signal.mailbox None
model : Signal Model
model =
Signal.foldp update initialModel actions.signal
emptyParam : Param
emptyParam =
( "", "" )
isKeyNotEmpty : Param -> Bool
isKeyNotEmpty =
\( key, _ ) -> key /= ""
isValueNotEmpty : Param -> Bool
isValueNotEmpty =
\( _, value ) -> value /= ""
isParamNotEmpty param =
isKeyNotEmpty param || isValueNotEmpty param
removeIndex : List a -> Int -> List a
removeIndex ls index =
ls
|> List.indexedMap (,)
|> List.Extra.removeWhen (\( i, _ ) -> i == index)
|> List.map (\( _, x ) -> x)
update : Action -> Model -> Model
update action model =
case action of
UpdateParam { index, isKey, param } ->
let
isNotEmpty =
isParamNotEmpty param
newQueryParams =
if isNotEmpty then
updateAtIndex model.queryParams index param
else
removeIndex model.queryParams index
newFocus =
if isNotEmpty then
Just
( index
, if isKey then
"key"
else
"value"
)
else
Nothing
in
{ model
| queryParams =
newQueryParams
, focus = newFocus
}
Add ->
let
newQueryParams =
List.append model.queryParams [ emptyParam ]
in
{ model
| queryParams = newQueryParams
, focus = Just ( (List.length newQueryParams) - 1, "key" )
}
None ->
model
createRow : Signal.Address Action -> ( Int, Param ) -> Html
createRow address ( index, ( key, value ) ) =
Html.tr
[]
[ Html.td
[ columnStyles ]
[ input
[ Html.Attributes.class "key"
, Html.Attributes.value key
, on "input" targetValue (\newKey -> Signal.message address (UpdateParam { index = index, isKey = True, param = ( newKey, value ) }))
, inputStyles
]
[]
]
, Html.td
[]
[ input
[ Html.Attributes.class "value"
, Html.Attributes.value value
, on "input" targetValue (\newValue -> Signal.message address (UpdateParam { index = index, isKey = False, param = ( key, newValue ) }))
, inputStyles
]
[]
]
]
view : Signal.Address Action -> Model -> Html
view address model =
div
[]
[ div [] [ button [ Html.Events.onClick address Add ] [ text "Add" ] ]
, Html.hr [] []
, Html.table
[ tableStyles ]
[ Html.thead [] [ Html.tr [] [ Html.th [ columnStyles ] [ text "key" ], Html.th [] [ text "value" ] ] ]
, Html.tbody
[]
(model.queryParams
|> List.indexedMap (,)
|> List.map (createRow address)
)
]
]
port outputQueryParamsStr : Signal String
port outputQueryParamsStr =
model
|> Signal.map
(\{ queryParams } ->
(queryParams
--Filter qualified
|>
List.filter isKeyNotEmpty
|>
List.map encodeParam
|>
List.map (\( key, value ) -> String.concat [ key, "=", value ])
|>
String.join "&"
)
)
|> Signal.dropRepeats
|> Signal.Time.settledAfter 300
--https://github.com/evancz/elm-architecture-tutorial/issues/49
port focus : Signal (Maybe ( Int, String ))
port focus =
model
|> Signal.map (\{ focus } -> focus)
main : Signal Html
main =
Signal.map (view actions.address) model
tableStyles : Attribute
tableStyles =
Html.Attributes.style
[ ( "width", "250px" ) ]
columnStyles : Attribute
columnStyles =
Html.Attributes.style
[ ( "width", "50%" ) ]
inputStyles : Attribute
inputStyles =
Html.Attributes.style
[ ( "width", "100%" )
, ( "box-sizing", "border-box" )
]