Subversion Repositories Applications.papyrus

Rev

Rev 1371 | 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
 
11
dojo.provide("dojo.widget.TreeBasicController");
12
dojo.require("dojo.event.*");
13
dojo.require("dojo.json");
14
dojo.require("dojo.io.*");
15
dojo.widget.defineWidget("dojo.widget.TreeBasicController", dojo.widget.HtmlWidget, {widgetType:"TreeBasicController", DNDController:"", dieWithTree:false, initialize:function (args, frag) {
16
	if (this.DNDController == "create") {
17
		dojo.require("dojo.dnd.TreeDragAndDrop");
18
		this.DNDController = new dojo.dnd.TreeDNDController(this);
19
	}
20
}, listenTree:function (tree) {
21
	dojo.event.topic.subscribe(tree.eventNames.createDOMNode, this, "onCreateDOMNode");
22
	dojo.event.topic.subscribe(tree.eventNames.treeClick, this, "onTreeClick");
23
	dojo.event.topic.subscribe(tree.eventNames.treeCreate, this, "onTreeCreate");
24
	dojo.event.topic.subscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
25
	if (this.DNDController) {
26
		this.DNDController.listenTree(tree);
27
	}
28
}, unlistenTree:function (tree) {
29
	dojo.event.topic.unsubscribe(tree.eventNames.createDOMNode, this, "onCreateDOMNode");
30
	dojo.event.topic.unsubscribe(tree.eventNames.treeClick, this, "onTreeClick");
31
	dojo.event.topic.unsubscribe(tree.eventNames.treeCreate, this, "onTreeCreate");
32
	dojo.event.topic.unsubscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
33
}, onTreeDestroy:function (message) {
34
	var tree = message.source;
35
	this.unlistenTree(tree);
36
	if (this.dieWithTree) {
37
		this.destroy();
38
	}
39
}, onCreateDOMNode:function (message) {
40
	var node = message.source;
41
	if (node.expandLevel > 0) {
42
		this.expandToLevel(node, node.expandLevel);
43
	}
44
}, onTreeCreate:function (message) {
45
	var tree = message.source;
46
	var _this = this;
47
	if (tree.expandLevel) {
48
		dojo.lang.forEach(tree.children, function (child) {
49
			_this.expandToLevel(child, tree.expandLevel - 1);
50
		});
51
	}
52
}, expandToLevel:function (node, level) {
53
	if (level == 0) {
54
		return;
55
	}
56
	var children = node.children;
57
	var _this = this;
58
	var handler = function (node, expandLevel) {
59
		this.node = node;
60
		this.expandLevel = expandLevel;
61
		this.process = function () {
62
			for (var i = 0; i < this.node.children.length; i++) {
63
				var child = node.children[i];
64
				_this.expandToLevel(child, this.expandLevel);
65
			}
66
		};
67
	};
68
	var h = new handler(node, level - 1);
69
	this.expand(node, false, h, h.process);
70
}, onTreeClick:function (message) {
71
	var node = message.source;
72
	if (node.isLocked()) {
73
		return false;
74
	}
75
	if (node.isExpanded) {
76
		this.collapse(node);
77
	} else {
78
		this.expand(node);
79
	}
80
}, expand:function (node, sync, callObj, callFunc) {
81
	node.expand();
82
	if (callFunc) {
83
		callFunc.apply(callObj, [node]);
84
	}
85
}, collapse:function (node) {
86
	node.collapse();
87
}, canMove:function (child, newParent) {
88
	if (child.actionIsDisabled(child.actions.MOVE)) {
89
		return false;
90
	}
91
	if (child.parent !== newParent && newParent.actionIsDisabled(newParent.actions.ADDCHILD)) {
92
		return false;
93
	}
94
	var node = newParent;
95
	while (node.isTreeNode) {
96
		if (node === child) {
97
			return false;
98
		}
99
		node = node.parent;
100
	}
101
	return true;
102
}, move:function (child, newParent, index) {
103
	if (!this.canMove(child, newParent)) {
104
		return false;
105
	}
106
	var result = this.doMove(child, newParent, index);
107
	if (!result) {
108
		return result;
109
	}
110
	if (newParent.isTreeNode) {
111
		this.expand(newParent);
112
	}
113
	return result;
114
}, doMove:function (child, newParent, index) {
115
	child.tree.move(child, newParent, index);
116
	return true;
117
}, canRemoveNode:function (child) {
118
	if (child.actionIsDisabled(child.actions.REMOVE)) {
119
		return false;
120
	}
121
	return true;
122
}, removeNode:function (node, callObj, callFunc) {
123
	if (!this.canRemoveNode(node)) {
124
		return false;
125
	}
126
	return this.doRemoveNode(node, callObj, callFunc);
127
}, doRemoveNode:function (node, callObj, callFunc) {
128
	node.tree.removeNode(node);
129
	if (callFunc) {
130
		callFunc.apply(dojo.lang.isUndefined(callObj) ? this : callObj, [node]);
131
	}
132
}, canCreateChild:function (parent, index, data) {
133
	if (parent.actionIsDisabled(parent.actions.ADDCHILD)) {
134
		return false;
135
	}
136
	return true;
137
}, createChild:function (parent, index, data, callObj, callFunc) {
138
	if (!this.canCreateChild(parent, index, data)) {
139
		return false;
140
	}
141
	return this.doCreateChild.apply(this, arguments);
142
}, doCreateChild:function (parent, index, data, callObj, callFunc) {
143
	var widgetType = data.widgetType ? data.widgetType : "TreeNode";
144
	var newChild = dojo.widget.createWidget(widgetType, data);
145
	parent.addChild(newChild, index);
146
	this.expand(parent);
147
	if (callFunc) {
148
		callFunc.apply(callObj, [newChild]);
149
	}
150
	return newChild;
151
}});
152