2 * (c) 2005-2009 Richard Cowin (http://openrico.org)
3 * (c) 2005-2009 Matt Brown (http://dowdybrown.com)
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6 * file except in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the
11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12 * either express or implied. See the License for the specific language governing permissions
13 * and limitations under the License.
17 // Requires prototype.js and ricoCommon.js
18 // Data for the tree can be obtained via AJAX requests
19 // Each record in the AJAX response should contain 5 or 6 cells:
20 // cells[0]=parent node id
22 // cells[2]=description
23 // cells[3]=L/zero (leaf), C/non-zero (container)
24 // cells[4]=0->not selectable, 1->selectable (use default action), otherwise the node is selectable and cells[4] contains the action
25 // cells[5]=leafIcon (optional)
28 Rico.TreeControl = function(id,url,options) {
29 this.initialize(id,url,options);
32 Rico.TreeControl.prototype = {
34 * @class Implements a pop-up tree control.
37 * @param id unique identifier
38 * @param url data source
39 * @param options object may contain any of the following:<dl>
40 * <dt>nodeIdDisplay</dt><dd> first, last, tooltip, or none? default=none</dd>
41 * <dt>showCheckBox </dt><dd> show checkbox next to each item? default=false</dd>
42 * <dt>showFolders </dt><dd> show folder icons? default=false</dd>
43 * <dt>showPlusMinus</dt><dd> show +/- icons to open/close branches? default=true</dd>
44 * <dt>showLines </dt><dd> show vertical lines connecting each level? default=true</dd>
45 * <dt>defaultAction</dt><dd> Rico event handle to call when user clicks on an item, default is to call returnValue method</dd>
46 * <dt>height </dt><dd> control height? default=300px</dd>
47 * <dt>width </dt><dd> control width? default=300px</dd>
48 * <dt>leafIcon </dt><dd> url to img, default=doc.gif ('none'=no leaf icon)</dd>
51 initialize: function(id,url,options) {
52 Rico.extend(this, new Rico.Popup());
53 Rico.extend(this.options, {
60 defaultAction: Rico.eventHandle(this,'nodeClick'),
63 leafIcon: Rico.imgDir+'doc.gif'
65 Rico.extend(this.options, options || {});
68 this.close=this.closePopup;
69 this.hoverSet = new Rico.HoverSet([]);
73 var imgsrc = ["node.gif","nodelast.gif","folderopen.gif","folderclosed.gif"];
75 for (var i=0;i<imgsrc.length;i++) {
77 img.src = Rico.imgDir+imgsrc[i];
79 this.treeDiv=document.createElement("div");
80 this.treeDiv.id=this.id;
81 this.treeDiv.className='ricoTree';
82 if (Rico.theme.treeContent) Rico.addClass(this.treeDiv,Rico.theme.treeContent);
83 this.treeDiv.style.height=this.options.height;
84 this.treeDiv.style.width=this.options.width;
85 this.createContainer();
86 this.content.className=Rico.theme.tree || 'ricoTreeContainer';
87 this.content.appendChild(this.treeDiv);
88 if (this.options.showCheckBox) {
89 this.buttonDiv=document.createElement("div");
90 this.buttonDiv.style.width=this.options.width;
91 this.buttonDiv.className='ricoTreeButtons';
92 if (Rico.getStyle(this.container,'position')=='absolute') {
93 var span=document.createElement("span");
94 span.innerHTML=RicoTranslate.getPhraseById('treeSave');
95 Rico.setStyle(span,{'float':'left',cursor:'pointer'});
96 this.buttonDiv.appendChild(span);
97 Rico.eventBind(span, 'click', Rico.eventHandle(this,'saveSelection'));
99 var span=document.createElement("span");
100 span.innerHTML=RicoTranslate.getPhraseById('treeClear');
101 Rico.setStyle(span,{'float':'right',cursor:'pointer'});
102 this.buttonDiv.appendChild(span);
103 this.content.appendChild(this.buttonDiv);
104 Rico.eventBind(span, 'click', Rico.eventHandle(this,'clrCheckBoxEvent'));
109 setTreeDiv: function(divId) {
110 this.treeDiv = Rico.$(divId);
111 this.openPopup = function() {};
116 if (this.treeDiv.childNodes.length == 0 && this.dataSource) {
121 loadXMLDoc: function(branchPin) {
122 var parms = { id: this.id };
123 if (branchPin) parms.Parent=branchPin;
124 Rico.log('Tree loadXMLDoc: '+this.id);
126 new Rico.ajaxRequest(this.dataSource, {parameters:parms, method:'get', onComplete: function(request) { self.processResponse(request); } });
129 domID: function(nodeID,part) {
130 return 'RicoTree_'+part+'_'+this.id+'_'+nodeID;
133 processResponse: function(request) {
134 var response = request.responseXML.getElementsByTagName("ajax-response");
135 if (response == null || response.length != 1) return;
136 var rowsElement = response[0].getElementsByTagName('rows')[0];
137 var trs = rowsElement.getElementsByTagName("tr");
139 for (var i=0; i < trs.length; i++) {
140 var cells = trs[i].getElementsByTagName("td");
141 if (cells.length < 5) continue;
143 content[5]=this.options.leafIcon;
144 for (var j=0; j<cells.length; j++) {
145 content[j]=Rico.getContentAsString(cells[j],true);
147 content[3] = content[3].match(/^0|L$/i) ? 0 : 1;
148 content[4] = parseInt(content[4]);
149 rowdata.push(content);
151 for (var i=0; i < rowdata.length; i++) {
152 var moreChildren=(i < rowdata.length-1) && (rowdata[i][0]==rowdata[i+1][0]);
153 this.addNode(rowdata[i][0],rowdata[i][1],rowdata[i][2],rowdata[i][3],rowdata[i][4],rowdata[i][5],!moreChildren);
157 DisplayImages: function(row,arNames) {
159 for(i=0;i<arNames.length;i++) {
160 img = document.createElement("img");
161 img.src=Rico.imgDir+arNames[i] + ".gif";
162 td=row.insertCell(-1);
167 addNode: function(parentId, nodeId, nodeDesc, isContainer, isSelectable, leafIcon, isLast) {
168 var parentNode=Rico.$(this.domID(parentId,'Parent'));
169 var parentChildren=Rico.$(this.domID(parentId,'Children'));
170 var level=parentNode ? parentNode.TreeLevel+1 : 0;
171 //alert("addNode at level " + level + " (" + nodeId + ")")
172 var tab = document.createElement("table");
173 var div = document.createElement("div");
174 div.id=this.domID(nodeId,'Children');
175 div.className='ricoTreeBranch';
176 div.style.display=parentNode ? 'none' : '';
180 tab.id=this.domID(nodeId,'Parent');
182 tab.TreeContainer=isContainer;
183 tab.TreeFetchedChildren=this.dataSource ? false : true;
184 var row=tab.insertRow(0);
186 for (var i=0; i<level-1; i++) {
187 td[i]=row.insertCell(-1);
190 var tdParent=parentNode.getElementsByTagName('td');
191 for (var i=0; i<level-2; i++) {
192 td[i].innerHTML=tdParent[i].innerHTML;
194 var img = document.createElement("img");
195 img.src=Rico.imgDir+(parentChildren.nextSibling && this.options.showLines ? "nodeline" : "nodeblank")+".gif";
196 td[level-2].appendChild(img);
199 var suffix=isLast && this.options.showLines ? 'last' : '';
200 var prefix=this.options.showLines ? 'node' : '';
201 if (this.options.showPlusMinus && isContainer) {
202 var img = document.createElement("img");
204 img.style.cursor='pointer';
205 Rico.eventBind(img, 'click', Rico.eventHandle(this,'clickBranch'));
206 img.src=Rico.imgDir+prefix+"p"+suffix+".gif";
207 row.insertCell(-1).appendChild(img);
208 } else if (this.options.showLines) {
209 var img = document.createElement("img");
210 img.src=Rico.imgDir+"node"+suffix+".gif";
211 row.insertCell(-1).appendChild(img);
213 if (this.options.showFolders && (isContainer || (leafIcon && leafIcon!='none'))) {
214 var img = document.createElement("img");
219 img.style.cursor='pointer';
220 Rico.eventBind(img, 'click', Rico.eventHandle(this,'clickBranch'));
221 img.src=Rico.imgDir+"folderclosed.gif";
223 row.insertCell(-1).appendChild(img);
226 if (isSelectable && this.options.showCheckBox) {
227 var chkbx=document.createElement("input");
228 chkbx.type="checkbox";
230 row.insertCell(-1).appendChild(chkbx);
233 if (isSelectable && !this.options.showCheckBox) {
234 var span=document.createElement('a');
235 if (typeof isSelectable=='string') {
236 span.href=isSelectable;
238 span.href='javascript:void(0)';
239 Rico.eventBind(span, 'click', this.options.defaultAction);
241 this.hoverSet.add(span);
243 var span=document.createElement('p');
245 span.id=this.domID(nodeId,'Desc');
246 span.className='ricoTreeLevel'+level;
247 switch (this.options.nodeIdDisplay) {
248 case 'last': nodeDesc+=' ('+nodeId+')'; break;
249 case 'first': nodeDesc=nodeId+' - '+nodeDesc; break;
250 case 'tooltip': span.title=nodeId; break;
252 span.appendChild(document.createTextNode(nodeDesc));
253 row.insertCell(-1).appendChild(span);
255 var parent=parentChildren || this.treeDiv;
256 parent.appendChild(tab);
257 parent.appendChild(div);
260 nodeClick: function(e) {
261 var node=Rico.eventElement(e);
262 if (this.returnValue) {
263 var t=this.domID('','Desc');
264 this.returnValue(node.id.substr(t.length),node.innerHTML);
269 saveSelection: function(e) {
270 if (this.returnValue) {
271 this.returnValue(this.getCheckedItems());
276 getCheckedItems: function() {
277 var inp=this.treeDiv.getElementsByTagName('input');
279 for (var i=0; i<inp.length; i++) {
280 if (inp[i].type=='checkbox' && inp[i].checked) {
281 vals.push(inp[i].value);
287 setCheckBoxes: function(val) {
288 var inp=this.treeDiv.getElementsByTagName('input');
289 for (var i=0; i<inp.length; i++) {
290 if (inp[i].type=='checkbox') {
296 clrCheckBoxEvent: function(e) {
298 this.setCheckBoxes(false);
301 clickBranch: function(e) {
302 var node=Rico.eventElement(e);
303 var tab=Rico.getParentByTagName(node,'table');
304 if (!tab || !tab.TreeContainer) return;
305 var a=tab.id.split('_');
307 var childDiv=Rico.$(a.join('_'));
308 Rico.toggle(childDiv);
309 if (node.tagName=='IMG') {
310 var v=Rico.visible(childDiv);
311 if (node.src.match(/node(p|m)(last)?\.gif$/)) {
312 node.src=node.src.replace(/nodep|nodem/,'node'+(v ? 'm' : 'p'));
313 } else if (node.src.match(/folder(open|closed)\.gif$/)) {
314 node.src=node.src.replace(/folder(open|closed)/,'folder'+(v ? 'open' : 'closed'));
315 } else if (node.src.match(/\b(m|p)\.gif$/)) {
316 node.src=node.src.replace(/(p|m)\.gif/,v ? 'm\.gif' : 'p\.gif');
319 if (!tab.TreeFetchedChildren) {
320 tab.TreeFetchedChildren=1;
321 this.loadXMLDoc(node.name);