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
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.widget.TreeBasicControllerV3");
14
dojo.require("dojo.event.*");
15
dojo.require("dojo.json");
16
dojo.require("dojo.io.*");
17
dojo.require("dojo.widget.TreeCommon");
18
dojo.require("dojo.widget.TreeNodeV3");
19
dojo.require("dojo.widget.TreeV3");
20
dojo.widget.defineWidget("dojo.widget.TreeBasicControllerV3", [dojo.widget.HtmlWidget, dojo.widget.TreeCommon], function () {
21
	this.listenedTrees = {};
22
}, {listenTreeEvents:["afterSetFolder", "afterTreeCreate", "beforeTreeDestroy"], listenNodeFilter:function (elem) {
23
	return elem instanceof dojo.widget.Widget;
24
}, editor:null, initialize:function (args) {
25
	if (args.editor) {
26
		this.editor = dojo.widget.byId(args.editor);
27
		this.editor.controller = this;
28
	}
29
}, getInfo:function (elem) {
30
	return elem.getInfo();
31
}, onBeforeTreeDestroy:function (message) {
32
	this.unlistenTree(message.source);
33
}, onAfterSetFolder:function (message) {
34
	if (message.source.expandLevel > 0) {
35
		this.expandToLevel(message.source, message.source.expandLevel);
36
	}
37
	if (message.source.loadLevel > 0) {
38
		this.loadToLevel(message.source, message.source.loadLevel);
39
	}
40
}, _focusNextVisible:function (nodeWidget) {
41
	if (nodeWidget.isFolder && nodeWidget.isExpanded && nodeWidget.children.length > 0) {
42
		returnWidget = nodeWidget.children[0];
43
	} else {
44
		while (nodeWidget.isTreeNode && nodeWidget.isLastChild()) {
45
			nodeWidget = nodeWidget.parent;
46
		}
47
		if (nodeWidget.isTreeNode) {
48
			var returnWidget = nodeWidget.parent.children[nodeWidget.getParentIndex() + 1];
49
		}
50
	}
51
	if (returnWidget && returnWidget.isTreeNode) {
52
		this._focusLabel(returnWidget);
53
		return returnWidget;
54
	}
55
}, _focusPreviousVisible:function (nodeWidget) {
56
	var returnWidget = nodeWidget;
57
	if (!nodeWidget.isFirstChild()) {
58
		var previousSibling = nodeWidget.parent.children[nodeWidget.getParentIndex() - 1];
59
		nodeWidget = previousSibling;
60
		while (nodeWidget.isFolder && nodeWidget.isExpanded && nodeWidget.children.length > 0) {
61
			returnWidget = nodeWidget;
62
			nodeWidget = nodeWidget.children[nodeWidget.children.length - 1];
63
		}
64
	} else {
65
		nodeWidget = nodeWidget.parent;
66
	}
67
	if (nodeWidget && nodeWidget.isTreeNode) {
68
		returnWidget = nodeWidget;
69
	}
70
	if (returnWidget && returnWidget.isTreeNode) {
71
		this._focusLabel(returnWidget);
72
		return returnWidget;
73
	}
74
}, _focusZoomIn:function (nodeWidget) {
75
	var returnWidget = nodeWidget;
76
	if (nodeWidget.isFolder && !nodeWidget.isExpanded) {
77
		this.expand(nodeWidget);
78
	} else {
79
		if (nodeWidget.children.length > 0) {
80
			nodeWidget = nodeWidget.children[0];
81
		}
82
	}
83
	if (nodeWidget && nodeWidget.isTreeNode) {
84
		returnWidget = nodeWidget;
85
	}
86
	if (returnWidget && returnWidget.isTreeNode) {
87
		this._focusLabel(returnWidget);
88
		return returnWidget;
89
	}
90
}, _focusZoomOut:function (node) {
91
	var returnWidget = node;
92
	if (node.isFolder && node.isExpanded) {
93
		this.collapse(node);
94
	} else {
95
		node = node.parent;
96
	}
97
	if (node && node.isTreeNode) {
98
		returnWidget = node;
99
	}
100
	if (returnWidget && returnWidget.isTreeNode) {
101
		this._focusLabel(returnWidget);
102
		return returnWidget;
103
	}
104
}, onFocusNode:function (e) {
105
	var node = this.domElement2TreeNode(e.target);
106
	if (node) {
107
		node.viewFocus();
108
		dojo.event.browser.stopEvent(e);
109
	}
110
}, onBlurNode:function (e) {
111
	var node = this.domElement2TreeNode(e.target);
112
	if (!node) {
113
		return;
114
	}
115
	var labelNode = node.labelNode;
116
	labelNode.setAttribute("tabIndex", "-1");
117
	node.viewUnfocus();
118
	dojo.event.browser.stopEvent(e);
119
	node.tree.domNode.setAttribute("tabIndex", "0");
120
}, _focusLabel:function (node) {
121
	var lastFocused = node.tree.lastFocused;
122
	var labelNode;
123
	if (lastFocused && lastFocused.labelNode) {
124
		labelNode = lastFocused.labelNode;
125
		dojo.event.disconnect(labelNode, "onblur", this, "onBlurNode");
126
		labelNode.setAttribute("tabIndex", "-1");
127
		dojo.html.removeClass(labelNode, "TreeLabelFocused");
128
	}
129
	labelNode = node.labelNode;
130
	labelNode.setAttribute("tabIndex", "0");
131
	node.tree.lastFocused = node;
132
	dojo.html.addClass(labelNode, "TreeLabelFocused");
133
	dojo.event.connectOnce(labelNode, "onblur", this, "onBlurNode");
134
	dojo.event.connectOnce(labelNode, "onfocus", this, "onFocusNode");
135
	labelNode.focus();
136
}, onKey:function (e) {
137
	if (!e.key || e.ctrkKey || e.altKey) {
138
		return;
139
	}
140
	var nodeWidget = this.domElement2TreeNode(e.target);
141
	if (!nodeWidget) {
142
		return;
143
	}
144
	var treeWidget = nodeWidget.tree;
145
	if (treeWidget.lastFocused && treeWidget.lastFocused.labelNode) {
146
		nodeWidget = treeWidget.lastFocused;
147
	}
148
	switch (e.key) {
149
	  case e.KEY_TAB:
150
		if (e.shiftKey) {
151
			treeWidget.domNode.setAttribute("tabIndex", "-1");
152
		}
153
		break;
154
	  case e.KEY_RIGHT_ARROW:
155
		this._focusZoomIn(nodeWidget);
156
		dojo.event.browser.stopEvent(e);
157
		break;
158
	  case e.KEY_LEFT_ARROW:
159
		this._focusZoomOut(nodeWidget);
160
		dojo.event.browser.stopEvent(e);
161
		break;
162
	  case e.KEY_UP_ARROW:
163
		this._focusPreviousVisible(nodeWidget);
164
		dojo.event.browser.stopEvent(e);
165
		break;
166
	  case e.KEY_DOWN_ARROW:
167
		this._focusNextVisible(nodeWidget);
168
		dojo.event.browser.stopEvent(e);
169
		break;
170
	}
171
}, onFocusTree:function (e) {
172
	if (!e.currentTarget) {
173
		return;
174
	}
175
	try {
176
		var treeWidget = this.getWidgetByNode(e.currentTarget);
177
		if (!treeWidget || !treeWidget.isTree) {
178
			return;
179
		}
180
		var nodeWidget = this.getWidgetByNode(treeWidget.domNode.firstChild);
181
		if (nodeWidget && nodeWidget.isTreeNode) {
182
			if (treeWidget.lastFocused && treeWidget.lastFocused.isTreeNode) {
183
				nodeWidget = treeWidget.lastFocused;
184
			}
185
			this._focusLabel(nodeWidget);
186
		}
187
	}
188
	catch (e) {
189
	}
190
}, onAfterTreeCreate:function (message) {
191
	var tree = message.source;
192
	dojo.event.browser.addListener(tree.domNode, "onKey", dojo.lang.hitch(this, this.onKey));
193
	dojo.event.browser.addListener(tree.domNode, "onmousedown", dojo.lang.hitch(this, this.onTreeMouseDown));
194
	dojo.event.browser.addListener(tree.domNode, "onclick", dojo.lang.hitch(this, this.onTreeClick));
195
	dojo.event.browser.addListener(tree.domNode, "onfocus", dojo.lang.hitch(this, this.onFocusTree));
196
	tree.domNode.setAttribute("tabIndex", "0");
197
	if (tree.expandLevel) {
198
		this.expandToLevel(tree, tree.expandLevel);
199
	}
200
	if (tree.loadLevel) {
201
		this.loadToLevel(tree, tree.loadLevel);
202
	}
203
}, onTreeMouseDown:function (e) {
204
}, onTreeClick:function (e) {
205
	var domElement = e.target;
206
	var node = this.domElement2TreeNode(domElement);
207
	if (!node || !node.isTreeNode) {
208
		return;
209
	}
210
	var checkExpandClick = function (el) {
211
		return el === node.expandNode;
212
	};
213
	if (this.checkPathCondition(domElement, checkExpandClick)) {
214
		this.processExpandClick(node);
215
	}
216
	this._focusLabel(node);
217
}, processExpandClick:function (node) {
218
	if (node.isExpanded) {
219
		this.collapse(node);
220
	} else {
221
		this.expand(node);
222
	}
223
}, batchExpandTimeout:20, expandAll:function (nodeOrTree) {
224
	return this.expandToLevel(nodeOrTree, Number.POSITIVE_INFINITY);
225
}, collapseAll:function (nodeOrTree) {
226
	var _this = this;
227
	var filter = function (elem) {
228
		return (elem instanceof dojo.widget.Widget) && elem.isFolder && elem.isExpanded;
229
	};
230
	if (nodeOrTree.isTreeNode) {
231
		this.processDescendants(nodeOrTree, filter, this.collapse);
232
	} else {
233
		if (nodeOrTree.isTree) {
234
			dojo.lang.forEach(nodeOrTree.children, function (c) {
235
				_this.processDescendants(c, filter, _this.collapse);
236
			});
237
		}
238
	}
239
}, expandToNode:function (node, withSelected) {
240
	n = withSelected ? node : node.parent;
241
	s = [];
242
	while (!n.isExpanded) {
243
		s.push(n);
244
		n = n.parent;
245
	}
246
	dojo.lang.forEach(s, function (n) {
247
		n.expand();
248
	});
249
}, expandToLevel:function (nodeOrTree, level) {
250
	dojo.require("dojo.widget.TreeTimeoutIterator");
251
	var _this = this;
252
	var filterFunc = function (elem) {
253
		var res = elem.isFolder || elem.children && elem.children.length;
254
		return res;
255
	};
256
	var callFunc = function (node, iterator) {
257
		_this.expand(node, true);
258
		iterator.forward();
259
	};
260
	var iterator = new dojo.widget.TreeTimeoutIterator(nodeOrTree, callFunc, this);
261
	iterator.setFilter(filterFunc);
262
	iterator.timeout = this.batchExpandTimeout;
263
	iterator.setMaxLevel(nodeOrTree.isTreeNode ? level - 1 : level);
264
	return iterator.start(nodeOrTree.isTreeNode);
265
}, getWidgetByNode:function (node) {
266
	var widgetId;
267
	var newNode = node;
268
	while (!(widgetId = newNode.widgetId)) {
269
		newNode = newNode.parentNode;
270
		if (newNode == null) {
271
			break;
272
		}
273
	}
274
	if (widgetId) {
275
		return dojo.widget.byId(widgetId);
276
	} else {
277
		if (node == null) {
278
			return null;
279
		} else {
280
			return dojo.widget.manager.byNode(node);
281
		}
282
	}
283
}, expand:function (node) {
284
	if (node.isFolder) {
285
		node.expand();
286
	}
287
}, collapse:function (node) {
288
	if (node.isFolder) {
289
		node.collapse();
290
	}
291
}, canEditLabel:function (node) {
292
	if (node.actionIsDisabledNow(node.actions.EDIT)) {
293
		return false;
294
	}
295
	return true;
296
}, editLabelStart:function (node) {
297
	if (!this.canEditLabel(node)) {
298
		return false;
299
	}
300
	if (!this.editor.isClosed()) {
301
		this.editLabelFinish(this.editor.saveOnBlur);
302
	}
303
	this.doEditLabelStart(node);
304
}, editLabelFinish:function (save) {
305
	this.doEditLabelFinish(save);
306
}, doEditLabelStart:function (node) {
307
	if (!this.editor) {
308
		dojo.raise(this.widgetType + ": no editor specified");
309
	}
310
	this.editor.open(node);
311
}, doEditLabelFinish:function (save, server_data) {
312
	if (!this.editor) {
313
		dojo.raise(this.widgetType + ": no editor specified");
314
	}
315
	var node = this.editor.node;
316
	var editorTitle = this.editor.getContents();
317
	this.editor.close(save);
318
	if (save) {
319
		var data = {title:editorTitle};
320
		if (server_data) {
321
			dojo.lang.mixin(data, server_data);
322
		}
323
		if (node.isPhantom) {
324
			var parent = node.parent;
325
			var index = node.getParentIndex();
326
			node.destroy();
327
			dojo.widget.TreeBasicControllerV3.prototype.doCreateChild.call(this, parent, index, data);
328
		} else {
329
			var title = server_data && server_data.title ? server_data.title : editorTitle;
330
			node.setTitle(title);
331
		}
332
	} else {
333
		if (node.isPhantom) {
334
			node.destroy();
335
		}
336
	}
337
}, makeDefaultNode:function (parent, index) {
338
	var data = {title:parent.tree.defaultChildTitle};
339
	return dojo.widget.TreeBasicControllerV3.prototype.doCreateChild.call(this, parent, index, data);
340
}, runStages:function (check, prepare, make, finalize, expose, args) {
341
	if (check && !check.apply(this, args)) {
342
		return false;
343
	}
344
	if (prepare && !prepare.apply(this, args)) {
345
		return false;
346
	}
347
	var result = make.apply(this, args);
348
	if (finalize) {
349
		finalize.apply(this, args);
350
	}
351
	if (!result) {
352
		return result;
353
	}
354
	if (expose) {
355
		expose.apply(this, args);
356
	}
357
	return result;
358
}});
359
dojo.lang.extend(dojo.widget.TreeBasicControllerV3, {createAndEdit:function (parent, index) {
360
	var data = {title:parent.tree.defaultChildTitle};
361
	if (!this.canCreateChild(parent, index, data)) {
362
		return false;
363
	}
364
	var child = this.doCreateChild(parent, index, data);
365
	if (!child) {
366
		return false;
367
	}
368
	this.exposeCreateChild(parent, index, data);
369
	child.isPhantom = true;
370
	if (!this.editor.isClosed()) {
371
		this.editLabelFinish(this.editor.saveOnBlur);
372
	}
373
	this.doEditLabelStart(child);
374
}});
375
dojo.lang.extend(dojo.widget.TreeBasicControllerV3, {canClone:function (child, newParent, index, deep) {
376
	return true;
377
}, clone:function (child, newParent, index, deep) {
378
	return this.runStages(this.canClone, this.prepareClone, this.doClone, this.finalizeClone, this.exposeClone, arguments);
379
}, exposeClone:function (child, newParent) {
380
	if (newParent.isTreeNode) {
381
		this.expand(newParent);
382
	}
383
}, doClone:function (child, newParent, index, deep) {
384
	var cloned = child.clone(deep);
385
	newParent.addChild(cloned, index);
386
	return cloned;
387
}});
388
dojo.lang.extend(dojo.widget.TreeBasicControllerV3, {canDetach:function (child) {
389
	if (child.actionIsDisabledNow(child.actions.DETACH)) {
390
		return false;
391
	}
392
	return true;
393
}, detach:function (node) {
394
	return this.runStages(this.canDetach, this.prepareDetach, this.doDetach, this.finalizeDetach, this.exposeDetach, arguments);
395
}, doDetach:function (node, callObj, callFunc) {
396
	node.detach();
397
}});
398
dojo.lang.extend(dojo.widget.TreeBasicControllerV3, {canDestroyChild:function (child) {
399
	if (child.parent && !this.canDetach(child)) {
400
		return false;
401
	}
402
	return true;
403
}, destroyChild:function (node) {
404
	return this.runStages(this.canDestroyChild, this.prepareDestroyChild, this.doDestroyChild, this.finalizeDestroyChild, this.exposeDestroyChild, arguments);
405
}, doDestroyChild:function (node) {
406
	node.destroy();
407
}});
408
dojo.lang.extend(dojo.widget.TreeBasicControllerV3, {canMoveNotANode:function (child, parent) {
409
	if (child.treeCanMove) {
410
		return child.treeCanMove(parent);
411
	}
412
	return true;
413
}, canMove:function (child, newParent) {
414
	if (!child.isTreeNode) {
415
		return this.canMoveNotANode(child, newParent);
416
	}
417
	if (child.actionIsDisabledNow(child.actions.MOVE)) {
418
		return false;
419
	}
420
	if (child.parent !== newParent && newParent.actionIsDisabledNow(newParent.actions.ADDCHILD)) {
421
		return false;
422
	}
423
	var node = newParent;
424
	while (node.isTreeNode) {
425
		if (node === child) {
426
			return false;
427
		}
428
		node = node.parent;
429
	}
430
	return true;
431
}, move:function (child, newParent, index) {
432
	return this.runStages(this.canMove, this.prepareMove, this.doMove, this.finalizeMove, this.exposeMove, arguments);
433
}, doMove:function (child, newParent, index) {
434
	child.tree.move(child, newParent, index);
435
	return true;
436
}, exposeMove:function (child, newParent) {
437
	if (newParent.isTreeNode) {
438
		this.expand(newParent);
439
	}
440
}});
441
dojo.lang.extend(dojo.widget.TreeBasicControllerV3, {canCreateChild:function (parent, index, data) {
442
	if (parent.actionIsDisabledNow(parent.actions.ADDCHILD)) {
443
		return false;
444
	}
445
	return true;
446
}, createChild:function (parent, index, data) {
447
	if (!data) {
448
		data = {title:parent.tree.defaultChildTitle};
449
	}
450
	return this.runStages(this.canCreateChild, this.prepareCreateChild, this.doCreateChild, this.finalizeCreateChild, this.exposeCreateChild, [parent, index, data]);
451
}, prepareCreateChild:function () {
452
	return true;
453
}, finalizeCreateChild:function () {
454
}, doCreateChild:function (parent, index, data) {
455
	var newChild = parent.tree.createNode(data);
456
	parent.addChild(newChild, index);
457
	return newChild;
458
}, exposeCreateChild:function (parent) {
459
	return this.expand(parent);
460
}});
461