Skip to content

Commit 8cc2bad

Browse files
committed
0.4.0
1 parent a8edaae commit 8cc2bad

File tree

19 files changed

+7208
-0
lines changed

19 files changed

+7208
-0
lines changed

dist/amd/browsers.js

Lines changed: 776 additions & 0 deletions
Large diffs are not rendered by default.

dist/amd/drag.js

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
2+
define(function (require, exports, module) {
3+
var syn = require('./synthetic');
4+
(function dragSupport() {
5+
if (!document.body) {
6+
syn.schedule(dragSupport, 1);
7+
return;
8+
}
9+
var div = document.createElement('div');
10+
document.body.appendChild(div);
11+
syn.helpers.extend(div.style, {
12+
width: '100px',
13+
height: '10000px',
14+
backgroundColor: 'blue',
15+
position: 'absolute',
16+
top: '10px',
17+
left: '0px',
18+
zIndex: 19999
19+
});
20+
document.body.scrollTop = 11;
21+
if (!document.elementFromPoint) {
22+
return;
23+
}
24+
var el = document.elementFromPoint(3, 1);
25+
if (el === div) {
26+
syn.support.elementFromClient = true;
27+
} else {
28+
syn.support.elementFromPage = true;
29+
}
30+
document.body.removeChild(div);
31+
document.body.scrollTop = 0;
32+
}());
33+
var elementFromPoint = function (point, element) {
34+
var clientX = point.clientX, clientY = point.clientY, win = syn.helpers.getWindow(element), el;
35+
if (syn.support.elementFromPage) {
36+
var off = syn.helpers.scrollOffset(win);
37+
clientX = clientX + off.left;
38+
clientY = clientY + off.top;
39+
}
40+
el = win.document.elementFromPoint ? win.document.elementFromPoint(clientX, clientY) : element;
41+
if (el === win.document.documentElement && (point.clientY < 0 || point.clientX < 0)) {
42+
return element;
43+
} else {
44+
return el;
45+
}
46+
}, createEventAtPoint = function (event, point, element) {
47+
var el = elementFromPoint(point, element);
48+
syn.trigger(el || element, event, point);
49+
return el;
50+
}, mouseMove = function (point, element, last) {
51+
var el = elementFromPoint(point, element);
52+
if (last !== el && el && last) {
53+
var options = syn.helpers.extend({}, point);
54+
options.relatedTarget = el;
55+
syn.trigger(last, 'mouseout', options);
56+
options.relatedTarget = last;
57+
syn.trigger(el, 'mouseover', options);
58+
}
59+
syn.trigger(el || element, 'mousemove', point);
60+
return el;
61+
}, startMove = function (start, end, duration, element, callback) {
62+
var startTime = new Date(), distX = end.clientX - start.clientX, distY = end.clientY - start.clientY, win = syn.helpers.getWindow(element), current = elementFromPoint(start, element), cursor = win.document.createElement('div'), calls = 0, move;
63+
move = function onmove() {
64+
var now = new Date(), scrollOffset = syn.helpers.scrollOffset(win), fraction = (calls === 0 ? 0 : now - startTime) / duration, options = {
65+
clientX: distX * fraction + start.clientX,
66+
clientY: distY * fraction + start.clientY
67+
};
68+
calls++;
69+
if (fraction < 1) {
70+
syn.helpers.extend(cursor.style, {
71+
left: options.clientX + scrollOffset.left + 2 + 'px',
72+
top: options.clientY + scrollOffset.top + 2 + 'px'
73+
});
74+
current = mouseMove(options, element, current);
75+
syn.schedule(onmove, 15);
76+
} else {
77+
current = mouseMove(end, element, current);
78+
win.document.body.removeChild(cursor);
79+
callback();
80+
}
81+
};
82+
syn.helpers.extend(cursor.style, {
83+
height: '5px',
84+
width: '5px',
85+
backgroundColor: 'red',
86+
position: 'absolute',
87+
zIndex: 19999,
88+
fontSize: '1px'
89+
});
90+
win.document.body.appendChild(cursor);
91+
move();
92+
}, startDrag = function (start, end, duration, element, callback) {
93+
createEventAtPoint('mousedown', start, element);
94+
startMove(start, end, duration, element, function () {
95+
createEventAtPoint('mouseup', end, element);
96+
callback();
97+
});
98+
}, center = function (el) {
99+
var j = syn.jquery()(el), o = j.offset();
100+
return {
101+
pageX: o.left + j.outerWidth() / 2,
102+
pageY: o.top + j.outerHeight() / 2
103+
};
104+
}, convertOption = function (option, win, from) {
105+
var page = /(\d+)[x ](\d+)/, client = /(\d+)X(\d+)/, relative = /([+-]\d+)[xX ]([+-]\d+)/, parts;
106+
if (typeof option === 'string' && relative.test(option) && from) {
107+
var cent = center(from);
108+
parts = option.match(relative);
109+
option = {
110+
pageX: cent.pageX + parseInt(parts[1]),
111+
pageY: cent.pageY + parseInt(parts[2])
112+
};
113+
}
114+
if (typeof option === 'string' && page.test(option)) {
115+
parts = option.match(page);
116+
option = {
117+
pageX: parseInt(parts[1]),
118+
pageY: parseInt(parts[2])
119+
};
120+
}
121+
if (typeof option === 'string' && client.test(option)) {
122+
parts = option.match(client);
123+
option = {
124+
clientX: parseInt(parts[1]),
125+
clientY: parseInt(parts[2])
126+
};
127+
}
128+
if (typeof option === 'string') {
129+
option = syn.jquery()(option, win.document)[0];
130+
}
131+
if (option.nodeName) {
132+
option = center(option);
133+
}
134+
if (option.pageX != null) {
135+
var off = syn.helpers.scrollOffset(win);
136+
option = {
137+
clientX: option.pageX - off.left,
138+
clientY: option.pageY - off.top
139+
};
140+
}
141+
return option;
142+
}, adjust = function (from, to, win) {
143+
if (from.clientY < 0) {
144+
var off = syn.helpers.scrollOffset(win);
145+
var top = off.top + from.clientY - 100, diff = top - off.top;
146+
if (top > 0) {
147+
} else {
148+
top = 0;
149+
diff = -off.top;
150+
}
151+
from.clientY = from.clientY - diff;
152+
to.clientY = to.clientY - diff;
153+
syn.helpers.scrollOffset(win, {
154+
top: top,
155+
left: off.left
156+
});
157+
}
158+
};
159+
syn.helpers.extend(syn.init.prototype, {
160+
_move: function (from, options, callback) {
161+
var win = syn.helpers.getWindow(from), fro = convertOption(options.from || from, win, from), to = convertOption(options.to || options, win, from);
162+
if (options.adjust !== false) {
163+
adjust(fro, to, win);
164+
}
165+
startMove(fro, to, options.duration || 500, from, callback);
166+
},
167+
_drag: function (from, options, callback) {
168+
var win = syn.helpers.getWindow(from), fro = convertOption(options.from || from, win, from), to = convertOption(options.to || options, win, from);
169+
if (options.adjust !== false) {
170+
adjust(fro, to, win);
171+
}
172+
startDrag(fro, to, options.duration || 500, from, callback);
173+
}
174+
});
175+
});

0 commit comments

Comments
 (0)