-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdroppabilly.js
128 lines (114 loc) · 4.59 KB
/
droppabilly.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
// droppabilly.js
// Version: 0.1.0
// Author: Jeff Chen; github=@chienhungchen twitter=@jeffchen330
// Plugin for creating droppables to use with draggabilly.js
// This is suppose to be a barebones alternative (when coupled with draggabilly.js) to jQuery UI + touchpunch
(function(root){
"use strict";
//Methods
var overlap = function(a, b) {
var p1 = getPositions(a),
p2 = getPositions(b);
return comparePositions(p1[0], p2[0]) && comparePositions(p1[1], p2[1]);
},
getPositions = function(el) {
var pos = offset(el),
width = el.offsetWidth,
height = el.offsetHeight;
return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ];
},
comparePositions = function(p1, p2) {
var r1, r2;
r1 = p1[0] < p2[0] ? p1 : p2;
r2 = p1[0] < p2[0] ? p2 : p1;
return r1[1] > r2[0] || r1[0] === r2[0];
},
getDefined = function(f) {
return (f === undefined || f === null) ? function() {} : f;
},
offset = function(el) {
//Taken from http://cvmlrobotics.blogspot.co.at/2013/03/angularjs-get-element-offset-position.html
var _x = 0;
var _y = 0;
var body = document.documentElement || document.body;
var scrollX = window.pageXOffset || body.scrollLeft;
var scrollY = window.pageYOffset || body.scrollTop;
_x = el.getBoundingClientRect().left + scrollX;
_y = el.getBoundingClientRect().top + scrollY;
return { left: _x, top:_y };
};
//Methods End
//Droppabilly Start
root.Droppabilly = function(el, params) {
var curDroppabilly = el,
flags = {
wasOver : false,
hasDropped : false,
hasReleased : true,
invokedOverFunc : false,
invokedOutFunc : false,
invokedDropFunc : false
},
draggies = document.getElementsByClassName(params.dragstersClassName);
function dragMove(event, droppabilly, draggabilly, flags) {
//event: over
if(overlap(droppabilly, draggabilly)) {
flags.wasOver = true;
flags.invokedOutFunc = false;
if(!flags.hasDropped && !flags.invokedOverFunc) {
if(params.over(droppabilly, draggabilly)) {
flags.invokedOverFunc = true;
}
}
}
//event: out
else if(flags.wasOver && !flags.hasReleased) {
if(!flags.invokedOutFunc) {
if(params.out(droppabilly, draggabilly)) {
flags.invokedOutFunc = true;
}
}
flags.invokedOverFunc = false;
}
};
function dragEnd(event, droppabilly, draggabilly, flags) {
//event: drop on drop zone
if(overlap(droppabilly, draggabilly)) {
if(!flags.invokedDropFunc) {
params.drop(droppabilly, draggabilly);
flags.invokedDropFunc = true;
}
flags.hasDropped = true;
}
flags.hasReleased = true;
flags.wasOver = false;
flags.invokedOverFunc = false;
flags.invokedOutFunc = false;
flags.invokedDropFunc = false;
};
function dragStart(event, droppabilly, draggabilly, flags) {
flags.hasDropped = false;
flags.hasReleased = false;
};
for(var i = 0; i < draggies.length; i++) {
draggies[i].onmousemove = function(event) {
dragMove(event, curDroppabilly, this, flags);
};
draggies[i].addEventListener("touchmove", function(event) {
dragMove(event, curDroppabilly, this, flags);
}, false);
draggies[i].onmouseup = function(event) {
dragEnd(event, curDroppabilly, this, flags);
};
draggies[i].addEventListener("touchend", function(event) {
dragEnd(event, curDroppabilly, this, flags);
}, false);
draggies[i].onmousedown = function(event) {
dragStart(event, curDroppabilly, this, flags);
};
draggies[i].addEventListener("touchstart", function(event) {
dragStart(event, curDroppabilly, this, flags);
}, false);
}
};
})(this);