46d4a6b655b297e659ddc02b7fc9f4e219531dd5
[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', {src:rootimg,border:0,style:'padding-right: 4px;'});
22 }
23
24 SlimTree.prototype.cacheMe = function( id, pid, name, action, title ) {
25         if(this.cache[id]) return;
26         this.cache[id]                          = {};
27         this.cache[id].pid              = pid
28         this.cache[id].name             = name
29         this.cache[id].action   = action
30         this.cache[id].title            = title
31 }
32
33 SlimTree.prototype.flushCache = function() {
34         for( var c in this.cache ) {
35                 var obj = this.cache[c];
36                 if(obj && getId(obj.pid)) {
37                         this.cache[c] = null;
38                         this.addNode(c,obj.pid, obj.name, obj.action,obj.title);
39                 }
40         }
41 }
42
43 SlimTree.prototype.addNode = function( id, pid, name, action, title ) {
44
45         if( pid != -1 && !getId(pid)) {
46                 if(this.cache[pid]) {
47                         var obj = this.cache[pid];
48                         this.addNode(pid, obj.pid,obj.name, obj.action,obj.title );
49                         this.cache[pid] = null;
50                 } else {
51                         this.cacheMe(id, pid, name, action, title);
52                         return;
53                 }
54         }
55
56         var div                 = elem('div',{id:id});
57         var topdiv              = elem('div',{style:'vertical-align:middle'});
58         var link                        = elem('a', {id:'stlink_' + id}); 
59         var actionref   = elem('a',{href:action}, name);
60         var contdiv             = elem('div',{id:'stcont_' + id});
61         if(action) actionref.setAttribute('href',action);
62         if(title) actionref.setAttribute('title',title);
63         else actionref.setAttribute('title',name);
64
65         _apc(topdiv,link);
66         _apc(topdiv,actionref);
67         _apc(div,topdiv);
68         _apc(div,contdiv);
69
70         if( pid == -1 ) { 
71                 this.rootid = id;
72                 _apc(this.context,div);
73                 if(this.rootimg) _apc(link,this.rootimg.cloneNode(true));
74                 else _apc(link,stimgblank.cloneNode(true));
75         } else {
76                 if(pid == this.rootid) this.open(pid);
77                 else this.close(pid);
78                 getId(pid).setAttribute('haschild','1');
79                 _apc(link,stimgblank.cloneNode(true));
80                 div.style.paddingLeft = '18px';
81                 div.style.backgroundImage = 'url('+stpicjoinb+')';
82                 div.style.backgroundRepeat = 'no-repeat';
83                 _apc(getId('stcont_' + pid), div);
84                 if (div.previousSibling) stMakePaths(div);
85         }
86         this.flushCache();
87 }
88
89 function stMakePaths(div) {
90         _apc(div.previousSibling.firstChild,stimgjoin.cloneNode(true));
91         _apc(div.previousSibling.firstChild,div.previousSibling.firstChild.firstChild);
92         _apc(div.previousSibling.firstChild,div.previousSibling.firstChild.firstChild);
93         div.previousSibling.firstChild.firstChild.style.marginLeft = '-18px';
94         div.previousSibling.style.backgroundImage = 'url('+stpicline+')';
95         div.previousSibling.style.backgroundRepeat = 'repeat-y';
96 }
97
98 SlimTree.prototype.expandAll = function() { this.flex(this.rootid, 'open'); }
99 SlimTree.prototype.closeAll = function() { this.flex(this.rootid,'close'); }
100 SlimTree.prototype.flex = function(id, type) {
101         if(type=='open') this.open(id);
102         else { if (id != this.rootid) this.close(id); }
103         var n = getId('stcont_' + id);
104         for( var c = 0; c != n.childNodes.length; c++ ) {
105                 var ch = n.childNodes[c];
106                 if(ch.nodeName.toLowerCase() == 'div') {
107                         if(getId(ch.id).getAttribute('haschild') == '1') 
108                                 this.flex(ch.id, type);
109                 }
110         }
111 }
112
113 SlimTree.prototype.open = function(id) {
114         var link = getId('stlink_' + id);
115         if(id != this.rootid || !this.rootimg) {
116                 removeChildren(link);
117                 _apc(link,stimgclose.cloneNode(true));
118         }
119         link.setAttribute('href','javascript:' + this.handle + '.close("'+id+'");');
120         unHideMe(getId('stcont_' + id));
121 }
122
123 SlimTree.prototype.close = function(id) {
124         var link = getId('stlink_' + id);
125         if(id != this.rootid || !this.rootimg) {
126                 removeChildren(link);
127                 _apc(link,stimgopen.cloneNode(true));
128         }
129         link.setAttribute('href','javascript:' + this.handle + '.open("'+id+'");');
130         hideMe(getId('stcont_' + id));
131 }
132