-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sheet.elm
269 lines (211 loc) · 6.32 KB
/
Sheet.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
module Task.Cells.View.Sheet exposing
( Handlers
, Model
, Msg
, UpdateOptions
, ViewOptions
, init
, update
, view
)
import Html as H
import Html.Attributes as HA
import Html.Events as HE
import Json.Decode as JD
import Support.Lib as Lib
import Task.Cells.Data.Cell as Cell
import Task.Cells.Data.Column as Column exposing (Column)
import Task.Cells.Data.Coord as Coord exposing (Coord)
import Task.Cells.Data.Row as Row exposing (Row)
import Task.Cells.Data.Sheet as Sheet exposing (Sheet)
-- MODEL
type Model
= Model State
type alias State =
{ maybeEdit : Maybe Edit
}
type alias Edit =
{ coord : Coord
, rawInput : String
}
type alias Handlers msg =
{ onChange : Msg -> msg
, onInput : Coord -> String -> msg
}
init : Model
init =
Model initState
initState : State
initState =
{ maybeEdit = Nothing
}
-- UPDATE
type alias UpdateOptions msg =
{ handlers : Handlers msg
, sheet : Sheet
}
type Msg
= DoubleClickedCell Coord
| FocusedInput
| BlurredInput
| Input String
| PressedEsc
| PressedEnter
update : UpdateOptions msg -> Msg -> Model -> ( Model, Cmd msg )
update { handlers, sheet } msg (Model state) =
case msg of
DoubleClickedCell coord ->
let
cell =
Sheet.get coord sheet
in
( Model
{ state
| maybeEdit =
Just
{ coord = coord
, rawInput = Cell.toEditableString cell
}
}
, Lib.focus (inputId coord) (handlers.onChange FocusedInput)
)
FocusedInput ->
( Model state
, Cmd.none
)
BlurredInput ->
( Model { state | maybeEdit = Nothing }
, Cmd.none
)
Input rawInput ->
( Model { state | maybeEdit = Maybe.map (\edit -> { edit | rawInput = rawInput }) state.maybeEdit }
, Cmd.none
)
PressedEsc ->
( Model { state | maybeEdit = Nothing }
, Cmd.none
)
PressedEnter ->
( Model { state | maybeEdit = Nothing }
, case state.maybeEdit of
Just { coord, rawInput } ->
Lib.dispatch <| handlers.onInput coord rawInput
Nothing ->
Cmd.none
)
-- VIEW
type alias ViewOptions msg =
{ handlers : Handlers msg
, sheet : Sheet
}
view : ViewOptions msg -> Model -> H.Html msg
view options (Model state) =
H.table [ HA.class "sheet" ]
[ viewColumnHeaders
, viewRows options state
]
viewColumnHeaders : H.Html msg
viewColumnHeaders =
let
noTitle =
H.th
[ HA.class "sheet__th sheet__row-headers-column-header" ]
[]
columns =
Column.map viewColumnHeader
in
H.thead
[ HA.class "sheet__thead" ]
[ H.tr [ HA.class "sheet__tr" ] (noTitle :: columns) ]
viewColumnHeader : Column -> H.Html msg
viewColumnHeader column =
H.th
[ HA.class "sheet__th sheet__column-header" ]
[ H.text <| Column.toString column ]
viewRows : ViewOptions msg -> State -> H.Html msg
viewRows options state =
H.tbody [ HA.class "sheet__tbody" ] <| Row.map (viewRow options state)
viewRow : ViewOptions msg -> State -> Row -> H.Html msg
viewRow options state row =
let
header =
H.th
[ HA.class "sheet__th sheet__row-header" ]
[ H.text <| Row.toString row ]
viewCellWrapper column =
viewCell options state (Coord column row)
in
H.tr [ HA.class "sheet__tr" ] (header :: Column.map viewCellWrapper)
viewCell : ViewOptions msg -> State -> Coord -> H.Html msg
viewCell { handlers, sheet } { maybeEdit } coord =
let
maybeBeingEdited =
maybeEdit
|> Maybe.andThen
(\edit ->
if edit.coord == coord then
Just edit
else
Nothing
)
in
H.map handlers.onChange <|
case maybeBeingEdited of
Just edit ->
H.td
[ HA.class "sheet__td sheet__cell" ]
[ H.input
[ HA.id <| inputId coord
, HA.class "sheet__cell-input"
, HA.type_ "text"
, HA.spellcheck False
, HA.value edit.rawInput
, HE.onInput Input
, HE.onBlur BlurredInput
, onKey
{ esc = PressedEsc
, enter = PressedEnter
}
]
[]
]
Nothing ->
H.td
[ HA.class "sheet__td sheet__cell"
, HE.onDoubleClick <| DoubleClickedCell coord
]
[ let
cell =
Sheet.get coord sheet
in
H.div
[ HA.class "sheet__cell-value"
, HA.classList
[ ( "sheet__cell-value--has-error"
, Cell.hasError cell
)
]
]
[ H.text <| Cell.toString cell
]
]
inputId : Coord -> String
inputId coord =
"sheet__input-" ++ Coord.toString coord
onKey : { esc : msg, enter : msg } -> H.Attribute msg
onKey { esc, enter } =
let
decoder =
HE.keyCode
|> JD.andThen
(\code ->
case code of
13 ->
JD.succeed enter
27 ->
JD.succeed esc
_ ->
JD.fail "ignored"
)
in
HE.on "keydown" decoder