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.TreeDragAndDropV3");
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.Deferred");
17
dojo.require("dojo.html.layout");
18
dojo.dnd.TreeDragSourceV3 = function (node, syncController, type, treeNode) {
19
	this.controller = syncController;
20
	this.treeNode = treeNode;
21
	dojo.dnd.HtmlDragSource.call(this, node, type);
22
};
23
dojo.inherits(dojo.dnd.TreeDragSourceV3, dojo.dnd.HtmlDragSource);
24
dojo.dnd.TreeDropTargetV3 = function (domNode, controller, type, treeNode) {
25
	this.treeNode = treeNode;
26
	this.controller = controller;
27
	dojo.dnd.HtmlDropTarget.call(this, domNode, type);
28
};
29
dojo.inherits(dojo.dnd.TreeDropTargetV3, dojo.dnd.HtmlDropTarget);
30
dojo.lang.extend(dojo.dnd.TreeDropTargetV3, {autoExpandDelay:1500, autoExpandTimer:null, position:null, indicatorStyle:"2px black groove", showIndicator:function (position) {
31
	if (this.position == position) {
32
		return;
33
	}
34
	this.hideIndicator();
35
	this.position = position;
36
	var node = this.treeNode;
37
	node.contentNode.style.width = dojo.html.getBorderBox(node.labelNode).width + "px";
38
	if (position == "onto") {
39
		node.contentNode.style.border = this.indicatorStyle;
40
	} else {
41
		if (position == "before") {
42
			node.contentNode.style.borderTop = this.indicatorStyle;
43
		} else {
44
			if (position == "after") {
45
				node.contentNode.style.borderBottom = this.indicatorStyle;
46
			}
47
		}
48
	}
49
}, hideIndicator:function () {
50
	this.treeNode.contentNode.style.borderBottom = "";
51
	this.treeNode.contentNode.style.borderTop = "";
52
	this.treeNode.contentNode.style.border = "";
53
	this.treeNode.contentNode.style.width = "";
54
	this.position = null;
55
}, onDragOver:function (e) {
56
	var accepts = dojo.dnd.HtmlDropTarget.prototype.onDragOver.apply(this, arguments);
57
	if (accepts && this.treeNode.isFolder && !this.treeNode.isExpanded) {
58
		this.setAutoExpandTimer();
59
	}
60
	if (accepts) {
61
		this.cacheNodeCoords();
62
	}
63
	return accepts;
64
}, accepts:function (dragObjects) {
65
	var accepts = dojo.dnd.HtmlDropTarget.prototype.accepts.apply(this, arguments);
66
	if (!accepts) {
67
		return false;
68
	}
69
	for (var i = 0; i < dragObjects.length; i++) {
70
		var sourceTreeNode = dragObjects[i].treeNode;
71
		if (sourceTreeNode === this.treeNode) {
72
			return false;
73
		}
74
	}
75
	return true;
76
}, setAutoExpandTimer:function () {
77
	var _this = this;
78
	var autoExpand = function () {
79
		if (dojo.dnd.dragManager.currentDropTarget === _this) {
80
			_this.controller.expand(_this.treeNode);
81
			dojo.dnd.dragManager.cacheTargetLocations();
82
		}
83
	};
84
	this.autoExpandTimer = dojo.lang.setTimeout(autoExpand, _this.autoExpandDelay);
85
}, getAcceptPosition:function (e, dragObjects) {
86
	var DndMode = this.treeNode.tree.DndMode;
87
	if (DndMode & dojo.widget.TreeV3.prototype.DndModes.ONTO && this.treeNode.actionIsDisabledNow(this.treeNode.actions.ADDCHILD)) {
88
		DndMode &= ~dojo.widget.TreeV3.prototype.DndModes.ONTO;
89
	}
90
	var position = this.getPosition(e, DndMode);
91
	if (position == "onto") {
92
		return position;
93
	}
94
	for (var i = 0; i < dragObjects.length; i++) {
95
		var source = dragObjects[i].dragSource;
96
		if (source.treeNode && this.isAdjacentNode(source.treeNode, position)) {
97
			continue;
98
		}
99
		if (!this.controller.canMove(source.treeNode ? source.treeNode : source, this.treeNode.parent)) {
100
			return false;
101
		}
102
	}
103
	return position;
104
}, onDropEnd:function (e) {
105
	this.clearAutoExpandTimer();
106
	this.hideIndicator();
107
}, onDragOut:function (e) {
108
	this.clearAutoExpandTimer();
109
	this.hideIndicator();
110
}, clearAutoExpandTimer:function () {
111
	if (this.autoExpandTimer) {
112
		clearTimeout(this.autoExpandTimer);
113
		this.autoExpandTimer = null;
114
	}
115
}, onDragMove:function (e, dragObjects) {
116
	var position = this.getAcceptPosition(e, dragObjects);
117
	if (position) {
118
		this.showIndicator(position);
119
	}
120
}, isAdjacentNode:function (sourceNode, position) {
121
	if (sourceNode === this.treeNode) {
122
		return true;
123
	}
124
	if (sourceNode.getNextSibling() === this.treeNode && position == "before") {
125
		return true;
126
	}
127
	if (sourceNode.getPreviousSibling() === this.treeNode && position == "after") {
128
		return true;
129
	}
130
	return false;
131
}, cacheNodeCoords:function () {
132
	var node = this.treeNode.contentNode;
133
	this.cachedNodeY = dojo.html.getAbsolutePosition(node).y;
134
	this.cachedNodeHeight = dojo.html.getBorderBox(node).height;
135
}, getPosition:function (e, DndMode) {
136
	var mousey = e.pageY || e.clientY + dojo.body().scrollTop;
137
	var relY = mousey - this.cachedNodeY;
138
	var p = relY / this.cachedNodeHeight;
139
	var position = "";
140
	if (DndMode & dojo.widget.TreeV3.prototype.DndModes.ONTO && DndMode & dojo.widget.TreeV3.prototype.DndModes.BETWEEN) {
141
		if (p <= 0.33) {
142
			position = "before";
143
		} else {
144
			if (p <= 0.66 || this.treeNode.isExpanded && this.treeNode.children.length && !this.treeNode.isLastChild()) {
145
				position = "onto";
146
			} else {
147
				position = "after";
148
			}
149
		}
150
	} else {
151
		if (DndMode & dojo.widget.TreeV3.prototype.DndModes.BETWEEN) {
152
			if (p <= 0.5 || this.treeNode.isExpanded && this.treeNode.children.length && !this.treeNode.isLastChild()) {
153
				position = "before";
154
			} else {
155
				position = "after";
156
			}
157
		} else {
158
			if (DndMode & dojo.widget.TreeV3.prototype.DndModes.ONTO) {
159
				position = "onto";
160
			}
161
		}
162
	}
163
	return position;
164
}, getTargetParentIndex:function (source, position) {
165
	var index = position == "before" ? this.treeNode.getParentIndex() : this.treeNode.getParentIndex() + 1;
166
	if (source.treeNode && this.treeNode.parent === source.treeNode.parent && this.treeNode.getParentIndex() > source.treeNode.getParentIndex()) {
167
		index--;
168
	}
169
	return index;
170
}, onDrop:function (e) {
171
	var position = this.position;
172
	var source = e.dragObject.dragSource;
173
	var targetParent, targetIndex;
174
	if (position == "onto") {
175
		targetParent = this.treeNode;
176
		targetIndex = 0;
177
	} else {
178
		targetIndex = this.getTargetParentIndex(source, position);
179
		targetParent = this.treeNode.parent;
180
	}
181
	var r = this.getDropHandler(e, source, targetParent, targetIndex)();
182
	return r;
183
}, getDropHandler:function (e, source, targetParent, targetIndex) {
184
	var handler;
185
	var _this = this;
186
	handler = function () {
187
		var result;
188
		if (source.treeNode) {
189
			result = _this.controller.move(source.treeNode, targetParent, targetIndex, true);
190
		} else {
191
			if (dojo.lang.isFunction(source.onDrop)) {
192
				source.onDrop(targetParent, targetIndex);
193
			}
194
			var treeNode = source.getTreeNode();
195
			if (treeNode) {
196
				result = _this.controller.createChild(targetParent, targetIndex, treeNode, true);
197
			} else {
198
				result = true;
199
			}
200
		}
201
		if (result instanceof dojo.Deferred) {
202
			var isSuccess = result.fired == 0;
203
			if (!isSuccess) {
204
				_this.handleDropError(source, targetParent, targetIndex, result);
205
			}
206
			return isSuccess;
207
		} else {
208
			return result;
209
		}
210
	};
211
	return handler;
212
}, handleDropError:function (source, parent, index, result) {
213
	dojo.debug("TreeDropTargetV3.handleDropError: DND error occured");
214
	dojo.debugShallow(result);
215
}});
216