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.TreeDragAndDrop");
12
dojo.require("dojo.dnd.HtmlDragAndDrop");
13
dojo.require("dojo.lang.func");
14
dojo.require("dojo.lang.array");
15
dojo.require("dojo.lang.extras");
16
dojo.require("dojo.html.layout");
17
dojo.dnd.TreeDragSource = function (node, syncController, type, treeNode) {
18
	this.controller = syncController;
19
	this.treeNode = treeNode;
20
	dojo.dnd.HtmlDragSource.call(this, node, type);
21
};
22
dojo.inherits(dojo.dnd.TreeDragSource, dojo.dnd.HtmlDragSource);
23
dojo.lang.extend(dojo.dnd.TreeDragSource, {onDragStart:function () {
24
	var dragObject = dojo.dnd.HtmlDragSource.prototype.onDragStart.call(this);
25
	dragObject.treeNode = this.treeNode;
26
	dragObject.onDragStart = dojo.lang.hitch(dragObject, function (e) {
27
		this.savedSelectedNode = this.treeNode.tree.selector.selectedNode;
28
		if (this.savedSelectedNode) {
29
			this.savedSelectedNode.unMarkSelected();
30
		}
31
		var result = dojo.dnd.HtmlDragObject.prototype.onDragStart.apply(this, arguments);
32
		var cloneGrid = this.dragClone.getElementsByTagName("img");
33
		for (var i = 0; i < cloneGrid.length; i++) {
34
			cloneGrid.item(i).style.backgroundImage = "url()";
35
		}
36
		return result;
37
	});
38
	dragObject.onDragEnd = function (e) {
39
		if (this.savedSelectedNode) {
40
			this.savedSelectedNode.markSelected();
41
		}
42
		return dojo.dnd.HtmlDragObject.prototype.onDragEnd.apply(this, arguments);
43
	};
44
	return dragObject;
45
}, onDragEnd:function (e) {
46
	var res = dojo.dnd.HtmlDragSource.prototype.onDragEnd.call(this, e);
47
	return res;
48
}});
49
dojo.dnd.TreeDropTarget = function (domNode, controller, type, treeNode) {
50
	this.treeNode = treeNode;
51
	this.controller = controller;
52
	dojo.dnd.HtmlDropTarget.apply(this, [domNode, type]);
53
};
54
dojo.inherits(dojo.dnd.TreeDropTarget, dojo.dnd.HtmlDropTarget);
55
dojo.lang.extend(dojo.dnd.TreeDropTarget, {autoExpandDelay:1500, autoExpandTimer:null, position:null, indicatorStyle:"2px black solid", showIndicator:function (position) {
56
	if (this.position == position) {
57
		return;
58
	}
59
	this.hideIndicator();
60
	this.position = position;
61
	if (position == "before") {
62
		this.treeNode.labelNode.style.borderTop = this.indicatorStyle;
63
	} else {
64
		if (position == "after") {
65
			this.treeNode.labelNode.style.borderBottom = this.indicatorStyle;
66
		} else {
67
			if (position == "onto") {
68
				this.treeNode.markSelected();
69
			}
70
		}
71
	}
72
}, hideIndicator:function () {
73
	this.treeNode.labelNode.style.borderBottom = "";
74
	this.treeNode.labelNode.style.borderTop = "";
75
	this.treeNode.unMarkSelected();
76
	this.position = null;
77
}, onDragOver:function (e) {
78
	var accepts = dojo.dnd.HtmlDropTarget.prototype.onDragOver.apply(this, arguments);
79
	if (accepts && this.treeNode.isFolder && !this.treeNode.isExpanded) {
80
		this.setAutoExpandTimer();
81
	}
82
	return accepts;
83
}, accepts:function (dragObjects) {
84
	var accepts = dojo.dnd.HtmlDropTarget.prototype.accepts.apply(this, arguments);
85
	if (!accepts) {
86
		return false;
87
	}
88
	var sourceTreeNode = dragObjects[0].treeNode;
89
	if (dojo.lang.isUndefined(sourceTreeNode) || !sourceTreeNode || !sourceTreeNode.isTreeNode) {
90
		dojo.raise("Source is not TreeNode or not found");
91
	}
92
	if (sourceTreeNode === this.treeNode) {
93
		return false;
94
	}
95
	return true;
96
}, setAutoExpandTimer:function () {
97
	var _this = this;
98
	var autoExpand = function () {
99
		if (dojo.dnd.dragManager.currentDropTarget === _this) {
100
			_this.controller.expand(_this.treeNode);
101
		}
102
	};
103
	this.autoExpandTimer = dojo.lang.setTimeout(autoExpand, _this.autoExpandDelay);
104
}, getDNDMode:function () {
105
	return this.treeNode.tree.DNDMode;
106
}, getAcceptPosition:function (e, sourceTreeNode) {
107
	var DNDMode = this.getDNDMode();
108
	if (DNDMode & dojo.widget.Tree.prototype.DNDModes.ONTO && !(!this.treeNode.actionIsDisabled(dojo.widget.TreeNode.prototype.actions.ADDCHILD) && sourceTreeNode.parent !== this.treeNode && this.controller.canMove(sourceTreeNode, this.treeNode))) {
109
		DNDMode &= ~dojo.widget.Tree.prototype.DNDModes.ONTO;
110
	}
111
	var position = this.getPosition(e, DNDMode);
112
	if (position == "onto" || (!this.isAdjacentNode(sourceTreeNode, position) && this.controller.canMove(sourceTreeNode, this.treeNode.parent))) {
113
		return position;
114
	} else {
115
		return false;
116
	}
117
}, onDragOut:function (e) {
118
	this.clearAutoExpandTimer();
119
	this.hideIndicator();
120
}, clearAutoExpandTimer:function () {
121
	if (this.autoExpandTimer) {
122
		clearTimeout(this.autoExpandTimer);
123
		this.autoExpandTimer = null;
124
	}
125
}, onDragMove:function (e, dragObjects) {
126
	var sourceTreeNode = dragObjects[0].treeNode;
127
	var position = this.getAcceptPosition(e, sourceTreeNode);
128
	if (position) {
129
		this.showIndicator(position);
130
	}
131
}, isAdjacentNode:function (sourceNode, position) {
132
	if (sourceNode === this.treeNode) {
133
		return true;
134
	}
135
	if (sourceNode.getNextSibling() === this.treeNode && position == "before") {
136
		return true;
137
	}
138
	if (sourceNode.getPreviousSibling() === this.treeNode && position == "after") {
139
		return true;
140
	}
141
	return false;
142
}, getPosition:function (e, DNDMode) {
143
	var node = dojo.byId(this.treeNode.labelNode);
144
	var mousey = e.pageY || e.clientY + dojo.body().scrollTop;
145
	var nodey = dojo.html.getAbsolutePosition(node).y;
146
	var height = dojo.html.getBorderBox(node).height;
147
	var relY = mousey - nodey;
148
	var p = relY / height;
149
	var position = "";
150
	if (DNDMode & dojo.widget.Tree.prototype.DNDModes.ONTO && DNDMode & dojo.widget.Tree.prototype.DNDModes.BETWEEN) {
151
		if (p <= 0.3) {
152
			position = "before";
153
		} else {
154
			if (p <= 0.7) {
155
				position = "onto";
156
			} else {
157
				position = "after";
158
			}
159
		}
160
	} else {
161
		if (DNDMode & dojo.widget.Tree.prototype.DNDModes.BETWEEN) {
162
			if (p <= 0.5) {
163
				position = "before";
164
			} else {
165
				position = "after";
166
			}
167
		} else {
168
			if (DNDMode & dojo.widget.Tree.prototype.DNDModes.ONTO) {
169
				position = "onto";
170
			}
171
		}
172
	}
173
	return position;
174
}, getTargetParentIndex:function (sourceTreeNode, position) {
175
	var index = position == "before" ? this.treeNode.getParentIndex() : this.treeNode.getParentIndex() + 1;
176
	if (this.treeNode.parent === sourceTreeNode.parent && this.treeNode.getParentIndex() > sourceTreeNode.getParentIndex()) {
177
		index--;
178
	}
179
	return index;
180
}, onDrop:function (e) {
181
	var position = this.position;
182
	this.onDragOut(e);
183
	var sourceTreeNode = e.dragObject.treeNode;
184
	if (!dojo.lang.isObject(sourceTreeNode)) {
185
		dojo.raise("TreeNode not found in dragObject");
186
	}
187
	if (position == "onto") {
188
		return this.controller.move(sourceTreeNode, this.treeNode, 0);
189
	} else {
190
		var index = this.getTargetParentIndex(sourceTreeNode, position);
191
		return this.controller.move(sourceTreeNode, this.treeNode.parent, index);
192
	}
193
}});
194
dojo.dnd.TreeDNDController = function (treeController) {
195
	this.treeController = treeController;
196
	this.dragSources = {};
197
	this.dropTargets = {};
198
};
199
dojo.lang.extend(dojo.dnd.TreeDNDController, {listenTree:function (tree) {
200
	dojo.event.topic.subscribe(tree.eventNames.createDOMNode, this, "onCreateDOMNode");
201
	dojo.event.topic.subscribe(tree.eventNames.moveFrom, this, "onMoveFrom");
202
	dojo.event.topic.subscribe(tree.eventNames.moveTo, this, "onMoveTo");
203
	dojo.event.topic.subscribe(tree.eventNames.addChild, this, "onAddChild");
204
	dojo.event.topic.subscribe(tree.eventNames.removeNode, this, "onRemoveNode");
205
	dojo.event.topic.subscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
206
}, unlistenTree:function (tree) {
207
	dojo.event.topic.unsubscribe(tree.eventNames.createDOMNode, this, "onCreateDOMNode");
208
	dojo.event.topic.unsubscribe(tree.eventNames.moveFrom, this, "onMoveFrom");
209
	dojo.event.topic.unsubscribe(tree.eventNames.moveTo, this, "onMoveTo");
210
	dojo.event.topic.unsubscribe(tree.eventNames.addChild, this, "onAddChild");
211
	dojo.event.topic.unsubscribe(tree.eventNames.removeNode, this, "onRemoveNode");
212
	dojo.event.topic.unsubscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
213
}, onTreeDestroy:function (message) {
214
	this.unlistenTree(message.source);
215
}, onCreateDOMNode:function (message) {
216
	this.registerDNDNode(message.source);
217
}, onAddChild:function (message) {
218
	this.registerDNDNode(message.child);
219
}, onMoveFrom:function (message) {
220
	var _this = this;
221
	dojo.lang.forEach(message.child.getDescendants(), function (node) {
222
		_this.unregisterDNDNode(node);
223
	});
224
}, onMoveTo:function (message) {
225
	var _this = this;
226
	dojo.lang.forEach(message.child.getDescendants(), function (node) {
227
		_this.registerDNDNode(node);
228
	});
229
}, registerDNDNode:function (node) {
230
	if (!node.tree.DNDMode) {
231
		return;
232
	}
233
	var source = null;
234
	var target = null;
235
	if (!node.actionIsDisabled(node.actions.MOVE)) {
236
		var source = new dojo.dnd.TreeDragSource(node.labelNode, this, node.tree.widgetId, node);
237
		this.dragSources[node.widgetId] = source;
238
	}
239
	var target = new dojo.dnd.TreeDropTarget(node.labelNode, this.treeController, node.tree.DNDAcceptTypes, node);
240
	this.dropTargets[node.widgetId] = target;
241
}, unregisterDNDNode:function (node) {
242
	if (this.dragSources[node.widgetId]) {
243
		dojo.dnd.dragManager.unregisterDragSource(this.dragSources[node.widgetId]);
244
		delete this.dragSources[node.widgetId];
245
	}
246
	if (this.dropTargets[node.widgetId]) {
247
		dojo.dnd.dragManager.unregisterDropTarget(this.dropTargets[node.widgetId]);
248
		delete this.dropTargets[node.widgetId];
249
	}
250
}});
251