Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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