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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/**
* Javascript Drag&Drop - Modified for ownCloud
*
* @author Robin Appelman
* @copyright 2010 Robin Appelman icewind1991@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
position=function(x,y){
if(x)this.x=x;
if(y)this.y=y;
return this;
}
position.prototype={
x:0,
y:0,
add:function(pos2){
return new position(this.x+pos2.x,this.y+pos2.y);
},
substract:function(pos2){
return new position(this.x-pos2.x,this.y-pos2.y);
},toString:function(){
return 'x:'+this.x+',y:'+this.y;
},inside:function(pos2){
return Math.abs(this.x)<Math.abs(pos2.x) && Math.abs(this.y)<Math.abs(pos2.y) && Math.sign(this.x)==Math.sign(pos2.x) && Math.sign(this.y)==Math.sign(pos2.y);
},outside:function(pos2){
return !this.inside(pos2);
}
}
Node.prototype.drag=new Object
/**
* is the node dragable
*/
Node.prototype.drag.dragable=false;
/**
* Are we currently dragging the node
*/
Node.prototype.drag.active=false;
/**
* Create a clone to drag around
*/
Node.prototype.drag.clone=true;
/**
* The node we (visually drag around)
*/
Node.prototype.drag.node=false;
/**
* can we drop nodes on this
*/
Node.prototype.drag.isDropTarget=false;
/**
* our current drop target
*/
Node.prototype.drag.dropTarget=null;
/**
* can we drop this node now
*/
Node.prototype.drag.dropable=false;
/**
* function called when we are being dropped on a node
* @return bool
*/
Node.prototype.drag.onDrop=function(node){};
/**
* function called when an node is dropped on us
* @param Node node
* @return bool
*/
Node.prototype.drag.onDropOn=function(node){};
/**
* where did we start the drag
*/
Node.prototype.drag.startPosition=new position();
/**
* where are we now
*/
Node.prototype.drag.position=new position();
/**
* how big are we
*/
Node.prototype.drag.size=new position();
/**
* where is the mouse
*/
Node.prototype.drag.mousePosition=new position();
/**
* where is the mouse relative to our node
*/
Node.prototype.drag.mouseOffset=new position();
document.drag=new Object();
/**
* is there currently something dragged
*/
document.drag.active=false;
/**
* what is currently being dragged
*/
document.drag.node=null;
document.drag.dropTargets=Array();
/**
* start the dragging. (onmousedown)
* @param Event event
*/
Node.prototype.drag.start=function(event){
if(!event)var event=window.event;
if(!this.drag.active && this.drag.dragable){
document.drag.active=true;
document.drag.node=this;
this.drag.active=true;
this.drag.position=this.getPosition();
this.drag.startPosition=this.getPosition();
this.drag.mousePosition=getMousePosition(event);
this.drag.mouseOffset=this.drag.mousePosition.substract(this.drag.position);
}
}
/**
* update the dragging. (onmousemove)
* @param Event event
*/
Node.prototype.drag.update=function(event){
if(!event)var event=window.event;
if(this.drag.active && this.drag.dragable){
this.drag.mousePosition=getMousePosition(event);
this.drag.position=this.drag.mousePosition.substract(this.drag.mouseOffset);
if(this.drag.clone && !this.drag.node){
this.drag.node=this.cloneNode(true);
this.drag.node.className='dragClone';
if(this.drag.node.hasAttribute('id')){
this.drag.node.setAttribute('id',this.drag.node.getAttribute('id')+'_dragClone');
}
document.getElementsByTagName('body').item(0).appendChild(this.drag.node);
}else if(!this.drag.node){
this.drag.node=this;
this.drag.node.style.position='absolute';
}
this.drag.node.style.left=this.drag.position.x+'px';
this.drag.node.style.top=this.drag.position.y+'px';
}
return true;
}
/**
* stop the dragging/drop. (onmouseup)
* @param Event event
* @return bool
*/
Node.prototype.drag.stop=function(event){
if(!event)var event=window.event;
if(this.drag.active && this.drag.dragable){
this.drag.active=false;
this.drag.mousePosition=getMousePosition(event);
this.drag.position=this.drag.mousePosition.substract(this.drag.mouseOffset);
if(this.drag.node){
this.drag.node.style.left=this.drag.position.x;
this.drag.node.style.top=this.drag.position.y;
}
var target;
this.drag.dropTarget=null;
this.drag.dropable=false;
for(var i=0;i<document.drag.dropTargets.length;i++){
target=document.drag.dropTargets[i];
target.drag.checkDropTarget.call(target,event);
}
if(this.drag.dropable && this.drag.dropTarget){
if(this.drag.onDrop){
this.drag.onDrop.call(this,event,this.drag.dropTarget);
this.triggerEvent.call(this,'ondrop',event,this.drag.dropTarget);
}
if(this.drag.dropTarget.drag.onDropOn){
this.drag.dropTarget.drag.onDropOn.call(this.drag.dropTarget,event,this);
this.drag.dropTarget.triggerEvent.call(this.drag.dropTarget,'ondropon',event,this);
}
}
if(this.drag.clone && this.drag.node){
this.drag.node.parentNode.removeChild(this.drag.node);
this.drag.node=null;
}
document.drag.active=false;
document.drag.node=null;
}
}
/**
* is there currently something being dragged over us
* @param Event event
*/
Node.prototype.drag.checkDropTarget=function(event){
if(this.drag.isDropTarget & document.drag.active){
mousePos=getMousePosition(event);
this.drag.position=this.getPosition();
this.drag.size=this.getSize(true);
var offSet=mousePos.substract(this.drag.position);
if(offSet.inside(this.drag.size)){
document.drag.node.drag.dropTarget=this;
document.drag.node.drag.dropable=true;
setDebug('ontarget');
}
}
}
/**
* called when the mouse is leaving a drop target
* @param Event event
*/
Node.prototype.drag.leaveDropTarget=function(event){
if(this.drag.isDropTarget & document.drag.active){
document.drag.node.drag.dropTarget=null;
document.drag.node.drag.dropable=false;
setDebug('offtarget');
}
}
/**
* initiate the node as drop target
*/
Node.prototype.drag.initDropTarget=function(){
this.drag.isDropTarget=true;
document.drag.dropTargets.push(this);
}
Node.prototype.makeDropTarget=function(){
this.drag.initDropTarget.call(this);
}
/**
* initiate the node as draggable
*/
Node.prototype.drag.init=function(){
this.drag.dragable=true;
this.drag.size.x=this.getStyle('width');
this.drag.size.y=this.getStyle('height');
this.addEvent('onmousedown',new callBack(this.drag.start,this));
}
Node.prototype.makeDraggable=function(){
this.drag.init.call(this);
}
/**
* update the dragging. (onmousemove)
* @param Event event
*/
document.drag.update=function(event){
var target;
if(document.drag.active && document.drag.node){
document.drag.node.drag.update.call(document.drag.node,event);
}
}
/**
* update the dragging. (onmousemove)
* @param Event event
*/
document.drag.stop=function(event){
if(document.drag.active && document.drag.node){
document.drag.node.drag.stop.call(document.drag.node,event);
}
}
document.events.add(document,'onmousemove',document.drag.update);
document.events.add(document,'onmouseup',document.drag.stop);
function getMousePosition(event){
var pos=new position();
if(!event)var event = window.event;
if(event.pageX||event.pageY){
pos.x=event.pageX;
pos.y=event.pageY;
}
else if(event.clientX||event.clientY){
pos.x=event.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
pos.y=event.clientY+document.body.scrollTop+document.documentElement.scrollTop;
}
return pos;
}
/**
* get our position
**/
Node.prototype.getPosition=function(){
var pos=new position();
element=this;
do{
pos.y+=element.offsetTop;
pos.x+=element.offsetLeft;
}while(element=element.offsetParent);
return pos;
}
/**
* get our size
* @param bool full (also include padding and border)
**/
Node.prototype.getSize=function(full){
var pos=new position();
pos.y= parseInt(this.getStyle('height'));
pos.x= parseInt(this.getStyle('width'));
if(full){
var extraY=['border-size','padding-top','padding-bottom','border-size'];
var extraX=['border-size','padding-left','padding-right','border-size'];
var tmp;
for(var i=0;i<extraY.length;i++){
tmp=parseInt(this.getStyle(extraY[i]));
if(tmp){
pos.y+=tmp;
}
}
for(var i=0;i<extraX.length;i++){
tmp=parseInt(this.getStyle(extraX[i]));
if(tmp){
pos.x+=tmp;
}
}
}
return pos;
}
function mouseTest(event){
var pos=getMousePosition(event);
setDebug(pos.toString());
}
function testDrag(){
var node=document.getElementById('debug');
// document.addEvent('onclick',getOffSet,[node]);
node.makeDropTarget();
}
function getOffSet(node,event){
var nodePos=node.getPosition();
var mousePos=getMousePosition(event);
return mousePos.substract(nodePos);
}
// OC_onload.add(testDrag);
|