-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
35 lines (25 loc) · 959 Bytes
/
index.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
Emitter = require "emitter"
module.exports = (element) ->
emitter = new Emitter
startX = 0
startY = 0
originX = 0
originY = 0
element.addEventListener "mousedown", initEvent = (e) ->
originX = element.offsetLeft
originY = element.offsetTop
startX = event.pageX
startY = event.pageY
emitter.emit "dragstart", {element, originX, originY, startX, startY}
document.addEventListener "mousemove", moveEvent = (e) ->
element.style.left = (originX + (e.pageX - startX)) + "px"
element.style.top = (originY + (e.pageY - startY)) + "px"
emitter.emit "drag"
document.addEventListener "mouseup", stopEvent = (e) ->
stopX = e.pageX
stopY = e.pageY
emitter.emit "dragstop", {element, originX, originY, startX, startY, stopX, stopY}
document.removeEventListener "mousemove", moveEvent
document.removeEventListener "mouseup", stopEvent
emitter.on "remove", -> element.removeEventListener "mousedown", initEvent
emitter