drag.js
11.7 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
$axure.internal(function($ax) {
var widgetDragInfo = new Object();
var _drag = {};
$ax.drag = _drag;
$ax.drag.GetWidgetDragInfo = function() {
return $.extend({}, widgetDragInfo);
};
$ax.drag.StartDragWidget = function(event, id) {
$ax.setjBrowserEvent(jQuery.Event(event));
//we should only start drag on one target, otherwise the _dragWidget and _stopDragWidget events from multiple targets will be conflicted
if(event.donotdrag || widgetDragInfo.started) return;
var x, y;
var tg;
if(IE_10_AND_BELOW) {
x = window.event.clientX + window.document.documentElement.scrollLeft + window.document.body.scrollLeft;
y = window.event.clientY + window.document.documentElement.scrollTop + window.document.body.scrollTop;
tg = window.event.srcElement;
} else {
if(event.changedTouches) {
x = event.changedTouches[0].pageX;
y = event.changedTouches[0].pageY;
} else {
x = event.pageX;
y = event.pageY;
event.preventDefault();
}
tg = event.target;
}
widgetDragInfo.started = true;
widgetDragInfo.hasDragged= false;
widgetDragInfo.widgetId = id;
widgetDragInfo.cursorStartX = x;
widgetDragInfo.cursorStartY = y;
widgetDragInfo.lastX = x;
widgetDragInfo.lastY = y;
widgetDragInfo.currentX = x;
widgetDragInfo.currentY = y;
widgetDragInfo.movedWidgets = new Object();
widgetDragInfo.startTime = (new Date()).getTime();
widgetDragInfo.targetWidget = tg;
var movedownName = IE_10_AND_BELOW && $ax.features.supports.windowsMobile ?
$ax.features.eventNames.mouseDownName : $ax.features.eventNames.mouseMoveName;
$ax.event.addEvent(document, movedownName, _dragWidget, true);
$ax.event.addEvent(document, $ax.features.eventNames.mouseUpName, _stopDragWidget, true);
//$ax.legacy.SuppressBubble(event);
};
var _dragWidget = function(event) {
$ax.setjBrowserEvent(jQuery.Event(event));
var x, y;
if(IE_10_AND_BELOW) {
x = window.event.clientX + window.document.documentElement.scrollLeft + window.document.body.scrollLeft;
y = window.event.clientY + window.document.documentElement.scrollTop + window.document.body.scrollTop;
} else {
if(event.changedTouches) {
x = event.changedTouches[0].pageX;
y = event.changedTouches[0].pageY;
//allow scroll (defaults) if only swipe events have cases and delta x is less than 5px and not blocking scrolling
var deltaX = x - widgetDragInfo.currentX;
var target = window.document.getElementById(widgetDragInfo.widgetId);
if($ax.event.hasSyntheticEvent(widgetDragInfo.widgetId, "onDrag") || $ax.event.hasSyntheticEvent(widgetDragInfo.widgetId, "onSwipeUp") ||
$ax.event.hasSyntheticEvent(widgetDragInfo.widgetId, "onSwipeDown") || (deltaX * deltaX) > 25
|| ($ax.document.configuration.preventScroll && $ax.legacy.GetScrollable(target) == window.document.body)) {
event.preventDefault();
}
} else {
x = event.pageX;
y = event.pageY;
}
}
widgetDragInfo.xDelta = x - widgetDragInfo.currentX;
widgetDragInfo.yDelta = y - widgetDragInfo.currentY;
widgetDragInfo.lastX = widgetDragInfo.currentX;
widgetDragInfo.lastY = widgetDragInfo.currentY;
widgetDragInfo.currentX = x;
widgetDragInfo.currentY = y;
widgetDragInfo.currentTime = (new Date()).getTime();
// $ax.legacy.SuppressBubble(event);
if(!widgetDragInfo.hasDragged) {
widgetDragInfo.hasDragged = true;
$ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onDragStart");
//only update to move cursor is we are moving objects
if($ax.event.hasSyntheticEvent(widgetDragInfo.widgetId, "onDrag")) {
widgetDragInfo.cursorChanged = true;
widgetDragInfo.oldBodyCursor = window.document.body.style.cursor;
window.document.body.style.cursor = 'move';
var widget = window.document.getElementById(widgetDragInfo.widgetId);
widgetDragInfo.oldCursor = widget.style.cursor;
widget.style.cursor = 'move';
//need to do this in order to change the cursor under nice scroll
var niceScrollContainer = $ax.adaptive.getNiceScrollContainer(widget);
if(niceScrollContainer) {
widgetDragInfo.oldNiceScrollContainerCursor = niceScrollContainer.style.cursor;
niceScrollContainer.style.cursor = 'move';
}
}
}
$ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onDrag");
};
var _suppressClickAfterDrag = function(event) {
_removeSuppressEvents();
$ax.legacy.SuppressBubble(event);
};
var _removeSuppressEvents = function () {
if(IE_10_AND_BELOW) {
$ax.event.removeEvent(event.srcElement, 'click', _suppressClickAfterDrag, undefined, true);
$ax.event.removeEvent(widgetDragInfo.targetWidget, 'mousemove', _removeSuppressEvents, undefined, true);
} else {
$ax.event.removeEvent(document, "click", _suppressClickAfterDrag, true);
$ax.event.removeEvent(document, 'mousemove', _removeSuppressEvents, true);
}
};
var _stopDragWidget = function(event) {
$ax.setjBrowserEvent(jQuery.Event(event));
var tg;
var movedownName = IE_10_AND_BELOW && $ax.features.supports.windowsMobile ?
$ax.features.eventNames.mouseDownName : $ax.features.eventNames.mouseMoveName;
$ax.event.removeEvent(document, movedownName, _dragWidget, true);
$ax.event.removeEvent(document, $ax.features.eventNames.mouseUpName, _stopDragWidget, true);
tg = IE_10_AND_BELOW ? window.event.srcElement : event.target;
if(widgetDragInfo.hasDragged) {
widgetDragInfo.currentTime = (new Date()).getTime();
$ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onDragDrop");
if($ax.globalVariableProvider.getVariableValue('totaldragx') < -30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
$ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeLeft");
}
if($ax.globalVariableProvider.getVariableValue('totaldragx') > 30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
$ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeRight");
}
var totalDragY = $ax.globalVariableProvider.getVariableValue('totaldragy');
if(totalDragY < -30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
$ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeUp");
}
if(totalDragY > 30 && $ax.globalVariableProvider.getVariableValue('dragtime') < 1000) {
$ax.event.raiseSyntheticEvent(widgetDragInfo.widgetId, "onSwipeDown");
}
if(widgetDragInfo.cursorChanged) {
window.document.body.style.cursor = widgetDragInfo.oldBodyCursor;
var widget = window.document.getElementById(widgetDragInfo.widgetId);
// It may be null if OnDragDrop filtered out the widget
if(widget != null) widget.style.cursor = widgetDragInfo.oldCursor;
//we don't seems need to reset nicescroll cursor on container, nicescroll seems updates its cursor
// if(widgetDragInfo.oldNiceScrollContainerCursor != undefined) {
// var niceScrollContainer = $ax.adaptive.getNiceScrollContainer(widget);
// if(niceScrollContainer) niceScrollContainer.style.cursor = widgetDragInfo.oldNiceScrollContainerCursor;
// widgetDragInfo.oldNiceScrollContainerCursor = undefined;
// }
widgetDragInfo.cursorChanged = undefined;
}
if(widgetDragInfo.targetWidget == tg && !event.changedTouches) {
// suppress the click after the drag on desktop browsers
if(IE_10_AND_BELOW && widgetDragInfo.targetWidget) {
$ax.event.addEvent(widgetDragInfo.targetWidget, 'click', _suppressClickAfterDrag, true, true);
$ax.event.addEvent(widgetDragInfo.targetWidget, "onmousemove", _removeSuppressEvents, true, true);
} else {
$ax.event.addEvent(document, "click", _suppressClickAfterDrag, true);
$ax.event.addEvent(document, "mousemove", _removeSuppressEvents, true);
}
}
}
widgetDragInfo.hasDragged = false;
widgetDragInfo.movedWidgets = new Object();
widgetDragInfo.started = false;
return false;
};
$ax.drag.GetDragX = function() {
if(widgetDragInfo.hasDragged) return widgetDragInfo.xDelta;
return 0;
};
$ax.drag.GetDragY = function() {
if(widgetDragInfo.hasDragged) return widgetDragInfo.yDelta;
return 0;
};
$ax.drag.GetTotalDragX = function() {
if(widgetDragInfo.hasDragged) return widgetDragInfo.currentX - widgetDragInfo.cursorStartX;
return 0;
};
$ax.drag.GetTotalDragY = function() {
if(widgetDragInfo.hasDragged) return widgetDragInfo.currentY - widgetDragInfo.cursorStartY;
return 0;
};
$ax.drag.GetDragTime = function() {
if(widgetDragInfo.hasDragged) return widgetDragInfo.currentTime - widgetDragInfo.startTime;
return 600000;
};
$ax.drag.LogMovedWidgetForDrag = function (id, dragInfo) {
dragInfo = dragInfo || widgetDragInfo;
if(dragInfo.hasDragged) {
var containerIndex = id.indexOf('_container');
if(containerIndex != -1) id = id.substring(0, containerIndex);
// If state or other non-widget id, this should not be dragged, and should exit out to avoid exceptions.
if(!$obj(id)) return;
var query = $ax('#' + id);
//var x = query.left();
//var y = query.top();
var viewportLocation = query.viewportLocation();
var x = viewportLocation.left;
var y = viewportLocation.top;
var movedWidgets = dragInfo.movedWidgets;
if(!movedWidgets[id]) {
movedWidgets[id] = new Location(x, y);
}
}
};
var Location = function(x, y) {
this.x = x;
this.y = y;
};
$ax.drag.location = Location;
var Rectangle = $ax.drag.Rectangle = function(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.right = x + width;
this.bottom = y + height;
};
Rectangle.prototype.IntersectsWith = function(rect) {
if(this.Invalid()) return false;
if(rect.length) {
for(var i = 0; i < rect.length; i++) if(!rect[i].Invalid && this.IntersectsWith(rect[i])) return true;
return false;
}
if(rect.Invalid()) return false;
return this.x < rect.right && this.right > rect.x && this.y < rect.bottom && this.bottom > rect.y;
};
Rectangle.prototype.Invalid = function() {
return this.x == -1 && this.y == -1 && this.width == -1 && this.height == -1;
};
Rectangle.prototype.Move = function(x, y) {
return new Rectangle(x, y, this.width, this.height);
};
});