Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.dnd.HtmlDragManager");
12
dojo.require("dojo.dnd.DragAndDrop");
13
dojo.require("dojo.event.*");
14
dojo.require("dojo.lang.array");
15
dojo.require("dojo.html.common");
16
dojo.require("dojo.html.layout");
17
dojo.declare("dojo.dnd.HtmlDragManager", dojo.dnd.DragManager, {disabled:false, nestedTargets:false, mouseDownTimer:null, dsCounter:0, dsPrefix:"dojoDragSource", dropTargetDimensions:[], currentDropTarget:null, previousDropTarget:null, _dragTriggered:false, selectedSources:[], dragObjects:[], dragSources:[], dropTargets:[], currentX:null, currentY:null, lastX:null, lastY:null, mouseDownX:null, mouseDownY:null, threshold:7, dropAcceptable:false, cancelEvent:function (e) {
18
	e.stopPropagation();
19
	e.preventDefault();
20
}, registerDragSource:function (ds) {
21
	if (ds["domNode"]) {
22
		var dp = this.dsPrefix;
23
		var dpIdx = dp + "Idx_" + (this.dsCounter++);
24
		ds.dragSourceId = dpIdx;
25
		this.dragSources[dpIdx] = ds;
26
		ds.domNode.setAttribute(dp, dpIdx);
27
		if (dojo.render.html.ie) {
28
			dojo.event.browser.addListener(ds.domNode, "ondragstart", this.cancelEvent);
29
		}
30
	}
31
}, unregisterDragSource:function (ds) {
32
	if (ds["domNode"]) {
33
		var dp = this.dsPrefix;
34
		var dpIdx = ds.dragSourceId;
35
		delete ds.dragSourceId;
36
		delete this.dragSources[dpIdx];
37
		ds.domNode.setAttribute(dp, null);
38
		if (dojo.render.html.ie) {
39
			dojo.event.browser.removeListener(ds.domNode, "ondragstart", this.cancelEvent);
40
		}
41
	}
42
}, registerDropTarget:function (dt) {
43
	this.dropTargets.push(dt);
44
}, unregisterDropTarget:function (dt) {
45
	var index = dojo.lang.find(this.dropTargets, dt, true);
46
	if (index >= 0) {
47
		this.dropTargets.splice(index, 1);
48
	}
49
}, getDragSource:function (e) {
50
	var tn = e.target;
51
	if (tn === dojo.body()) {
52
		return;
53
	}
54
	var ta = dojo.html.getAttribute(tn, this.dsPrefix);
55
	while ((!ta) && (tn)) {
56
		tn = tn.parentNode;
57
		if ((!tn) || (tn === dojo.body())) {
58
			return;
59
		}
60
		ta = dojo.html.getAttribute(tn, this.dsPrefix);
61
	}
62
	return this.dragSources[ta];
63
}, onKeyDown:function (e) {
64
}, onMouseDown:function (e) {
65
	if (this.disabled) {
66
		return;
67
	}
68
	if (dojo.render.html.ie) {
69
		if (e.button != 1) {
70
			return;
71
		}
72
	} else {
73
		if (e.which != 1) {
74
			return;
75
		}
76
	}
77
	var target = e.target.nodeType == dojo.html.TEXT_NODE ? e.target.parentNode : e.target;
78
	if (dojo.html.isTag(target, "button", "textarea", "input", "select", "option")) {
79
		return;
80
	}
81
	var ds = this.getDragSource(e);
82
	if (!ds) {
83
		return;
84
	}
85
	if (!dojo.lang.inArray(this.selectedSources, ds)) {
86
		this.selectedSources.push(ds);
87
		ds.onSelected();
88
	}
89
	this.mouseDownX = e.pageX;
90
	this.mouseDownY = e.pageY;
91
	e.preventDefault();
92
	dojo.event.connect(document, "onmousemove", this, "onMouseMove");
93
}, onMouseUp:function (e, cancel) {
94
	if (this.selectedSources.length == 0) {
95
		return;
96
	}
97
	this.mouseDownX = null;
98
	this.mouseDownY = null;
99
	this._dragTriggered = false;
100
	e.dragSource = this.dragSource;
101
	if ((!e.shiftKey) && (!e.ctrlKey)) {
102
		if (this.currentDropTarget) {
103
			this.currentDropTarget.onDropStart();
104
		}
105
		dojo.lang.forEach(this.dragObjects, function (tempDragObj) {
106
			var ret = null;
107
			if (!tempDragObj) {
108
				return;
109
			}
110
			if (this.currentDropTarget) {
111
				e.dragObject = tempDragObj;
112
				var ce = this.currentDropTarget.domNode.childNodes;
113
				if (ce.length > 0) {
114
					e.dropTarget = ce[0];
115
					while (e.dropTarget == tempDragObj.domNode) {
116
						e.dropTarget = e.dropTarget.nextSibling;
117
					}
118
				} else {
119
					e.dropTarget = this.currentDropTarget.domNode;
120
				}
121
				if (this.dropAcceptable) {
122
					ret = this.currentDropTarget.onDrop(e);
123
				} else {
124
					this.currentDropTarget.onDragOut(e);
125
				}
126
			}
127
			e.dragStatus = this.dropAcceptable && ret ? "dropSuccess" : "dropFailure";
128
			dojo.lang.delayThese([function () {
129
				try {
130
					tempDragObj.dragSource.onDragEnd(e);
131
				}
132
				catch (err) {
133
					var ecopy = {};
134
					for (var i in e) {
135
						if (i == "type") {
136
							ecopy.type = "mouseup";
137
							continue;
138
						}
139
						ecopy[i] = e[i];
140
					}
141
					tempDragObj.dragSource.onDragEnd(ecopy);
142
				}
143
			}, function () {
144
				tempDragObj.onDragEnd(e);
145
			}]);
146
		}, this);
147
		this.selectedSources = [];
148
		this.dragObjects = [];
149
		this.dragSource = null;
150
		if (this.currentDropTarget) {
151
			this.currentDropTarget.onDropEnd();
152
		}
153
	} else {
154
	}
155
	dojo.event.disconnect(document, "onmousemove", this, "onMouseMove");
156
	this.currentDropTarget = null;
157
}, onScroll:function () {
158
	for (var i = 0; i < this.dragObjects.length; i++) {
159
		if (this.dragObjects[i].updateDragOffset) {
160
			this.dragObjects[i].updateDragOffset();
161
		}
162
	}
163
	if (this.dragObjects.length) {
164
		this.cacheTargetLocations();
165
	}
166
}, _dragStartDistance:function (x, y) {
167
	if ((!this.mouseDownX) || (!this.mouseDownX)) {
168
		return;
169
	}
170
	var dx = Math.abs(x - this.mouseDownX);
171
	var dx2 = dx * dx;
172
	var dy = Math.abs(y - this.mouseDownY);
173
	var dy2 = dy * dy;
174
	return parseInt(Math.sqrt(dx2 + dy2), 10);
175
}, cacheTargetLocations:function () {
176
	dojo.profile.start("cacheTargetLocations");
177
	this.dropTargetDimensions = [];
178
	dojo.lang.forEach(this.dropTargets, function (tempTarget) {
179
		var tn = tempTarget.domNode;
180
		if (!tn || !tempTarget.accepts([this.dragSource])) {
181
			return;
182
		}
183
		var abs = dojo.html.getAbsolutePosition(tn, true);
184
		var bb = dojo.html.getBorderBox(tn);
185
		this.dropTargetDimensions.push([[abs.x, abs.y], [abs.x + bb.width, abs.y + bb.height], tempTarget]);
186
	}, this);
187
	dojo.profile.end("cacheTargetLocations");
188
}, onMouseMove:function (e) {
189
	if ((dojo.render.html.ie) && (e.button != 1)) {
190
		this.currentDropTarget = null;
191
		this.onMouseUp(e, true);
192
		return;
193
	}
194
	if ((this.selectedSources.length) && (!this.dragObjects.length)) {
195
		var dx;
196
		var dy;
197
		if (!this._dragTriggered) {
198
			this._dragTriggered = (this._dragStartDistance(e.pageX, e.pageY) > this.threshold);
199
			if (!this._dragTriggered) {
200
				return;
201
			}
202
			dx = e.pageX - this.mouseDownX;
203
			dy = e.pageY - this.mouseDownY;
204
		}
205
		this.dragSource = this.selectedSources[0];
206
		dojo.lang.forEach(this.selectedSources, function (tempSource) {
207
			if (!tempSource) {
208
				return;
209
			}
210
			var tdo = tempSource.onDragStart(e);
211
			if (tdo) {
212
				tdo.onDragStart(e);
213
				tdo.dragOffset.y += dy;
214
				tdo.dragOffset.x += dx;
215
				tdo.dragSource = tempSource;
216
				this.dragObjects.push(tdo);
217
			}
218
		}, this);
219
		this.previousDropTarget = null;
220
		this.cacheTargetLocations();
221
	}
222
	dojo.lang.forEach(this.dragObjects, function (dragObj) {
223
		if (dragObj) {
224
			dragObj.onDragMove(e);
225
		}
226
	});
227
	if (this.currentDropTarget) {
228
		var c = dojo.html.toCoordinateObject(this.currentDropTarget.domNode, true);
229
		var dtp = [[c.x, c.y], [c.x + c.width, c.y + c.height]];
230
	}
231
	if ((!this.nestedTargets) && (dtp) && (this.isInsideBox(e, dtp))) {
232
		if (this.dropAcceptable) {
233
			this.currentDropTarget.onDragMove(e, this.dragObjects);
234
		}
235
	} else {
236
		var bestBox = this.findBestTarget(e);
237
		if (bestBox.target === null) {
238
			if (this.currentDropTarget) {
239
				this.currentDropTarget.onDragOut(e);
240
				this.previousDropTarget = this.currentDropTarget;
241
				this.currentDropTarget = null;
242
			}
243
			this.dropAcceptable = false;
244
			return;
245
		}
246
		if (this.currentDropTarget !== bestBox.target) {
247
			if (this.currentDropTarget) {
248
				this.previousDropTarget = this.currentDropTarget;
249
				this.currentDropTarget.onDragOut(e);
250
			}
251
			this.currentDropTarget = bestBox.target;
252
			e.dragObjects = this.dragObjects;
253
			this.dropAcceptable = this.currentDropTarget.onDragOver(e);
254
		} else {
255
			if (this.dropAcceptable) {
256
				this.currentDropTarget.onDragMove(e, this.dragObjects);
257
			}
258
		}
259
	}
260
}, findBestTarget:function (e) {
261
	var _this = this;
262
	var bestBox = new Object();
263
	bestBox.target = null;
264
	bestBox.points = null;
265
	dojo.lang.every(this.dropTargetDimensions, function (tmpDA) {
266
		if (!_this.isInsideBox(e, tmpDA)) {
267
			return true;
268
		}
269
		bestBox.target = tmpDA[2];
270
		bestBox.points = tmpDA;
271
		return Boolean(_this.nestedTargets);
272
	});
273
	return bestBox;
274
}, isInsideBox:function (e, coords) {
275
	if ((e.pageX > coords[0][0]) && (e.pageX < coords[1][0]) && (e.pageY > coords[0][1]) && (e.pageY < coords[1][1])) {
276
		return true;
277
	}
278
	return false;
279
}, onMouseOver:function (e) {
280
}, onMouseOut:function (e) {
281
}});
282
dojo.dnd.dragManager = new dojo.dnd.HtmlDragManager();
283
(function () {
284
	var d = document;
285
	var dm = dojo.dnd.dragManager;
286
	dojo.event.connect(d, "onkeydown", dm, "onKeyDown");
287
	dojo.event.connect(d, "onmouseover", dm, "onMouseOver");
288
	dojo.event.connect(d, "onmouseout", dm, "onMouseOut");
289
	dojo.event.connect(d, "onmousedown", dm, "onMouseDown");
290
	dojo.event.connect(d, "onmouseup", dm, "onMouseUp");
291
	dojo.event.connect(window, "onscroll", dm, "onScroll");
292
})();
293