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