-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortableList.elm
265 lines (203 loc) · 6.28 KB
/
SortableList.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
module SortableList exposing (..)
import Array exposing (Array)
import Draggable
import Draggable.Events exposing (onDragBy, onMouseDown)
import Array.Extra as ArrayX
import Html exposing (Html)
import Html.Attributes
import Css exposing (..)
import Html.Events exposing (onMouseUp)
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
type alias Model =
{ items : Items
, draggingItemIndex : Maybe Index
, draggedDistance : Distance
, drag : Draggable.State
}
type alias Items =
Array Item
type alias Item =
{ text : String
}
type Distance
= Distance HorizontalDistance VerticalDistance
type alias Delta =
( HorizontalDistance, VerticalDistance )
type alias Index =
Int
type alias VerticalDistance =
Float
type alias HorizontalDistance =
Float
type Msg
= DragBy Delta
| StartDragging String
| StopDragging
| DragMsg Draggable.Msg
init : ( Model, Cmd Msg )
init =
( { items = Array.fromList [ item "#1 first item", item "#2 second item", item "#3 third item" ]
, draggingItemIndex = Nothing
, draggedDistance = noDistance
, drag = Draggable.init
}
, Cmd.none
)
item : String -> Item
item =
Item
noDistance : Distance
noDistance =
Distance 0 0
increaseDistance : Delta -> Distance -> Distance
increaseDistance ( dx, dy ) (Distance h v) =
Distance (h + dx) (v + dy)
vertical : Distance -> VerticalDistance
vertical (Distance _ verticalDistance) =
verticalDistance
dragConfig : Draggable.Config Msg
dragConfig =
Draggable.customConfig
[ onDragBy DragBy
, onMouseDown StartDragging
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg ({ items, draggingItemIndex, draggedDistance } as model) =
case msg of
DragMsg dragMsg ->
Draggable.update dragConfig dragMsg model
DragBy delta ->
( { model | draggedDistance = increaseDistance delta draggedDistance }, Cmd.none )
StartDragging index ->
( { model | draggingItemIndex = index |> String.toInt |> Result.toMaybe }, Cmd.none )
StopDragging ->
( { model
| draggingItemIndex = Nothing
, draggedDistance =
noDistance
, items =
case draggingItemIndex of
Just oldIndex ->
reorderItems
oldIndex
(virtualIndex (vertical draggedDistance) oldIndex)
items
Nothing ->
items
}
, Cmd.none
)
reorderItems : Index -> Index -> Array a -> Array a
reorderItems oldIndex newIndex items =
let
movedItem =
ArrayX.getUnsafe oldIndex items
in
items
|> ArrayX.removeAt oldIndex
|> insertAt newIndex movedItem
insertAt : Index -> a -> Array a -> Array a
insertAt index newItem items =
let
( before, after ) =
ArrayX.splitAt index items
in
before
|> Array.push newItem
|> flip Array.append after
subscriptions : Model -> Sub Msg
subscriptions { drag } =
Draggable.subscriptions DragMsg drag
styles : List Mixin -> Html.Attribute msg
styles =
Css.asPairs >> Html.Attributes.style
view : Model -> Html Msg
view { items, draggingItemIndex, draggedDistance } =
Html.ul
[ styles
[ width (px 200)
, border3 (px 1) dashed (hex "#000")
, displayFlex
, flexDirection column
, padding (px 0)
, margin (px 32)
]
]
(Array.toList <| Array.indexedMap (itemView draggingItemIndex draggedDistance) items)
itemHeight : Float
itemHeight =
50
itemMargin : Float
itemMargin =
5
virtualIndex : VerticalDistance -> Index -> Index
virtualIndex verticalDistance activeIndex =
activeIndex + floor (verticalDistance / itemHeight + 0.5)
itemView : Maybe Index -> Distance -> Index -> Item -> Html Msg
itemView activeIndex (Distance h v) index { text } =
let
( dx, dy ) =
case activeIndex of
Just justActiveIndex ->
if justActiveIndex == index then
( h, v )
else if index <= (virtualIndex v justActiveIndex) && index >= justActiveIndex then
( 0, -itemHeight )
else if index >= (virtualIndex v justActiveIndex) && index <= justActiveIndex then
( 0, itemHeight )
else
( 0, 0 )
Nothing ->
( 0, 0 )
transition =
if List.member activeIndex [ Just index, Nothing ] then
""
else
"transform 200ms"
zIndex =
if activeIndex == Just index then
"1"
else
"0"
scaleFactor =
if activeIndex == Just index then
1.05
else
1
shadow =
if activeIndex == Just index then
boxShadow3 (px 3) (px 3) (px 10)
else
boxShadow4 (px 3) (px 3) (px 10) (px -5)
in
Html.li
[ styles
[ displayFlex
, boxSizing borderBox
, transforms
[ translate2 (px dx) (px dy)
, scale scaleFactor
]
, padding2 (px 0) (px 10)
, alignItems center
, height (px <| itemHeight - 2 * itemMargin)
, border3 (px 1) solid (hex "#d00")
, margin (px itemMargin)
, cursor nsResize
, backgroundColor (hex "#fff")
, shadow
, property "-webkit-user-select" "none"
, property "transition" transition
, property "z-index" zIndex
]
, Draggable.mouseTrigger (toString index) DragMsg
, onMouseUp StopDragging
]
[ Html.text text ]