Subversion Repositories Applications.papyrus

Rev

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