/
drag.js
233 lines (214 loc) · 9.67 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
S.drag = {
items: [], timer: null, timerStart: null,
item: {
index: null,
elem: null,
win: { w: 0, h: 0, scrollx: 0, scrolly: 0 },
pos: { x: 0, y: 0, w: 0, h: 0 },
start: { x: 0, y: 0 },
cursor: { x: 0, y: 0 },
options: null,
hasOnDrag: false
},
add: function (elem, dragElem, onStart, onDrag, onStop, onClick, options) {
/* options = {
* boundTop:0 , boundRight:0 , boundLeft:0 , boundRight:0,
* useElemPos:false, callee:S.drag, offsetX: 0, offsetY: 0,
* hideArea: false, hideAreaOffset: 0 , delay:200, speed: 1000 / 30
* } */
this.items.push({ elem: elem, dragElem: dragElem, onStart: onStart, onDrag: onDrag, onStop: onStop, onClick: onClick, options: options ? options : {} });
var x = this.items.length - 1;
$(elem).on('mousedown', function (e) { S.drag.events.start.call(S.drag, x, e) });
if (options.touch === true) {
c.on('touchstart', (e) => S.drag.events.touchstart(x, e, { ...opts, istouch: true }));
c.on('touchmove', (e) => S.drag.events.touchmove(x, e, { ...opts, istouch: true }));
c.on('touchend', (e) => S.drag.events.touchend(x, e, { ...opts, istouch: true }));
}
},
has: function (elem) {
this.items.forEach(function (item) {
if (item.elem == elem) { return true; }
});
return false;
},
alteredDOM: function () {
//manually call this function if the DOM was altered and
//changed the position of the drag element's parent element
S.drag.item.parent.y = S.drag.item.element.parent().offset().top;
},
events: {
start: function (index, e) {
var self = this;
var item = this.items[index];
var delay = 200;
if (item.options.delay != null) {
delay = item.options.delay;
}
if (item.options.stopPropagation == null) {
item.options.stopPropagation = true;
}
this.item = { index: index };
this.timer = null;
this.timerStart = setTimeout(function () {
//allow a delay for user click before starting drag animation
var el = $(item.elem);
var elem = $(item.dragElem);
var elpos = el.offset();
var pos = elem.position();
var parentPos = elem.parent().offset();
var win = $(window);
var speed = 1000 / 30;
var hideArea = false;
var hideAreaOffset = 0;
if (item.options) {
if (item.options.useElemPos == true) {
pos = { left: elpos.left - pos.left, top: elpos.top - pos.top };
}
if (item.options.speed != null) {
speed = item.options.speed;
}
//if (item.options.offsetX) {
// pos.left += item.options.offsetX;
//}
//if (item.options.offsetY) {
// pos.top += item.options.offsetY;
//}
hideArea = item.options.hideArea || false;
hideAreaOffset = item.options.hideAreaOffset || 0;
if (hideArea == true) {
//hide the relative area that the DOM element being dragged previously occupied
//by subtracting the element's height from the element's margin, which will
//cover the element's tracks & force siblings below (in the DOM) to be pushed upwards
elem.css({ marginBottom: -1 * (elem.height() + hideAreaOffset) });
}
}
self.item = {
index: index,
elem: item.dragElem,
element: elem,
win: {
w: win.width(),
h: win.height()
},
start: {
x: e.clientX + document.body.scrollLeft,
y: e.clientY + document.body.scrollTop
},
cursor: {
x: e.clientX + document.body.scrollLeft,
y: e.clientY + document.body.scrollTop
},
pos: {
x: pos.left,
y: pos.top,
w: elem.width(),
h: elem.height()
},
offset: {
x: item.options.offsetX || 0,
y: item.options.offsetY || 0
},
parent: {
y: parentPos.top,
y1: parentPos.top
},
options: item.options,
hasOnDrag: typeof item.onDrag == 'function',
hideArea: hideArea,
hideAreaOffset: hideAreaOffset
}
if (typeof item.onStart == 'function') {
item.onStart.call(item.options ? (item.options.callee ? item.options.callee : this) : this, item);
}
//set up document-level drag events
$(window).on('mousemove', S.drag.events.doc.move);
S.drag.events.drag.call(S.drag);
clearInterval(this.timer);
self.timer = setInterval(function () { S.drag.events.drag.call(S.drag); }, speed);
}, delay);
//set up document-level click event
$(window).on('mouseup', S.drag.events.doc.up);
//don't let drag event select text on the page
if (item.options.stopPropagation == true) {
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.returnValue = false;
return false;
}
},
doc: {
move: function (e) { S.drag.events.mouse.call(S.drag, e); },
up: function (e) { S.drag.events.stop.call(S.drag) }
},
mouse: function (e) {
this.item.cursor.x = e.clientX + document.body.scrollLeft;
this.item.cursor.y = e.clientY + document.body.scrollTop;
this.item.parent.y = this.item.element.parent().offset().top;
},
drag: function () {
var item = this.item;
if (item.hasOnDrag == true) { if (this.items[item.index].onDrag.call(item.options ? (item.options.callee ? item.options.callee : this) : this, item) == false) { return; } }
var x = (item.pos.x + item.offset.x + (item.cursor.x - item.start.x));
var y = (item.pos.y + item.offset.y + (item.cursor.y - item.start.y) - (item.parent.y - item.parent.y1));
if (item.options) {
if (item.options.boundTop != null) {
if (item.options.boundTop > y) { y = item.options.boundTop; }
}
if (item.options.boundRight != null) {
if (item.win.w - item.options.boundRight < x + item.pos.w) { x = item.win.w - item.options.boundRight - item.pos.w; }
}
if (item.options.boundBottom != null) {
if (item.win.h - item.options.boundBottom < y + item.pos.h) { y = item.win.h - item.options.boundBottom - item.pos.h; }
}
if (item.options.boundLeft != null) {
if (item.options.boundLeft > x) { x = item.options.boundLeft; }
}
}
item.elem[0].style.left = x + 'px';
item.elem[0].style.top = y + 'px';
},
stop: function (index) {
var item = S.drag.items[S.drag.item.index];
$(window).off('mouseup', S.drag.events.doc.up);
if (this.timer == null) {
//user click
clearTimeout(this.timerStart);
this.timerStart = null;
if (typeof item.onClick == 'function') {
item.onClick.call(item.options ? (item.options.callee ? item.options.callee : this) : this, item);
}
return;
}
clearInterval(this.timer);
this.timer == null;
$(window).off('mousemove', S.drag.events.doc.move);
if (S.drag.item.hideArea == true) {
S.drag.item.elem.css({ marginBottom: 0 });
}
if (typeof item.onStop == 'function') {
item.onStop.call(item.options ? (item.options.callee ? item.options.callee : this) : this, item);
}
},
touchstart: function (index, e, options) {
S.drag.events.start(index, e.touches[0]);
if (typeof options.touchStart == 'function') {
options.touchStart(index, e, options);
}
},
touchmove: function (index, e, options) {
var touch = e.touches[0];
var sel = S.scrollbar.selected;
S.scrollbar.selected.currentY = sel.cursorY + ((sel.cursorY - touch.clientY) * sel.diff);
if (typeof options.touchMove == 'function') {
options.touchMove(index, e, options);
}
},
touchend: function (index, e, options) {
S.scrollbar.selected.scrolling = false;
S.scrollbar.selected.container.removeClass('scrolling');
if (typeof options.touchEnd == 'function') {
options.touchEnd(index, e, options);
}
},
}
};