-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
76 lines (59 loc) · 2.03 KB
/
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
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
Emitter = require "emitter"
classes = require "classes"
handleDirs =
n: cursor: "n", n: true
s: cursor: "s", s: true
e: cursor: "e", e: true
w: cursor: "w", w: true
ne: cursor: "ne", n: true, e: true
se: cursor: "se", s: true, e: true
sw: cursor: "sw", s: true, w: true
nw: cursor: "nw", n: true, w: true
ResizableElement = (@element) ->
@_handles = {}
self = this
startX = 0
startY = 0
startW = 0
startH = 0
for handleDir, details of handleDirs
do (handleDir, details) ->
self._handles[handleDir] = document.createElement "div"
classes(self._handles[handleDir])
.add("resize-handle")
.add("resize-handle-#{handleDir}")
element.appendChild self._handles[handleDir]
self._handles[handleDir].addEventListener "mousedown", (e) ->
e.stopPropagation()
startX = e.pageX
startY = e.pageY
style = window.getComputedStyle element
startW = parseInt style.width
startH = parseInt style.height
self.emit "resizestart", {element, handleDir}
document.addEventListener "mousemove", resizeMove = (e) ->
if details.n
element.style.top = (e.pageY) + "px"
element.style.height = (startH + (startY - e.pageY)) + "px"
if details.e
element.style.width = (startW + (e.pageX - startX)) + "px"
if details.s
element.style.height = (startH + (e.pageY - startY)) + "px"
if details.w
element.style.left = (e.pageX) + "px"
element.style.width = (startW + (startX - e.pageX)) + "px"
self.emit "resize", {element, handleDir}
document.addEventListener "mouseup", resizeStop = (e) ->
document.removeEventListener "mousemove", resizeMove
document.removeEventListener "mouseup", resizeStop
self.emit "resizestop", {element, handleDir}
self
ResizableElement.prototype.handles = (dirs...) ->
for dir, el of @_handles
if dir in dirs
classes(el).remove "resize-handle-hidden"
else
classes(el).add "resize-handle-hidden"
this
Emitter ResizableElement.prototype
module.exports = (element) -> new ResizableElement element