-
Notifications
You must be signed in to change notification settings - Fork 1
/
draghandle.coffee
74 lines (61 loc) · 2.08 KB
/
draghandle.coffee
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
{isString,clone} = require("./_helpers")
getEvent = (el, event, throttle,stop, cb) -> el: el, event:event, stop: stop,prevent: !stop, notPrevented: true, cbs:[cb], throttle: throttle
dEl = document.documentElement
start = (o, name, name2, e) ->
if e.changedTouches?
e = e.changedTouches[0]
isTouch = true
o.start = x: e.clientX, y: e.clientY
o.firstMove = true
o.onStart?.call(@, o)
o._moveRemover = @$on getEvent(dEl, name + "move", true, isTouch, move.bind(@,o))
@$once getEvent(dEl, name + name2, false, isTouch, end.bind(@,o))
getDelta = (s, e) ->
x: e.clientX - s.x, y: e.clientY - s.y, start: s
move = (o, e) ->
e = e.changedTouches[0] if e.changedTouches?
o.secondMove = o.firstMove
o.onFirstMove?.call(@, o, e) if o.firstMove
o.onMove?.call(@, getDelta(o.start,e) , o, e)
o.firstMove = false
end = (o, e) ->
o._moveRemover?()
o._moveRemover = null
if o.firstMove
o.onClick?.call(@,o)
else
o.onEnd?.call(@, getDelta(o.start,e), o)
o.onClick?.call(@,o) if o.secondMove
module.exports =
_name: "draghandle"
_v: 1
mixins:[
require("./events")
require("./parseElement")
require("./style")
]
methods:
$draghandle: (o) ->
o.handle ?= document.createElement "div"
o.activate = ->
_el = @$parseElement.byString(o.el)
unless o.wasActivated
if o.initStyle?
@$style.set(o.handle,o.initStyle)
if o.style?
@$computed.orWatch o.style, @$style.set.bind(@,o.handle)
@$on getEvent o.handle, "touchstart", false, true, start.bind(@, o, "touch", "end")
@$on getEvent o.handle, "mousedown", false, false, start.bind(@, o, "mouse", "up")
@$path.resolveMultiple(o,["onStart","onFirstMove","onMove","onEnd","onClick"])
_el.appendChild o.handle
return ->
_el.removeChild o.handle
return @$parseActive(o)
connectedCallback: ->
if @_isFirstConnect and @draghandle
for k,v of @draghandle
v = clone(v)
v.el ?= k
@$draghandle(v)
test module.exports, {}, (el) ->
it "should work", ->