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.TreeSelectorV3");
12
dojo.require("dojo.widget.HtmlWidget");
13
dojo.require("dojo.widget.TreeCommon");
14
dojo.widget.defineWidget("dojo.widget.TreeSelectorV3", [dojo.widget.HtmlWidget, dojo.widget.TreeCommon], function () {
15
	this.eventNames = {};
16
	this.listenedTrees = {};
17
	this.selectedNodes = [];
18
	this.lastClicked = {};
19
}, {listenTreeEvents:["afterTreeCreate", "afterCollapse", "afterChangeTree", "afterDetach", "beforeTreeDestroy"], listenNodeFilter:function (elem) {
20
	return elem instanceof dojo.widget.Widget;
21
}, allowedMulti:true, dblselectTimeout:300, eventNamesDefault:{select:"select", deselect:"deselect", dblselect:"dblselect"}, onAfterTreeCreate:function (message) {
22
	var tree = message.source;
23
	dojo.event.browser.addListener(tree.domNode, "onclick", dojo.lang.hitch(this, this.onTreeClick));
24
	if (dojo.render.html.ie) {
25
		dojo.event.browser.addListener(tree.domNode, "ondblclick", dojo.lang.hitch(this, this.onTreeDblClick));
26
	}
27
	dojo.event.browser.addListener(tree.domNode, "onKey", dojo.lang.hitch(this, this.onKey));
28
}, onKey:function (e) {
29
	if (!e.key || e.ctrkKey || e.altKey) {
30
		return;
31
	}
32
	switch (e.key) {
33
	  case e.KEY_ENTER:
34
		var node = this.domElement2TreeNode(e.target);
35
		if (node) {
36
			this.processNode(node, e);
37
		}
38
	}
39
}, onAfterChangeTree:function (message) {
40
	if (!message.oldTree && message.node.selected) {
41
		this.select(message.node);
42
	}
43
	if (!message.newTree || !this.listenedTrees[message.newTree.widgetId]) {
44
		if (this.selectedNode && message.node.children) {
45
			this.deselectIfAncestorMatch(message.node);
46
		}
47
	}
48
}, initialize:function (args) {
49
	for (name in this.eventNamesDefault) {
50
		if (dojo.lang.isUndefined(this.eventNames[name])) {
51
			this.eventNames[name] = this.widgetId + "/" + this.eventNamesDefault[name];
52
		}
53
	}
54
}, onBeforeTreeDestroy:function (message) {
55
	this.unlistenTree(message.source);
56
}, onAfterCollapse:function (message) {
57
	this.deselectIfAncestorMatch(message.source);
58
}, onTreeDblClick:function (event) {
59
	this.onTreeClick(event);
60
}, checkSpecialEvent:function (event) {
61
	return event.shiftKey || event.ctrlKey;
62
}, onTreeClick:function (event) {
63
	var node = this.domElement2TreeNode(event.target);
64
	if (!node) {
65
		return;
66
	}
67
	var checkLabelClick = function (domElement) {
68
		return domElement === node.labelNode;
69
	};
70
	if (this.checkPathCondition(event.target, checkLabelClick)) {
71
		this.processNode(node, event);
72
	}
73
}, processNode:function (node, event) {
74
	if (node.actionIsDisabled(node.actions.SELECT)) {
75
		return;
76
	}
77
	if (dojo.lang.inArray(this.selectedNodes, node)) {
78
		if (this.checkSpecialEvent(event)) {
79
			this.deselect(node);
80
			return;
81
		}
82
		var _this = this;
83
		var i = 0;
84
		var selectedNode;
85
		while (this.selectedNodes.length > i) {
86
			selectedNode = this.selectedNodes[i];
87
			if (selectedNode !== node) {
88
				this.deselect(selectedNode);
89
				continue;
90
			}
91
			i++;
92
		}
93
		var wasJustClicked = this.checkRecentClick(node);
94
		eventName = wasJustClicked ? this.eventNames.dblselect : this.eventNames.select;
95
		if (wasJustClicked) {
96
			eventName = this.eventNames.dblselect;
97
			this.forgetLastClicked();
98
		} else {
99
			eventName = this.eventNames.select;
100
			this.setLastClicked(node);
101
		}
102
		dojo.event.topic.publish(eventName, {node:node});
103
		return;
104
	}
105
	this.deselectIfNoMulti(event);
106
	this.setLastClicked(node);
107
	this.select(node);
108
}, forgetLastClicked:function () {
109
	this.lastClicked = {};
110
}, setLastClicked:function (node) {
111
	this.lastClicked.date = new Date();
112
	this.lastClicked.node = node;
113
}, checkRecentClick:function (node) {
114
	var diff = new Date() - this.lastClicked.date;
115
	if (this.lastClicked.node && diff < this.dblselectTimeout) {
116
		return true;
117
	} else {
118
		return false;
119
	}
120
}, deselectIfNoMulti:function (event) {
121
	if (!this.checkSpecialEvent(event) || !this.allowedMulti) {
122
		this.deselectAll();
123
	}
124
}, deselectIfAncestorMatch:function (ancestor) {
125
	var _this = this;
126
	dojo.lang.forEach(this.selectedNodes, function (node) {
127
		var selectedNode = node;
128
		node = node.parent;
129
		while (node && node.isTreeNode) {
130
			if (node === ancestor) {
131
				_this.deselect(selectedNode);
132
				return;
133
			}
134
			node = node.parent;
135
		}
136
	});
137
}, onAfterDetach:function (message) {
138
	this.deselectIfAncestorMatch(message.child);
139
}, select:function (node) {
140
	var index = dojo.lang.find(this.selectedNodes, node, true);
141
	if (index >= 0) {
142
		return;
143
	}
144
	this.selectedNodes.push(node);
145
	dojo.event.topic.publish(this.eventNames.select, {node:node});
146
}, deselect:function (node) {
147
	var index = dojo.lang.find(this.selectedNodes, node, true);
148
	if (index < 0) {
149
		return;
150
	}
151
	this.selectedNodes.splice(index, 1);
152
	dojo.event.topic.publish(this.eventNames.deselect, {node:node});
153
}, deselectAll:function () {
154
	while (this.selectedNodes.length) {
155
		this.deselect(this.selectedNodes[0]);
156
	}
157
}});
158