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.Tree");
|
|
|
12 |
dojo.require("dojo.widget.*");
|
|
|
13 |
dojo.require("dojo.event.*");
|
|
|
14 |
dojo.require("dojo.io.*");
|
|
|
15 |
dojo.require("dojo.widget.HtmlWidget");
|
|
|
16 |
dojo.require("dojo.widget.TreeNode");
|
|
|
17 |
dojo.require("dojo.html.common");
|
|
|
18 |
dojo.require("dojo.html.selection");
|
|
|
19 |
dojo.widget.defineWidget("dojo.widget.Tree", dojo.widget.HtmlWidget, function () {
|
|
|
20 |
this.eventNames = {};
|
|
|
21 |
this.tree = this;
|
|
|
22 |
this.DNDAcceptTypes = [];
|
|
|
23 |
this.actionsDisabled = [];
|
|
|
24 |
}, {widgetType:"Tree", eventNamesDefault:{createDOMNode:"createDOMNode", treeCreate:"treeCreate", treeDestroy:"treeDestroy", treeClick:"treeClick", iconClick:"iconClick", titleClick:"titleClick", moveFrom:"moveFrom", moveTo:"moveTo", addChild:"addChild", removeNode:"removeNode", expand:"expand", collapse:"collapse"}, isContainer:true, DNDMode:"off", lockLevel:0, strictFolders:true, DNDModes:{BETWEEN:1, ONTO:2}, DNDAcceptTypes:"", templateCssString:"\n.dojoTree {\n\tfont: caption;\n\tfont-size: 11px;\n\tfont-weight: normal;\n\toverflow: auto;\n}\n\n\n.dojoTreeNodeLabelTitle {\n\tpadding-left: 2px;\n\tcolor: WindowText;\n}\n\n.dojoTreeNodeLabel {\n\tcursor:hand;\n\tcursor:pointer;\n}\n\n.dojoTreeNodeLabelTitle:hover {\n\ttext-decoration: underline;\n}\n\n.dojoTreeNodeLabelSelected {\n\tbackground-color: Highlight;\n\tcolor: HighlightText;\n}\n\n.dojoTree div {\n\twhite-space: nowrap;\n}\n\n.dojoTree img, .dojoTreeNodeLabel img {\n\tvertical-align: middle;\n}\n\n", templateCssPath:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/Tree.css"), templateString:"<div class=\"dojoTree\"></div>", isExpanded:true, isTree:true, objectId:"", controller:"", selector:"", menu:"", expandLevel:"", blankIconSrc:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_blank.gif"), gridIconSrcT:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_t.gif"), gridIconSrcL:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_l.gif"), gridIconSrcV:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_v.gif"), gridIconSrcP:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_p.gif"), gridIconSrcC:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_c.gif"), gridIconSrcX:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_x.gif"), gridIconSrcY:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_y.gif"), gridIconSrcZ:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_grid_z.gif"), expandIconSrcPlus:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_expand_plus.gif"), expandIconSrcMinus:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_expand_minus.gif"), expandIconSrcLoading:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/treenode_loading.gif"), iconWidth:18, iconHeight:18, showGrid:true, showRootGrid:true, actionIsDisabled:function (action) {
|
|
|
25 |
var _this = this;
|
|
|
26 |
return dojo.lang.inArray(_this.actionsDisabled, action);
|
|
|
27 |
}, actions:{ADDCHILD:"ADDCHILD"}, getInfo:function () {
|
|
|
28 |
var info = {widgetId:this.widgetId, objectId:this.objectId};
|
|
|
29 |
return info;
|
|
|
30 |
}, initializeController:function () {
|
|
|
31 |
if (this.controller != "off") {
|
|
|
32 |
if (this.controller) {
|
|
|
33 |
this.controller = dojo.widget.byId(this.controller);
|
|
|
34 |
} else {
|
|
|
35 |
dojo.require("dojo.widget.TreeBasicController");
|
|
|
36 |
this.controller = dojo.widget.createWidget("TreeBasicController", {DNDController:(this.DNDMode ? "create" : ""), dieWithTree:true});
|
|
|
37 |
}
|
|
|
38 |
this.controller.listenTree(this);
|
|
|
39 |
} else {
|
|
|
40 |
this.controller = null;
|
|
|
41 |
}
|
|
|
42 |
}, initializeSelector:function () {
|
|
|
43 |
if (this.selector != "off") {
|
|
|
44 |
if (this.selector) {
|
|
|
45 |
this.selector = dojo.widget.byId(this.selector);
|
|
|
46 |
} else {
|
|
|
47 |
dojo.require("dojo.widget.TreeSelector");
|
|
|
48 |
this.selector = dojo.widget.createWidget("TreeSelector", {dieWithTree:true});
|
|
|
49 |
}
|
|
|
50 |
this.selector.listenTree(this);
|
|
|
51 |
} else {
|
|
|
52 |
this.selector = null;
|
|
|
53 |
}
|
|
|
54 |
}, initialize:function (args, frag) {
|
|
|
55 |
var _this = this;
|
|
|
56 |
for (name in this.eventNamesDefault) {
|
|
|
57 |
if (dojo.lang.isUndefined(this.eventNames[name])) {
|
|
|
58 |
this.eventNames[name] = this.widgetId + "/" + this.eventNamesDefault[name];
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
for (var i = 0; i < this.actionsDisabled.length; i++) {
|
|
|
62 |
this.actionsDisabled[i] = this.actionsDisabled[i].toUpperCase();
|
|
|
63 |
}
|
|
|
64 |
if (this.DNDMode == "off") {
|
|
|
65 |
this.DNDMode = 0;
|
|
|
66 |
} else {
|
|
|
67 |
if (this.DNDMode == "between") {
|
|
|
68 |
this.DNDMode = this.DNDModes.ONTO | this.DNDModes.BETWEEN;
|
|
|
69 |
} else {
|
|
|
70 |
if (this.DNDMode == "onto") {
|
|
|
71 |
this.DNDMode = this.DNDModes.ONTO;
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
this.expandLevel = parseInt(this.expandLevel);
|
|
|
76 |
this.initializeSelector();
|
|
|
77 |
this.initializeController();
|
|
|
78 |
if (this.menu) {
|
|
|
79 |
this.menu = dojo.widget.byId(this.menu);
|
|
|
80 |
this.menu.listenTree(this);
|
|
|
81 |
}
|
|
|
82 |
this.containerNode = this.domNode;
|
|
|
83 |
}, postCreate:function () {
|
|
|
84 |
this.createDOMNode();
|
|
|
85 |
}, createDOMNode:function () {
|
|
|
86 |
dojo.html.disableSelection(this.domNode);
|
|
|
87 |
for (var i = 0; i < this.children.length; i++) {
|
|
|
88 |
this.children[i].parent = this;
|
|
|
89 |
var node = this.children[i].createDOMNode(this, 0);
|
|
|
90 |
this.domNode.appendChild(node);
|
|
|
91 |
}
|
|
|
92 |
if (!this.showRootGrid) {
|
|
|
93 |
for (var i = 0; i < this.children.length; i++) {
|
|
|
94 |
this.children[i].expand();
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
dojo.event.topic.publish(this.eventNames.treeCreate, {source:this});
|
|
|
98 |
}, destroy:function () {
|
|
|
99 |
dojo.event.topic.publish(this.tree.eventNames.treeDestroy, {source:this});
|
|
|
100 |
return dojo.widget.HtmlWidget.prototype.destroy.apply(this, arguments);
|
|
|
101 |
}, addChild:function (child, index) {
|
|
|
102 |
var message = {child:child, index:index, parent:this, domNodeInitialized:child.domNodeInitialized};
|
|
|
103 |
this.doAddChild.apply(this, arguments);
|
|
|
104 |
dojo.event.topic.publish(this.tree.eventNames.addChild, message);
|
|
|
105 |
}, doAddChild:function (child, index) {
|
|
|
106 |
if (dojo.lang.isUndefined(index)) {
|
|
|
107 |
index = this.children.length;
|
|
|
108 |
}
|
|
|
109 |
if (!child.isTreeNode) {
|
|
|
110 |
dojo.raise("You can only add TreeNode widgets to a " + this.widgetType + " widget!");
|
|
|
111 |
return;
|
|
|
112 |
}
|
|
|
113 |
if (this.isTreeNode) {
|
|
|
114 |
if (!this.isFolder) {
|
|
|
115 |
this.setFolder();
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
var _this = this;
|
|
|
119 |
dojo.lang.forEach(child.getDescendants(), function (elem) {
|
|
|
120 |
elem.tree = _this.tree;
|
|
|
121 |
});
|
|
|
122 |
child.parent = this;
|
|
|
123 |
if (this.isTreeNode) {
|
|
|
124 |
this.state = this.loadStates.LOADED;
|
|
|
125 |
}
|
|
|
126 |
if (index < this.children.length) {
|
|
|
127 |
dojo.html.insertBefore(child.domNode, this.children[index].domNode);
|
|
|
128 |
} else {
|
|
|
129 |
this.containerNode.appendChild(child.domNode);
|
|
|
130 |
if (this.isExpanded && this.isTreeNode) {
|
|
|
131 |
this.showChildren();
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
this.children.splice(index, 0, child);
|
|
|
135 |
if (child.domNodeInitialized) {
|
|
|
136 |
var d = this.isTreeNode ? this.depth : -1;
|
|
|
137 |
child.adjustDepth(d - child.depth + 1);
|
|
|
138 |
child.updateIconTree();
|
|
|
139 |
} else {
|
|
|
140 |
child.depth = this.isTreeNode ? this.depth + 1 : 0;
|
|
|
141 |
child.createDOMNode(child.tree, child.depth);
|
|
|
142 |
}
|
|
|
143 |
var prevSibling = child.getPreviousSibling();
|
|
|
144 |
if (child.isLastChild() && prevSibling) {
|
|
|
145 |
prevSibling.updateExpandGridColumn();
|
|
|
146 |
}
|
|
|
147 |
}, makeBlankImg:function () {
|
|
|
148 |
var img = document.createElement("img");
|
|
|
149 |
img.style.width = this.iconWidth + "px";
|
|
|
150 |
img.style.height = this.iconHeight + "px";
|
|
|
151 |
img.src = this.blankIconSrc;
|
|
|
152 |
img.style.verticalAlign = "middle";
|
|
|
153 |
return img;
|
|
|
154 |
}, updateIconTree:function () {
|
|
|
155 |
if (!this.isTree) {
|
|
|
156 |
this.updateIcons();
|
|
|
157 |
}
|
|
|
158 |
for (var i = 0; i < this.children.length; i++) {
|
|
|
159 |
this.children[i].updateIconTree();
|
|
|
160 |
}
|
|
|
161 |
}, toString:function () {
|
|
|
162 |
return "[" + this.widgetType + " ID:" + this.widgetId + "]";
|
|
|
163 |
}, move:function (child, newParent, index) {
|
|
|
164 |
var oldParent = child.parent;
|
|
|
165 |
var oldTree = child.tree;
|
|
|
166 |
this.doMove.apply(this, arguments);
|
|
|
167 |
var newParent = child.parent;
|
|
|
168 |
var newTree = child.tree;
|
|
|
169 |
var message = {oldParent:oldParent, oldTree:oldTree, newParent:newParent, newTree:newTree, child:child};
|
|
|
170 |
dojo.event.topic.publish(oldTree.eventNames.moveFrom, message);
|
|
|
171 |
dojo.event.topic.publish(newTree.eventNames.moveTo, message);
|
|
|
172 |
}, doMove:function (child, newParent, index) {
|
|
|
173 |
child.parent.doRemoveNode(child);
|
|
|
174 |
newParent.doAddChild(child, index);
|
|
|
175 |
}, removeNode:function (child) {
|
|
|
176 |
if (!child.parent) {
|
|
|
177 |
return;
|
|
|
178 |
}
|
|
|
179 |
var oldTree = child.tree;
|
|
|
180 |
var oldParent = child.parent;
|
|
|
181 |
var removedChild = this.doRemoveNode.apply(this, arguments);
|
|
|
182 |
dojo.event.topic.publish(this.tree.eventNames.removeNode, {child:removedChild, tree:oldTree, parent:oldParent});
|
|
|
183 |
return removedChild;
|
|
|
184 |
}, doRemoveNode:function (child) {
|
|
|
185 |
if (!child.parent) {
|
|
|
186 |
return;
|
|
|
187 |
}
|
|
|
188 |
var parent = child.parent;
|
|
|
189 |
var children = parent.children;
|
|
|
190 |
var index = child.getParentIndex();
|
|
|
191 |
if (index < 0) {
|
|
|
192 |
dojo.raise("Couldn't find node " + child + " for removal");
|
|
|
193 |
}
|
|
|
194 |
children.splice(index, 1);
|
|
|
195 |
dojo.html.removeNode(child.domNode);
|
|
|
196 |
if (parent.children.length == 0 && !parent.isTree) {
|
|
|
197 |
parent.containerNode.style.display = "none";
|
|
|
198 |
}
|
|
|
199 |
if (index == children.length && index > 0) {
|
|
|
200 |
children[index - 1].updateExpandGridColumn();
|
|
|
201 |
}
|
|
|
202 |
if (parent instanceof dojo.widget.Tree && index == 0 && children.length > 0) {
|
|
|
203 |
children[0].updateExpandGrid();
|
|
|
204 |
}
|
|
|
205 |
child.parent = child.tree = null;
|
|
|
206 |
return child;
|
|
|
207 |
}, markLoading:function () {
|
|
|
208 |
}, unMarkLoading:function () {
|
|
|
209 |
}, lock:function () {
|
|
|
210 |
!this.lockLevel && this.markLoading();
|
|
|
211 |
this.lockLevel++;
|
|
|
212 |
}, unlock:function () {
|
|
|
213 |
if (!this.lockLevel) {
|
|
|
214 |
dojo.raise("unlock: not locked");
|
|
|
215 |
}
|
|
|
216 |
this.lockLevel--;
|
|
|
217 |
!this.lockLevel && this.unMarkLoading();
|
|
|
218 |
}, isLocked:function () {
|
|
|
219 |
var node = this;
|
|
|
220 |
while (true) {
|
|
|
221 |
if (node.lockLevel) {
|
|
|
222 |
return true;
|
|
|
223 |
}
|
|
|
224 |
if (node instanceof dojo.widget.Tree) {
|
|
|
225 |
break;
|
|
|
226 |
}
|
|
|
227 |
node = node.parent;
|
|
|
228 |
}
|
|
|
229 |
return false;
|
|
|
230 |
}, flushLock:function () {
|
|
|
231 |
this.lockLevel = 0;
|
|
|
232 |
this.unMarkLoading();
|
|
|
233 |
}});
|
|
|
234 |
|