-
Notifications
You must be signed in to change notification settings - Fork 1
/
Draggable.js
112 lines (90 loc) · 2.92 KB
/
Draggable.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
define([
'wrap!underscore',
'jquery',
'./Droppable'
],
function (
_,
$,
Droppable
) {
$(window).bind('touchstart', function () {}) // ios hack
return {
setDraggable: function (draggable) {
if (draggable) {
this.enableDraggable()
}
else {
this.disableDraggable()
}
},
enableDraggable: function () {
if (this.draggable) return
this.draggable = true
if (!this.x) this.x = 0
if (!this.y) this.y = 0
if (!this._draggable_downBound) {
this._draggable_downBound = _.bind(this._draggable_down, this)
// just do them all now
this._draggable_moveBound = _.bind(this._draggable_mouseMove, this)
this._draggable_upBound = _.bind(this._draggable_mouseUp, this)
}
this.$el.bind("mousedown touchstart", this._draggable_downBound)
},
disableDraggable: function () {
if (!this.draggable) return
this.draggable = false
this.$el.unbind("mousedown", this._draggable_downBound)
},
_draggable_down: function (e) {
e = e.originalEvent
if (e.type === 'mousedown' && e.which !== 1) return
e.preventDefault()
this.trigger("dragstart")
if (e.targetTouches) {
this._draggable_startDragX = e.targetTouches[0].pageX
this._draggable_startDragY = e.targetTouches[0].pageY
$(window).bind('touchmove', this._draggable_moveBound)
.bind('touchend touchcancel', this._draggable_upBound)
}
else {
this._draggable_startDragX = e.clientX
this._draggable_startDragY = e.clientY
$(window).mousemove(this._draggable_moveBound)
.mouseup(this._draggable_upBound)
.mouseleave(this._draggable_upBound)
}
this._draggable_startTime = new Date().getTime()
},
_draggable_mouseMove: function (e) {
e = e.originalEvent
var x, y, changeX, changeY
if (e.targetTouches) {
x = e.targetTouches[0].pageX
y = e.targetTouches[0].pageY
}
else {
x = e.clientX
y = e.clientY
}
changeX = x - this._draggable_startDragX
changeY = y - this._draggable_startDragY
this.x += changeX
this.y += changeY
this.trigger("dragmove", changeX, changeY)
Droppable._droppableOnDragMove(this, this.x, this.y, this.width, this.height)
this._draggable_startDragX = x
this._draggable_startDragY = y
},
_draggable_mouseUp: function () {
var dropTarget = Droppable._droppableOnDragEnd(this, this.x, this.y, this.width, this.height)
this.trigger("dragend", dropTarget)
var dragtime = new Date().getTime() - this._draggable_startTime
if (dragtime < 130) {
this.trigger("dragclick")
}
$(window).unbind("mousemove touchmove", this._draggable_moveBound)
.unbind("mousemove mouseup mouseleave touchend touchcancel", this._draggable_upBound)
}
}
})