File: ghostdrag.js

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (191 lines) | stat: -rw-r--r-- 5,348 bytes parent folder | download | duplicates (2)
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
/*
 *  $Id: ghostdrag.js,v 1.13.2.4 2010/04/06 16:46:12 source Exp $
 *
 *  This file is part of the OpenLink Software Ajax Toolkit (OAT) project.
 *
 *  Copyright (C) 2005-2010 OpenLink Software
 *
 *  See LICENSE file for details.
 */
/*
	new OAT.GhostDrag()
	GhostDrag.addSource(elm,process,callback); -- we will callback(target,x,y) when this elm is successfully dragged
	GhostDrag.delSource(elm);
	GhostDrag.clearSources();
	GhostDrag.addTarget(elm, [customTest], [isLast]);
	GhostDrag.delTarget(elm);
	GhostDrag.clearTargets();
*/

OAT.GhostDragData = {
	lock:false,

	up:function(event) {
		if (!OAT.GhostDragData.lock) return;
		var elm = OAT.GhostDragData.lock; /* moving ghost */
		var obj = elm.object;
		if (obj.pending) {
			obj.pending = 0;
			OAT.GhostDragData.lock = false;
			return;
		}
		OAT.GhostDragData.lock = false;
		var exact = OAT.Event.position(event);
		var x = exact[0];
		var y = exact[1];
		var ok = 0;
		for (var i=0;i<obj.targets.length;i++) {
			var t = obj.targets[i];
			var test = (t[1] ? t[1](x,y) : OAT.GhostDragData.pos(t[0],x,y));
			if (!ok && test) { /* only the first gets executed! */
				ok = 1;
				obj.callback(t[0],x,y);
			}
		}
		if (ok) {
			/* mouseup at correct place - remove element */
			OAT.Dom.unlink(elm);
			OAT.MSG.send(obj,"GD_END",elm);
		} else {
			/* mouseup at wrong place - let's animate it back */
			OAT.MSG.send(obj,"GD_ABORT",elm);
			obj.onFail();
			var coords = OAT.Dom.position(obj.originalElement);
			var x = coords[0];
			var y = coords[1];
			var sf = function() { OAT.Dom.unlink(elm); }
			var anim = new OAT.AnimationPosition(elm,{speed:10,delay:10,left:x,top:y});
			OAT.MSG.attach(anim.animation,"ANIMATION_STOP",sf);
			anim.start();
		}
	}, /* OAT.GhostDragData.up() */

	move:function(event) {
		if (!OAT.GhostDragData.lock) return;
		OAT.Event.prevent(event);
		var elm = OAT.GhostDragData.lock;
		var obj = elm.object;
		if (obj.pending) {
			/* create the duplicate */
			document.body.appendChild(elm);
			elm.style.zIndex = 2000;
			if (obj.process) { obj.process(elm); }
			obj.pending = 0;
			OAT.MSG.send(obj,"GD_START",elm);
		}
		/*
			selection sukz. detect it and remove!
		*/
		OAT.Dom.removeSelection();

		/* ok, now move */
		var offs_x = event.clientX - elm.mouse_x;
		var offs_y = event.clientY - elm.mouse_y;
		var new_x = parseInt(OAT.Style.get(elm,"left")) + offs_x;
		var new_y = parseInt(OAT.Style.get(elm,"top")) + offs_y;
		elm.style.left = new_x + "px";
		elm.style.top = new_y + "px";
		elm.mouse_x = event.clientX;
		elm.mouse_y = event.clientY;
	}, /* OAT.GhostDragData.move(); */

	pos:function(elm,x_,y_) {
		/* is [x_,y_] inside elm ? */
		if (!elm) return 0;
		if (elm.style.display.toLowerCase() == "none") return 0;
		var coords = OAT.Dom.position(elm);
		var x = coords[0]-2;
		var y = coords[1]-2;
		var w = parseInt(elm.offsetWidth)+2;
		var h = parseInt(elm.offsetHeight)+2;
		return (x_ >= x && x_ <= x+w && y_ >= y && y_ <= y+h);
	} /* OAT.GhostDragData.pos(); */
}

OAT.GhostDrag = function() {
	var self = this;
	this.onFail = function(){};
	this.sources = [];
	this.processes = [];
	this.callbacks = [];
	this.targets = [];
	this.pending = 0; /* mouse is down, waiting for move to appear */

	this.addSource = function(node,process,callback) {
		var elm = $(node);
		self.sources.push(elm);
		self.processes.push(process);
		self.callbacks.push(callback);
		var cica = true;
		var ref = function(event) {
			OAT.Event.prevent(event);
			var index = self.sources.indexOf(elm);
			if (index == -1) return;
			var x = event.clientX;
			var y = event.clientY;
			self.startDrag(self.sources[index],self.processes[index],self.callbacks[index],x,y);
		}
		OAT.Event.attach(elm,"mousedown",ref);
	}

	this.delSource = function(node) {
		var elm = $(node);
		var index = self.sources.indexOf(elm);
		if (index == -1) { return; }
		self.sources.splice(index,1);
		self.processes.splice(index,1);
		self.callbacks.splice(index,1);
	}

	this.clearSources = function() {
		self.sources = [];
		self.processes = [];
		self.callbacks = [];
	}

	this.addTarget = function(node,customTest,isLast) {
		var elm = $(node);
		var newTriple = [elm,customTest,isLast];
		if (self.targets.length && self.targets[self.targets.length-1][2]) {
			/* there is last target */
			self.targets.splice(self.targets.length-1,0,newTriple);
		} else {
			self.targets.push(newTriple);
		}
	}

	this.delTarget = function(node) {
		var elm = $(node);
		var index = -1;
		for (var i=0;i<self.targets.length;i++) {
			if (self.targets[i][0] == elm) { index = i; }
		}
		if (index == -1) { return; }
		self.targets.splice(index,1);
	}

	this.clearTargets = function() {
		self.targets = [];
	}

	this.startDrag = function(elm,process,callback,x,y) {
		if (OAT.GhostDragData.lock) { return; }
		self.pending = 1;
		self.originalElement = elm;
		self.callback = callback;
		var obj = OAT.Dom.create("div",{position:"absolute"});
		self.process = process;
		var coords = OAT.Dom.position(elm);
		obj.style.left = coords[0]+"px";
		obj.style.top = coords[1]+"px";
		OAT.Style.set(obj,{opacity:0.5});
		obj.appendChild(elm.cloneNode(true));
		obj.mouse_x = x;
		obj.mouse_y = y;
		obj.object = self;
		OAT.GhostDragData.lock = obj;
	}
}

OAT.Event.attach(document,"mousemove",OAT.GhostDragData.move);
OAT.Event.attach(document,"mouseup",OAT.GhostDragData.up);