Subversion Repositories Applications.papyrus

Rev

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