This repository has been archived by the owner on Nov 29, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTiDrop.js
122 lines (110 loc) · 3.7 KB
/
TiDrop.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
var TiDrop = {
touching: false,
position: {
elementYStart: 0,
elementXStart: 0,
yStart: 0,
xStart: 0,
yCurrent: 0,
xCurrent: 0
},
init: function(_element, _container, _callback) {
_element.addEventListener("touchstart", function(e) { TiDrop.touchHandler(e); }, false );
_element.addEventListener("touchmove", function(e) { TiDrop.touchHandler(e); }, false );
_element.addEventListener("touchend", function(e) { TiDrop.touchHandler(e); }, false );
_element.addEventListener("touchcancel", function(e) { TiDrop.touchHandler(e); }, false );
if(_container) {
this.container = _container;
}
if(_callback && typeof _callback == "function") {
this.callback = _callback;
} else {
this.callback = false;
}
},
touchHandler: function(_event) {
if(_event.type == "touchstart") {
if(!this.touching) {
this.touching = true;
this.element = _event.source;
switch(Ti.UI.orientation){
case Ti.UI.PORTRAIT:
case Ti.UI.UPSIDE_PORTRAIT:
this.position.elementYStart = this.element.top;
this.position.elementXStart = this.element.left;
break;
case Ti.UI.LANDSCAPE_RIGHT:
case Ti.UI.LANDSCAPE_LEFT:
this.position.elementYStart = this.element.left;
this.position.elementXStart = this.element.top;
break;
}
this.position.yStart = parseInt(_event.globalPoint.y, 10);
this.position.xStart = parseInt(_event.globalPoint.x, 10);
}
} else if(_event.type == "touchmove") {
if(this.touching){
this.position.yCurrent = parseInt(_event.globalPoint.y, 10);
this.position.xCurrent = parseInt(_event.globalPoint.x, 10);
var yDistance = this.position.yCurrent - this.position.yStart;
var xDistance = this.position.xCurrent - this.position.xStart;
switch(Ti.UI.orientation) {
case Ti.UI.PORTRAIT:
_event.source.top = this.position.elementYStart + yDistance;
_event.source.left = this.position.elementXStart + xDistance;
break;
case Ti.UI.UPSIDE_PORTRAIT:
_event.source.top = this.position.elementYStart - yDistance;
_event.source.left = this.position.elementXStart - xDistance;
break;
case Ti.UI.LANDSCAPE_RIGHT:
_event.source.left = this.position.elementYStart + yDistance;
_event.source.top = this.position.elementXStart - xDistance;
break;
case Ti.UI.LANDSCAPE_LEFT:
_event.source.left = this.position.elementYStart - yDistance;
_event.source.top = this.position.elementXStart + xDistance;
break;
}
}
} else if(_event.type == "touchend" || _event.type == "touchcancel") {
if(this.callback) {
var _data = {
source: this.element,
position: {
y: this.position.yCurrent,
x: this.position.xCurrent
},
contained: this.withinContainer()
};
this.callback(_data);
}
this.element = null;
this.touching = false;
this.position = {
elementYStart: 0,
elementXStart: 0,
yStart: 0,
xStart: 0,
yCurrent: 0,
xCurrent: 0
};
} else {
Ti.API.info("TiDrop: Not a valid touch event");
}
},
withinContainer: function() {
var contained = true;
if(this.container) {
if(this.element.left < this.container.left) { contained = false; }
if(this.element.left > this.container.left + this.container.width) { contained = false; }
if(this.element.left + this.element.width > this.container.left + this.container.width) { contained = false; }
if(this.element.top < this.container.top) { contained = false; }
if(this.element.top > this.container.top + this.container.height) { contained = false; }
if(this.element.top + this.element.height > this.container.top + this.container.height) { contained = false; }
} else {
contained = false;
}
return contained;
}
};