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.widget.TreeNodeV3");
12
dojo.require("dojo.html.*");
13
dojo.require("dojo.event.*");
14
dojo.require("dojo.io.*");
15
dojo.require("dojo.widget.TreeWithNode");
16
dojo.widget.defineWidget("dojo.widget.TreeNodeV3", [dojo.widget.HtmlWidget, dojo.widget.TreeWithNode], function () {
17
	this.actionsDisabled = [];
18
	this.object = {};
19
}, {tryLazyInit:true, actions:{MOVE:"MOVE", DETACH:"DETACH", EDIT:"EDIT", ADDCHILD:"ADDCHILD", SELECT:"SELECT"}, labelClass:"", contentClass:"", expandNode:null, labelNode:null, nodeDocType:"", selected:false, getnodeDocType:function () {
20
	return this.nodeDocType;
21
}, cloneProperties:["actionsDisabled", "tryLazyInit", "nodeDocType", "objectId", "object", "title", "isFolder", "isExpanded", "state"], clone:function (deep) {
22
	var ret = new this.constructor();
23
	for (var i = 0; i < this.cloneProperties.length; i++) {
24
		var prop = this.cloneProperties[i];
25
		ret[prop] = dojo.lang.shallowCopy(this[prop], true);
26
	}
27
	if (this.tree.unsetFolderOnEmpty && !deep && this.isFolder) {
28
		ret.isFolder = false;
29
	}
30
	ret.toggleObj = this.toggleObj;
31
	dojo.widget.manager.add(ret);
32
	ret.tree = this.tree;
33
	ret.buildRendering({}, {});
34
	ret.initialize({}, {});
35
	if (deep && this.children.length) {
36
		for (var i = 0; i < this.children.length; i++) {
37
			var child = this.children[i];
38
			if (child.clone) {
39
				ret.children.push(child.clone(deep));
40
			} else {
41
				ret.children.push(dojo.lang.shallowCopy(child, deep));
42
			}
43
		}
44
		ret.setChildren();
45
	}
46
	return ret;
47
}, markProcessing:function () {
48
	this.markProcessingSavedClass = dojo.html.getClass(this.expandNode);
49
	dojo.html.setClass(this.expandNode, this.tree.classPrefix + "ExpandLoading");
50
}, unmarkProcessing:function () {
51
	dojo.html.setClass(this.expandNode, this.markProcessingSavedClass);
52
}, buildRendering:function (args, fragment, parent) {
53
	if (args.tree) {
54
		this.tree = dojo.lang.isString(args.tree) ? dojo.widget.manager.getWidgetById(args.tree) : args.tree;
55
	} else {
56
		if (parent && parent.tree) {
57
			this.tree = parent.tree;
58
		}
59
	}
60
	if (!this.tree) {
61
		dojo.raise("Can't evaluate tree from arguments or parent");
62
	}
63
	this.domNode = this.tree.nodeTemplate.cloneNode(true);
64
	this.expandNode = this.domNode.firstChild;
65
	this.contentNode = this.domNode.childNodes[1];
66
	this.labelNode = this.contentNode.firstChild;
67
	if (this.labelClass) {
68
		dojo.html.addClass(this.labelNode, this.labelClass);
69
	}
70
	if (this.contentClass) {
71
		dojo.html.addClass(this.contentNode, this.contentClass);
72
	}
73
	this.domNode.widgetId = this.widgetId;
74
	this.labelNode.innerHTML = this.title;
75
}, isTreeNode:true, object:{}, title:"", isFolder:null, contentNode:null, expandClass:"", isExpanded:false, containerNode:null, getInfo:function () {
76
	var info = {widgetId:this.widgetId, objectId:this.objectId, index:this.getParentIndex()};
77
	return info;
78
}, setFolder:function () {
79
	this.isFolder = true;
80
	this.viewSetExpand();
81
	if (!this.containerNode) {
82
		this.viewAddContainer();
83
	}
84
	dojo.event.topic.publish(this.tree.eventNames.afterSetFolder, {source:this});
85
}, initialize:function (args, frag, parent) {
86
	if (args.isFolder) {
87
		this.isFolder = true;
88
	}
89
	if (this.children.length || this.isFolder) {
90
		this.setFolder();
91
	} else {
92
		this.viewSetExpand();
93
	}
94
	for (var i = 0; i < this.actionsDisabled.length; i++) {
95
		this.actionsDisabled[i] = this.actionsDisabled[i].toUpperCase();
96
	}
97
	dojo.event.topic.publish(this.tree.eventNames.afterChangeTree, {oldTree:null, newTree:this.tree, node:this});
98
}, unsetFolder:function () {
99
	this.isFolder = false;
100
	this.viewSetExpand();
101
	dojo.event.topic.publish(this.tree.eventNames.afterUnsetFolder, {source:this});
102
}, insertNode:function (parent, index) {
103
	if (!index) {
104
		index = 0;
105
	}
106
	if (index == 0) {
107
		dojo.html.prependChild(this.domNode, parent.containerNode);
108
	} else {
109
		dojo.html.insertAfter(this.domNode, parent.children[index - 1].domNode);
110
	}
111
}, updateTree:function (newTree) {
112
	if (this.tree === newTree) {
113
		return;
114
	}
115
	var oldTree = this.tree;
116
	dojo.lang.forEach(this.getDescendants(), function (elem) {
117
		elem.tree = newTree;
118
	});
119
	if (oldTree.classPrefix != newTree.classPrefix) {
120
		var stack = [this.domNode];
121
		var elem;
122
		var reg = new RegExp("(^|\\s)" + oldTree.classPrefix, "g");
123
		while (elem = stack.pop()) {
124
			for (var i = 0; i < elem.childNodes.length; i++) {
125
				var childNode = elem.childNodes[i];
126
				if (childNode.nodeDocType != 1) {
127
					continue;
128
				}
129
				dojo.html.setClass(childNode, dojo.html.getClass(childNode).replace(reg, "$1" + newTree.classPrefix));
130
				stack.push(childNode);
131
			}
132
		}
133
	}
134
	var message = {oldTree:oldTree, newTree:newTree, node:this};
135
	dojo.event.topic.publish(this.tree.eventNames.afterChangeTree, message);
136
	dojo.event.topic.publish(newTree.eventNames.afterChangeTree, message);
137
}, addedTo:function (parent, index, dontPublishEvent) {
138
	if (this.tree !== parent.tree) {
139
		this.updateTree(parent.tree);
140
	}
141
	if (parent.isTreeNode) {
142
		if (!parent.isFolder) {
143
			parent.setFolder();
144
			parent.state = parent.loadStates.LOADED;
145
		}
146
	}
147
	var siblingsCount = parent.children.length;
148
	this.insertNode(parent, index);
149
	this.viewAddLayout();
150
	if (siblingsCount > 1) {
151
		if (index == 0 && parent.children[1] instanceof dojo.widget.Widget) {
152
			parent.children[1].viewUpdateLayout();
153
		}
154
		if (index == siblingsCount - 1 && parent.children[siblingsCount - 2] instanceof dojo.widget.Widget) {
155
			parent.children[siblingsCount - 2].viewUpdateLayout();
156
		}
157
	} else {
158
		if (parent.isTreeNode) {
159
			parent.viewSetHasChildren();
160
		}
161
	}
162
	if (!dontPublishEvent) {
163
		var message = {child:this, index:index, parent:parent};
164
		dojo.event.topic.publish(this.tree.eventNames.afterAddChild, message);
165
	}
166
}, createSimple:function (args, parent) {
167
	if (args.tree) {
168
		var tree = args.tree;
169
	} else {
170
		if (parent) {
171
			var tree = parent.tree;
172
		} else {
173
			dojo.raise("createSimple: can't evaluate tree");
174
		}
175
	}
176
	tree = dojo.widget.byId(tree);
177
	var treeNode = new tree.defaultChildWidget();
178
	for (var x in args) {
179
		treeNode[x] = args[x];
180
	}
181
	treeNode.toggleObj = dojo.lfx.toggle[treeNode.toggle.toLowerCase()] || dojo.lfx.toggle.plain;
182
	dojo.widget.manager.add(treeNode);
183
	treeNode.buildRendering(args, {}, parent);
184
	treeNode.initialize(args, {}, parent);
185
	if (treeNode.parent) {
186
		delete dojo.widget.manager.topWidgets[treeNode.widgetId];
187
	}
188
	return treeNode;
189
}, viewUpdateLayout:function () {
190
	this.viewRemoveLayout();
191
	this.viewAddLayout();
192
}, viewAddContainer:function () {
193
	this.containerNode = this.tree.containerNodeTemplate.cloneNode(true);
194
	this.domNode.appendChild(this.containerNode);
195
}, viewAddLayout:function () {
196
	if (this.parent["isTree"]) {
197
		dojo.html.setClass(this.domNode, dojo.html.getClass(this.domNode) + " " + this.tree.classPrefix + "IsRoot");
198
	}
199
	if (this.isLastChild()) {
200
		dojo.html.setClass(this.domNode, dojo.html.getClass(this.domNode) + " " + this.tree.classPrefix + "IsLast");
201
	}
202
}, viewRemoveLayout:function () {
203
	dojo.html.removeClass(this.domNode, this.tree.classPrefix + "IsRoot");
204
	dojo.html.removeClass(this.domNode, this.tree.classPrefix + "IsLast");
205
}, viewGetExpandClass:function () {
206
	if (this.isFolder) {
207
		return this.isExpanded ? "ExpandOpen" : "ExpandClosed";
208
	} else {
209
		return "ExpandLeaf";
210
	}
211
}, viewSetExpand:function () {
212
	var expand = this.tree.classPrefix + this.viewGetExpandClass();
213
	var reg = new RegExp("(^|\\s)" + this.tree.classPrefix + "Expand\\w+", "g");
214
	dojo.html.setClass(this.domNode, dojo.html.getClass(this.domNode).replace(reg, "") + " " + expand);
215
	this.viewSetHasChildrenAndExpand();
216
}, viewGetChildrenClass:function () {
217
	return "Children" + (this.children.length ? "Yes" : "No");
218
}, viewSetHasChildren:function () {
219
	var clazz = this.tree.classPrefix + this.viewGetChildrenClass();
220
	var reg = new RegExp("(^|\\s)" + this.tree.classPrefix + "Children\\w+", "g");
221
	dojo.html.setClass(this.domNode, dojo.html.getClass(this.domNode).replace(reg, "") + " " + clazz);
222
	this.viewSetHasChildrenAndExpand();
223
}, viewSetHasChildrenAndExpand:function () {
224
	var clazz = this.tree.classPrefix + "State" + this.viewGetChildrenClass() + "-" + this.viewGetExpandClass();
225
	var reg = new RegExp("(^|\\s)" + this.tree.classPrefix + "State[\\w-]+", "g");
226
	dojo.html.setClass(this.domNode, dojo.html.getClass(this.domNode).replace(reg, "") + " " + clazz);
227
}, viewUnfocus:function () {
228
	dojo.html.removeClass(this.labelNode, this.tree.classPrefix + "LabelFocused");
229
}, viewFocus:function () {
230
	dojo.html.addClass(this.labelNode, this.tree.classPrefix + "LabelFocused");
231
}, viewEmphasize:function () {
232
	dojo.html.clearSelection(this.labelNode);
233
	dojo.html.addClass(this.labelNode, this.tree.classPrefix + "NodeEmphasized");
234
}, viewUnemphasize:function () {
235
	dojo.html.removeClass(this.labelNode, this.tree.classPrefix + "NodeEmphasized");
236
}, detach:function () {
237
	if (!this.parent) {
238
		return;
239
	}
240
	var parent = this.parent;
241
	var index = this.getParentIndex();
242
	this.doDetach.apply(this, arguments);
243
	dojo.event.topic.publish(this.tree.eventNames.afterDetach, {child:this, parent:parent, index:index});
244
}, doDetach:function () {
245
	var parent = this.parent;
246
	if (!parent) {
247
		return;
248
	}
249
	var index = this.getParentIndex();
250
	this.viewRemoveLayout();
251
	dojo.widget.DomWidget.prototype.removeChild.call(parent, this);
252
	var siblingsCount = parent.children.length;
253
	if (siblingsCount > 0) {
254
		if (index == 0) {
255
			parent.children[0].viewUpdateLayout();
256
		}
257
		if (index == siblingsCount) {
258
			parent.children[siblingsCount - 1].viewUpdateLayout();
259
		}
260
	} else {
261
		if (parent.isTreeNode) {
262
			parent.viewSetHasChildren();
263
		}
264
	}
265
	if (this.tree.unsetFolderOnEmpty && !parent.children.length && parent.isTreeNode) {
266
		parent.unsetFolder();
267
	}
268
	this.parent = null;
269
}, destroy:function () {
270
	dojo.event.topic.publish(this.tree.eventNames.beforeNodeDestroy, {source:this});
271
	this.detach();
272
	return dojo.widget.HtmlWidget.prototype.destroy.apply(this, arguments);
273
}, expand:function () {
274
	if (this.isExpanded) {
275
		return;
276
	}
277
	if (this.tryLazyInit) {
278
		this.setChildren();
279
		this.tryLazyInit = false;
280
	}
281
	this.isExpanded = true;
282
	this.viewSetExpand();
283
	this.showChildren();
284
}, collapse:function () {
285
	if (!this.isExpanded) {
286
		return;
287
	}
288
	this.isExpanded = false;
289
	this.hideChildren();
290
}, hideChildren:function () {
291
	this.tree.toggleObj.hide(this.containerNode, this.tree.toggleDuration, this.explodeSrc, dojo.lang.hitch(this, "onHideChildren"));
292
}, showChildren:function () {
293
	this.tree.toggleObj.show(this.containerNode, this.tree.toggleDuration, this.explodeSrc, dojo.lang.hitch(this, "onShowChildren"));
294
}, onShowChildren:function () {
295
	this.onShow();
296
	dojo.event.topic.publish(this.tree.eventNames.afterExpand, {source:this});
297
}, onHideChildren:function () {
298
	this.viewSetExpand();
299
	this.onHide();
300
	dojo.event.topic.publish(this.tree.eventNames.afterCollapse, {source:this});
301
}, setTitle:function (title) {
302
	var oldTitle = this.title;
303
	this.labelNode.innerHTML = this.title = title;
304
	dojo.event.topic.publish(this.tree.eventNames.afterSetTitle, {source:this, oldTitle:oldTitle});
305
}, toString:function () {
306
	return "[" + this.widgetType + ", " + this.title + "]";
307
}});
308