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.TreeNode");
|
|
|
14 |
dojo.require("dojo.html.*");
|
|
|
15 |
dojo.require("dojo.event.*");
|
|
|
16 |
dojo.require("dojo.io.*");
|
|
|
17 |
dojo.widget.defineWidget("dojo.widget.TreeNode", dojo.widget.HtmlWidget, function () {
|
|
|
18 |
this.actionsDisabled = [];
|
|
|
19 |
}, {widgetType:"TreeNode", loadStates:{UNCHECKED:"UNCHECKED", LOADING:"LOADING", LOADED:"LOADED"}, actions:{MOVE:"MOVE", REMOVE:"REMOVE", EDIT:"EDIT", ADDCHILD:"ADDCHILD"}, isContainer:true, lockLevel:0, templateString:("<div class=\"dojoTreeNode\"> " + "<span treeNode=\"${this.widgetId}\" class=\"dojoTreeNodeLabel\" dojoAttachPoint=\"labelNode\"> " + "\t\t<span dojoAttachPoint=\"titleNode\" dojoAttachEvent=\"onClick: onTitleClick\" class=\"dojoTreeNodeLabelTitle\">${this.title}</span> " + "</span> " + "<span class=\"dojoTreeNodeAfterLabel\" dojoAttachPoint=\"afterLabelNode\">${this.afterLabel}</span> " + "<div dojoAttachPoint=\"containerNode\" style=\"display:none\"></div> " + "</div>").replace(/(>|<)\s+/g, "$1"), childIconSrc:"", childIconFolderSrc:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/closed.gif"), childIconDocumentSrc:dojo.uri.moduleUri("dojo.widget", "templates/images/Tree/document.gif"), childIcon:null, isTreeNode:true, objectId:"", afterLabel:"", afterLabelNode:null, expandIcon:null, title:"", object:"", isFolder:false, labelNode:null, titleNode:null, imgs:null, expandLevel:"", tree:null, depth:0, isExpanded:false, state:null, domNodeInitialized:false, isFirstChild:function () {
|
|
|
20 |
return this.getParentIndex() == 0 ? true : false;
|
|
|
21 |
}, isLastChild:function () {
|
|
|
22 |
return this.getParentIndex() == this.parent.children.length - 1 ? true : false;
|
|
|
23 |
}, lock:function () {
|
|
|
24 |
return this.tree.lock.apply(this, arguments);
|
|
|
25 |
}, unlock:function () {
|
|
|
26 |
return this.tree.unlock.apply(this, arguments);
|
|
|
27 |
}, isLocked:function () {
|
|
|
28 |
return this.tree.isLocked.apply(this, arguments);
|
|
|
29 |
}, cleanLock:function () {
|
|
|
30 |
return this.tree.cleanLock.apply(this, arguments);
|
|
|
31 |
}, actionIsDisabled:function (action) {
|
|
|
32 |
var _this = this;
|
|
|
33 |
var disabled = false;
|
|
|
34 |
if (this.tree.strictFolders && action == this.actions.ADDCHILD && !this.isFolder) {
|
|
|
35 |
disabled = true;
|
|
|
36 |
}
|
|
|
37 |
if (dojo.lang.inArray(_this.actionsDisabled, action)) {
|
|
|
38 |
disabled = true;
|
|
|
39 |
}
|
|
|
40 |
if (this.isLocked()) {
|
|
|
41 |
disabled = true;
|
|
|
42 |
}
|
|
|
43 |
return disabled;
|
|
|
44 |
}, getInfo:function () {
|
|
|
45 |
var info = {widgetId:this.widgetId, objectId:this.objectId, index:this.getParentIndex(), isFolder:this.isFolder};
|
|
|
46 |
return info;
|
|
|
47 |
}, initialize:function (args, frag) {
|
|
|
48 |
this.state = this.loadStates.UNCHECKED;
|
|
|
49 |
for (var i = 0; i < this.actionsDisabled.length; i++) {
|
|
|
50 |
this.actionsDisabled[i] = this.actionsDisabled[i].toUpperCase();
|
|
|
51 |
}
|
|
|
52 |
this.expandLevel = parseInt(this.expandLevel);
|
|
|
53 |
}, adjustDepth:function (depthDiff) {
|
|
|
54 |
for (var i = 0; i < this.children.length; i++) {
|
|
|
55 |
this.children[i].adjustDepth(depthDiff);
|
|
|
56 |
}
|
|
|
57 |
this.depth += depthDiff;
|
|
|
58 |
if (depthDiff > 0) {
|
|
|
59 |
for (var i = 0; i < depthDiff; i++) {
|
|
|
60 |
var img = this.tree.makeBlankImg();
|
|
|
61 |
this.imgs.unshift(img);
|
|
|
62 |
dojo.html.insertBefore(this.imgs[0], this.domNode.firstChild);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
if (depthDiff < 0) {
|
|
|
66 |
for (var i = 0; i < -depthDiff; i++) {
|
|
|
67 |
this.imgs.shift();
|
|
|
68 |
dojo.html.removeNode(this.domNode.firstChild);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
}, markLoading:function () {
|
|
|
72 |
this._markLoadingSavedIcon = this.expandIcon.src;
|
|
|
73 |
this.expandIcon.src = this.tree.expandIconSrcLoading;
|
|
|
74 |
}, unMarkLoading:function () {
|
|
|
75 |
if (!this._markLoadingSavedIcon) {
|
|
|
76 |
return;
|
|
|
77 |
}
|
|
|
78 |
var im = new Image();
|
|
|
79 |
im.src = this.tree.expandIconSrcLoading;
|
|
|
80 |
if (this.expandIcon.src == im.src) {
|
|
|
81 |
this.expandIcon.src = this._markLoadingSavedIcon;
|
|
|
82 |
}
|
|
|
83 |
this._markLoadingSavedIcon = null;
|
|
|
84 |
}, setFolder:function () {
|
|
|
85 |
dojo.event.connect(this.expandIcon, "onclick", this, "onTreeClick");
|
|
|
86 |
this.expandIcon.src = this.isExpanded ? this.tree.expandIconSrcMinus : this.tree.expandIconSrcPlus;
|
|
|
87 |
this.isFolder = true;
|
|
|
88 |
}, createDOMNode:function (tree, depth) {
|
|
|
89 |
this.tree = tree;
|
|
|
90 |
this.depth = depth;
|
|
|
91 |
this.imgs = [];
|
|
|
92 |
for (var i = 0; i < this.depth + 1; i++) {
|
|
|
93 |
var img = this.tree.makeBlankImg();
|
|
|
94 |
this.domNode.insertBefore(img, this.labelNode);
|
|
|
95 |
this.imgs.push(img);
|
|
|
96 |
}
|
|
|
97 |
this.expandIcon = this.imgs[this.imgs.length - 1];
|
|
|
98 |
this.childIcon = this.tree.makeBlankImg();
|
|
|
99 |
this.imgs.push(this.childIcon);
|
|
|
100 |
dojo.html.insertBefore(this.childIcon, this.titleNode);
|
|
|
101 |
if (this.children.length || this.isFolder) {
|
|
|
102 |
this.setFolder();
|
|
|
103 |
} else {
|
|
|
104 |
this.state = this.loadStates.LOADED;
|
|
|
105 |
}
|
|
|
106 |
dojo.event.connect(this.childIcon, "onclick", this, "onIconClick");
|
|
|
107 |
for (var i = 0; i < this.children.length; i++) {
|
|
|
108 |
this.children[i].parent = this;
|
|
|
109 |
var node = this.children[i].createDOMNode(this.tree, this.depth + 1);
|
|
|
110 |
this.containerNode.appendChild(node);
|
|
|
111 |
}
|
|
|
112 |
if (this.children.length) {
|
|
|
113 |
this.state = this.loadStates.LOADED;
|
|
|
114 |
}
|
|
|
115 |
this.updateIcons();
|
|
|
116 |
this.domNodeInitialized = true;
|
|
|
117 |
dojo.event.topic.publish(this.tree.eventNames.createDOMNode, {source:this});
|
|
|
118 |
return this.domNode;
|
|
|
119 |
}, onTreeClick:function (e) {
|
|
|
120 |
dojo.event.topic.publish(this.tree.eventNames.treeClick, {source:this, event:e});
|
|
|
121 |
}, onIconClick:function (e) {
|
|
|
122 |
dojo.event.topic.publish(this.tree.eventNames.iconClick, {source:this, event:e});
|
|
|
123 |
}, onTitleClick:function (e) {
|
|
|
124 |
dojo.event.topic.publish(this.tree.eventNames.titleClick, {source:this, event:e});
|
|
|
125 |
}, markSelected:function () {
|
|
|
126 |
dojo.html.addClass(this.titleNode, "dojoTreeNodeLabelSelected");
|
|
|
127 |
}, unMarkSelected:function () {
|
|
|
128 |
dojo.html.removeClass(this.titleNode, "dojoTreeNodeLabelSelected");
|
|
|
129 |
}, updateExpandIcon:function () {
|
|
|
130 |
if (this.isFolder) {
|
|
|
131 |
this.expandIcon.src = this.isExpanded ? this.tree.expandIconSrcMinus : this.tree.expandIconSrcPlus;
|
|
|
132 |
} else {
|
|
|
133 |
this.expandIcon.src = this.tree.blankIconSrc;
|
|
|
134 |
}
|
|
|
135 |
}, updateExpandGrid:function () {
|
|
|
136 |
if (this.tree.showGrid) {
|
|
|
137 |
if (this.depth) {
|
|
|
138 |
this.setGridImage(-2, this.isLastChild() ? this.tree.gridIconSrcL : this.tree.gridIconSrcT);
|
|
|
139 |
} else {
|
|
|
140 |
if (this.isFirstChild()) {
|
|
|
141 |
this.setGridImage(-2, this.isLastChild() ? this.tree.gridIconSrcX : this.tree.gridIconSrcY);
|
|
|
142 |
} else {
|
|
|
143 |
this.setGridImage(-2, this.isLastChild() ? this.tree.gridIconSrcL : this.tree.gridIconSrcT);
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
} else {
|
|
|
147 |
this.setGridImage(-2, this.tree.blankIconSrc);
|
|
|
148 |
}
|
|
|
149 |
}, updateChildGrid:function () {
|
|
|
150 |
if ((this.depth || this.tree.showRootGrid) && this.tree.showGrid) {
|
|
|
151 |
this.setGridImage(-1, (this.children.length && this.isExpanded) ? this.tree.gridIconSrcP : this.tree.gridIconSrcC);
|
|
|
152 |
} else {
|
|
|
153 |
if (this.tree.showGrid && !this.tree.showRootGrid) {
|
|
|
154 |
this.setGridImage(-1, (this.children.length && this.isExpanded) ? this.tree.gridIconSrcZ : this.tree.blankIconSrc);
|
|
|
155 |
} else {
|
|
|
156 |
this.setGridImage(-1, this.tree.blankIconSrc);
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
}, updateParentGrid:function () {
|
|
|
160 |
var parent = this.parent;
|
|
|
161 |
for (var i = 0; i < this.depth; i++) {
|
|
|
162 |
var idx = this.imgs.length - (3 + i);
|
|
|
163 |
var img = (this.tree.showGrid && !parent.isLastChild()) ? this.tree.gridIconSrcV : this.tree.blankIconSrc;
|
|
|
164 |
this.setGridImage(idx, img);
|
|
|
165 |
parent = parent.parent;
|
|
|
166 |
}
|
|
|
167 |
}, updateExpandGridColumn:function () {
|
|
|
168 |
if (!this.tree.showGrid) {
|
|
|
169 |
return;
|
|
|
170 |
}
|
|
|
171 |
var _this = this;
|
|
|
172 |
var icon = this.isLastChild() ? this.tree.blankIconSrc : this.tree.gridIconSrcV;
|
|
|
173 |
dojo.lang.forEach(_this.getDescendants(), function (node) {
|
|
|
174 |
node.setGridImage(_this.depth, icon);
|
|
|
175 |
});
|
|
|
176 |
this.updateExpandGrid();
|
|
|
177 |
}, updateIcons:function () {
|
|
|
178 |
this.imgs[0].style.display = this.tree.showRootGrid ? "inline" : "none";
|
|
|
179 |
this.buildChildIcon();
|
|
|
180 |
this.updateExpandGrid();
|
|
|
181 |
this.updateChildGrid();
|
|
|
182 |
this.updateParentGrid();
|
|
|
183 |
dojo.profile.stop("updateIcons");
|
|
|
184 |
}, buildChildIcon:function () {
|
|
|
185 |
if (this.childIconSrc) {
|
|
|
186 |
this.childIcon.src = this.childIconSrc;
|
|
|
187 |
}
|
|
|
188 |
this.childIcon.style.display = this.childIconSrc ? "inline" : "none";
|
|
|
189 |
}, setGridImage:function (idx, src) {
|
|
|
190 |
if (idx < 0) {
|
|
|
191 |
idx = this.imgs.length + idx;
|
|
|
192 |
}
|
|
|
193 |
this.imgs[idx].style.backgroundImage = "url(" + src + ")";
|
|
|
194 |
}, updateIconTree:function () {
|
|
|
195 |
this.tree.updateIconTree.call(this);
|
|
|
196 |
}, expand:function () {
|
|
|
197 |
if (this.isExpanded) {
|
|
|
198 |
return;
|
|
|
199 |
}
|
|
|
200 |
if (this.children.length) {
|
|
|
201 |
this.showChildren();
|
|
|
202 |
}
|
|
|
203 |
this.isExpanded = true;
|
|
|
204 |
this.updateExpandIcon();
|
|
|
205 |
dojo.event.topic.publish(this.tree.eventNames.expand, {source:this});
|
|
|
206 |
}, collapse:function () {
|
|
|
207 |
if (!this.isExpanded) {
|
|
|
208 |
return;
|
|
|
209 |
}
|
|
|
210 |
this.hideChildren();
|
|
|
211 |
this.isExpanded = false;
|
|
|
212 |
this.updateExpandIcon();
|
|
|
213 |
dojo.event.topic.publish(this.tree.eventNames.collapse, {source:this});
|
|
|
214 |
}, hideChildren:function () {
|
|
|
215 |
this.tree.toggleObj.hide(this.containerNode, this.toggleDuration, this.explodeSrc, dojo.lang.hitch(this, "onHide"));
|
|
|
216 |
if (dojo.exists(dojo, "dnd.dragManager.dragObjects") && dojo.dnd.dragManager.dragObjects.length) {
|
|
|
217 |
dojo.dnd.dragManager.cacheTargetLocations();
|
|
|
218 |
}
|
|
|
219 |
}, showChildren:function () {
|
|
|
220 |
this.tree.toggleObj.show(this.containerNode, this.toggleDuration, this.explodeSrc, dojo.lang.hitch(this, "onShow"));
|
|
|
221 |
if (dojo.exists(dojo, "dnd.dragManager.dragObjects") && dojo.dnd.dragManager.dragObjects.length) {
|
|
|
222 |
dojo.dnd.dragManager.cacheTargetLocations();
|
|
|
223 |
}
|
|
|
224 |
}, addChild:function () {
|
|
|
225 |
return this.tree.addChild.apply(this, arguments);
|
|
|
226 |
}, doAddChild:function () {
|
|
|
227 |
return this.tree.doAddChild.apply(this, arguments);
|
|
|
228 |
}, edit:function (props) {
|
|
|
229 |
dojo.lang.mixin(this, props);
|
|
|
230 |
if (props.title) {
|
|
|
231 |
this.titleNode.innerHTML = this.title;
|
|
|
232 |
}
|
|
|
233 |
if (props.afterLabel) {
|
|
|
234 |
this.afterLabelNode.innerHTML = this.afterLabel;
|
|
|
235 |
}
|
|
|
236 |
if (props.childIconSrc) {
|
|
|
237 |
this.buildChildIcon();
|
|
|
238 |
}
|
|
|
239 |
}, removeNode:function () {
|
|
|
240 |
return this.tree.removeNode.apply(this, arguments);
|
|
|
241 |
}, doRemoveNode:function () {
|
|
|
242 |
return this.tree.doRemoveNode.apply(this, arguments);
|
|
|
243 |
}, toString:function () {
|
|
|
244 |
return "[" + this.widgetType + " Tree:" + this.tree + " ID:" + this.widgetId + " Title:" + this.title + "]";
|
|
|
245 |
}});
|
|
|
246 |
|