This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
drag.js
190 lines (182 loc) · 6.13 KB
/
drag.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
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
/*!
* drag.js - copyright Jake Luer 2011
* https://github.com/jakeluer/drag.js
* MIT License
*/
!function (context, doc) {
if ('undefined' == typeof bean) bean = require('bean');
var b = bean;
var current = getComputedStyle || currentStyle;
var is_touch_device = ('ontouchstart' in doc.documentElement) ? true : false;
drag = function (selector) {
return new Drag(drag.select(selector));
};
drag.select = function (selector) {
if ('string' == typeof selector) {
return document.getElementById(selector) || document.querySelectorAll(selector)[0];
} else {
return selector;
}
};
drag.value = function (el, prop, val) {
if (!val) {
return parseFloat(current(el).getPropertyValue(prop));
}
else {
el.style[prop] = val;
}
};
drag.evs = (function () {
if (is_touch_device) {
return {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
};
}
else {
return {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
};
}
}());
Drag = function Drag(el) {
if (!(this instanceof Drag)) return new Drag(el);
this.el = el;
this._axis = 'both';
this._start = [];
this._dragging = [];
this._end = [];
this.pos = {};
return this;
};
Drag.prototype.current = function (prop) {
return drag.value(this.el, prop);
};
Drag.prototype.axis = function (axis) {
if (axis == 'x' || axis == 'y' || axis == 'both') this._axis = axis;
return this;
};
Drag.prototype.container = function (selector) {
this._container = drag.select(selector);
return this;
};
Drag.prototype.handle = function (selector) {
this._handle = drag.select(selector);
return this;
};
Drag.prototype.start = function (fn) {
if (fn && 'function' == typeof fn) this._start.push(fn);
return this;
};
Drag.prototype.dragging = function (fn) {
if (fn && 'function' == typeof fn) this._dragging.push(fn);
return this;
};
Drag.prototype.end = function (fn) {
if (fn && 'function' == typeof fn) this._end.push(fn);
return this;
};
Drag.prototype.getPos = function (fn) {
this.pos.x =this.current('left');
this.pos.y =this.current('top');
if (fn && 'function' == typeof fn) fn.apply(this);
return this;
};
Drag.prototype.bind = function () {
var self = this;
this.unbind();
this._eventHandler = function (e) {
var posX = self.current('left'),
posY = self.current('top');
var moveHandler = function (e2) {
var offsetX, offsetY, newX, newY;
if (!is_touch_device) {
offsetX = e2.clientX - e.clientX;
offsetY = e2.clientY - e.clientY;
} else {
if (e.touches.length == 1) {
offsetX = e2.touches[0].clientX - e.touches[0].clientX;
offsetY = e2.touches[0].clientY - e.touches[0].clientY;
}
}
newX = posX + offsetX;
newY = posY + offsetY;
self.pos.dX = newX - self.current('left');
self.pos.dY = newY - self.current('top');
if (self._container) {
var c = self._container;
if (newX < 0) newX = 0;
else if (newX >= 0) {
maxX = (drag.value(c, 'padding-left') + drag.value(c, 'width') + drag.value(c, 'padding-right')) - (drag.value(self.el, 'margin-left') + drag.value(self.el, 'border-left-width') + drag.value(self.el, 'padding-left') + drag.value(self.el, 'width') + drag.value(self.el, 'padding-right') + drag.value(self.el, 'border-right-width') + drag.value(self.el, 'margin-right'));
if (newX > maxX) newX = maxX;
}
if (newY < 0) newY = 0;
else if (newY >= 0) {
maxY = (drag.value(c, 'padding-top') + drag.value(c, 'height') + drag.value(c, 'padding-bottom')) - (drag.value(self.el, 'margin-top') + drag.value(self.el, 'border-top-width') + drag.value(self.el, 'padding-top') + drag.value(self.el, 'height') + drag.value(self.el, 'padding-bottom') + drag.value(self.el, 'border-bottom-width') + drag.value(self.el, 'margin-bottom'));
if (newY > maxY) newY = maxY;
}
}
if (self._axis == 'x') {
drag.value(self.el, 'left', newX + 'px');
}
else if (self._axis == 'y') {
drag.value(self.el, 'top', newY + 'px');
}
else {
drag.value(self.el, 'left', newX + 'px');
drag.value(self.el, 'top', newY + 'px');
}
self.getPos();
for (var func in self._dragging) {
self._dragging[func].apply(self);
}
e2.preventDefault();
e2.stopPropagation();
};
var cleanup = function () {
b.remove(doc, drag.evs.move, moveHandler);
b.remove(doc, drag.evs.end, endHandler);
b.remove(doc, 'selectstart', prevDef);
b.remove(self._handle, 'dragstart', prevDef);
};
var endHandler = function (e2) {
for (var func in self._end) {
self._end[func].apply(self);
}
cleanup();
};
var prevDef = function (e3) {
e3.preventDefault();
e3.stopPropagation();
};
for (var func in self._start) {
self._start[func].apply(self);
}
doc.body.focus();
b.add(doc, 'selectstart', prevDef);
b.add(self._handle, 'dragstart', prevDef);
b.add(doc, drag.evs.move, moveHandler);
b.add(doc, drag.evs.end, endHandler);
};
this.getPos();
if (!this._handle) this._handle = this.el;
b.add(this._handle, drag.evs.start, this._eventHandler);
return this;
};
Drag.prototype.unbind = function () {
if (!this._eventHandler) return this;
b.remove(this.el, drag.evs.start, this._eventHandler);
b.remove(this._handle, drag.evs.start, this._eventHandler);
return this;
};
var oldDrag = context.drag;
drag.noConflict = function () {
context.drag = oldDrag;
return this;
};
(typeof module !== 'undefined' && module.exports && (module.exports = drag));
context['drag'] = drag;
}(this, document);