This repository was archived by the owner on Sep 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdragging_service.js
More file actions
125 lines (101 loc) · 2.48 KB
/
dragging_service.js
File metadata and controls
125 lines (101 loc) · 2.48 KB
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
angular.module('dragging', ['mouseCapture', ] )
//
// Service used to help with dragging and clicking on elements.
//
.factory('dragging', function ($rootScope, mouseCapture) {
//
// Threshold for dragging.
// When the mouse moves by at least this amount dragging starts.
//
var threshold = 5;
return {
//
// Called by users of the service to register a mousedown event and start dragging.
// Acquires the 'mouse capture' until the mouseup event.
//
startDrag: function (evt, config) {
var dragging = false,
x,
y,
type = evt.type,
isTouch = (type == "touchstart" || type == "touchmove");
//
// Handler for mousemove events while the mouse is 'captured'.
//
if (isTouch) {
x = evt.originalEvent.touches[0].pageX;
y = evt.originalEvent.touches[0].pageY;
} else {
x = evt.pageX;
y = evt.pageY;
}
var mouseMove = function (evt) {
var pageX, pageY;
if (isTouch) {
pageX = evt.originalEvent.touches[0].pageX;
pageY = evt.originalEvent.touches[0].pageY;
} else {
pageX = evt.pageX;
pageY = evt.pageY;
}
if (!dragging) {
if (Math.abs(pageX - x) > threshold ||
Math.abs(pageY - y) > threshold)
{
dragging = true;
if (config.dragStarted) {
config.dragStarted(x, y, evt);
}
if (config.dragging) {
// First 'dragging' call to take into account that we have
// already moved the mouse by a 'threshold' amount.
config.dragging(pageX, pageY, evt);
}
}
}
else {
if (config.dragging) {
config.dragging(pageX, pageY, evt);
}
x = pageX;
y = pageY;
}
};
//
// Handler for when mouse capture is released.
//
var released = function() {
if (dragging) {
if (config.dragEnded) {
config.dragEnded();
}
}
else {
if (config.clicked) {
config.clicked();
}
}
};
//
// Handler for mouseup event while the mouse is 'captured'.
// Mouseup releases the mouse capture.
//
var mouseUp = function (evt) {
mouseCapture.release();
evt.stopPropagation();
evt.preventDefault();
};
//
// Acquire the mouse capture and start handling mouse events.
//
mouseCapture.acquire(evt, {
mouseMove: mouseMove,
mouseUp: mouseUp,
released: released,
});
evt.stopPropagation();
evt.preventDefault();
},
};
})
;