-
Notifications
You must be signed in to change notification settings - Fork 3
/
dragdropresize.js
159 lines (127 loc) · 3.97 KB
/
dragdropresize.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
var _startX = 0; // mouse starting positions
var _startY = 0;
var _offsetX = 0; // current element offset
var _offsetY = 0;
var _dragWidget; // needs to be passed from OnMouseDown to OnMouseMove
var _oldZIndex = 0; // we temporarily increase the z-index during drag
InitDragDropResize();
function InitDragDropResize() {
document.onmousedown = OnMouseDown;
document.onmouseup = OnMouseUp;
window.onresize = OnResize;
}
// The z-index of the topmost widget, all others have a lower z-index.
var Z_INDEX = 1000;
function OnMouseDown(e) {
// pass the event object for IE
if (e == null) {
e = window.event;
}
// retrieve clicked element
// IE uses srcElement, others use target
var clickedElem = e.target != null ? e.target : e.srcElement;
// stop if clicked element is attached class 'nodrag'
if ($('#'+clickedElem.id).hasClass('nodrag')) {
return;
}
// from that name, determine the vizId
var m = new RegExp('[a-zA-Z]+_([0-9]+)').exec(clickedElem.id);
if (m) {
vizId = m[1];
} else {
vizId = null;
}
// stop if no vizId found
if (!vizId) {
return;
}
// determine clickWidget
clickWidget = document.getElementById('widget_'+vizId);
// stop if no clickWidget
if (!clickWidget) {
return;
}
// stop if not a click
// for IE, left click == 1
// for Firefox, left click == 0
if (
!(e.button==1 && window.event!=null) &&
!(e.button==0) &&
!(e.touches)
) {
return;
}
// if you get here, drag the widget
// get the mouse position
if (e.targetTouches && e.targetTouches.length == 1) {
e.preventDefault();
var touch = e.targetTouches[0];
_startX = touch.pageX;
_startY = touch.pageY;
} else {
_startX = e.clientX;
_startY = e.clientY;
}
// grab the clicked widget's position
_offsetX = ExtractNumber(clickWidget.style.left);
_offsetY = ExtractNumber(clickWidget.style.top);
// bring the clicked element to the front while it is being dragged
var oldZIndex = parseInt(clickWidget.style.zIndex, 10);
clickWidget.style.zIndex = Z_INDEX;
if (oldZIndex) {
$(".widget").each(function() {
var thisZIndex = parseInt($(this).css("z-index"), 10);
if (thisZIndex >= oldZIndex) {
$(this).css("z-index", thisZIndex - 1);
}
});
}
// remember which widget to drag
_dragWidget = clickWidget;
// enable move events
document.onmousemove = OnMouseMove;
document.ontouchmove = OnMouseMove;
// cancel out any text selections
document.body.focus();
// prevent text selection in IE
document.onselectstart = function () { return false; };
// prevent IE from trying to drag an image
clickWidget.ondragstart = function() { return false; };
// prevent text selection (except IE)
return false;
}
function OnMouseMove(e) {
if (e == null) {
var e = window.event;
}
var posX, posY;
if (e.targetTouches && e.targetTouches.length == 1) {
var target = e.targetTouches[0];
posX = target.pageX;
posY = target.pageY;
} else {
posX = e.clientX;
posY = e.clientY;
}
// this is the actual drag code
_dragWidget.style.left = (_offsetX + posX - _startX) + 'px';
_dragWidget.style.top = (_offsetY + posY - _startY) + 'px';
}
function OnMouseUp(e) {
if (_dragWidget != null) {
// we're done with these events until the next OnMouseDown
document.onmousemove = null;
document.onselectstart = null;
_dragWidget.ondragstart = null;
// this is how we know we're not dragging
_dragWidget = null;
}
}
function OnResize(e) {
// re-position the logo
positionLogo();
}
function ExtractNumber(value) {
var n = parseInt(value);
return n == null || isNaN(n) ? 0 : n;
}