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