-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (125 loc) · 3.49 KB
/
index.js
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
// Generated by CoffeeScript 1.3.3
(function() {
var Emitter, ResizableElement, classes, handleDirs,
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
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 = function(element) {
var details, handleDir, self, startH, startW, startX, startY, _fn;
this.element = element;
this._handles = {};
self = this;
startX = 0;
startY = 0;
startW = 0;
startH = 0;
_fn = function(handleDir, details) {
self._handles[handleDir] = document.createElement("div");
classes(self._handles[handleDir]).add("resize-handle").add("resize-handle-" + handleDir);
element.appendChild(self._handles[handleDir]);
return self._handles[handleDir].addEventListener("mousedown", function(e) {
var resizeMove, resizeStop, style;
e.stopPropagation();
startX = e.pageX;
startY = e.pageY;
style = window.getComputedStyle(element);
startW = parseInt(style.width);
startH = parseInt(style.height);
self.emit("resizestart", {
element: element,
handleDir: handleDir
});
document.addEventListener("mousemove", resizeMove = function(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";
}
return self.emit("resize", {
element: element,
handleDir: handleDir
});
});
return document.addEventListener("mouseup", resizeStop = function(e) {
document.removeEventListener("mousemove", resizeMove);
document.removeEventListener("mouseup", resizeStop);
return self.emit("resizestop", {
element: element,
handleDir: handleDir
});
});
});
};
for (handleDir in handleDirs) {
details = handleDirs[handleDir];
_fn(handleDir, details);
}
return self;
};
ResizableElement.prototype.handles = function() {
var dir, dirs, el, _ref;
dirs = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
_ref = this._handles;
for (dir in _ref) {
el = _ref[dir];
if (__indexOf.call(dirs, dir) >= 0) {
classes(el).remove("resize-handle-hidden");
} else {
classes(el).add("resize-handle-hidden");
}
}
return this;
};
Emitter(ResizableElement.prototype);
module.exports = function(element) {
return new ResizableElement(element);
};
}).call(this);