added toggle method to slimtree
[Evergreen.git] / Open-ILS / web / opac / common / js / slimtree.js
1 var stpicopen   = '../../../images/slimtree/folder.gif';
2 var stpicclose = '../../../images/slimtree/folderopen.gif';
3 var stpicblank = '../../../images/slimtree/page.gif';
4 var stpicline   = '../../../images/slimtree/line.gif';
5 var stpicjoin   = '../../../images/slimtree/join.gif';
6 var stpicjoinb = '../../../images/slimtree/joinbottom.gif';
7
8 var stimgopen   = elem('img',{src:stpicopen,border:0});
9 var stimgclose  = elem('img',{src:stpicclose,border:0});
10 var stimgblank  = elem('img',{src:stpicblank,border:0});
11 var stimgline   = elem('img',{src:stpicline,border:0});
12 var stimgjoin   = elem('img',{src:stpicjoin,border:0, style:'display:inline;'});
13
14 function _apc(root,node) { root.appendChild(node); }
15
16 function SlimTree(context, handle, rootimg) { 
17         this.context    = context; 
18         this.handle             = handle;
19         this.cache              = new Object();
20         if(rootimg) 
21                 this.rootimg = elem('img', 
22                         {src:rootimg,border:0,style:'padding-right: 4px;'});
23 }
24
25 SlimTree.prototype.addCachedChildren = function(pid) {
26         var child;
27         while( child = this.cache[pid].shift() ) 
28                 this.addNode( child.id, child.pid, 
29                         child.name, child.action, child.title );
30         this.cache[pid] = null;
31 }
32
33 SlimTree.prototype.addNode = function( id, pid, name, action, title ) {
34
35         if( pid != -1 && !$(pid)) {
36                 if(!this.cache[pid]) this.cache[pid] = new Array();
37                 this.cache[pid].push(
38                         {id:id,pid:pid,name:name,action:action,title:title });
39                 return;
40         }
41
42         var div                 = elem('div',{id:id});
43         var topdiv              = elem('div',{style:'vertical-align:middle'});
44         var link                        = elem('a', {id:'stlink_' + id}); 
45         var actionref   = elem('a',{href:action}, name);
46         var contdiv             = elem('div',{id:'stcont_' + id});
47         if(action) actionref.setAttribute('href',action);
48         if(title) actionref.setAttribute('title',title);
49         else actionref.setAttribute('title',name);
50
51         _apc(topdiv,link);
52         _apc(topdiv,actionref);
53         _apc(div,topdiv);
54         _apc(div,contdiv);
55
56         if( pid == -1 ) { 
57
58                 this.rootid = id;
59                 _apc(this.context,div);
60                 if(this.rootimg) _apc(link,this.rootimg.cloneNode(true));
61                 else _apc(link,stimgblank.cloneNode(true));
62
63         } else {
64
65                 if(pid == this.rootid) this.open(pid);
66                 else this.close(pid);
67                 $(pid).setAttribute('haschild','1');
68                 _apc(link,stimgblank.cloneNode(true));
69                 div.style.paddingLeft = '18px';
70                 div.style.backgroundImage = 'url('+stpicjoinb+')';
71                 div.style.backgroundRepeat = 'no-repeat';
72                 _apc($('stcont_' + pid), div);
73                 if (div.previousSibling) stMakePaths(div);
74         }
75         if(this.cache[id]) this.addCachedChildren(id);
76 }
77
78 function stMakePaths(div) {
79         _apc(div.previousSibling.firstChild,stimgjoin.cloneNode(true));
80         _apc(div.previousSibling.firstChild,div.previousSibling.firstChild.firstChild);
81         _apc(div.previousSibling.firstChild,div.previousSibling.firstChild.firstChild);
82         div.previousSibling.firstChild.firstChild.style.marginLeft = '-18px';
83         div.previousSibling.style.backgroundImage = 'url('+stpicline+')';
84         div.previousSibling.style.backgroundRepeat = 'repeat-y';
85 }
86
87 SlimTree.prototype.expandAll = function() { this.flex(this.rootid, 'open'); }
88 SlimTree.prototype.closeAll = function() { this.flex(this.rootid,'close'); }
89 SlimTree.prototype.flex = function(id, type) {
90         if(type=='open') this.open(id);
91         else { if (id != this.rootid) this.close(id); }
92         var n = $('stcont_' + id);
93         for( var c = 0; c != n.childNodes.length; c++ ) {
94                 var ch = n.childNodes[c];
95                 if(ch.nodeName.toLowerCase() == 'div') {
96                         if($(ch.id).getAttribute('haschild') == '1') 
97                                 this.flex(ch.id, type);
98                 }
99         }
100 }
101
102 SlimTree.prototype.toggle = function(id) {
103         if($(id).getAttribute('ostate') == '1') this.open(id);
104         else if($(id).getAttribute('ostate') == '2') this.close(id);
105 }
106
107 SlimTree.prototype.open = function(id) {
108         if($(id).getAttribute('ostate') == '2') return;
109         var link = $('stlink_' + id);
110         if(id != this.rootid || !this.rootimg) {
111                 removeChildren(link);
112                 _apc(link,stimgclose.cloneNode(true));
113         }
114         link.setAttribute('href','javascript:' + this.handle + '.close("'+id+'");');
115         unHideMe($('stcont_' + id));
116         $(id).setAttribute('ostate','2');
117 }
118
119 SlimTree.prototype.close = function(id) {
120         var link = $('stlink_' + id);
121         if(id != this.rootid || !this.rootimg) {
122                 removeChildren(link);
123                 _apc(link,stimgopen.cloneNode(true));
124         }
125         link.setAttribute('href','javascript:' + this.handle + '.open("'+id+'");');
126         hideMe($('stcont_' + id));
127         $(id).setAttribute('ostate','1');
128 }
129