Subversion Repositories Applications.papyrus

Rev

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