1 |
|
2 |
|
3 |
|
4 |
|
5 | El.drag = function(handler, node, next, params) {
|
6 | var mouseStart, dragStart, topPos, leftPos
|
7 |
|
8 | if (typeof handler == "string") {
|
9 | handler = El.find(handler)
|
10 | }
|
11 | if (typeof node == "string") {
|
12 | node = El.find(node)
|
13 | }
|
14 |
|
15 | if (!node) node = handler
|
16 |
|
17 | var parent = node.parentNode
|
18 |
|
19 | Event.touchAsMouse(handler)
|
20 | El.on(handler, "mousedown", drag)
|
21 |
|
22 | function drag(e) {
|
23 | if (e.button) return
|
24 |
|
25 | var target = e.target || e.srcElement
|
26 | , tagName = target && target.tagName
|
27 | , top = node.style.top
|
28 |
|
29 | if (
|
30 | !tagName ||
|
31 | tagName === "INPUT" ||
|
32 | tagName === "TEXTAREA" ||
|
33 | tagName === "SELECT" ||
|
34 | El.hasClass(target, "no-drag")
|
35 | ){
|
36 | return
|
37 | }
|
38 |
|
39 | mouseStart = El.mouse(e)
|
40 |
|
41 | dragStart = {
|
42 | top: parseInt(top, 10) || 0,
|
43 | left: parseInt(node.style.left, 10) || 0
|
44 | }
|
45 | if (top && top.indexOf("%") > -1) {
|
46 | dragStart.top *= parent.offsetHeight / 100
|
47 | dragStart.left *= parent.offsetWidth / 100
|
48 | }
|
49 |
|
50 | El.on(document, "mouseup", dragEnd)
|
51 | El.on(document, "mousemove", dragMove)
|
52 | if (e.preventDefault) e.preventDefault()
|
53 | return false
|
54 | }
|
55 |
|
56 | function dragMove(e) {
|
57 | var mouse = El.mouse(e)
|
58 | , leftMax = parent.clientWidth >> 2
|
59 | , leftMin = leftMax - node.offsetWidth
|
60 | , topMax = parent.clientHeight >> 2
|
61 | , topMin = topMax - node.offsetHeight
|
62 | leftMax *= 3
|
63 | topMax *= 3
|
64 |
|
65 | leftPos = dragStart.left - mouseStart.left + mouse.left
|
66 | topPos = dragStart.top - mouseStart.top + mouse.top
|
67 |
|
68 | if (leftPos > leftMax) leftPos = leftMax
|
69 | else if (leftPos < leftMin) leftPos = leftMin
|
70 | if (topPos > topMax) topPos = topMax
|
71 | else if (topPos < topMin) topPos = topMin
|
72 |
|
73 | node.style.left = leftPos + "px"
|
74 | node.style.top = topPos + "px"
|
75 | return false
|
76 | }
|
77 |
|
78 | function dragEnd() {
|
79 | if (next && Math.abs(topPos - dragStart.top) + Math.abs(leftPos - dragStart.left) > 5) {
|
80 | next()
|
81 | }
|
82 | El.off(document, "mousemove", dragMove)
|
83 | El.off(document, "mouseup", dragEnd)
|
84 | return false
|
85 | }
|
86 | }
|
87 |
|
88 |
|