forked from HairyRabbit/Draggable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraggable.elm
More file actions
236 lines (185 loc) · 4.13 KB
/
Draggable.elm
File metadata and controls
236 lines (185 loc) · 4.13 KB
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
module Draggable.Draggable exposing (..)
{-| Draggable Component.
@TODO add the current value
-}
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Mouse exposing (Position)
import Json.Decode as Json
-- MODEL
type State
= Dragged
| Dragging Int
type Axis
= Both
| X
| Y
type alias Scope =
{ minX : Maybe Int
, maxX : Maybe Int
, minY : Maybe Int
, maxY : Maybe Int
}
type alias Drag =
{ start : Position
, curr : Position
}
type alias Model =
{ position : Position
, drag : Maybe Drag
, axis : Axis
, grid : Maybe Int
, scope : Scope
, state : State
}
initScope : Scope
initScope =
{ minX = Nothing
, maxX = Nothing
, minY = Nothing
, maxY = Nothing
}
initModel : Model
initModel =
{ position = Position 0 0
, drag = Nothing
, axis = Both
, grid = Nothing
, scope = initScope
, state = Dragged
}
init : (Model, Cmd Msg)
init =
(initModel, Cmd.none)
-- UPDATE
type Msg
= NoOp
| DragStart Position
| DragMove Position
| DragEnd Position
| SetPositionX Int
| SetPositionY Int
| SetAxis Axis
| SetGrid (Maybe Int)
update : Msg -> Model -> (Model, Cmd Msg)
update msg ({ position, drag } as model) =
case msg of
DragStart position ->
( { model | drag = Just <| Drag position position }
, Cmd.none
)
DragMove position ->
( { model | drag = Maybe.map (\{ start } -> Drag start position) drag }
, Cmd.none
)
DragEnd _ ->
( { model |
position = getPosition model
, drag = Nothing
}
, Cmd.none
)
SetPositionX x ->
( { model | position = Position x position.y }
, Cmd.none
)
SetPositionY y ->
( { model | position = Position position.x y }
, Cmd.none
)
SetAxis axis ->
( { model | axis = axis }
, Cmd.none
)
SetGrid grid ->
( { model | grid = grid }
, Cmd.none
)
_ ->
(model, Cmd.none)
getPosition : Model -> Position
getPosition { position, drag, axis, grid, scope } =
case drag of
Nothing ->
position
Just { start, curr } ->
let
relative f =
(f curr) - (f start)
relativeX =
relative .x
relativeY =
relative .y
stepGrow b s =
b // s * s
(positionX, positionY) =
case grid of
Nothing ->
( position.x + relativeX
, position.y + relativeY
)
Just grid' ->
( position.x + stepGrow relativeX grid'
, position.y + stepGrow relativeY grid'
)
scopeVal =
valueInScope scope
in
case axis of
X ->
Position (scopeVal X positionX) position.y
Y ->
Position position.x (scopeVal Y positionY)
_ ->
Position (scopeVal X positionX) (scopeVal Y positionY)
valueInScope : Scope -> Axis -> Int -> Int
valueInScope { minX, minY, maxX, maxY } axis val =
let
defaultVal cmp =
Maybe.withDefault val cmp
in
case axis of
X ->
clamp (defaultVal minX) (defaultVal maxX) val
Y ->
clamp (defaultVal minY) (defaultVal maxY) val
_ ->
val
-- SUBSCRITIONS
subscriptions : Model -> Sub Msg
subscriptions { drag } =
case drag of
Just _ ->
Sub.batch
[ Mouse.moves DragMove
, Mouse.ups DragEnd
]
Nothing ->
Sub.none
-- VIEW
view : List (Attribute Msg) -> Model -> Html Msg
view attrs model =
let
draggableAttrs =
[ on "mousedown" (Json.map DragStart Mouse.position)
, cursorStyle model
]
view' =
div (attrs ++ draggableAttrs) []
in
view'
(=>) = (,)
intToPxStr : Int -> String
intToPxStr n =
toString n ++ "px"
cursorStyle : Model -> Attribute msg
cursorStyle ({ position, drag } as model) =
let
{ x, y } =
getPosition model
in
style
[ "transform" => ("translate3d(" ++ (intToPxStr x) ++ "," ++ (intToPxStr y) ++ ",0)")
, "cursor" => "pointer"
]